This commit is contained in:
Alan Justino da Silva 2013-08-15 12:33:26 -07:00
commit 41ca63ddab

View File

@ -4,7 +4,7 @@ Provides various authentication policies.
from __future__ import unicode_literals from __future__ import unicode_literals
import base64 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 django.core.exceptions import ImproperlyConfigured
from rest_framework import exceptions, HTTP_HEADER_ENCODING from rest_framework import exceptions, HTTP_HEADER_ENCODING
from rest_framework.compat import CsrfViewMiddleware from rest_framework.compat import CsrfViewMiddleware
@ -346,3 +346,21 @@ class OAuth2Authentication(BaseAuthentication):
Check details on the `OAuth2Authentication.authenticate` method Check details on the `OAuth2Authentication.authenticate` method
""" """
return 'Bearer realm="%s"' % self.www_authenticate_realm 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