Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add model for JWT refresh token #3268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions changelog.d/3268.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add database model for JWT refresh tokens
22 changes: 22 additions & 0 deletions python/nav/models/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,25 @@ def get_absolute_url(self):

class Meta(object):
db_table = 'apitoken'


class JWTRefreshToken(models.Model):

name = VarcharField(unique=True)
description = models.TextField(null=True, blank=True)
expires = models.DateTimeField()
activates = models.DateTimeField()
hash = VarcharField()

def __str__(self):
return self.name

def is_active(self) -> bool:
"""True if token is active. A token is considered active when
`activates` is in the past and `expires` is in the future.
"""
now = datetime.now()
return now >= self.activates and now < self.expires

class Meta(object):
db_table = 'jwtrefreshtoken'
8 changes: 8 additions & 0 deletions python/nav/models/sql/changes/sc.05.13.0001.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CREATE TABLE manage.JWTRefreshToken (
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL UNIQUE,
description VARCHAR,
expires TIMESTAMP NOT NULL,
activates TIMESTAMP NOT NULL,
hash VARCHAR NOT NULL
);
58 changes: 58 additions & 0 deletions tests/unittests/models/jwtrefreshtoken_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
from datetime import datetime, timedelta

from nav.models.api import JWTRefreshToken


class TestIsActive:
def test_should_return_false_if_token_activates_in_the_future(self):
now = datetime.now()
token = JWTRefreshToken(
name="testtoken",
hash="dummyhash",
expires=now + timedelta(hours=1),
activates=now + timedelta(hours=1),
)
assert not token.is_active()

def test_should_return_false_if_token_expires_in_the_past(self):
now = datetime.now()
token = JWTRefreshToken(
name="testtoken",
hash="dummyhash",
expires=now - timedelta(hours=1),
activates=now - timedelta(hours=1),
)
assert not token.is_active()

def test_should_return_true_if_token_activates_in_the_past_and_expires_in_the_future(
self,
):
now = datetime.now()
token = JWTRefreshToken(
name="testtoken",
hash="dummyhash",
expires=now + timedelta(hours=1),
activates=now - timedelta(hours=1),
)
assert token.is_active()

def test_should_return_true_if_token_activates_now_and_expires_in_the_future(self):
now = datetime.now()
token = JWTRefreshToken(
name="testtoken",
hash="dummyhash",
expires=now + timedelta(hours=1),
activates=now,
)
assert token.is_active()


def test_string_representation_should_match_name():
now = datetime.now()
token = JWTRefreshToken(
name="testtoken",
hash="dummyhash",
expires=now + timedelta(hours=1),
activates=now - timedelta(hours=1),
)
assert str(token) == token.name
Loading