Skip to main content

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.

Release files for fastapi-sso 0.2.12

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

Source distribution (sdist)

Source distribution for fastapi-sso 0.2.12
File Size Uploaded
fastapi-sso-0.2.12.tar.gz 7.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-sso 0.2.12
File Interpreter ABI Platform
fastapi_sso-0.2.12-py3-none-any.whl Python 3 none any Details

Total release size: 16.2 kB

Release files / fastapi-sso-0.2.12.tar.gz

Download URL fastapi-sso-0.2.12.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
671d00d0ee0630fccd9279f275d4f898c2012aa80354c8e86b33bd3dfe945387
BLAKE2b-256 checksum
How to use checksums
64905134d04a640f5ee4821856f2785057cc6d7964e45d1542705745c3932236
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.10.0 Linux/5.10.71

Release files / fastapi_sso-0.2.12-py3-none-any.whl

Download URL fastapi_sso-0.2.12-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
38da189b1bdacb06e9b08097bc560afbf2f7b87fbd620ca01a05a8039af1c99f
BLAKE2b-256 checksum
How to use checksums
443fb8cb2cec0648ccddde8d47ddf625e4b20606ac818de5790c90f0c37f2be2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.10.0 Linux/5.10.71

Release history Release notifications | RSS feed

0.23.0

2 release files

0.22.0

2 release files

0.21.1

2 release files

0.21.0

2 release files

0.19.0

2 release files

0.18.0

2 release files

0.15.0

2 release files

0.14.2

2 release files

0.14.1

2 release files

0.13.0

2 release files

0.12.2

2 release files

0.12.1

2 release files

0.12.0

2 release files

0.10.0

2 release files

0.9.1

2 release files

0.9.0

2 release files

0.8.0

2 release files

0.7.4

2 release files

0.7.3

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.4

2 release files

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.13

2 release files

This release

0.2.12 This release

2 release files

0.2.11

2 release files

0.2.9

2 release files

0.2.8

2 release files

0.2.7

2 release files

0.2.6

2 release files

0.2.5

2 release files

0.2.4

2 release files

0.2.3

2 release files

0.2.2

2 release files

0.2.1

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