Skip to main content

A simple library for parsing and verifying externally issued OIDC ID tokens in fastapi.

Project description

FastAPI OIDC

Test Documentation Status Package version


:warning: See this issue for simple role-your-own example of checking OIDC tokens.

Verify and decrypt 3rd party OIDC ID tokens to protect your fastapi endpoints.

Documentation: ReadTheDocs

Source code: Github

Installation

pip install fastapi-oidc

Usage

Verify ID Tokens Issued by Third Party

This is great if you just want to use something like Okta or google to handle your auth. All you need to do is verify the token and then you can extract user ID info from it.

from fastapi import Depends
from fastapi import FastAPI

# Set up our OIDC
from fastapi_oidc import IDToken
from fastapi_oidc import get_auth

OIDC_config = {
    "client_id": "0oa1e3pv9opbyq2Gm4x7",
    # Audience can be omitted in which case the aud value defaults to client_id
    "audience": "https://yourapi.url.com/api",
    "base_authorization_server_uri": "https://dev-126594.okta.com",
    "issuer": "dev-126594.okta.com",
    "signature_cache_ttl": 3600,
}

authenticate_user: Callable = get_auth(**OIDC_config)

app = FastAPI()

@app.get("/protected")
def protected(id_token: IDToken = Depends(authenticate_user)):
    return {"Hello": "World", "user_email": id_token.email}

Using your own tokens

The IDToken class will accept any number of extra field but if you want to craft your own token class and validation that's accounted for too.

class CustomIDToken(fastapi_oidc.IDToken):
    custom_field: str
    custom_default: float = 3.14


authenticate_user: Callable = get_auth(**OIDC_config, token_type=CustomIDToken)

app = FastAPI()


@app.get("/protected")
def protected(id_token: CustomIDToken = Depends(authenticate_user)):
    return {"Hello": "World", "user_email": id_token.custom_default}

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_oidc-0.0.10.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

fastapi_oidc-0.0.10-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_oidc-0.0.10.tar.gz.

File metadata

  • Download URL: fastapi_oidc-0.0.10.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.8.4 Darwin/23.5.0

File hashes

Hashes for fastapi_oidc-0.0.10.tar.gz
Algorithm Hash digest
SHA256 aeb31a44759f320e809f707d315fee2ec91a878e7d16221a1b2a40b2b424fd85
MD5 735c5ae80391adc3b1010dcf55b99240
BLAKE2b-256 a8fb02db95ebe8c487303d324640e630295add67e0283327c930a4922132f616

See more details on using hashes here.

File details

Details for the file fastapi_oidc-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: fastapi_oidc-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.3.1 CPython/3.8.4 Darwin/23.5.0

File hashes

Hashes for fastapi_oidc-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 44f459a3ebd2f316ac819111ea10ed9708ef314c16d30d675970645120d229b7
MD5 e38b293a872e6246e1556d8366b77934
BLAKE2b-256 9888ac0e9910174250ced2bbef45fa6f55b67dd8540958d5ce33b5808aefe709

See more details on using hashes here.

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