Skip to content
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

OAuth example for python #13

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

TancsicsGergely
Copy link

This PR contains an OAuth module and a database manager module.
The database module is used to store refresh tokens and retrieve them in case the OAuth request needs it.
Both modules are written for asynchronous usage and can be imported into other files.

This commit contains a cronjob example for the database-oriented structure of this OAuth example. This cronjob should be called every 10-20 days, to keep every refresh token up-to-date. Contains PHPMailer dependency if the mailer function is required by the user.
@Tchekda
Copy link
Member

Tchekda commented Mar 14, 2024

Hi,
Thank you very much for your contribution.
But I can't accept a mix of Python and PHP as a code sample to our OAuth2 services.
Also, try to make something more generic and lightweight that anyone can reuse instead of having something something specific

@TancsicsGergely
Copy link
Author

Hi,
Sure I can rework it for generic usage
I'll remove the database and PHP parts and replace them in the oauth file as well
Then it'll still be async and contain token refreshing and user data request, just with "hardcoded" refresh token which will have to be passed as a variable, and still show a basic concept on how to use the API endpoints, if that fits the requirements

@Tchekda
Copy link
Member

Tchekda commented Mar 15, 2024

The 2 use-case we try to showcase in this repo are:

  • client_credentials authentication to get an application token and use it to fetch some non-personal data
  • SSO flow where a user logs in and his access token is used to fetch some personal data

@TancsicsGergely
Copy link
Author

@Tchekda Hey David, the SSO flow part should be done, utilizing a Flask web server to listen to the callback, rest of the functions are appended to the original oauth2 file

@Tchekda
Copy link
Member

Tchekda commented Mar 19, 2024

The OAuth file seems correct but the server part is really strange. You are storing the authorization code in a global variable which causes a lot of issues (security, multi-user, etc...)
Please try to make something generic that can be used in a common website or bot

@TancsicsGergely
Copy link
Author

Hi, I agree that is not the best practice, but that was made only to demonstrate how to listen to a callback when attempting to authenticate with the SSO. I will rework it, what would be better in your opinion for this example, should I show how to store authorization codes in JSON, or put back the database handler file, and make this example database-dependent? Apart from that I wouldn't really modify the rest of the code, as this should be enough to implement basically any API interaction which needs OAuth credentials.

@Tchekda
Copy link
Member

Tchekda commented Mar 19, 2024

My suggestion:

  • Home page with a login link
  • Login handler that redirects to the SSO authorize URL
  • SSO Callback handler that extracts the authorization code and exchanges it for an access token
    • With the access token you can query /users/me
    • If you want to store the token, you can use the session or cookie storage

The authentication procedure is now updated:
- Authorization token is now saved in browser cookies
- Added new endpoints/functions to demonstrate and show the data that can be generated/displayed/used with this script
- Updated example env file
@TancsicsGergely
Copy link
Author

Hi @Tchekda, updated the code as requested

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants