diff --git a/python/nav/models/api.py b/python/nav/models/api.py index e9ba485fee..0e15a3d91c 100644 --- a/python/nav/models/api.py +++ b/python/nav/models/api.py @@ -85,8 +85,8 @@ class JWTRefreshToken(models.Model): def __str__(self): return self.token - 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: @@ -94,15 +94,15 @@ def is_active(self) -> bool: 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() @@ -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): diff --git a/tests/unittests/models/jwtrefreshtoken_test.py b/tests/unittests/models/jwtrefreshtoken_test.py index bc9a7e74bf..0a94085b13 100644 --- a/tests/unittests/models/jwtrefreshtoken_test.py +++ b/tests/unittests/models/jwtrefreshtoken_test.py @@ -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: @@ -90,16 +90,16 @@ 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) @@ -107,7 +107,7 @@ def test_should_return_same_body_as_when_token_was_encoded( 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) @@ -116,8 +116,8 @@ 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, @@ -125,12 +125,12 @@ def refresh_token_data() -> Dict[Any, str]: "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\