|
| 1 | +# coding: utf-8 |
| 2 | +from __future__ import absolute_import, unicode_literals |
| 3 | + |
| 4 | +import jwt |
| 5 | +from django.contrib.auth import get_user_model |
| 6 | +from django.utils.translation import ugettext as _ |
| 7 | +from rest_framework import exceptions |
| 8 | +from rest_framework.authentication import ( |
| 9 | + BaseAuthentication, get_authorization_header |
| 10 | +) |
| 11 | + |
| 12 | +from rest_framework_sso.settings import api_settings |
| 13 | + |
| 14 | +decode_jwt_token = api_settings.DECODE_JWT_TOKEN |
| 15 | + |
| 16 | + |
| 17 | +class JWTAuthentication(BaseAuthentication): |
| 18 | + """ |
| 19 | + JWT token based authentication. |
| 20 | +
|
| 21 | + Clients should authenticate by passing the token key in the "Authorization" |
| 22 | + HTTP header, prepended with the string "JWT ". For example: |
| 23 | +
|
| 24 | + Authorization: JWT eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJsb2NhbG... |
| 25 | + """ |
| 26 | + |
| 27 | + def authenticate(self, request): |
| 28 | + auth = get_authorization_header(request).split() |
| 29 | + |
| 30 | + if not auth or auth[0].lower() != api_settings.AUTHENTICATE_HEADER.lower().encode('utf-8'): |
| 31 | + return None |
| 32 | + |
| 33 | + if len(auth) == 1: |
| 34 | + msg = _('Invalid token header. No credentials provided.') |
| 35 | + raise exceptions.AuthenticationFailed(msg) |
| 36 | + elif len(auth) > 2: |
| 37 | + msg = _('Invalid token header. Token string should not contain spaces.') |
| 38 | + raise exceptions.AuthenticationFailed(msg) |
| 39 | + |
| 40 | + try: |
| 41 | + token = auth[1].decode() |
| 42 | + except UnicodeError: |
| 43 | + msg = _('Invalid token header. Token string should not contain invalid characters.') |
| 44 | + raise exceptions.AuthenticationFailed(msg) |
| 45 | + |
| 46 | + try: |
| 47 | + payload = decode_jwt_token(token=token) |
| 48 | + except jwt.ExpiredSignature: |
| 49 | + msg = _('Signature has expired.') |
| 50 | + raise exceptions.AuthenticationFailed(msg) |
| 51 | + except jwt.DecodeError: |
| 52 | + msg = _('Error decoding signature.') |
| 53 | + raise exceptions.AuthenticationFailed(msg) |
| 54 | + except jwt.InvalidTokenError: |
| 55 | + raise exceptions.AuthenticationFailed() |
| 56 | + |
| 57 | + return self.authenticate_credentials(payload=payload) |
| 58 | + |
| 59 | + def authenticate_credentials(self, payload): |
| 60 | + from rest_framework_sso.models import SessionToken |
| 61 | + |
| 62 | + user_model = get_user_model() |
| 63 | + |
| 64 | + if not SessionToken._meta.abstract: |
| 65 | + try: |
| 66 | + SessionToken.objects.active().get(pk=payload.get('sid'), uid=payload.get('uid')) |
| 67 | + except SessionToken.DoesNotExist: |
| 68 | + raise exceptions.AuthenticationFailed(_('Invalid token.')) |
| 69 | + |
| 70 | + try: |
| 71 | + user = user_model.objects.get(pk=payload.get('uid')) |
| 72 | + except user_model.DoesNotExist: |
| 73 | + raise exceptions.AuthenticationFailed(_('Invalid token.')) |
| 74 | + |
| 75 | + if not user.is_active: |
| 76 | + raise exceptions.AuthenticationFailed(_('User inactive or deleted.')) |
| 77 | + |
| 78 | + return user, payload |
| 79 | + |
| 80 | + def authenticate_header(self, request): |
| 81 | + return api_settings.AUTHENTICATE_HEADER |
0 commit comments