-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
0.1.6 - JWT refactoring including custom django middleware and auth b…
…ackend
- Loading branch information
Tomáš Rychlik
committed
May 5, 2014
1 parent
db6848f
commit 872877f
Showing
7 changed files
with
100 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from views import JWTAuthorizationMixin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.backends import ModelBackend | ||
|
||
from .utils import parse_token, JWTParseError | ||
|
||
class JWTAuthenticationBackend(ModelBackend): | ||
"""Custom django authentication backend using JWT""" | ||
def authenticate(self, authorization_token=None, **kwargs): | ||
UserModel = get_user_model() | ||
if authorization_token is None: | ||
return | ||
try: | ||
token_data = parse_token(authorization_token) | ||
return UserModel._default_manager.get(pk=token_data['id']) | ||
except JWTParseError: | ||
pass | ||
except UserModel.DoesNotExist: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from django.contrib.auth import authenticate, login, get_user_model | ||
from django.core.exceptions import ImproperlyConfigured | ||
|
||
class JWTAuthMiddleware(object): | ||
"""Authentication Middleware that checks for a JSON Web Token in the Authorization header | ||
JWTAuthenticationBackend needs to be added to AUTHENTICATION_BACKENDS as well""" | ||
|
||
# Used HTTP Header | ||
header = 'HTTP_AUTHORIZATION' | ||
|
||
# Required header prefix | ||
required_auth_prefix = 'Bearer' | ||
|
||
def process_request(self, request): | ||
if not hasattr(request, 'user'): | ||
raise ImproperlyConfigured( | ||
"The JWT auth middleware requires the authentication middleware to be installed. Edit your" | ||
" MIDDLEWARE_CLASSES setting to insert 'django.contrib.auth.middleware.AuthenticationMiddleware'" | ||
" before the JWTAuthMiddleware class.") | ||
|
||
try: | ||
auth_prefix, auth_token = request.META[self.header].split(' ') | ||
if auth_prefix != self.required_auth_prefix: | ||
raise ValueError | ||
|
||
user = authenticate(authorization_token=auth_token) | ||
if user: | ||
request.user = user | ||
login(request, user) | ||
|
||
except KeyError: | ||
# There is no self.header | ||
pass | ||
except ValueError: | ||
# Header prefix doesn't match | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import jwt | ||
|
||
from django.conf import settings | ||
|
||
class JWTParseError(Exception): | ||
pass | ||
|
||
class JWTDecodeError(JWTParseError): | ||
pass | ||
|
||
class JWTExpiredError(JWTParseError): | ||
pass | ||
|
||
class JWTNoDataError(JWTDecodeError): | ||
pass | ||
|
||
def parse_token(auth_token): | ||
"""Parser given JSON Web Token and returns contained data""" | ||
try: | ||
decoded_token = jwt.decode(auth_token, settings.FIREBASE_SECRET) | ||
except (jwt.DecodeError, ValueError): | ||
raise JWTDecodeError('Decoding of authorization token failed') | ||
except jwt.ExpiredSignature: | ||
raise JWTExpiredError('Expired authorization token') | ||
|
||
try: | ||
return decoded_token['d'] | ||
except (TypeError, KeyError): | ||
raise JWTNoDataError('No data in authorization token') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
#!/usr/bin/env python | ||
|
||
from distutils.core import setup | ||
from setuptools import setup | ||
|
||
setup( | ||
name='django-jsonis', | ||
version='0.1.5', | ||
version='0.1.6', | ||
description='Django JSON Utils', | ||
author='Tomas Rychlik', | ||
author_email='[email protected]', | ||
packages=['jsonis', 'jsonis.templatetags'], | ||
packages=['jsonis', 'jsonis.templatetags', 'jsonis.jwt'], | ||
license='MIT', | ||
url='https://github.com/rychlis/django-jsonis', | ||
install_requires=[ | ||
|