|
| 1 | +from .shared import OpenPAYGOTokenShared, TokenType |
| 2 | +from .shared_extended import OpenPAYGOTokenSharedExtended |
| 3 | + |
| 4 | + |
| 5 | +class OpenPAYGOTokenDecoder(object): |
| 6 | + MAX_TOKEN_JUMP = 64 |
| 7 | + MAX_TOKEN_JUMP_COUNTER_SYNC = 100 |
| 8 | + MAX_UNUSED_OLDER_TOKENS = 8*2 |
| 9 | + |
| 10 | + @classmethod |
| 11 | + def decode_token(cls, token, secret_key, count, used_counts=None, starting_code=None, value_divider=1, restricted_digit_set=False): |
| 12 | + secret_key = OpenPAYGOTokenShared.load_secret_key_from_hex(secret_key) |
| 13 | + if not starting_code: |
| 14 | + # We generate the starting code from the key if not provided |
| 15 | + starting_code = OpenPAYGOTokenShared.generate_starting_code(secret_key) |
| 16 | + if not restricted_digit_set: |
| 17 | + if len(token) <= 9: |
| 18 | + extended_token = False |
| 19 | + elif len(token) <= 12: |
| 20 | + extended_token = True |
| 21 | + else: |
| 22 | + raise ValueError("Token is too long") |
| 23 | + elif restricted_digit_set: |
| 24 | + if len(token) <= 15: |
| 25 | + extended_token = False |
| 26 | + elif len(token) <= 20: |
| 27 | + extended_token = True |
| 28 | + else: |
| 29 | + raise ValueError("Token is too long") |
| 30 | + if not extended_token: |
| 31 | + value, token_type, count, updated_counts = cls.get_activation_value_count_and_type_from_token(token, starting_code, secret_key, count, restricted_digit_set, used_counts) |
| 32 | + else: |
| 33 | + value, token_type, count, updated_counts = cls.get_activation_value_count_from_extended_token(token, starting_code, secret_key, count, restricted_digit_set, used_counts) |
| 34 | + if value and value_divider: |
| 35 | + value = value / value_divider |
| 36 | + return value, token_type, count, updated_counts |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def get_activation_value_count_and_type_from_token(cls, token, starting_code, key, last_count, |
| 40 | + restricted_digit_set=False, used_counts=None): |
| 41 | + if restricted_digit_set: |
| 42 | + token = OpenPAYGOTokenShared.convert_from_4_digit_token(token) |
| 43 | + valid_older_token = False |
| 44 | + token_base = OpenPAYGOTokenShared.get_token_base(token) # We get the base of the token |
| 45 | + current_code = OpenPAYGOTokenShared.put_base_in_token(starting_code, token_base) # We put it into the starting code |
| 46 | + starting_code_base = OpenPAYGOTokenShared.get_token_base(starting_code) # We get the base of the starting code |
| 47 | + value = cls._decode_base(starting_code_base, token_base) # If there is a match we get the value from the token |
| 48 | + # We try all combination up until last_count + TOKEN_JUMP, or to the larger jump if syncing counter |
| 49 | + # We could start directly the loop at the last count if we kept the token value for the last count |
| 50 | + if value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE: |
| 51 | + max_count_try = last_count + cls.MAX_TOKEN_JUMP_COUNTER_SYNC + 1 |
| 52 | + else: |
| 53 | + max_count_try = last_count + cls.MAX_TOKEN_JUMP + 1 |
| 54 | + for count in range(0, max_count_try): |
| 55 | + masked_token = OpenPAYGOTokenShared.put_base_in_token(current_code, token_base) |
| 56 | + if count % 2: |
| 57 | + if value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE: |
| 58 | + this_type = TokenType.COUNTER_SYNC |
| 59 | + elif value == OpenPAYGOTokenShared.PAYG_DISABLE_VALUE: |
| 60 | + this_type = TokenType.DISABLE_PAYG |
| 61 | + else: |
| 62 | + this_type = TokenType.SET_TIME |
| 63 | + else: |
| 64 | + this_type = TokenType.ADD_TIME |
| 65 | + if masked_token == token: |
| 66 | + if cls._count_is_valid(count, last_count, value, this_type, used_counts): |
| 67 | + updated_counts = cls.update_used_counts(used_counts, value, count, this_type) |
| 68 | + return value, this_type, count, updated_counts |
| 69 | + else: |
| 70 | + valid_older_token = True |
| 71 | + current_code = OpenPAYGOTokenShared.generate_next_token(current_code, key) # If not we go to the next token |
| 72 | + if valid_older_token: |
| 73 | + return None, TokenType.ALREADY_USED, None, None |
| 74 | + return None, TokenType.INVALID, None, None |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def _count_is_valid(cls, count, last_count, value, type, used_counts): |
| 78 | + if value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE: |
| 79 | + if count > (last_count - cls.MAX_TOKEN_JUMP): |
| 80 | + return True |
| 81 | + elif count > last_count: |
| 82 | + return True |
| 83 | + elif cls.MAX_UNUSED_OLDER_TOKENS > 0: |
| 84 | + if count > last_count - cls.MAX_UNUSED_OLDER_TOKENS: |
| 85 | + if count not in used_counts and type == TokenType.ADD_TIME: |
| 86 | + return True |
| 87 | + return False |
| 88 | + |
| 89 | + @classmethod |
| 90 | + def update_used_counts(cls, past_used_counts, value, new_count, type): |
| 91 | + if not past_used_counts: |
| 92 | + return None |
| 93 | + highest_count = max(past_used_counts) if past_used_counts else 0 |
| 94 | + if new_count > highest_count: |
| 95 | + highest_count = new_count |
| 96 | + bottom_range = highest_count-cls.MAX_UNUSED_OLDER_TOKENS |
| 97 | + used_counts = [] |
| 98 | + if type != TokenType.ADD_TIME or value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE or value == OpenPAYGOTokenShared.PAYG_DISABLE_VALUE: |
| 99 | + # If it is not an Add-Time token, we mark all the past tokens as used in the range |
| 100 | + for count in range(bottom_range, highest_count+1): |
| 101 | + used_counts.append(count) |
| 102 | + else: |
| 103 | + # If it is an Add-Time token, we just mark the tokens actually used in the range |
| 104 | + for count in range(bottom_range, highest_count+1): |
| 105 | + if count == new_count or count in past_used_counts: |
| 106 | + used_counts.append(count) |
| 107 | + return used_counts |
| 108 | + |
| 109 | + @classmethod |
| 110 | + def _decode_base(cls, starting_code_base, token_base): |
| 111 | + decoded_value = token_base - starting_code_base |
| 112 | + if decoded_value < 0: |
| 113 | + return decoded_value + 1000 |
| 114 | + else: |
| 115 | + return decoded_value |
| 116 | + |
| 117 | + @classmethod |
| 118 | + def get_activation_value_count_from_extended_token(cls, token, starting_code, key, last_count, |
| 119 | + restricted_digit_set=False, used_counts=None): |
| 120 | + if restricted_digit_set: |
| 121 | + token = OpenPAYGOTokenSharedExtended.convert_from_4_digit_token(token) |
| 122 | + token_base = OpenPAYGOTokenSharedExtended.get_token_base(token) # We get the base of the token |
| 123 | + current_code = OpenPAYGOTokenSharedExtended.put_base_in_token(starting_code, token_base) # We put it into the starting code |
| 124 | + starting_code_base = OpenPAYGOTokenSharedExtended.get_token_base(starting_code) # We get the base of the starting code |
| 125 | + value = cls._decode_base_extended(starting_code_base, token_base) # If there is a match we get the value from the token |
| 126 | + max_count_try = last_count + cls.MAX_TOKEN_JUMP + 1 |
| 127 | + for count in range(0, max_count_try): |
| 128 | + masked_token = OpenPAYGOTokenSharedExtended.put_base_in_token(current_code, token_base) |
| 129 | + if count % 2: |
| 130 | + this_type = TokenType.SET_TIME |
| 131 | + else: |
| 132 | + this_type = TokenType.ADD_TIME |
| 133 | + if masked_token == token: |
| 134 | + if cls._count_is_valid(count, last_count, value, this_type, used_counts): |
| 135 | + updated_counts = cls.update_used_counts(used_counts, value, count, this_type) |
| 136 | + return value, this_type, count, updated_counts |
| 137 | + else: |
| 138 | + valid_older_token = True |
| 139 | + current_code = OpenPAYGOTokenSharedExtended.generate_next_token(current_code, key) # If not we go to the next token |
| 140 | + if valid_older_token: |
| 141 | + return None, TokenType.ALREADY_USED, None, None |
| 142 | + return None, TokenType.INVALID, None, None |
| 143 | + |
| 144 | + @classmethod |
| 145 | + def _decode_base_extended(cls, starting_code_base, token_base): |
| 146 | + decoded_value = token_base - starting_code_base |
| 147 | + if decoded_value < 0: |
| 148 | + return decoded_value + 1000000 |
| 149 | + else: |
| 150 | + return decoded_value |
0 commit comments