88
99from .models import ProtectedCapability
1010
11+ import apps .logging .request_logger as logging
12+
13+ logger = logging .getLogger (logging .DEBUG_GENERAL )
14+
1115
1216class BBCapabilitiesPermissionTokenScopeMissingException (APIException ):
1317 # BB2-237 custom exception
@@ -17,6 +21,7 @@ class BBCapabilitiesPermissionTokenScopeMissingException(APIException):
1721class TokenHasProtectedCapability (permissions .BasePermission ):
1822
1923 def has_permission (self , request , view ) -> bool : # type: ignore
24+ logger .warning ({"has_permission" : "start" })
2025 token = request .auth
2126 access_token_query_param = request .GET .get ("access_token" , None )
2227
@@ -27,9 +32,11 @@ def has_permission(self, request, view) -> bool: # type: ignore
2732 )
2833
2934 if not token :
35+ logger .warning ("has_permission: not token" )
3036 return False
3137
3238 if not switch_is_active ("require-scopes" ):
39+ logger .warning ("has_permission: switch_is_active('require-scopes')" )
3340 return True
3441
3542 if hasattr (token , "scope" ): # OAuth 2
@@ -43,17 +50,33 @@ def has_permission(self, request, view) -> bool: # type: ignore
4350 slug__in = token_scopes
4451 ).values_list ('protected_resources' , flat = True ).all ())
4552
53+ logger .warning ({"token_scopes" : token_scopes , "scopes" : scopes })
54+
4655 for scope in scopes :
4756 for method , path in json .loads (scope ):
57+ logger .warning ({"scope in scopes" : scope ,
58+ "method" : method ,
59+ "path" : path ,
60+ "request.method" : request .method ,
61+ "request.path" : request .path })
4862 if method != request .method :
63+ logger .warning ({"A" : 1 })
64+ logger .warning ({"request_method" : request .method })
4965 continue
5066 if path == request .path :
67+ logger .warning ({"A" : 2 })
68+ logger .warning ({"path == request.path" : (path == request .path )})
5169 return True
5270 if re .fullmatch (path , request .path ) is not None :
71+ logger .warning ({"A" : 3 })
5372 return True
73+ logger .warning ({"end-of-scope-in-scopes loop" : "here" })
74+
75+ logger .warning ("has_permission: scope not matched/found" )
5476 return False
5577 else :
5678 # BB2-237: Replaces ASSERT with exception. We should never reach here.
5779 mesg = ("TokenHasScope requires the `oauth2_provider.rest_framework.OAuth2Authentication`"
5880 " authentication class to be used." )
81+ logger .warning ("has_permission: end of line scope missing exception" )
5982 raise BBCapabilitiesPermissionTokenScopeMissingException (mesg )
0 commit comments