Skip to main content

FastAPI Resource Server

Build an OIDC resource server using FastAPI.

Your aplication receives the claims decoded from the access token.

Usage

Run keycloak on port 8888:

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

Install dependencies

pip install fastapi fastapi_resource_server uvicorn

Create the main.py module

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

from fastapi_resource_server import JwtDecodeOptions, OidcResourceServer

app = FastAPI()

decode_options = JwtDecodeOptions(verify_aud=False)

auth_scheme = OidcResourceServer(
    "http://localhost:8888/auth/realms/master",
    scheme_name="Keycloak",
    jwt_decode_options=decode_options,
)


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

Release files for fastapi-resource-server 0.1.2

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-resource-server 0.1.2
File Size Uploaded
fastapi-resource-server-0.1.2.tar.gz 4.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-resource-server 0.1.2
File Interpreter ABI Platform
fastapi_resource_server-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.6 kB

Release files / fastapi-resource-server-0.1.2.tar.gz

Download URL fastapi-resource-server-0.1.2.tar.gz
Size 4.3 kB
Tags Source
SHA-256 checksum
How to use checksums
0bb4d122c74ae1dbde732bb03204af07243d6fb156c27f77f6c72c2c2e73c935
BLAKE2b-256 checksum
How to use checksums
fa3f1282232fdb685ffbda84a23dea2f21b173da75dbe0f0141142c262fb6590
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.6 Linux/5.8.0-53-generic

Release files / fastapi_resource_server-0.1.2-py3-none-any.whl

Download URL fastapi_resource_server-0.1.2-py3-none-any.whl
Size 4.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
079195c4c9de35a1b265a6a433affbcb7cbfb44cf7ca6a5ab223c0f4f83bc3fb
BLAKE2b-256 checksum
How to use checksums
609376b2d698fa270ab4a220e666cf0c393d9daef66ec0ade859d409567f1860
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.4 CPython/3.8.6 Linux/5.8.0-53-generic

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.0

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