Skip to content

Commit 3ee8536

Browse files
committed
Improvements for 0.5.0
1 parent 436421d commit 3ee8536

4 files changed

Lines changed: 21 additions & 3 deletions

File tree

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ It provides the following methods:
244244
- `is_auth_valid()`: Returns `true` if the authentication provided is valid or `false` if not. Note that it checks both that the signature is valid and that the `request_count` or `timestamp` are more recent than the one provided in the device parameters.
245245
- `get_simple_metrics()`: Returns the metrics provided in the simple expanded format. It will also convert relative timestamps into explicit timestamps for easier processing.
246246
- `get_data_timestamp()`: Returns the timestamp of the data, either the `data_collection_timestamp` if available or the timestamp `timestamp` or the time of the request as fallback.
247+
- `get_request_timestamp()`: This is the `timestamp` that was explicitely set in the request and was signed, used to avoid replay attacks. It might be different from the data timestamp.
248+
- `get_request_count()`: This is the `request_count` set in the request and was signed, used to avoid replay attacks.
247249
- `get_token_count()`: Returns the token count provided in the request (if any).
248250
- `expects_token_answer()`: Return `true` if the payload requested tokens in the answer. You can set the tokens to be returned by calling `add_tokens_to_answer(token_list)` with `token_list` being a list of token strings.
249251
- `expects_time_answer()`: Return `true` if the payload requested either relative time or absolute time in the answer. You can set the time to be returned by calling `add_time_to_answer(target_datetime)` with `target_datetime` being a datetime object. The function will automatically provide it in the correct format based on the request.
@@ -298,13 +300,22 @@ def device_data():
298300
metrics.add_time_to_answer(device.expiration_datetime)
299301
# We can add extra data
300302
metrics.add_settings_to_answer({'language': 'fr-FR'})
303+
# We update the request timestamp or the count if provided to be able to reject duplicate or replay requests
304+
if metrics.get_request_count():
305+
device.last_request_count = metrics.get_request_count()
306+
if metrics.get_request_timestamp():
307+
device.last_request_timestamp = metrics.get_request_timestamp()
301308
# The handler handles the signature, etc.
302309
return metrics.get_answer_payload(), 200
303310
```
304311

305312

306313
## Changelog
307314

315+
### 2023-10-12 - v0.5.0
316+
- Added convenience functions for accessing the current request count and request timestamp
317+
- Improved documentation on how to avoid replay attacks
318+
308319
### 2023-10-12 - v0.4.0
309320
- Added convenience functions for accessing token count and data timestamp
310321
- Added automatic verification of last request count or timestamp during auth

openpaygo/metrics_response.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ def __init__(self, received_metrics, data_format=None, secret_key=None, last_req
1111
# We convert the base variable names to simple
1212
self.request_dict = OpenPAYGOMetricsShared.convert_dict_keys_to_simple(self.request_dict)
1313
# We add the reception timestamp if not timestamp was provided
14+
self.request_timestamp = self.request_dict.get('timestamp')
1415
if not self.request_dict.get('timestamp'):
1516
self.timestamp = int(datetime.now().timestamp())
1617
else:
@@ -73,7 +74,13 @@ def get_simple_metrics(self):
7374
return simple_dict
7475

7576
def get_data_timestamp(self):
76-
return self.request_dict.get('data_collection_timestamp', self.request_dict.get('timestamp'))
77+
return self.request_dict.get('data_collection_timestamp', self.timestamp)
78+
79+
def get_request_timestmap(self):
80+
return self.request_timestamp
81+
82+
def get_request_count(self):
83+
return self.request_dict.get('request_count')
7784

7885
def get_token_count(self):
7986
data = self._get_simple_data()

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = openpaygo
3-
version = 0.4.0
3+
version = 0.5.0
44
url = https://github.com/EnAccess/OpenPAYGO-python/
55
description-file=README.md
66
license_files=LICENSE

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
setup(
55
name="openpaygo",
66
packages=find_packages(),
7-
version='0.4.0',
7+
version='0.5.0',
88
license='MIT',
99
author="Solaris Offgrid",
1010
url='https://github.com/EnAccess/OpenPAYGO-python/',

0 commit comments

Comments
 (0)