This commit is contained in:
Raphaël Stefanini 2014-08-03 20:39:27 +00:00
commit a07e620fbe

View File

@ -140,15 +140,17 @@ class TokenAuthentication(BaseAuthentication):
HTTP header, prepended with the string "Token ". For example: HTTP header, prepended with the string "Token ". For example:
Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a Authorization: Token 401f7ac837da42b97f613d789819ff93537bee6a
You can override this class to use a different Token model by specifying
* model -- Your Token Model
* user_field_name -- the name field that hold the user
* key_field_name -- the name of field that hold an authentification key
""" """
model = Token model = Token
""" user_field_name = 'user'
A custom token model may be used, but must have the following properties. key_field_name = 'key'
* key -- The string identifying the token
* user -- The user to which the token belongs
"""
def authenticate(self, request): def authenticate(self, request):
auth = get_authorization_header(request).split() auth = get_authorization_header(request).split()
@ -167,14 +169,16 @@ class TokenAuthentication(BaseAuthentication):
def authenticate_credentials(self, key): def authenticate_credentials(self, key):
try: try:
token = self.model.objects.get(key=key) token = self.model.objects.get(**{self.key_field_name: key})
except self.model.DoesNotExist: except self.model.DoesNotExist:
raise exceptions.AuthenticationFailed('Invalid token') raise exceptions.AuthenticationFailed('Invalid token')
if not token.user.is_active: user = getattr(token, self.user_field_name)
if not user.is_active:
raise exceptions.AuthenticationFailed('User inactive or deleted') raise exceptions.AuthenticationFailed('User inactive or deleted')
return (token.user, token) return (user, token)
def authenticate_header(self, request): def authenticate_header(self, request):
return 'Token' return 'Token'