Skip to content

Commit

Permalink
Use term "data" instead of "body"
Browse files Browse the repository at this point in the history
"data" and "body" were both used for the same thing, so
changed to only use one of the terms for clarity
  • Loading branch information
stveit committed Nov 13, 2023
1 parent 17759b3 commit 5fe8ee2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 32 deletions.
14 changes: 7 additions & 7 deletions python/nav/models/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,24 +85,24 @@ class JWTRefreshToken(models.Model):
def __str__(self):
return self.token

Check warning on line 86 in python/nav/models/api.py

View check run for this annotation

Codecov / codecov/patch

python/nav/models/api.py#L86

Added line #L86 was not covered by tests

def get_body(self) -> Dict[str, Any]:
"""Body of token as a dict"""
def data(self) -> Dict[str, Any]:
"""Data of token as a dict"""
return self._decode_token(self.token)

def is_active(self) -> bool:
"""True if token is active. A token is considered active when
the nbf claim is in the past and the exp claim is in the future
"""
now = datetime.now()
body = self.get_body()
nbf = datetime.fromtimestamp(body['nbf'])
exp = datetime.fromtimestamp(body['exp'])
data = self.data()
nbf = datetime.fromtimestamp(data['nbf'])
exp = datetime.fromtimestamp(data['exp'])
return now >= nbf and now < exp

def expire(self):
"""Expires the token"""
# Base claims for expired token on existing claims
expired_data = self.get_body()
expired_data = self.data()
expired_data['exp'] = (datetime.now() - timedelta(hours=1)).timestamp()
self.token = self._encode_token(expired_data)
self.save()
Expand Down Expand Up @@ -155,7 +155,7 @@ def _encode_token(cls, token_data: Dict[str, Any]) -> str:

@classmethod
def _decode_token(cls, token: str) -> Dict[str, Any]:
"""Decodes a token in JWT format and returns the body of the decoded token"""
"""Decodes a token in JWT format and returns the data of the decoded token"""
return jwt.decode(token, options={'verify_signature': False})

class Meta(object):
Expand Down
50 changes: 25 additions & 25 deletions tests/unittests/models/jwtrefreshtoken_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,45 @@
class TestGenerateAccessToken:
def test_nbf_should_be_in_the_past(self):
encoded_token = JWTRefreshToken.generate_access_token()
body = JWTRefreshToken._decode_token(encoded_token)
assert body['nbf'] < datetime.now().timestamp()
data = JWTRefreshToken._decode_token(encoded_token)
assert data['nbf'] < datetime.now().timestamp()

def test_exp_should_be_in_the_future(self):
encoded_token = JWTRefreshToken.generate_access_token()
body = JWTRefreshToken._decode_token(encoded_token)
assert body['exp'] > datetime.now().timestamp()
data = JWTRefreshToken._decode_token(encoded_token)
assert data['exp'] > datetime.now().timestamp()

def test_iat_should_be_in_the_past(self):
encoded_token = JWTRefreshToken.generate_access_token()
body = JWTRefreshToken._decode_token(encoded_token)
assert body['iat'] < datetime.now().timestamp()
data = JWTRefreshToken._decode_token(encoded_token)
assert data['iat'] < datetime.now().timestamp()

def test_token_type_should_be_access_token(self):
encoded_token = JWTRefreshToken.generate_access_token()
body = JWTRefreshToken._decode_token(encoded_token)
assert body['token_type'] == "access_token"
data = JWTRefreshToken._decode_token(encoded_token)
assert data['token_type'] == "access_token"


class TestGenerateRefreshToken:
def test_nbf_should_be_in_the_past(self):
encoded_token = JWTRefreshToken.generate_refresh_token()
body = JWTRefreshToken._decode_token(encoded_token)
assert body['nbf'] < datetime.now().timestamp()
data = JWTRefreshToken._decode_token(encoded_token)
assert data['nbf'] < datetime.now().timestamp()

def test_exp_should_be_in_the_future(self):
encoded_token = JWTRefreshToken.generate_refresh_token()
body = JWTRefreshToken._decode_token(encoded_token)
assert body['exp'] > datetime.now().timestamp()
data = JWTRefreshToken._decode_token(encoded_token)
assert data['exp'] > datetime.now().timestamp()

def test_iat_should_be_in_the_past(self):
encoded_token = JWTRefreshToken.generate_refresh_token()
body = JWTRefreshToken._decode_token(encoded_token)
assert body['iat'] < datetime.now().timestamp()
data = JWTRefreshToken._decode_token(encoded_token)
assert data['iat'] < datetime.now().timestamp()

def test_token_type_should_be_refresh_token(self):
encoded_token = JWTRefreshToken.generate_refresh_token()
body = JWTRefreshToken._decode_token(encoded_token)
assert body['token_type'] == "refresh_token"
data = JWTRefreshToken._decode_token(encoded_token)
assert data['token_type'] == "refresh_token"


class TestIsActive:
Expand Down Expand Up @@ -90,24 +90,24 @@ def test_should_make_active_token_inactive(self, refresh_token_data):
assert not token.is_active()


class TestGetBody:
def test_should_return_accurate_representation_of_token_body(
class TestData:
def test_should_return_accurate_representation_of_token_data(
self, refresh_token, refresh_token_data
):
token = JWTRefreshToken(token=refresh_token)
assert token.get_body() == refresh_token_data
assert token.data() == refresh_token_data


class TestDecodeToken:
def test_should_return_same_body_as_when_token_was_encoded(
def test_should_return_same_data_as_when_token_was_encoded(
self, refresh_token, refresh_token_data
):
decoded_data = JWTRefreshToken._decode_token(refresh_token)
assert decoded_data == refresh_token_data


class TestEncodeToken:
def test_should_generate_a_known_token_using_the_same_body(
def test_should_generate_a_known_token_using_the_same_data(
self, refresh_token, refresh_token_data
):
encoded_token = JWTRefreshToken._encode_token(refresh_token_data)
Expand All @@ -116,21 +116,21 @@ def test_should_generate_a_known_token_using_the_same_body(

@pytest.fixture()
def refresh_token_data() -> Dict[Any, str]:
"""Yields the body of the token in the refresh_token fixture"""
body = {
"""Yields the data of the token in the refresh_token fixture"""
data = {
"exp": 1516339022,
"nbf": 1516239022,
"iat": 1516239022,
"aud": "nav",
"iss": "nav",
"token_type": "refresh_token",
}
yield body
yield data


@pytest.fixture()
def refresh_token() -> str:
"""Yields a refresh token with a body matching the refresh_token_data fixture"""
"""Yields a refresh token with data matching the refresh_token_data fixture"""
token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1\
MTYzMzkwMjIsIm5iZiI6MTUxNjIzOTAyMiwiaWF0IjoxNTE2MjM5MDIyLCJh\
dWQiOiJuYXYiLCJpc3MiOiJuYXYiLCJ0b2tlbl90eXBlIjoicmVmcmVzaF90\
Expand Down

0 comments on commit 5fe8ee2

Please sign in to comment.