-
Notifications
You must be signed in to change notification settings - Fork 818
Add an authorize endpoint that uses JSON instead of a Django template… #1306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add an authorize endpoint that uses JSON instead of a Django template… #1306
Conversation
for more information, see https://pre-commit.ci
Codecov Report
@@ Coverage Diff @@
## master #1306 +/- ##
=======================================
Coverage 97.37% 97.38%
=======================================
Files 32 32
Lines 2022 2028 +6
=======================================
+ Hits 1969 1975 +6
Misses 53 53
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
…ango-oauth-toolkit into issue-1305-json-auth-endpoint
…ango-oauth-toolkit into issue-1305-json-auth-endpoint
| elif require_approval == "auto": | ||
| tokens = ( | ||
| get_access_token_model() | ||
| .objects.filter( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you could potentially avoid iteration by filtering by your desired scopes (assuming the scopes var here is the required scopes of the view)
| .objects.filter( | |
| scopes_filter = { 'scopes__icontains': scope for scope in scopes } | |
| .objects.filter( | |
| user=request.user, | |
| application=kwargs["application"], | |
| expires__gt=timezone.now(), | |
| **scopes_filter | |
| ).first() |
|
@jhnbyrn can you rebase this.. i'm open to using a mixin here if it enables you use case and doesn't break existing functionality. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the AuthorizationView.get() method by extracting its core authorization logic into a new AuthorizationMixin.get_context() method, enabling code reuse across multiple authorization views.
- Introduces
AuthorizationMixinclass withget_context()method containing the extracted authorization logic - Simplifies
AuthorizationView.get()to callget_context()and handle the response type - Removes a blank line in
form_valid()method
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| class AuthorizationView(BaseAuthorizationView, FormView): | ||
| class AuthorizationMixin: | ||
| def get_context(self, request, *args, **kwargs): |
Copilot
AI
Nov 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The get_context method lacks a docstring. This method performs complex authorization logic and has multiple return types (dict or HttpResponse). Add a docstring explaining its purpose, parameters, return values (including the dual return type behavior), and any exceptions that may be raised.
| def get_context(self, request, *args, **kwargs): | |
| def get_context(self, request, *args, **kwargs): | |
| """ | |
| Process the OAuth2 authorization request and build the context for the authorization view. | |
| This method performs complex authorization logic, including validating the authorization request, | |
| handling special prompt parameters, checking for prior user consent, and preparing context data | |
| for the authorization form. | |
| Parameters: | |
| request (HttpRequest): The current HTTP request object. | |
| *args: Additional positional arguments. | |
| **kwargs: Additional keyword arguments used to build the context. | |
| Returns: | |
| dict: A context dictionary containing authorization data for rendering the authorization form, | |
| if user consent is required. | |
| HttpResponse: An HTTP response (such as a redirect or error response) if the authorization | |
| request is invalid, or if user consent can be skipped. | |
| Side Effects: | |
| Sets self.oauth2_data to the context dictionary if consent is required. | |
| Exceptions: | |
| OAuthToolkitError: Raised if the authorization request is invalid. In this case, an error | |
| response is returned. | |
| """ |
| context = self.get_context(request, *args, **kwargs) | ||
| if isinstance(context, dict): | ||
| form = self.get_form(self.get_form_class()) | ||
| context["form"] = form | ||
| return self.render_to_response(self.get_context_data(**context)) | ||
| else: | ||
| return context |
Copilot
AI
Nov 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] The dual return type pattern (dict vs HttpResponse) from get_context() creates implicit behavior that's difficult to follow. Consider using a more explicit pattern, such as a tuple (should_render, data) or separate methods for validation vs. context preparation to make the control flow clearer.
| context = self.get_context(request, *args, **kwargs) | |
| if isinstance(context, dict): | |
| form = self.get_form(self.get_form_class()) | |
| context["form"] = form | |
| return self.render_to_response(self.get_context_data(**context)) | |
| else: | |
| return context | |
| should_render, data = self.get_context(request, *args, **kwargs) | |
| if should_render: | |
| form = self.get_form(self.get_form_class()) | |
| data["form"] = form | |
| return self.render_to_response(self.get_context_data(**data)) | |
| else: | |
| return data |
| try: | ||
| # If skip_authorization field is True, skip the authorization screen even | ||
| # if this is the first use of the application and there was no previous authorization. | ||
| # This is useful for in-house applications-> assume an in-house applications |
Copilot
AI
Nov 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grammar error: 'an in-house applications' should be 'in-house applications' (remove 'an'). Also, there's a missing space after the arrow: 'applications->' should be 'applications ->'.
| # This is useful for in-house applications-> assume an in-house applications | |
| # This is useful for in-house applications -> assume in-house applications |
|
|
||
|
|
||
| class AuthorizationView(BaseAuthorizationView, FormView): | ||
| class AuthorizationMixin: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rename AuthorizationMixin to AuthorizationViewMixin.
| ) | ||
| return self.redirect(uri, application) | ||
|
|
||
| except OAuthToolkitError as error: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having the function return the context or response is awkard. It should just return the context.
let the exception throw and hoist the try/except to the get method, so the get method is returning the error_response
…/HTML form
Fixes #1305
Description of the Change
For single page applications it would be handy to be able to get the data for the authorization page as JSON and then render the authorization page on the client side, and similarly post the results as JSON rather than as a HTML form.
Checklist
CHANGELOG.mdupdated (only for user relevant changes)AUTHORS