Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add AuthorizationViewMixin to simplify overriding the AuthorizationView#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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
jhnbyrn wants to merge9 commits intodjango-oauth:master
base:master
Choose a base branch
Loading
fromjhnbyrn:issue-1305-json-auth-endpoint

Conversation

@jhnbyrn
Copy link
Contributor

@jhnbyrnjhnbyrn commentedAug 12, 2023
edited
Loading

…/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

  • PR only contains one change (considered splitting up PR)
  • unit-test added
  • documentation updated
  • CHANGELOG.md updated (only for user relevant changes)
  • author name inAUTHORS

@jhnbyrnjhnbyrn mentioned this pull requestAug 12, 2023
@codecov
Copy link

codecovbot commentedAug 12, 2023
edited
Loading

Codecov Report

Merging#1306 (78bd3e8) intomaster (a4ae1d4) willincrease coverage by0.00%.
The diff coverage is95.83%.

@@           Coverage Diff           @@##           master    #1306   +/-   ##=======================================  Coverage   97.37%   97.38%           =======================================  Files          32       32             Lines        2022     2028    +6     =======================================+ Hits         1969     1975    +6  Misses         53       53
Files ChangedCoverage Δ
oauth2_provider/views/base.py97.97% <95.83%> (+0.08%)⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times.Learn more

@jhnbyrnjhnbyrn marked this pull request as ready for reviewAugust 31, 2023 12:11
@jhnbyrnjhnbyrn marked this pull request as draftAugust 31, 2023 12:36
elifrequire_approval=="auto":
tokens= (
get_access_token_model()
.objects.filter(
Copy link

@dashdanwdashdanwSep 13, 2023
edited
Loading

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 thescopes var here is the required scopes of the view)

Suggested change
.objects.filter(
scopes_filter= {'scopes__icontains':scopeforscopeinscopes }
.objects.filter(
user=request.user,
application=kwargs["application"],
expires__gt=timezone.now(),
**scopes_filter
).first()

@dopry
Copy link
Member

@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.

@doprydopry requested a review fromCopilotNovember 3, 2025 03:19
Copy link

CopilotAI left a 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 theAuthorizationView.get() method by extracting its core authorization logic into a newAuthorizationMixin.get_context() method, enabling code reuse across multiple authorization views.

  • IntroducesAuthorizationMixin class withget_context() method containing the extracted authorization logic
  • SimplifiesAuthorizationView.get() to callget_context() and handle the response type
  • Removes a blank line inform_valid() method

💡Add Copilot custom instructions for smarter, more guided reviews.Learn how to get started.


classAuthorizationView(BaseAuthorizationView,FormView):
classAuthorizationMixin:
defget_context(self,request,*args,**kwargs):
Copy link

CopilotAINov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Theget_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.

Suggested change
defget_context(self,request,*args,**kwargs):
defget_context(self,request,*args,**kwargs):
"""
ProcesstheOAuth2authorizationrequestandbuildthecontextfortheauthorizationview.
Thismethodperformscomplexauthorizationlogic,includingvalidatingtheauthorizationrequest,
handlingspecialpromptparameters,checkingforprioruserconsent,andpreparingcontextdata
fortheauthorizationform.
Parameters:
request (HttpRequest):ThecurrentHTTPrequestobject.
*args:Additionalpositionalarguments.
**kwargs:Additionalkeywordargumentsusedtobuildthecontext.
Returns:
dict:Acontextdictionarycontainingauthorizationdataforrenderingtheauthorizationform,
ifuserconsentisrequired.
HttpResponse:AnHTTPresponse (suchasaredirectorerrorresponse)iftheauthorization
requestisinvalid,orifuserconsentcanbeskipped.
SideEffects:
Setsself.oauth2_datatothecontextdictionaryifconsentisrequired.
Exceptions:
OAuthToolkitError:Raisediftheauthorizationrequestisinvalid.Inthiscase,anerror
responseisreturned.
"""

Copilot uses AI. Check for mistakes.
Comment on lines +219 to +225
context=self.get_context(request,*args,**kwargs)
ifisinstance(context,dict):
form=self.get_form(self.get_form_class())
context["form"]=form
returnself.render_to_response(self.get_context_data(**context))
else:
returncontext
Copy link

CopilotAINov 3, 2025

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) fromget_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.

Suggested change
context=self.get_context(request,*args,**kwargs)
ifisinstance(context,dict):
form=self.get_form(self.get_form_class())
context["form"]=form
returnself.render_to_response(self.get_context_data(**context))
else:
returncontext
should_render,data=self.get_context(request,*args,**kwargs)
ifshould_render:
form=self.get_form(self.get_form_class())
data["form"]=form
returnself.render_to_response(self.get_context_data(**data))
else:
returndata

Copilot uses AI. Check for mistakes.
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
Copy link

CopilotAINov 3, 2025

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 ->'.

Suggested change
# This is useful for in-house applications-> assume an in-house applications
# This is useful for in-house applications-> assume in-house applications

Copilot uses AI. Check for mistakes.


classAuthorizationView(BaseAuthorizationView,FormView):
classAuthorizationMixin:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Rename AuthorizationMixin to AuthorizationViewMixin.

)
returnself.redirect(uri,application)

exceptOAuthToolkitErroraserror:
Copy link
Member

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 theget method, so the get method is returning the error_response

@doprydopry changed the titleAdd an authorize endpoint that uses JSON instead of a Django template…Add AuthorizationViewMixin to simplify overriding the AuthorizationViewNov 14, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

Copilot code reviewCopilotCopilot left review comments

@doprydoprydopry requested changes

+1 more reviewer

@dashdanwdashdanwdashdanw left review comments

Reviewers whose approvals may not affect merge requirements

Requested changes must be addressed to merge this pull request.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

JSON endpoint for auth URL

3 participants

@jhnbyrn@dopry@dashdanw

[8]ページ先頭

©2009-2025 Movatter.jp