-
Notifications
You must be signed in to change notification settings - Fork 7
/
authentication.py
28 lines (22 loc) · 994 Bytes
/
authentication.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from django.core.exceptions import ValidationError
from django.contrib.auth import get_user_model
from django.utils.translation import ugettext_lazy as _
from rest_framework.authentication import (
TokenAuthentication, exceptions)
from gwml2.models.well_management.user import UserUUID
User = get_user_model()
class GWMLTokenAthentication(TokenAuthentication):
""" Authentication using GWML token
"""
def authenticate_credentials(self, key):
model = UserUUID
try:
token = model.objects.get(uuid=key)
user = User.objects.get(id=token.user_id)
except (model.DoesNotExist, ValidationError):
raise exceptions.AuthenticationFailed(_('Invalid token.'))
except User.DoesNotExist:
raise exceptions.AuthenticationFailed(_('No user found for the token.'))
if not user.is_active:
raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))
return (user, token)