1- import base64
2- from rest_framework import permissions
1+ import logging
32from django .conf import settings
4- from api import manager
3+ from rest_framework import permissions
4+
5+ from api import clients
56from api .models import blocklist
67from api .models .workspace import Workspace , WorkspaceMember
78
9+ logger = logging .getLogger (__name__ )
10+
811
912def get_app_status (app ):
1013 block = blocklist .Blocklist .get_blocklist (app )
1114 if block :
1215 return False , block .remark
1316 if settings .WORKFLOW_MANAGER_URL :
14- status = manager .WorkspaceAPI ().get_status (app .workspace_id )
17+ status = clients .WorkspaceAPI ().get_status (app .workspace_id )
1518 if not status ["is_active" ]:
1619 return False , status ["message" ]
1720 return True , None
@@ -41,8 +44,9 @@ def has_object_permission(self, request, view, obj):
4144 return True
4245 elif getattr (obj , "user" , None ) == request .user :
4346 return True
44- elif isinstance (obj , Workspace ) or hasattr (obj , 'workspace' ):
45- workspace = obj if isinstance (obj , Workspace ) else obj .workspace
47+ elif isinstance (obj , Workspace ) or hasattr (obj , 'workspace' ) or hasattr (obj , 'app' ):
48+ workspace = obj if isinstance (obj , Workspace ) else getattr (
49+ obj , "workspace" , None ) or getattr (getattr (obj , 'app' , None ), 'workspace' , None )
4650 if request .method in ["GET" , "HEAD" , "OPTIONS" ]:
4751 allowed_roles = ["viewer" , "member" , "admin" ]
4852 elif request .method in ["POST" , "PUT" , "PATCH" ]:
@@ -55,34 +59,23 @@ def has_object_permission(self, request, view, obj):
5559 return False
5660
5761
58- class IsServiceToken (permissions .BasePermission ):
62+ class HasOAuthScope (permissions .BasePermission ):
5963 """
60- The service token is used for internal communication between Drycc components,
61- such as the builder and Quickwit.
64+ Object-level permission to allow only requests with specific OAuth scopes.
65+ The required scopes are defined on the view as `required_oauth_scopes = ['scope1', 'scope2']`
6266 """
67+ client = clients .PassportAPI ()
6368
6469 def has_permission (self , request , view ):
65- """
66- Return `True` if permission is granted, `False` otherwise.
67- """
68- auth_header = request .META .get ('HTTP_X_DRYCC_SERVICE_KEY' )
69- if not auth_header :
70- return False
71- return auth_header == settings .SERVICE_KEY
72-
73-
74- class IsWorkflowManager (permissions .BasePermission ):
75- """
76- View permission to allow workflow manager to perform actions
77- with a special HTTP header
78- """
70+ required_oauth_scopes = getattr (view , 'required_oauth_scopes' , [])
71+ if not required_oauth_scopes :
72+ return True
7973
80- def has_permission (self , request , view ):
81- if request .META .get ("HTTP_AUTHORIZATION" ):
82- token = request .META .get (
83- "HTTP_AUTHORIZATION" ).split (" " )[1 ].encode ("utf8" )
84- access_key , secret_key = base64 .b85decode (token ).decode ("utf8" ).split (":" )
85- if settings .WORKFLOW_MANAGER_ACCESS_KEY == access_key :
86- if settings .WORKFLOW_MANAGER_SECRET_KEY == secret_key :
87- return True
88- return False
74+ auth_header = request .META .get ('HTTP_AUTHORIZATION' , '' )
75+ parts = auth_header .split ()
76+ if len (parts ) == 2 and parts [0 ].lower () == 'bearer' :
77+ token = parts [1 ]
78+ else :
79+ return False
80+ scopes = self .client .get_scopes (token )
81+ return set (required_oauth_scopes ).issubset (scopes )
0 commit comments