Skip to main content

FastAPI plugin to enable SSO to most common providers (such as Facebook login, Google login and login via Microsoft Office 365 Account)

Project description

FastAPI SSO

FastAPI plugin to enable SSO to most common providers (such as Facebook login, Google login and login via Microsoft Office 365 account).

This allows you to implement the famous Login with Google/Facebook/Microsoft buttons functionality on your backend very easily.

Installation

Install using pip

pip install fastapi-sso

Install using poetry

poetry add fastapi-sso

Example

example.py

"""This is an example usage of fastapi-sso.
"""

from fastapi import FastAPI
from starlette.requests import Request
from fastapi_sso.sso.google import GoogleSSO

app = FastAPI()

google_sso = GoogleSSO("my-client-id", "my-client-secret", "https://my.awesome-web.com/google/callback")


@app.get("/google/login")
async def google_login():
    """Generate login url and redirect"""
    return await google_sso.get_login_redirect()


@app.get("/google/callback")
async def google_callback(request: Request):
    """Process login response from Google and return user info"""
    user = await google_sso.verify_and_process(request)
    return {
        "id": user.id,
        "picture": user.picture,
        "display_name": user.display_name,
        "email": user.email,
        "provider": user.provider,
    }

Run using uvicorn example:app.

HTTP and development

You should always use https in production. But in case you need to test on localhost and do not want to use self-signed certificate, make sure you set up redirect uri within your SSO provider to http://localhost:{port} and then add this to your environment:

OAUTHLIB_INSECURE_TRANSPORT=1

And make sure you pass allow_insecure_http = True to SSO class' constructor, such as:

google_sso = GoogleSSO("client-id", "client-secret", "callback-url", allow_insecure_http=True)

See this issue for more information.

State

State is used in OAuth to make sure server is responding to the request we send. It may cause you trouble as fastsapi-sso actually saves the state content as a cookie and attempts reading upon callback and this may fail (e.g. when loging in from different domain then the callback is landing on). If this is your case, you may want to disable state checking by passing use_state = False in SSO class's constructor, such as:

google_sso = GoogleSSO("client-id", "client-secret", "callback-url", use_state=False)

See more on state here.

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

fastapi-sso-0.2.10.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_sso-0.2.10-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-sso-0.2.10.tar.gz.

File metadata

  • Download URL: fastapi-sso-0.2.10.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.10.54

File hashes

Hashes for fastapi-sso-0.2.10.tar.gz
Algorithm Hash digest
SHA256 b034374c2159ffe74e8c9a55cd0ed7d0d1269c80c04ea27f2362b606114a0bd1
MD5 ab604b8d3f6509ca685d625c16f16bd3
BLAKE2b-256 e9a9391de9239adf33007bc0ada9ffbde23aeeb893166309d22ebce885b9a284

See more details on using hashes here.

File details

Details for the file fastapi_sso-0.2.10-py3-none-any.whl.

File metadata

  • Download URL: fastapi_sso-0.2.10-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.1 Linux/5.10.54

File hashes

Hashes for fastapi_sso-0.2.10-py3-none-any.whl
Algorithm Hash digest
SHA256 db2b7c40fd9fba5bdc34af0a01b070a639f457e45a352f07fcaf8ec1e38fd790
MD5 402c73049e6b4d52d7207f2099f57f6c
BLAKE2b-256 2facd45515a647a30c0afbfda13ea67a1721bdb60f8270a4c77d389fdd5cf283

See more details on using hashes here.

Supported by

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