Skip to content

A Python library for handling OAuth authentication, designed for use in third-party applications. This package simplifies the process of obtaining authorization from Google, exchanging authorization codes for access tokens, refreshing access tokens, and retrieving user information.

License

Notifications You must be signed in to change notification settings

limFakson/Python_OAuth_Core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OAuth_Core_Lib

A Python library for handling OAuth authentication, designed for use in third-party applications. This package simplifies the process of obtaining authorization from OAuth provider, exchanging authorization codes for access tokens, refreshing access tokens, and retrieving user information.

Table of Contents

Installation

To install the library, run:

pip install oauth-core-lib

Setup

Create a JSON Secrets File and add the following:

{
  "google": {
    "project_id": "your_project_id", // Optional buh good for identification
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "redirect_uris": "your_redirect_uri",
    "status":"" //Set status to test in test phase to keep getting the refreash token 
  }
}

Set Up Environment Variables Create a .env file in your project root directory with the following contents, the package will get the variables needed itself.

# Google credentials and url
GOOGLE_KEY=your_client_id
GOOGLE_SECRET=your_client_secret
GOOGLE_TOKEN_INFO=google_token_info_api
GOOGLE_API=google_api

Usage

Load the json file

from core.oauth import secrets

json_file = secrets.load_file(Path('your_json_file.json'))

Initialize the Oauth Class

# For google oauth
from core.oauth import google

oauth = google.GoogleOauth(json_file)

Authorize URL

Generate the URL to redirect users to Google for authorization

auth_url = oauth.authorise()
  • Returns: A URL string where users will be redirected to authorize your application

Exchange Authorization Code

Once the user is redirected back to your application, capture the query parameters (which include the authorization code) and exchange them for an access token (it takes in only dict)

query_params = {'code': 'authorization_code', 'state': 'your_state'} # query gotten from the redirect uri converted to dictionary
tokens = oauth.exchange_code(query_params)
  • Returns: A dictionary containing: access_token: The token to authenticate API requests. expires_in: The remaining lifetime of the access token (in seconds). refresh_token: A token to obtain a new access token when the current one expires. scope: The scope of access granted by the user. token_type: The type of token issued (typically "Bearer").

Check Access Token(optional)

Check if the access token is still valid

token_info = oauth.check_access_token(tokens['access_token'])
  • Returns: A dictionary containing: aud: The client ID to which the token was issued. user_id: The unique ID of the authenticated user. scope: The scope associated with the token. expires_in: The number of seconds remaining before the token expires.

Refresh Access Token

If the access token has expired, use the refresh token to get a new one note: refreash token does not come the second time

refreshed_tokens = oauth.refresh_token(tokens['refresh_token'])
  • Returns: A dictionary similar to the one returned by exchange_code, but without a new refreash_token.

Get User Information

Retrieve the authenticated user's information using the access token

user_info = oauth.do_auth(tokens['access_token'])
  • Returns: A dictionary containing: id: The user's Google ID. email: The user's email address. verified_email: A boolean indicating whether the email address is verified. name: The user's full name. given_name: The user's first name. family_name: The user's last name. picture: The URL of the user's profile picture.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any bug fixes or enhancements.

License

This project is licensed under the MIT License.

About

A Python library for handling OAuth authentication, designed for use in third-party applications. This package simplifies the process of obtaining authorization from Google, exchanging authorization codes for access tokens, refreshing access tokens, and retrieving user information.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages