Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions apns2/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ def get_authorization_header(self, topic: Optional[str]) -> str:
token = self._get_or_create_topic_token()
return 'bearer %s' % token

@staticmethod
def _is_expired_token(issue_date: float) -> bool:
return time.time() > issue_date + DEFAULT_TOKEN_LIFETIME
def _is_expired_token(self, issue_date: float) -> bool:
return time.time() > issue_date + self.__token_lifetime

@staticmethod
def _get_signing_key(key_path: str) -> str:
Expand Down
29 changes: 18 additions & 11 deletions test/test_credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,24 @@

@pytest.fixture
def token_credentials():
return TokenCredentials('test/eckey.pem', '1QBCDJ9RST', '3Z24IP123A')
return TokenCredentials(
auth_key_path='test/eckey.pem',
auth_key_id='1QBCDJ9RST',
team_id='3Z24IP123A',
token_lifetime=30, # seconds
)


def test_token_expiration(token_credentials):
# As long as the token lifetime hasn't elapsed, this should work. To
# be really careful, we should check how much time has elapsed to
# know if it fail. But, either way, we'd have to come up with a good
# lifetime for future tests...

with freeze_time('2012-01-14'):
expiring_header = token_credentials.get_authorization_header(TOPIC)

new_header = token_credentials.get_authorization_header(TOPIC)
assert expiring_header != new_header
with freeze_time('2012-01-14 12:00:00'):
header1 = token_credentials.get_authorization_header(TOPIC)

# 20 seconds later, before expiration, same JWT
with freeze_time('2012-01-14 12:00:20'):
header2 = token_credentials.get_authorization_header(TOPIC)
assert header1 == header2

# 35 seconds later, after expiration, new JWT
with freeze_time('2012-01-14 12:00:40'):
header3 = token_credentials.get_authorization_header(TOPIC)
assert header3 != header1