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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi-resource-server-0.1.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

fastapi_resource_server-0.1.1-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file fastapi-resource-server-0.1.1.tar.gz.

File metadata

  • Download URL: fastapi-resource-server-0.1.1.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Linux/5.8.0-53-generic

File hashes

Hashes for fastapi-resource-server-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0483867523153518fda12c55a40d7827483f97dfb97ba3280d4edf8c72ad9c2d
MD5 bd3e9c704ee23fefe24fabbe9c903d95
BLAKE2b-256 231de00f2751c07e45c02035425f788c5fbdf4dc637f33fc78ff65054571bd8f

See more details on using hashes here.

File details

Details for the file fastapi_resource_server-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_resource_server-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 724186c8961caa149ec2021bb601d10491af9f2f7bd81fa9c302779b120ac04c
MD5 7927c2b22c2aceae2bc26a897c2633f3
BLAKE2b-256 acdf8a04ae54af6bda76ed654a43e1999d84b0577cceca6772493617815d25be

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.2

2 files

This release

0.1.1 This release

2 files

0.1.0

2 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