Replace wrapper-based SessionMiddleware/OIDCApp with KayaMixin subclasses applied via KayaApp(mixins=[...]). Mixins hook into handle_request and handle_websocket via before/after hooks, so both ASGI and RSGI keep working. Mixin dependencies are applied automatically and deduplicated.
35 lines
1002 B
Python
35 lines
1002 B
Python
import os
|
|
|
|
from kaya.core import HttpContext, KayaApp
|
|
from kaya.oidc import OIDCConfig, OIDCMixin
|
|
from kaya.session import InMemorySessionStore, SessionMixin
|
|
|
|
session = SessionMixin(InMemorySessionStore())
|
|
oidc = OIDCMixin(
|
|
OIDCConfig(
|
|
issuer=os.environ.get('OIDC_ISSUER', 'https://accounts.google.com'),
|
|
client_id=os.environ.get('OIDC_CLIENT_ID', 'replace-me'),
|
|
client_secret=os.environ.get('OIDC_CLIENT_SECRET'),
|
|
redirect_uri=os.environ.get('OIDC_REDIRECT_URI', 'http://localhost:8000/auth/callback'),
|
|
fetch_userinfo=True,
|
|
),
|
|
session=session,
|
|
)
|
|
|
|
app = KayaApp(mixins=[session, oidc])
|
|
|
|
|
|
@app.GET('/')
|
|
async def home(ctx: HttpContext) -> None:
|
|
await ctx.send_str(200, 'public home')
|
|
|
|
|
|
@app.GET('/profile')
|
|
@oidc.require_auth
|
|
async def profile(ctx: HttpContext) -> None:
|
|
user = oidc.get_user(ctx)
|
|
if user is None:
|
|
await ctx.send_empty(401)
|
|
return
|
|
await ctx.send_str(200, f'Hello {user.name or user.email or user.sub}')
|