wallet/dot_restrict_scopes/scopes.py
2019-11-14 12:32:11 +03:30

43 lines
1.7 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Django OAuth Toolkit scopes backend for the dot-restrict-scopes package.
"""
from django.conf import settings
from django.utils import module_loading
from oauth2_provider.scopes import BaseScopes
class RestrictApplicationScopes(BaseScopes):
"""
Scopes backend that wraps another backend and restricts the scopes available
to an application based on the application's ``allowed_scopes``.
"""
def __init__(self):
# Initialise the wrapped backend from settings
self._wrapped = module_loading.import_string(
getattr(settings, 'DOT_RESTRICT_SCOPES', {}).get(
'WRAPPED_SCOPES_BACKEND_CLASS',
'oauth2_provider.scopes.SettingsScopes'
)
)()
def get_all_scopes(self):
# Just return all the available scopes from the wrapped backend
return self._wrapped.get_all_scopes()
def get_available_scopes(self, application = None, request = None, *args, **kwargs):
# Get the available scopes from the wrapped backend, then filter them
# based on the allowed_scopes of the application
scopes = self._wrapped.get_available_scopes(application, request, *args, **kwargs)
if application:
scopes = [s for s in scopes if s in application.allowed_scopes]
return scopes
def get_default_scopes(self, application = None, request = None, *args, **kwargs):
# Get the default scopes from the wrapped backend, then filter them
# based on the allowed_scopes of the application
scopes = self._wrapped.get_default_scopes(application, request, *args, **kwargs)
if application:
scopes = [s for s in scopes if s in application.allowed_scopes]
return scopes