Skip to main content

Build resource servers with FastAPI

Project description

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

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-resource-server-0.1.2.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: fastapi-resource-server-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 0bb4d122c74ae1dbde732bb03204af07243d6fb156c27f77f6c72c2c2e73c935
MD5 52ef0bbe9265c6f95fd69a7ea2a3aa5e
BLAKE2b-256 fa3f1282232fdb685ffbda84a23dea2f21b173da75dbe0f0141142c262fb6590

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for fastapi_resource_server-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 079195c4c9de35a1b265a6a433affbcb7cbfb44cf7ca6a5ab223c0f4f83bc3fb
MD5 01d156bae19ccf8ae4c638fd7df12c06
BLAKE2b-256 609376b2d698fa270ab4a220e666cf0c393d9daef66ec0ade859d409567f1860

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