You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+86-10Lines changed: 86 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,30 +33,29 @@ You can install the library by running `pip install openpaygo` or adding `openpa
33
33
34
34
You can use the `generate_token()` function to generate an OpenPAYGOToken Token. The function takes the following parameters, and they should match the configuration in the hardware of the device:
35
35
36
-
-`secret_key` (required): The secret key of the device
37
-
-`value` (required): The value to be passed in the token (typically the number of days of activation)
38
-
-`count` (required): The token count used to make the last token.
39
-
-`set_mode` (optional): If set to `true`, the generated token will be a Set Time token, otherwise it is Add Time token.
36
+
-`secret_key` (required): The secret key of the device. Must be passed as an hexadecimal string (with 32 characters).
37
+
-`count` (required): The token count used to make the last token.
38
+
-`value` (optional): The value to be passed in the token (typically the number of days of activation). Optional if the `token_type` is Disable PAYG or Counter Sync. The value must be between 0 and 995.
39
+
-`token_type` (optional): Used to set the type of token (default is Add Time). Token types can be found in the `TokenType` class: ADD_TIME, SET_TIME, DISABLE_PAYG, COUNTER_SYNC.
40
40
-`starting_code` (optional): If not provided, it is generated according to the method defined in the standard (SipHash-2-4 of the key, transformed to digit by the same method as the token generation).
41
41
-`value_divider` (optional): The dividing factor used for the value.
42
42
-`restricted_digit_set` (optional): If set to `true`, the the restricted digit set will be used (only digits from 1 to 4).
43
43
-`extended_token` (optional): If set to `true` then a larger token will be generated, able to contain values up to 999999. This is for special use cases of each device, such as settings change, and is not set in the standard.
44
44
45
-
46
-
The function returns the `token` as a string as well as the `updated_count` as a number.
45
+
The function returns the `updated_count` as a number as well as the `token` as a string, in that order. The function will raise a `ValueError` if the key is in the wrong format or the value invalid.
47
46
48
47
49
-
**Example:**
48
+
**Example 1 - Add 7 days:**
50
49
51
50
```
52
-
from openpaygo.token import generate_token
51
+
from openpaygo.optoken import generate_token
53
52
from myexampleproject import device_getter
54
53
55
54
# We get a device with the parameters we need from our database, this will be specific to your project
56
55
device = device_getter(serial=1234)
57
56
58
57
# We get the new token and update the count
59
-
new_token, device.count = generate_token(
58
+
device.count, new_token = generate_token(
60
59
secret_key=device.secret_key,
61
60
value=7,
62
61
count=device.count
@@ -66,4 +65,81 @@ print('Token: '+new_token)
66
65
device.save() # We save the new count that we set for the device
67
66
```
68
67
69
-
## Getting Started - Decoding a Token
68
+
**Example 2 - Disable PAYG (unlock forever):**
69
+
70
+
```
71
+
from openpaygo.optoken import generate_token, TokenType
72
+
73
+
...
74
+
75
+
# We get the new token and update the count
76
+
device.count, new_token = generate_token(
77
+
secret_key=device.secret_key,
78
+
token_type=TokenType.DISABLE_PAYG,
79
+
count=device.count
80
+
)
81
+
82
+
print('Token: '+new_token)
83
+
device.save() # We save the new count that we set for the device
84
+
```
85
+
86
+
87
+
## Getting Started - Decoding a Token
88
+
89
+
You can use the `decode_token()` function to generate an OpenPAYGOToken Token. The function takes the following parameters, and they should match the configuration in the hardware of the device:
90
+
91
+
-`token` (required): The token that was given by the user
92
+
-`secret_key` (required): The secret key of the device
93
+
-`count` (required): The token count of the last valid token. When a device is new, this is 1.
94
+
-`used_counts` (optional): An array of recently used token counts, as returned by the function itself after the last valid token was decoded. This allows for handling unordered token entry.
95
+
-`starting_code` (optional): If not provided, it is generated according to the method defined in the standard (SipHash-2-4 of the key, transformed to digit by the same method as the token generation).
96
+
-`value_divider` (optional): The dividing factor used for the value.
97
+
-`restricted_digit_set` (optional): If set to `true`, the the restricted digit set will be used (only digits from 1 to 4).
98
+
99
+
100
+
The function returns the following variable in this order:
101
+
-`value`: The value associated with the token (if the token is ADD_TIME or SET_TIME).
102
+
-`token_type`: The type of the token that was provided. Token types can be found in the `TokenType` class: ADD_TIME, SET_TIME, DISABLE_PAYG, COUNTER_SYNC or ALREADY_USED (if the token is valid but already used), INVALID (if the token was invalid).
103
+
-`updated_count`: The token count of the token, if it was valid.
104
+
-`updated_used_counts`: The updated array of recently used token, if the token was valid.
105
+
106
+
The function will raise a `ValueError` if the key is in the wrong format, but will not raise an error if the token is invalid (as it is a common expected behaviour), to check the validity of the token you must check the return `token_type` and proceed accordingly depending on the type of token.
107
+
108
+
109
+
**Example:**
110
+
111
+
```
112
+
from openpaygo.optoken import decode_token
113
+
114
+
# We assume the users enters a token and that the device state is saved in my_device_state
0 commit comments