-
Notifications
You must be signed in to change notification settings - Fork 335
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
Support a configurable oauth redirect URL #442
Open
thedumbterminal
wants to merge
11
commits into
python-restx:master
Choose a base branch
from
thedumbterminal:oauth-redirect
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
31c1ba4
adding redirect
thedumbterminal 84a56c7
support other oauth callback urls
thedumbterminal 3572d8e
added dummy auth handler
thedumbterminal 409b3f6
Merge branch 'master' into oauth-redirect
thedumbterminal da6cb1d
Merge branch 'master' into oauth-redirect
thedumbterminal 38ae0bd
Merge branch 'master' into oauth-redirect
thedumbterminal 2adc2e9
Merge branch 'master' into oauth-redirect
peter-doggart 331e314
still check for auth ID
thedumbterminal c1e49e9
Merge branch 'oauth-redirect' of github.com:thedumbterminal/flask-res…
thedumbterminal c50f7c9
Merge branch 'master' into oauth-redirect
thedumbterminal 0893287
added docs and required env var
thedumbterminal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
from flask import Flask | ||
from flask_restx import Api, Resource, fields | ||
from werkzeug.middleware.proxy_fix import ProxyFix | ||
import os | ||
|
||
app = Flask(__name__) | ||
|
||
app.config["SWAGGER_UI_OAUTH_REDIRECT_URL"] = os.environ.get("SWAGGER_UI_OAUTH_REDIRECT_URL") | ||
|
||
app.wsgi_app = ProxyFix(app.wsgi_app) | ||
|
||
authorizations = { | ||
'OAuth2': { | ||
'type': 'oauth2', | ||
'flow': 'accessCode', | ||
'authorizationUrl': 'https://github.com/login/oauth/authorize', | ||
'tokenUrl': '/', | ||
'scopes': { | ||
'user:email': 'Read user email addresses' | ||
} | ||
} | ||
} | ||
|
||
api = Api(app, version="1.0", title="Todo API", description="A simple TODO API", authorizations=authorizations) | ||
|
||
ns = api.namespace("todos", description="TODO operations") | ||
|
||
TODOS = { | ||
"todo1": {"task": "build an API"}, | ||
"todo2": {"task": "?????"}, | ||
"todo3": {"task": "profit!"}, | ||
} | ||
|
||
todo = api.model( | ||
"Todo", {"task": fields.String(required=True, description="The task details")} | ||
) | ||
|
||
listed_todo = api.model( | ||
"ListedTodo", | ||
{ | ||
"id": fields.String(required=True, description="The todo ID"), | ||
"todo": fields.Nested(todo, description="The Todo"), | ||
}, | ||
) | ||
|
||
|
||
def abort_if_todo_doesnt_exist(todo_id): | ||
if todo_id not in TODOS: | ||
api.abort(404, "Todo {} doesn't exist".format(todo_id)) | ||
|
||
|
||
parser = api.parser() | ||
parser.add_argument( | ||
"task", type=str, required=True, help="The task details", location="form" | ||
) | ||
|
||
|
||
@ns.route("/<string:todo_id>") | ||
@api.doc(responses={404: "Todo not found"}, params={"todo_id": "The Todo ID"}) | ||
class Todo(Resource): | ||
"""Show a single todo item and lets you delete them""" | ||
|
||
@api.doc(description="todo_id should be in {0}".format(", ".join(TODOS.keys()))) | ||
@api.marshal_with(todo) | ||
def get(self, todo_id): | ||
"""Fetch a given resource""" | ||
abort_if_todo_doesnt_exist(todo_id) | ||
return TODOS[todo_id] | ||
|
||
@api.doc(responses={204: "Todo deleted"}) | ||
def delete(self, todo_id): | ||
"""Delete a given resource""" | ||
abort_if_todo_doesnt_exist(todo_id) | ||
del TODOS[todo_id] | ||
return "", 204 | ||
|
||
@api.doc(parser=parser) | ||
@api.marshal_with(todo) | ||
def put(self, todo_id): | ||
"""Update a given resource""" | ||
args = parser.parse_args() | ||
task = {"task": args["task"]} | ||
TODOS[todo_id] = task | ||
return task | ||
|
||
|
||
@ns.route("/") | ||
class TodoList(Resource): | ||
"""Shows a list of all todos, and lets you POST to add new tasks""" | ||
|
||
@api.marshal_list_with(listed_todo) | ||
def get(self): | ||
"""List all todos""" | ||
return [{"id": id, "todo": todo} for id, todo in TODOS.items()] | ||
|
||
@api.doc(parser=parser) | ||
@api.marshal_with(todo, code=201) | ||
def post(self): | ||
"""Create a todo""" | ||
args = parser.parse_args() | ||
todo_id = "todo%d" % (len(TODOS) + 1) | ||
TODOS[todo_id] = {"task": args["task"]} | ||
return TODOS[todo_id], 201 | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Correct me if I'm wrong here, but does this introduce a small behaviour change to previously? By default,
config.SWAGGER_UI_OAUTH_CLIENT_ID
is not defined and therefore nooauth2RedirectUrl
should be set? I think we maybe just need to introduce another if statement? I don't have any OAUTH flows set up to easily test this change.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.
I've put back the original check and then nested the new check inside it
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.
Testing this.... hold off merging...
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.
OK testing done, could you re-review please