Skip to main content

Build resource servers with FastAPI

Project description

FastAPI Resource Backend

Build an OIDC resource server using FastAPI.

Your aplication receives the claims decoded from the access token.

Fork of fastapi-resource-server

Usage

Run keycloak on port 8888:

docker container run --name auth-server -d -p 8080:8080 \
    -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin \
    quay.io/keycloak/keycloak:latest

Install dependencies

pip install fastapi fastapi_oidc_backend uvicorn

Create the main.py module

from contextlib import asynccontextmanager
from fastapi import Depends, FastAPI, Security
from pydantic import BaseModel

from fastapi_oidc_backend.security import OidcResourceServer
from fastapi_oidc_backend.models import JwtKwargs

oidc_config = JwtKwargs(audience="myclient", issuer="http://localhost:8888/realms/myrealm")

@asynccontextmanager
async def app_startup(_app: FastAPI):
    await auth_scheme.load_configuration()
    yield

app = FastAPI(lifespan=app_startup,
              swagger_ui_init_oauth={
                  "clientId": oidc_config.audience,
                  "usePkceWithAuthorizationCodeGrant": True
              })
auth_scheme = OidcResourceServer(
    oidc_config,
    scheme_name="Keycloak",
)


class User(BaseModel):
    username: str
    given_name: str
    family_name: str
    email: str


def get_current_user(claims: dict = Security(auth_scheme)):
    claims.update(username=claims["preferred_username"])
    user = User.parse_obj(claims)
    return user


@app.get("/users/me")
def read_current_user(current_user: User = Depends(get_current_user)):
    return current_user

Run the application

uvicorn main:app

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_backend-0.6.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

fastapi_oidc_backend-0.6.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_oidc_backend-0.6.0.tar.gz.

File metadata

  • Download URL: fastapi_oidc_backend-0.6.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for fastapi_oidc_backend-0.6.0.tar.gz
Algorithm Hash digest
SHA256 d8889ebcf9223047444afba3d3add8ea9c22ce12ec3c9cd30e4a85fe4040e5a0
MD5 38f9f8fe07e79d51a41286466918c8de
BLAKE2b-256 b48d76508ba73410431007db83e39bbbaffb5919b1cdaf41c13c9b4f66aa9cf8

See more details on using hashes here.

File details

Details for the file fastapi_oidc_backend-0.6.0-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_oidc_backend-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91add7f72d1c14df0565f974080ebde4fef4836ca4fbf4df3719a504946daeff
MD5 4b95a72b16b0a9894370ddf43dc9d2f7
BLAKE2b-256 3a31873a4b7697bea4a5c4b4177c79ba62c4e7146eef3cbcbfdfe065ff3891f6

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