dop.py 1.95 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
"""
Adapter to isolate django-oauth2-provider dependencies
"""

from provider.oauth2 import models
from provider import constants, scope


class DOPAdapter(object):
    """
    Standard interface for working with django-oauth2-provider
    """

    backend = object()

    def create_confidential_client(self, name, user, redirect_uri, client_id=None):
        """
        Create an oauth client application that is confidential.
        """
        return models.Client.objects.create(
            name=name,
            user=user,
            client_id=client_id,
            redirect_uri=redirect_uri,
            client_type=constants.CONFIDENTIAL,
        )

    def create_public_client(self, name, user, redirect_uri, client_id=None):
        """
        Create an oauth client application that is public.
        """
        return models.Client.objects.create(
            name=name,
            user=user,
            client_id=client_id,
            redirect_uri=redirect_uri,
            client_type=constants.PUBLIC,
        )

    def get_client(self, **filters):
        """
        Get the oauth client application with the specified filters.

        Wraps django's queryset.get() method.
        """
        return models.Client.objects.get(**filters)

    def get_client_for_token(self, token):
        """
        Given an AccessToken object, return the associated client application.
        """
        return token.client

    def get_access_token(self, token_string):
        """
        Given a token string, return the matching AccessToken object.
        """
        return models.AccessToken.objects.get(token=token_string)

    def normalize_scopes(self, scopes):
        """
        Given a list of scopes, return a space-separated list of those scopes.
        """
        return ' '.join(scopes)

    def get_token_scope_names(self, token):
        """
        Given an access token object, return its scopes.
        """
        return scope.to_names(token.scope)