Flask integration#

Install#

pip install py-oidc-auth[flask]

Minimal application#

from flask import Flask
from py_oidc_auth import FlaskOIDCAuth

app = Flask(__name__)

auth = FlaskOIDCAuth(
    client_id="my client",
    client_secret="secret",
    discovery_url="https://idp.example.org/realms/demo/.well-known/openid-configuration",
    scopes="openid profile email",
)

app.register_blueprint(auth.create_auth_blueprint(prefix=""))

Protecting routes#

Flask uses decorators. The wrapped view receives the validated token as its first positional argument.

@app.get("/me")
@auth.required()
def me(token):
    return {"sub": token.sub}

@app.get("/maybe_me")
@auth.optional()
def maybe_me(token):
    if token is None:
        return {"anonymous": True}
    return {"sub": token.sub}

Request examples#

GET /auth/v2/login?redirect_uri=https%3A%2F%2Fapp.example.org%2Fcallback HTTP/1.1
Host: app.example.org
POST /auth/v2/token HTTP/1.1
Host: app.example.org
Content-Type: application/x-www-form-urlencoded

refresh_token=ref