diff options
| author | Tom Christie | 2013-03-07 17:43:13 +0000 | 
|---|---|---|
| committer | Tom Christie | 2013-03-07 17:43:13 +0000 | 
| commit | a4b33992a5e2affb710d0c16f2286d8ddc81f07c (patch) | |
| tree | 29da9798f52a8ab1376f08b70d729e65caabebd3 /rest_framework/authentication.py | |
| parent | 1d62594fa9ed87545a312681f999bbfa0237491b (diff) | |
| parent | 5a56f92abf5f52ac153c4faa1b75af519c96a207 (diff) | |
| download | django-rest-framework-a4b33992a5e2affb710d0c16f2286d8ddc81f07c.tar.bz2 | |
Merge OAuth2 work.
Diffstat (limited to 'rest_framework/authentication.py')
| -rw-r--r-- | rest_framework/authentication.py | 78 | 
1 files changed, 78 insertions, 0 deletions
| diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index 8ee3a900..4d6e3375 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -7,6 +7,7 @@ from django.core.exceptions import ImproperlyConfigured  from rest_framework import exceptions, HTTP_HEADER_ENCODING  from rest_framework.compat import CsrfViewMiddleware  from rest_framework.compat import oauth, oauth_provider, oauth_provider_store +from rest_framework.compat import oauth2_provider, oauth2_provider_forms, oauth2_provider_backends  from rest_framework.authtoken.models import Token  import base64 @@ -251,3 +252,80 @@ class OAuthAuthentication(BaseAuthentication):          Checks nonce of request, and return True if valid.          """          return oauth_provider_store.check_nonce(request, oauth_request, oauth_request['oauth_nonce']) + + +class OAuth2Authentication(BaseAuthentication): +    """ +    OAuth 2 authentication backend using `django-oauth2-provider` +    """ +    require_active = True + +    def __init__(self, **kwargs): +        super(OAuth2Authentication, self).__init__(**kwargs) +        if oauth2_provider is None: +            raise ImproperlyConfigured("The 'django-oauth2-provider' package could not be imported. It is required for use with the 'OAuth2Authentication' class.") + +    def authenticate(self, request): +        """ +        The Bearer type is the only finalized type + +        Read the spec for more details +        http://tools.ietf.org/html/rfc6749#section-7.1 +        """ +        auth = request.META.get('HTTP_AUTHORIZATION', '').split() +        if not auth or auth[0].lower() != "bearer": +            raise exceptions.AuthenticationFailed('Invalid Authorization token type') + +        if len(auth) != 2: +            raise exceptions.AuthenticationFailed('Invalid token header') + +        return self.authenticate_credentials(request, auth[1]) + +    def authenticate_credentials(self, request, access_token): +        """ +        :returns: two-tuple of (user, auth) if authentication succeeds, or None otherwise. +        """ + +        # authenticate the client +        oauth2_client_form = oauth2_provider_forms.ClientAuthForm(request.REQUEST) +        if not oauth2_client_form.is_valid(): +            raise exceptions.AuthenticationFailed("Client could not be validated") +        client = oauth2_client_form.cleaned_data.get('client') + +        # retrieve the `oauth2_provider.models.OAuth2AccessToken` instance from the access_token +        auth_backend = oauth2_provider_backends.AccessTokenBackend() +        token = auth_backend.authenticate(access_token, client) +        if token is None: +            raise exceptions.AuthenticationFailed("Invalid token")  # does not exist or is expired + +        # TODO check scope + +        if not self.check_active(token.user): +            raise exceptions.AuthenticationFailed('User not active: %s' % token.user.username) + +        if client and token: +            request.user = token.user +            return (request.user, None) + +        raise exceptions.AuthenticationFailed( +            'You are not allowed to access this resource.') + +    def authenticate_header(self, request): +        """ +        Bearer is the only finalized type currently + +        Check details on the `OAuth2Authentication.authenticate` method +        """ +        return 'Bearer' + +    def check_active(self, user): +        """ +        Ensures the user has an active account. + +        Optimized for the ``django.contrib.auth.models.User`` case. +        """ +        if not self.require_active: +            # Ignore & move on. +            return True + +        return user.is_active | 
