wallet/apps/gooyal_oauth2/scopes.py
2020-07-21 13:06:39 +04:30

33 lines
1 KiB
Python

"""
Django OAuth Toolkit scopes backend for the gooyal-dynamic-scopes package.
"""
from django.conf import settings
from django.utils import module_loading
from oauth2_provider.scopes import BaseScopes
from .models import Scope
class Scopes(BaseScopes):
"""
Scopes backend that provides scopes from a Django model.
"""
def get_queryset(self, application=None):
queryset = Scope.objects.all()
if application:
queryset = queryset.filter(name__in=application.allowed_scopes)
return queryset
def get_all_scopes(self):
return {scope.name: scope.description for scope in self.get_queryset().all()}
def get_available_scopes(self, application=None, request=None, *args, **kwargs):
scopes = [scope.name for scope in self.get_queryset(application).all()]
return scopes
def get_default_scopes(self, application=None, request=None, *args, **kwargs):
return [scope.name for scope in self.get_queryset(application).filter(is_default=True).all()]