Skip to content

Commit ee63212

Browse files
feat(auth): add handling for oauth token clock skew time (#63)
This PR updates the `is_token_expired` utility method to minimize the impact of clock skew between client and server during OAuth token validation. Honors provided a clock skew buffer. Accounts for round-trip time for a more accurate expiry calculation. Improves token validation reliability. closes #62
1 parent bab9d8d commit ee63212

3 files changed

Lines changed: 21 additions & 2 deletions

File tree

apimatic_core/utilities/auth_helper.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ def get_current_utc_timestamp():
2121
return calendar.timegm(datetime.now().utctimetuple())
2222

2323
@staticmethod
24-
def is_token_expired(token_expiry):
24+
def is_token_expired(token_expiry, clock_skew_time=None):
2525
""" Checks if OAuth token has expired.
2626
2727
Returns:
2828
bool: True if token has expired, False otherwise.
2929
3030
"""
31+
if clock_skew_time is not None and token_expiry is not None:
32+
token_expiry -= clock_skew_time
3133
utc_now = AuthHelper.get_current_utc_timestamp()
3234
return token_expiry is not None and token_expiry < utc_now
3335

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
setup(
1414
name='apimatic-core',
15-
version='0.2.12',
15+
version='0.2.13',
1616
description='A library that contains core logic and utilities for '
1717
'consuming REST APIs using Python SDKs generated by APIMatic.',
1818
long_description=long_description,

tests/apimatic_core/utility_tests/test_auth_helper.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,23 @@ def test_token_is_expired(self):
3434
expected_token_expired = True
3535
assert actual_token_expired == expected_token_expired
3636

37+
def test_token_is_expired_with_clock_skew(self):
38+
past_timestamp = AuthHelper.get_current_utc_timestamp() + 5
39+
actual_token_expired = AuthHelper.is_token_expired(past_timestamp, 10)
40+
expected_token_expired = True
41+
assert actual_token_expired == expected_token_expired
42+
43+
def test_token_is_not_expired_with_clock_skew(self):
44+
past_timestamp = AuthHelper.get_current_utc_timestamp() + 5
45+
actual_token_expired = AuthHelper.is_token_expired(past_timestamp, 3)
46+
expected_token_expired = False
47+
assert actual_token_expired == expected_token_expired
48+
49+
past_timestamp = AuthHelper.get_current_utc_timestamp() + 5
50+
actual_token_expired = AuthHelper.is_token_expired(past_timestamp, 5)
51+
expected_token_expired = False
52+
assert actual_token_expired == expected_token_expired
53+
3754
def test_token_is_not_expired(self):
3855
past_timestamp = AuthHelper.get_current_utc_timestamp() + 5
3956
actual_token_expired = AuthHelper.is_token_expired(past_timestamp)

0 commit comments

Comments
 (0)