You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In the current implementation of TokenHasScope, the get_scopes method retrieves the required scopes using this method:
defget_scopes(self, request, view):
try:
returngetattr(view, "required_scopes")
exceptAttributeError:
raiseImproperlyConfigured(
"TokenHasScope requires the view to define the required_scopes attribute"
)
This approach assumes a static required_scopes attribute on the view. However, this design doesn't support cases where the required scope changes dynamically such as different scopes for GET, PUT, PATCH, or DELETE methods in RetrieveUpdateDestroyAPIView like following example:
Modifying TokenHasScope to support a get_required_scopes method in addition to the existing required_scopes attribute. The new behavior could be:
Check if the view has a get_required_scopes method.
If the method exists, call it and use its return value as the required scopes.
If the method does not exist, fall back to the static required_scopes attribute.
Here’s how the updated get_scopes implementation might look:
defget_scopes(self, request, view):
ifhasattr(view, "get_required_scopes"):
returnview.get_required_scopes()
try:
returngetattr(view, "required_scopes")
exceptAttributeError:
raiseImproperlyConfigured(
"TokenHasScope requires the view to define either the required_scopes attribute or the get_required_scopes method"
)
Another solution is to adjust TokenHasScope to check for get_scopes() in the view, if the view uses ScopedResourceMixin, Instead of directly checking for the required_scopes attribute
The text was updated successfully, but these errors were encountered:
birddevelper
changed the title
lack of get_required_scopes in TokenHasScope for Dynamic Scope Handling in Views
lack of dynamic scope handling in views in TokenHasScope
Dec 18, 2024
birddevelper
changed the title
lack of dynamic scope handling in views in TokenHasScope
Lack of dynamic scope handling in TokenHasScope
Dec 18, 2024
The Current Limitation
In the current implementation of
TokenHasScope
, theget_scopes
method retrieves the required scopes using this method:This approach assumes a static
required_scopes
attribute on the view. However, this design doesn't support cases where the required scope changes dynamically such as different scopes forGET, PUT, PATCH, or DELETE
methods inRetrieveUpdateDestroyAPIView
like following example:Solutions
Modifying
TokenHasScope
to support aget_required_scopes
method in addition to the existingrequired_scopes
attribute. The new behavior could be:Here’s how the updated get_scopes implementation might look:
TokenHasScope
to check forget_scopes()
in the view, if the view usesScopedResourceMixin
, Instead of directly checking for therequired_scopes
attributeThe text was updated successfully, but these errors were encountered: