Refactor to composable KayaMixin architecture
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.
This commit is contained in:
@@ -1,19 +1,17 @@
|
||||
import base64
|
||||
import json
|
||||
import unittest
|
||||
from time import time
|
||||
from typing import Any, Mapping
|
||||
from typing import Mapping
|
||||
from urllib.parse import parse_qs, urlparse
|
||||
|
||||
import httpx
|
||||
import jwt
|
||||
from cryptography.hazmat.primitives import serialization
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
from pwo import async_test
|
||||
from kaya.core import HttpContext, KayaApp
|
||||
from kaya.session import InMemorySessionStore, SessionMiddleware
|
||||
from kaya.session import InMemorySessionStore, SessionMixin
|
||||
|
||||
from kaya.oidc import OIDCApp, OIDCConfig
|
||||
from kaya.oidc import OIDCConfig, OIDCMixin
|
||||
from kaya.oidc._client import OIDCClient
|
||||
|
||||
|
||||
@@ -197,14 +195,13 @@ class OIDCClientTest(unittest.TestCase):
|
||||
self.assertEqual(['/'], query['post_logout_redirect_uri'])
|
||||
|
||||
|
||||
class OIDCAppTest(unittest.TestCase):
|
||||
class OIDCMixinTest(unittest.TestCase):
|
||||
|
||||
def _build_app(self, fetch_userinfo: bool = False) -> tuple[OIDCApp, MockOIDCProvider, InMemorySessionStore]:
|
||||
def _build_app(self, fetch_userinfo: bool = False) -> tuple[KayaApp, OIDCMixin, MockOIDCProvider, InMemorySessionStore]:
|
||||
provider = MockOIDCProvider()
|
||||
http_client = httpx.AsyncClient(transport=MockTransport(provider))
|
||||
store = InMemorySessionStore()
|
||||
app = KayaApp()
|
||||
session_app = SessionMiddleware(app, store)
|
||||
session = SessionMixin(store)
|
||||
config = OIDCConfig(
|
||||
issuer=provider.issuer,
|
||||
client_id='client',
|
||||
@@ -213,34 +210,35 @@ class OIDCAppTest(unittest.TestCase):
|
||||
http_client=http_client,
|
||||
fetch_userinfo=fetch_userinfo,
|
||||
)
|
||||
oidc_app = OIDCApp(session_app, config)
|
||||
return oidc_app, provider, store
|
||||
oidc = OIDCMixin(config, session=session)
|
||||
app = KayaApp(mixins=[session, oidc])
|
||||
return app, oidc, provider, store
|
||||
|
||||
def _setup_routes(self, oidc_app: OIDCApp) -> None:
|
||||
@oidc_app.GET('/')
|
||||
def _setup_routes(self, app: KayaApp, oidc: OIDCMixin) -> None:
|
||||
@app.GET('/')
|
||||
async def home(ctx: HttpContext) -> None:
|
||||
await ctx.send_str(200, 'home')
|
||||
|
||||
@oidc_app.GET('/profile')
|
||||
@oidc_app.require_auth
|
||||
@app.GET('/profile')
|
||||
@oidc.require_auth
|
||||
async def profile(ctx: HttpContext) -> None:
|
||||
user = oidc_app.get_user(ctx)
|
||||
user = oidc.get_user(ctx)
|
||||
if user is None:
|
||||
await ctx.send_empty(401)
|
||||
return
|
||||
await ctx.send_str(200, f'Hello {user.email}')
|
||||
|
||||
@oidc_app.GET('/refresh')
|
||||
@oidc_app.require_auth
|
||||
@app.GET('/refresh')
|
||||
@oidc.require_auth
|
||||
async def refresh(ctx: HttpContext) -> None:
|
||||
new_token = await oidc_app.refresh_access_token(ctx.session)
|
||||
new_token = await oidc.refresh_access_token(ctx.session)
|
||||
await ctx.send_str(200, new_token or 'no-token')
|
||||
|
||||
@async_test
|
||||
async def test_login_redirect(self) -> None:
|
||||
oidc_app, provider, store = self._build_app()
|
||||
self._setup_routes(oidc_app)
|
||||
transport = httpx.ASGITransport(app=oidc_app)
|
||||
app, oidc, provider, store = self._build_app()
|
||||
self._setup_routes(app, oidc)
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url='http://127.0.0.1:80') as client:
|
||||
r = await client.get('/auth/login', follow_redirects=False)
|
||||
self.assertEqual(302, r.status_code)
|
||||
@@ -250,9 +248,9 @@ class OIDCAppTest(unittest.TestCase):
|
||||
|
||||
@async_test
|
||||
async def test_callback_success(self) -> None:
|
||||
oidc_app, provider, store = self._build_app(fetch_userinfo=True)
|
||||
self._setup_routes(oidc_app)
|
||||
transport = httpx.ASGITransport(app=oidc_app)
|
||||
app, oidc, provider, store = self._build_app(fetch_userinfo=True)
|
||||
self._setup_routes(app, oidc)
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url='http://127.0.0.1:80') as client:
|
||||
r = await client.get('/auth/login', follow_redirects=False)
|
||||
self.assertEqual(302, r.status_code)
|
||||
@@ -274,18 +272,18 @@ class OIDCAppTest(unittest.TestCase):
|
||||
|
||||
@async_test
|
||||
async def test_callback_invalid_state(self) -> None:
|
||||
oidc_app, provider, store = self._build_app()
|
||||
self._setup_routes(oidc_app)
|
||||
transport = httpx.ASGITransport(app=oidc_app)
|
||||
app, oidc, provider, store = self._build_app()
|
||||
self._setup_routes(app, oidc)
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url='http://127.0.0.1:80') as client:
|
||||
r = await client.get('/auth/callback', params={'code': 'mock-code', 'state': 'wrong'}, follow_redirects=False)
|
||||
self.assertEqual(400, r.status_code)
|
||||
|
||||
@async_test
|
||||
async def test_logout(self) -> None:
|
||||
oidc_app, provider, store = self._build_app()
|
||||
self._setup_routes(oidc_app)
|
||||
transport = httpx.ASGITransport(app=oidc_app)
|
||||
app, oidc, provider, store = self._build_app()
|
||||
self._setup_routes(app, oidc)
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url='http://127.0.0.1:80') as client:
|
||||
r = await client.get('/auth/login', follow_redirects=False)
|
||||
parsed = urlparse(r.headers['Location'])
|
||||
@@ -304,9 +302,9 @@ class OIDCAppTest(unittest.TestCase):
|
||||
|
||||
@async_test
|
||||
async def test_require_auth_redirect(self) -> None:
|
||||
oidc_app, provider, store = self._build_app()
|
||||
self._setup_routes(oidc_app)
|
||||
transport = httpx.ASGITransport(app=oidc_app)
|
||||
app, oidc, provider, store = self._build_app()
|
||||
self._setup_routes(app, oidc)
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url='http://127.0.0.1:80') as client:
|
||||
r = await client.get('/profile', follow_redirects=False)
|
||||
self.assertEqual(302, r.status_code)
|
||||
@@ -314,9 +312,9 @@ class OIDCAppTest(unittest.TestCase):
|
||||
|
||||
@async_test
|
||||
async def test_refresh_access_token(self) -> None:
|
||||
oidc_app, provider, store = self._build_app()
|
||||
self._setup_routes(oidc_app)
|
||||
transport = httpx.ASGITransport(app=oidc_app)
|
||||
app, oidc, provider, store = self._build_app()
|
||||
self._setup_routes(app, oidc)
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url='http://127.0.0.1:80') as client:
|
||||
r = await client.get('/auth/login', follow_redirects=False)
|
||||
parsed = urlparse(r.headers['Location'])
|
||||
@@ -329,6 +327,34 @@ class OIDCAppTest(unittest.TestCase):
|
||||
self.assertEqual(200, r.status_code)
|
||||
self.assertEqual('new-access-token', r.text)
|
||||
|
||||
@async_test
|
||||
async def test_dependency_applied_automatically(self) -> None:
|
||||
# Only pass oidc to KayaApp; SessionMixin should be applied via dependencies.
|
||||
provider = MockOIDCProvider()
|
||||
http_client = httpx.AsyncClient(transport=MockTransport(provider))
|
||||
store = InMemorySessionStore()
|
||||
session = SessionMixin(store)
|
||||
config = OIDCConfig(
|
||||
issuer=provider.issuer,
|
||||
client_id='client',
|
||||
client_secret='secret',
|
||||
redirect_uri='http://localhost:8000/auth/callback',
|
||||
http_client=http_client,
|
||||
)
|
||||
oidc = OIDCMixin(config, session=session)
|
||||
app = KayaApp(mixins=[oidc])
|
||||
|
||||
@app.GET('/')
|
||||
async def home(ctx: HttpContext) -> None:
|
||||
ctx.session['x'] = 1
|
||||
await ctx.send_str(200, 'ok')
|
||||
|
||||
transport = httpx.ASGITransport(app=app)
|
||||
async with httpx.AsyncClient(transport=transport, base_url='http://127.0.0.1:80') as client:
|
||||
r = await client.get('/')
|
||||
self.assertEqual(200, r.status_code)
|
||||
self.assertIn('Set-Cookie', r.headers)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user