Skip to main content

Simple Python oAuth Login

Project description

python-socialite

Codacy Badge Codacy Badge

The easy way to retrieve OAuth 2.0 Tokens from any provider

Simple and convenient way for fetching OAuth 2.0 tokens from any provider. Out of the box support for Facebook, Twitter, LinkedIn, Google, GitHub, GitLab and Bitbucket. Inspired by Laravel Socialite

Features

  • Supports multiple common providers
  • Supports any oAuth 2 compliant providers (You can provide a custom driver)
  • Straighforward unopinionated authentication
  • Can be implemented in any python framework

Usage

Installation

pip install python-socialite

Generate redirect uri

from python_socialite import OAuthProvider

config = {
    "google": {
        "client_id": "",
        "client_secret": "",
        "redirect_url": ""
    }
}
provider = OAuthProvider("google", config)
redirect_url = provider.get_auth_url()

# redirect user to the redirect_url using your frameworks supported redirect

Retrieving Access Token and User

code = "" # read code from GET variables in the url the provider redirected you to
provider = OAuthProvider("google", config)

token = provider.get_token(code)
user = provider.get_user(token["access_token"])

This package does not provide opinion on how you use the returned token or user profile. Add that to your application's business logic. Examples include hooking up to your authentication logic, fetching data associated with the returned access token e.t.c

Token Template

NB: Token attributes might vary between providers. Here's a sample returned by Google oAuth

{
   "access_token": "ya29.***",
   "expires_in": 3599,
   "scope": "https://www.googleapis.com/auth/userinfo.profile openid",
   "token_type": "Bearer",
   "id_token": "***jwt***"
}

User Template

user = {
    "id": "",
    "name": "",
    "email": "",
    "avatar": "",
    "raw": "",
    "provider": ""
}

The raw attribute contains all user data as returned by the oAuth provider. Fields in this attribute can be different across different oAuth providers

Requesting Scopes

By default the following scopes are requested

openid, email, profile

You can override requested scopes by adding them to the provider config or using set_scopes method

provider = OAuthProvider("google", config)
auth_url = provider.set_scopes(["openid", "email", "profile"]).get_auth_url()

NB: If no scopes are set in the config or in code the default scopes will be used

Config Options

The config must be a dict containing keys of any of the supported providers

# each provider key must have client_id, client_secret and redirect_url. It's advised to ensure your client_secret is properly secured

config = {
    "google": {
        "client_id": "",
        "client_secret": "",
        "redirect_url": "",
        "scopes": [] # optional
    },
    "facebook": {},
    "twitter": {},
    "linkedin": {},
    "github": {},
    "gitlab": {},
    "bitbucket": {}
}

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

python_socialite-0.1.4.tar.gz (8.3 kB view hashes)

Uploaded Source

Built Distribution

python_socialite-0.1.4-py3-none-any.whl (3.7 kB view hashes)

Uploaded Python 3

Supported by

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