Skip to main content

qualia_solid_bridge/
oidc_micro_idp.rs

1//! Demo-only mock Solid-OIDC-shaped provider for **local personal pod** testing.
2//!
3//! This is **not** a production identity root (see `NON_GOALS.md`).
4//! Tokens are clearly labeled `demo-` / `mock-` and must never be treated as
5//! real provenance. Enabled at **runtime** via `BridgeConfig.demo_oidc`
6//! (and/or the historical `--features demo` compile flag for unit tests).
7
8use serde_json::json;
9use warp::Filter;
10
11/// Demo OIDC + minimal WebID profile routes bound to `public_base`.
12pub fn oidc_routes(
13    public_base: String,
14) -> impl Filter<Extract = impl warp::Reply, Error = warp::Rejection> + Clone {
15    let base = public_base.trim_end_matches('/').to_string();
16    let base_cfg = base.clone();
17    let config = warp::path!(".well-known" / "openid-configuration")
18        .and(warp::get())
19        .map(move || {
20            warp::reply::json(&json!({
21                "issuer": base_cfg,
22                "authorization_endpoint": format!("{base_cfg}/authorize"),
23                "token_endpoint": format!("{base_cfg}/token"),
24                "jwks_uri": format!("{base_cfg}/jwks"),
25                "registration_endpoint": format!("{base_cfg}/register"),
26                "response_types_supported": ["code", "id_token", "token"],
27                "subject_types_supported": ["public"],
28                "id_token_signing_alg_values_supported": ["EdDSA", "RS256"],
29                "token_endpoint_auth_methods_supported": ["client_secret_basic", "client_secret_post", "none"],
30                "scopes_supported": ["openid", "profile", "webid", "offline_access"],
31                "claims_supported": ["sub", "webid", "iss", "aud"],
32                "code_challenge_methods_supported": ["S256", "plain"]
33            }))
34        });
35
36    let jwks = warp::path!("jwks").and(warp::get()).map(|| {
37        // Placeholder JWKS — demo only. Real keys require audited identity subsystem.
38        warp::reply::json(&json!({
39            "keys": [{
40                "kty": "OKP",
41                "crv": "Ed25519",
42                "use": "sig",
43                "kid": "qualia-demo-local-key",
44                "x": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
45                "alg": "EdDSA"
46            }]
47        }))
48    });
49
50    let base_tok = base.clone();
51    let token = warp::path!("token")
52        .and(warp::post())
53        .and(warp::body::bytes())
54        .map(move |_body: bytes::Bytes| {
55            let webid = format!("{base_tok}/profile/card#me");
56            // Opaque demo tokens — not real JWTs. Solid clients that only check
57            // presence of Bearer will proceed; full DPoP/JWT validation needs R-band work.
58            warp::reply::json(&json!({
59                "access_token": format!("demo-access-{}", webid),
60                "token_type": "Bearer",
61                "expires_in": 3600,
62                "id_token": format!("demo-id-token.webid={}", webid),
63                "scope": "openid profile webid"
64            }))
65        });
66
67    let base_auth = base.clone();
68    let authorize = warp::path!("authorize")
69        .and(warp::get())
70        .and(warp::query::<std::collections::HashMap<String, String>>())
71        .map(move |q: std::collections::HashMap<String, String>| {
72            // Auto-approve demo: redirect to redirect_uri with code=demo-code
73            let redirect = q
74                .get("redirect_uri")
75                .cloned()
76                .unwrap_or_else(|| format!("{base_auth}/"));
77            let state = q.get("state").cloned().unwrap_or_default();
78            let sep = if redirect.contains('?') { "&" } else { "?" };
79            let loc = format!("{redirect}{sep}code=demo-auth-code&state={state}");
80            warp::http::Response::builder()
81                .status(302)
82                .header("location", loc)
83                .header("content-type", "text/plain")
84                .body("demo OIDC auto-approve redirect".to_string())
85                .unwrap_or_else(|_| warp::http::Response::new("redirect failed".to_string()))
86        });
87
88    // Dynamic client registration stub (many Solid clients call this).
89    let register = warp::path!("register")
90        .and(warp::post())
91        .and(warp::body::bytes())
92        .map(move |_body: bytes::Bytes| {
93            warp::reply::json(&json!({
94                "client_id": "qualia-demo-client",
95                "client_secret": "demo-not-secret",
96                "redirect_uris": [],
97                "client_name": "Qualia Demo Solid Client",
98                "token_endpoint_auth_method": "client_secret_basic"
99            }))
100        });
101
102    let base_prof = base.clone();
103    let profile = warp::path!("webizen" / "profile" / "card")
104        .and(warp::get())
105        .map(move || {
106            let turtle = format!(
107                r#"@prefix foaf: <http://xmlns.com/foaf/0.1/> .
108@prefix solid: <http://www.w3.org/ns/solid/terms#> .
109
110<#me> a foaf:Person ;
111    foaf:name "Local Webizen" ;
112    solid:oidcIssuer <{base_prof}> .
113"#
114            );
115            warp::reply::with_header(turtle, "content-type", "text/turtle")
116        });
117
118    config
119        .or(jwks)
120        .or(token)
121        .or(authorize)
122        .or(register)
123        .or(profile)
124}