Skip to main content

Masonite Package GitHub Workflow Status Python Version GitHub release (latest by date including pre-releases) License Code style: black

Introduction

Social authentication for your Masonite app (OAuth 2).

Features

  • Officially supports GitHub, GitLab, BitBucket, Google, Apple and Facebook providers

Official Masonite Documentation

New to Masonite ? Please first read the Official Documentation. Masonite strives to have extremely comprehensive documentation 😃. It would be wise to go through the tutorials there. If you find any discrepencies or anything that doesn't make sense, be sure to comment directly on the documentation to start a discussion!

Hop on Masonite Discord Community to ask any questions you need!

Installation

pip install masonite-oauth2

Configuration

Add OauthProvider to your project in config/providers.py:

# config/providers.py
# ...
from masonite.oauth import OAuthProvider

# ...
PROVIDERS = [
    # ...
    # Third Party Providers
    OAuthProvider,
    # ...
]

Then you can publish the configuration file to your project:

python craft package:publish oauth

Finally you will need to add credentials for the OAuth providers your application utilizes.

# config/oauth.py
DRIVERS = {
  "github": {
    "client_id": env("GITHUB_CLIENT_ID"),
    "client_secret": env("GITHUB_CLIENT_SECRET"),
    "redirect": "auth.callback",
  }
}

redirect can be a route name or a path.

Configuration of your OAuth app:

Then you should create an OAuth App on your provider dashboard. Here are some links:

Usage

To authenticate users using an OAuth provider, you will need two routes: one for redirecting the user to the OAuth provider, and another for receiving the callback from the provider after authentication.

In your controller, you can then easily access OAuth facade methods:

  • redirect() is redirecting to the OAuth endpoint provider
  • user() is the route callback the user will be redirected to after entering its credentials through the OAuth provider screen. This method is returning a OAuthUser instance containing user informations.
from masonite.oauth import OAuth

class YourController(Controller):

    def auth(self):
        return OAuth.driver("github").redirect()

    def callback(self):
        user = OAuth.driver("github").user()
        # you now have a user object with data and a token

Get user data

When retrieving user data with user() method, you will get a OAuthUser with the following fields:

  • id
  • name
  • nickname
  • email
  • avatar
  • token

Get user data from a token

If you already have a valid access token for a user, you can retrieve user data using user_from_token():

user = OAuth.driver("github").user_from_token(token)

Scopes

OAuth providers have default scopes used when redirecting to OAuth provider screen:

  • GitHub: user:email
  • BitBucket: email
  • Gitlab: read_user
  • Google: openid, profile, email
  • Apple: name, email
  • Facebook: email

You can add new scopes in the redirect request by using scopes() method (merged with default scopes):

user = OAuth.driver("github").scopes(["admin:org", "read:discussion"]).redirect()
# scopes will be: user:email, admin:org, read:discussion

You can override all scopes in the redirect request by using set_scopes()method:

user = OAuth.driver("github").set_scopes(["user:email", "user:follow"]).redirect()
# scopes will be: user:email, user:follow

Optional parameters

Some OAuth providers support optional parameters. To include those in the redirect request, you can use with_data() method.

return OAuth.driver("github").with_data({"key": "value"})

Refresh token

Some OAuth providers support refreshing token (GitLab, BitBucket and Google at least). For that you need a refresh_token obtained when calling user():

new_user =  OAuth.driver("gitlab").refresh(user.refresh_token)
new_user.token #== is a new token

Revoke token programmatically

Some OAuth providers support revoking token programmatically. For that you need to pass the token to the revoke() method:

revoked = OAuth.driver("gitlab").revoke(token)

It returned a boolean to tell if it was successful or not.

Contributing

Please read the Contributing Documentation here. Feel free to open a PR to add a new OAuth 2.0 provider 😀 !

Maintainers

Credits

Based on non maintained package https://github.com/hellomasonite/masonite-socialite.

License

Masonite oauth is open-sourced software licensed under the MIT license.

Release files for masonite-oauth2 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for masonite-oauth2 1.2.0
File Size Uploaded
masonite-oauth2-1.2.0.tar.gz 27.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for masonite-oauth2 1.2.0
File Interpreter ABI Platform
masonite_oauth2-1.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 43.3 kB

Release files / masonite-oauth2-1.2.0.tar.gz

Download URL masonite-oauth2-1.2.0.tar.gz
Size 27.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a03a97eb64942da33b78988e85c19cf6bc0611188d09cdb4ca91bf6062638ac6
BLAKE2b-256 checksum
How to use checksums
ecd7283ecf67a36de1702f056a5820e603997c33329d31adf8a1c882ed2922f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release files / masonite_oauth2-1.2.0-py3-none-any.whl

Download URL masonite_oauth2-1.2.0-py3-none-any.whl
Size 15.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0b833cef70575194644d4e098c56ec9041df1a965387d57a7c21be1a59d1d7c2
BLAKE2b-256 checksum
How to use checksums
da6984bc53dba7f60593361f38488f932f0752420ab6732683020bef15b7c7ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page