Skip to content

Commit

Permalink
0.1.8 JWTAuthMiddleware: Can customize whether to login user after su…
Browse files Browse the repository at this point in the history
…ccessful authentication
  • Loading branch information
Tomáš Rychlik committed May 6, 2014
1 parent 95d9469 commit f7ce003
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
6 changes: 3 additions & 3 deletions jsonis/jwt/auth_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

class JWTAuthenticationBackend(ModelBackend):
"""Custom django authentication backend using JWT"""
def authenticate(self, authorization_token=None, **kwargs):
def authenticate(self, authentication_token=None, **kwargs):
UserModel = get_user_model()
if authorization_token is None:
if authentication_token is None:
return
try:
token_data = parse_token(authorization_token)
token_data = parse_token(authentication_token)
return UserModel._default_manager.get(pk=token_data['id'])
except JWTParseError:
pass
Expand Down
8 changes: 6 additions & 2 deletions jsonis/jwt/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ class JWTAuthMiddleware(object):
# Required header prefix
required_auth_prefix = 'Bearer'

# Login user after successful authentication
login_user = False

def process_request(self, request):
if not hasattr(request, 'user'):
raise ImproperlyConfigured(
Expand All @@ -24,10 +27,11 @@ def process_request(self, request):
if auth_prefix != self.required_auth_prefix:
raise ValueError

user = authenticate(authorization_token=auth_token)
user = authenticate(authentication_token=auth_token)
if user:
request.user = user
login(request, user)
if self.login_user:
login(request, user)

except KeyError:
# There is no self.header
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

setup(
name='django-jsonis',
version='0.1.7',
version='0.1.8',
description='Django JSON Utils',
author='Tomas Rychlik',
author_email='[email protected]',
Expand Down

0 comments on commit f7ce003

Please sign in to comment.