#31 API basic implementation Token Authentication

* adds token checking to AbstractModelAPIView
* adds user accessibility filtering for intervention API v1
* extends fetch_and_serialize() method to take a dict for db filtering instead of a single field and value
* organizes urlnames into supporting formats like "api:v1:intervention"
This commit is contained in:
2022-01-21 16:15:16 +01:00
parent 134651c8f7
commit cf82f4b223
6 changed files with 64 additions and 16 deletions

View File

@@ -1,4 +1,6 @@
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.utils import timezone
from konova.utils.generators import generate_token
@@ -21,3 +23,26 @@ class APIUserToken(models.Model):
def __str__(self):
return self.token
@staticmethod
def get_user_from_token(token: str):
""" Getter for the related user object
Args:
token (str): The used token
Returns:
user (User): Otherwise None
"""
_today = timezone.now().date()
try:
token_obj = APIUserToken.objects.get(
token=token,
)
if not token_obj.is_active:
raise PermissionError("Token unverified")
if token_obj.valid_until is not None and token_obj.valid_until < _today:
raise PermissionError("Token validity expired")
except ObjectDoesNotExist:
raise PermissionError("Token invalid")
return token_obj.user