notifications/apps/gooyal_oauth2/rest_framework.py
Sayyid Hamid Mahdavi ac5356bf9c init
2025-06-29 17:09:37 +03:30

27 lines
1.1 KiB
Python

import logging
from oauth2_provider.contrib.rest_framework import TokenMatchesOASRequirements, OAuth2Authentication
from rest_framework.permissions import (
IsAuthenticated
)
logger = logging.getLogger("oauth2_provider")
class IsAuthenticatedOrTokenMatchesOASRequirements(TokenMatchesOASRequirements):
def has_permission(self, request, view):
logger.debug(f'try to authenticate {request} for {view} in IsAuthenticatedOrTokenMatchesOASRequirements')
is_authenticated = IsAuthenticated().has_permission(request, view)
logger.debug(f'is_authenticated: {is_authenticated}')
oauth2authenticated = False
if is_authenticated:
oauth2authenticated = isinstance(request.successful_authenticator, OAuth2Authentication)
logger.debug(f'oauth2authenticated: {oauth2authenticated}')
token_has_scope = TokenMatchesOASRequirements()
logger.debug(f'token_has_scope: {token_has_scope}')
result = (is_authenticated and not oauth2authenticated) or token_has_scope.has_permission(request, view)
logger.debug(f'authentication result: {result}')
return result