Skip to main content

Social authentication for your apps (OAuth 2)

Project description

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!

Also be sure to join the Slack channel!

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.

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"})

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

masonite-oauth2-1.1.1.tar.gz (25.4 kB view details)

Uploaded Source

Built Distribution

masonite_oauth2-1.1.1-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file masonite-oauth2-1.1.1.tar.gz.

File metadata

  • Download URL: masonite-oauth2-1.1.1.tar.gz
  • Upload date:
  • Size: 25.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for masonite-oauth2-1.1.1.tar.gz
Algorithm Hash digest
SHA256 4bed0d6e8c3a1d0f1a17dcd59c78e40b8c8eb7dda3340adcb4d53a1a3093aac5
MD5 85d8a00a1f3b6d8ef53615dd522cac40
BLAKE2b-256 2938571cc8f54a1e31b67b3be19221789f75622b1d22644fac7d5b92cfd41e4d

See more details on using hashes here.

File details

Details for the file masonite_oauth2-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: masonite_oauth2-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.2

File hashes

Hashes for masonite_oauth2-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 632ac12052ac94f129aff1346fdd0f558e3d79d8be1d74ec1ebc840f6ff2f0ca
MD5 831bf7e9fedd45dd70233d5bf7eb72cd
BLAKE2b-256 fb4acac89267956a5d152067fca4b725b62ed7898462eb7b3ad341b616ebb58d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page