Skip to content

Commit

Permalink
#15 make sure token is valid for at least 5 more minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
hjacobs committed Apr 27, 2016
1 parent 7af0c9b commit c0ef4c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
from unittest.mock import MagicMock


def test_is_valid():
now = time.time()
assert not zign.api.is_valid({})
assert not zign.api.is_valid({'creation_time': now - 3610, 'expires_in': 3600})
assert zign.api.is_valid({'creation_time': now - 100, 'expires_in': 600})
# still valid for 2 minutes, but we only return tokens valid for at least 5 more minutes
assert not zign.api.is_valid({'creation_time': now - 3480, 'expires_in': 3600})


def test_get_new_token_auth_fail(monkeypatch):
response = MagicMock(status_code=401)
monkeypatch.setattr('requests.get', MagicMock(return_value=response))
Expand Down
4 changes: 3 additions & 1 deletion zign/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

from .config import KEYRING_KEY, TOKENS_FILE_PATH

TOKEN_MINIMUM_VALIDITY_SECONDS = 60*5 # 5 minutes


class ServerError(Exception):
def __init__(self, message):
Expand Down Expand Up @@ -150,7 +152,7 @@ def get_named_token(scope, realm, name, user, password, url=None,

def is_valid(token: dict):
now = time.time()
return token and now < (token.get('creation_time', 0) + token.get('expires_in', 0))
return token and now < (token.get('creation_time', 0) + token.get('expires_in', 0) - TOKEN_MINIMUM_VALIDITY_SECONDS)


def is_user_scope(scope: str):
Expand Down

0 comments on commit c0ef4c6

Please sign in to comment.