accounts/apps/gooyal_oauth2/scopes.py
Sayyid Hamid Mahdavi c1c55c45a6 bug fix
2026-04-06 14:34:54 +03:30

33 lines
1.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
# TODO: use redis for it
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.final_name: scope.final_description for scope in self.get_queryset().all()}
def get_available_scopes(self, application=None, request=None, *args, **kwargs):
scopes = [scope.final_name for scope in self.get_queryset(application).all()]
return scopes
def get_default_scopes(self, application=None, request=None, *args, **kwargs):
return [scope.final_name for scope in self.get_queryset(application).filter(is_default=True).all()]