Skip to content

Commit b95153f

Browse files
author
Andrea Bonomi
committed
fix for missing method in Python 3.6
1 parent 79b9b7e commit b95153f

4 files changed

Lines changed: 15 additions & 5 deletions

File tree

securid/jsontoken.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
import os
44
import os.path
55
import json
6-
from datetime import date
76
from typing import Any, Dict, Optional, Union
8-
from .utils import Bytes
7+
from .utils import Bytes, fromisoformat
98
from .token import SERIAL_LENGTH, Token, AbstractTokenFile
109
from .exceptions import ParseException, InvalidSeed, InvalidSerial
1110

@@ -81,7 +80,7 @@ def json_decode_token(cls, data: Union[Bytes, Dict[str, Any]]) -> Token:
8180
token = Token(
8281
digits=dct['digits'],
8382
interval=dct['period'],
84-
exp_date=date.fromisoformat(dct['exp_date'])
83+
exp_date=fromisoformat(dct['exp_date'])
8584
if dct.get('exp_date')
8685
else None,
8786
seed=bytes(dct['secret']),

securid/sdtid.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
aes_ecb_encrypt,
1515
xor_block,
1616
cbc_hash,
17+
fromisoformat,
1718
)
1819
from .exceptions import ParseException, InvalidSignature
1920

@@ -251,7 +252,7 @@ def get(self, name: str, default: Any = None, kind: Optional[str] = None) -> Any
251252
elif kind == 'int':
252253
value = int(value)
253254
elif kind == 'date':
254-
value = date.fromisoformat(value.replace('/', '-'))
255+
value = fromisoformat(value.replace('/', '-'))
255256
return value
256257

257258

securid/token.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
random,
1414
Bytearray,
1515
aes_ecb_encrypt,
16+
fromisoformat,
1617
)
1718

1819
__all__ = ['Token', 'AbstractTokenFile', 'SERIAL_LENGTH']
@@ -64,7 +65,7 @@ def __init__(
6465
if isinstance(seed, str):
6566
seed = bytes(seed, 'ascii')
6667
if isinstance(exp_date, str):
67-
exp_date = date.fromisoformat(exp_date)
68+
exp_date = fromisoformat(exp_date)
6869
self.seed = seed
6970
self.interval = interval
7071
self.digits = digits

securid/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python
22

3+
from datetime import datetime, date
34
from typing import Union, Optional
45
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes
56

@@ -20,6 +21,7 @@
2021
'aes_ecb_decrypt',
2122
'xor_block',
2223
'cbc_hash',
24+
'fromisoformat',
2325
]
2426

2527
AES_BLOCK_SIZE = 16
@@ -78,3 +80,10 @@ def cbc_hash(key: Bytes, iv: Bytes, data: Bytes) -> bytes:
7880
result = aes_ecb_encrypt(key, xor_block(result, data))
7981
data = data[AES_BLOCK_SIZE:]
8082
return bytes(result)
83+
84+
85+
def fromisoformat(dt: str) -> date:
86+
"""
87+
Convert a YYYY-MM-DD string into a date object
88+
"""
89+
return datetime.strptime(dt, '%Y-%m-%d').date()

0 commit comments

Comments
 (0)