-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
Add local WSGI server authorizer #128
base: main
Are you sure you want to change the base?
Conversation
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.
What I've found so far. I think this would be better suited as an example instead of being part of prawcore.
Regardless, how would one use this? Could you provide a working example?
* Raise InvalidInvocation exception with LocalWSGIServerAuthorizer if redirect URI location isn't local server location
The state was supposed to be user specified, I forgot to fix this while I was creating the class. The example for the flow can be found in the updated |
Example for flow #!/usr/bin/env python
"""This example demonstrates the flow for retrieving a refresh token.
In order for this example to work your application's redirect URI must be set
to http://localhost:65010/auth_callback.
This tool can be used to conveniently create refresh tokens for later use with
your web application OAuth2 credentials.
"""
import os
import sys
import uuid
import prawcore
def main():
"""Provide the program's entry point when directly executed."""
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} SCOPE...")
return 1
authorizer = prawcore.LocalWSGIServerAuthorizer(
prawcore.TrustedAuthenticator(
prawcore.Requestor("prawcore_refresh_token_example"),
os.environ["PRAWCORE_CLIENT_ID"],
os.environ["PRAWCORE_CLIENT_SECRET"],
"http://localhost:65010/auth_callback",
),
sys.argv[1:],
duration="permanent",
)
authorizer.authorize_local_server(state=str(uuid.uuid4()))
print(f"Refresh token: {authorizer.refresh_token}")
if __name__ == "__main__":
sys.exit(main()) |
* Also fix localhost port not being int type
…ServerAuthorizer * Open auth URL in webbrowser when local auth server is used in prawcore.Session
Using custom praw Reddit client
|
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.
Overall, something about the code this PR introduces doesn't quite feel right. Even in PRAW (this one more so) I'd argue provided code does more than it should. In a production environment where someone wishes to obtain access tokens for users I wouldn't recommend they use this Authorizer, and instead manually leverage one of the existing ones within their existing web application. For personal use, I'm curious what the short comings are for the existing example -- or why we couldn't provide a simpler example which leverages the wsgiref code.
What do you think?
|
||
from uuid import uuid4 | ||
from urllib.parse import parse_qs, urlparse | ||
from wsgiref.simple_server import make_server, WSGIRequestHandler |
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 didn't know this was in the standard library. Very cool.
My main intention for creating this PR was because the tutorial given in the docs to obtain a refreshable token looked too complex for my liking and is IMO not end-user friendly to easily obtain a refresh token for PRAW. I don't like entering my Reddit username / password / 2FA code through terminal to use the password flow and would like to avoid using this if possible. Even if this PR is not accepted, I personally will be maintaining a fork with the proposed authorizer for my own personal use. If there is any changes that needed to make this suitable for production environment, feel free to let me know. Otherwise, feel free to close this PR. |
No documentation string, tests or styling have been provided with initial commit. I'm currently looking for maintainer feedback for the proposed addition of new local WSGI server based authorizer class.