forked from EnAccess/OpenPAYGO-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_simulator.py
More file actions
110 lines (96 loc) · 4.12 KB
/
Copy pathserver_simulator.py
File metadata and controls
110 lines (96 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
from datetime import datetime
from openpaygo.token_encode import OpenPAYGOTokenEncoder
from openpaygo.token_shared import OpenPAYGOTokenShared, TokenType
class SingleDeviceServerSimulator(object):
def __init__(
self,
starting_code,
key,
starting_count=1,
restricted_digit_set=False,
time_divider=1,
):
self.starting_code = starting_code
self.key = key
self.count = starting_count
self.expiration_date = datetime.now()
self.furthest_expiration_date = datetime.now()
self.payg_enabled = True
self.time_divider = time_divider
self.restricted_digit_set = restricted_digit_set
def print_status(self):
print("Expiration Date: " + str(self.expiration_date))
print("Current count: " + str(self.count))
print("PAYG Enabled: " + str(self.payg_enabled))
def generate_payg_disable_token(self):
self.count, token = OpenPAYGOTokenEncoder.generate_token(
starting_code=self.starting_code,
secret_key=self.key,
value=0,
count=self.count,
token_type=TokenType.DISABLE_PAYG,
restricted_digit_set=self.restricted_digit_set,
)
return SingleDeviceServerSimulator._format_token(token)
def generate_counter_sync_token(self):
self.count, token = OpenPAYGOTokenEncoder.generate_token(
starting_code=self.starting_code,
secret_key=self.key,
value=0,
count=self.count,
token_type=TokenType.COUNTER_SYNC,
restricted_digit_set=self.restricted_digit_set,
)
return SingleDeviceServerSimulator._format_token(token)
def generate_token_from_date(self, new_expiration_date, force=False):
furthest_expiration_date = self.furthest_expiration_date
if new_expiration_date > self.furthest_expiration_date:
self.furthest_expiration_date = new_expiration_date
if new_expiration_date > furthest_expiration_date:
# If the date is strictly above the furthest date activated, use ADD
value = self._get_value_to_activate(
new_expiration_date, self.expiration_date, force
)
self.expiration_date = new_expiration_date
return self._generate_token_from_value(value, mode=TokenType.ADD_TIME)
else:
# If the date is below or equal to the furthest date activated, use SET
value = self._get_value_to_activate(
new_expiration_date, datetime.now(), force
)
self.expiration_date = new_expiration_date
return self._generate_token_from_value(value, mode=TokenType.SET_TIME)
def _generate_token_from_value(self, value, mode):
self.count, token = OpenPAYGOTokenEncoder.generate_token(
starting_code=self.starting_code,
secret_key=self.key,
value=value,
count=self.count,
token_type=mode,
restricted_digit_set=self.restricted_digit_set,
)
return SingleDeviceServerSimulator._format_token(token)
def _generate_extended_value_token(self, value):
pass
def _get_value_to_activate(self, new_time, reference_time, force_maximum=False):
if new_time <= reference_time:
return 0
else:
days = self._timedelta_to_days(new_time - reference_time)
value = int(round(days * self.time_divider, 0))
if value > OpenPAYGOTokenShared.MAX_ACTIVATION_VALUE:
if not force_maximum:
raise Exception("TOO_MANY_DAYS_TO_ACTIVATE")
else:
# Will need to be activated again after those days
return OpenPAYGOTokenShared.MAX_ACTIVATION_VALUE
return value
@staticmethod
def _timedelta_to_days(this_timedelta):
return this_timedelta.days + (this_timedelta.seconds / 3600 / 24)
@staticmethod
def _format_token(token):
token = str(token)
if len(token) < 9:
token = "0" * (9 - len(token)) + token
return token