From aeaf3517d31091c5740d8ee718f069e7fdcf8182 Mon Sep 17 00:00:00 2001 From: Alan Justino Date: Tue, 13 Aug 2013 14:55:18 -0300 Subject: [PATCH] New authentication.TokenBackend Django's backend To be used at settings like this: AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'rest_framework.authentication.TokenBackend', ) --- rest_framework/authentication.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index cf001a24d..b45b5aadf 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -4,7 +4,7 @@ Provides various authentication policies. from __future__ import unicode_literals import base64 -from django.contrib.auth import authenticate +from django.contrib.auth import authenticate, backends as django_backends from django.core.exceptions import ImproperlyConfigured from rest_framework import exceptions, HTTP_HEADER_ENCODING from rest_framework.compat import CsrfViewMiddleware @@ -346,3 +346,21 @@ class OAuth2Authentication(BaseAuthentication): Check details on the `OAuth2Authentication.authenticate` method """ return 'Bearer realm="%s"' % self.www_authenticate_realm + + +class TokenBackend(django_backends.ModelBackend): + """A Django authentication backend for Tokens + + Intend to be used listed at settings.AUTHENTICATION_BACKENDS + """ + def authenticate(self, token=None): + authenticator = TokenAuthentication() + user = None + + if token: + try: + user, token_object = authenticator.authenticate_credentials(token) + except exceptions.AuthenticationFailed: + pass # return None + + return user