Skip to content

Commit

Permalink
ensure use of a valid scope for request
Browse files Browse the repository at this point in the history
  • Loading branch information
mickmis committed Mar 19, 2024
1 parent 491563e commit a9c2a6d
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions monitoring/uss_qualifier/resources/astm/f3548/v21/dss.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,15 @@ def _uses_scope(self, *scopes: Tuple[str]) -> None:
f"{fullname(type(self))} client called {calling_function_name(1)} which requires the use of the scope `{scope}`, but this DSSInstance is only authorized to perform actions with the scopes {' or '.join(self._scopes_authorized)}"
)

def _uses_any_scope(self, *scopes: str) -> None:
if not any([scope in self._scopes_authorized for scope in scopes]):
raise ValueError(
f"{fullname(type(self))} client called {calling_function_name(1)} which requires the use of any of the scopes `{', '.join(scopes)}`, but this DSSInstance is only authorized to perform actions with the scopes {' or '.join(self._scopes_authorized)}"
)
def _uses_any_scope(self, *scopes: str) -> str:
"""Validates that at least a required scope is authorized for a request.
Additionally, returns a valid scope that may be used for the request."""
for scope in scopes:
if scope in self._scopes_authorized:
return scope
raise ValueError(
f"{fullname(type(self))} client called {calling_function_name(1)} which requires the use of any of the scopes `{', '.join(scopes)}`, but this DSSInstance is only authorized to perform actions with the scopes {' or '.join(self._scopes_authorized)}"
)

def can_use_scope(self, scope: str) -> bool:
return scope in self._scopes_authorized
Expand Down Expand Up @@ -390,7 +394,7 @@ def make_report(
Raises:
* QueryError: if request failed, if HTTP status code is different than 201, or if the parsing of the response failed.
"""
self._uses_any_scope(
use_scope = self._uses_any_scope(
Scope.ConstraintManagement,
Scope.ConstraintProcessing,
Scope.StrategicCoordination,
Expand All @@ -406,9 +410,7 @@ def make_report(
op.path,
QueryType.F3548v21DSSMakeDssReport,
self.participant_id,
scope=next(
iter(self._scopes_authorized)
), # any scope is valid for this endpoint
scope=use_scope,
json=req,
)

Expand Down

0 comments on commit a9c2a6d

Please sign in to comment.