structured-proxy 2.1.0

Universal gRPC→REST transcoding proxy — config-driven, works with any gRPC service
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! OpenID Connect discovery surface.
//!
//! When `oidc_discovery.enabled`, the proxy serves the OpenID Provider metadata
//! document at `/.well-known/openid-configuration` and a JWKS document at the
//! advertised `jwks_uri` path (built from the configured signing key, or an
//! empty set when none is set). This lets the proxy front an identity provider
//! so relying parties can discover endpoints and keys.

use std::path::Path;

use axum::routing::get;
use axum::{Json, Router};
use base64::engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD};
use base64::Engine;
use serde_json::{json, Map, Value};
use sha2::{Digest, Sha256};

use crate::config::OidcDiscoveryConfig;

/// Length of an Ed25519 public key in bytes (the tail of its SPKI encoding).
const ED25519_PUBLIC_KEY_LEN: usize = 32;

/// Fixed 12-byte SPKI header that precedes the 32-byte key in an Ed25519
/// `SubjectPublicKeyInfo` (`AlgorithmIdentifier` for `id-Ed25519` + BIT STRING).
const ED25519_SPKI_PREFIX: [u8; 12] = [
    0x30, 0x2a, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x70, 0x03, 0x21, 0x00,
];

/// Precomputed OIDC discovery responses.
pub struct Oidc {
    discovery: Value,
    jwks: Value,
    jwks_path: String,
}

impl Oidc {
    /// Build the discovery responses, or `None` when discovery is disabled.
    ///
    /// # Errors
    /// Returns an error when a configured signing key cannot be read or is not a
    /// supported (Ed25519) public key.
    pub fn build(config: &OidcDiscoveryConfig) -> Result<Option<Self>, String> {
        if !config.enabled {
            return Ok(None);
        }

        let alg = config
            .signing_key
            .as_ref()
            .map(|k| k.algorithm.clone())
            .unwrap_or_else(|| "EdDSA".to_string());
        let jwks_uri = config.jwks_uri.clone().unwrap_or_else(|| {
            format!(
                "{}/.well-known/jwks.json",
                config.issuer.trim_end_matches('/')
            )
        });

        let discovery = discovery_document(config, &alg, &jwks_uri);

        // Always have a JWKS to serve: an empty set when no signing key is
        // configured, so the advertised `jwks_uri` never 404s.
        let jwks = match &config.signing_key {
            Some(sk) => json!({ "keys": [ed25519_jwk(&sk.public_key_pem_file, &alg)?] }),
            None => json!({ "keys": [] }),
        };

        Ok(Some(Self {
            discovery,
            jwks,
            jwks_path: jwks_uri_path(&jwks_uri),
        }))
    }

    /// Routes serving the discovery document and the JWKS.
    pub fn routes<S>(&self) -> Router<S>
    where
        S: Clone + Send + Sync + 'static,
    {
        let discovery = self.discovery.clone();
        // Serialize the JWKS once; serve it with the RFC 7517 media type.
        let jwks_body =
            serde_json::to_string(&self.jwks).unwrap_or_else(|_| "{\"keys\":[]}".to_string());
        Router::new()
            .route(
                "/.well-known/openid-configuration",
                get(move || {
                    let doc = discovery.clone();
                    async move { Json(doc) }
                }),
            )
            .route(
                &self.jwks_path,
                get(move || {
                    let body = jwks_body.clone();
                    async move {
                        (
                            [(axum::http::header::CONTENT_TYPE, "application/jwk-set+json")],
                            body,
                        )
                    }
                }),
            )
    }
}

/// Build the OpenID Provider metadata document from config.
fn discovery_document(config: &OidcDiscoveryConfig, alg: &str, jwks_uri: &str) -> Value {
    let mut m = Map::new();
    m.insert("issuer".into(), json!(config.issuer));
    if let Some(v) = &config.authorization_endpoint {
        m.insert("authorization_endpoint".into(), json!(v));
    }
    if let Some(v) = &config.token_endpoint {
        m.insert("token_endpoint".into(), json!(v));
    }
    if let Some(v) = &config.userinfo_endpoint {
        m.insert("userinfo_endpoint".into(), json!(v));
    }
    m.insert("jwks_uri".into(), json!(jwks_uri));
    m.insert(
        "response_types_supported".into(),
        json!(["code", "id_token", "token id_token"]),
    );
    m.insert("subject_types_supported".into(), json!(["public"]));
    m.insert("id_token_signing_alg_values_supported".into(), json!([alg]));
    m.insert(
        "scopes_supported".into(),
        json!(["openid", "profile", "email"]),
    );
    m.insert(
        "token_endpoint_auth_methods_supported".into(),
        json!(["client_secret_basic", "client_secret_post"]),
    );
    Value::Object(m)
}

/// The path component of a (possibly absolute) JWKS URI.
fn jwks_uri_path(uri: &str) -> String {
    // Strip scheme://host if present, keep the path (default to a well-known).
    if let Some(rest) = uri.split_once("://") {
        match rest.1.find('/') {
            Some(idx) => rest.1[idx..].to_string(),
            None => "/.well-known/jwks.json".to_string(),
        }
    } else if uri.starts_with('/') {
        uri.to_string()
    } else {
        "/.well-known/jwks.json".to_string()
    }
}

/// Convert an Ed25519 public-key PEM into an OKP JWK.
fn ed25519_jwk(pem_path: &Path, alg: &str) -> Result<Value, String> {
    if !matches!(alg, "EdDSA" | "Ed25519") {
        return Err(format!(
            "oidc_discovery signing key algorithm {alg:?} is not supported (only EdDSA)"
        ));
    }
    let pem = std::fs::read_to_string(pem_path)
        .map_err(|e| format!("failed to read oidc signing key {pem_path:?}: {e}"))?;
    let der = decode_pem_body(&pem)?;
    // An Ed25519 SPKI is exactly the 12-byte prefix + 32-byte key. Verify both,
    // so an RSA/EC key (which would also be longer than 32 bytes) is rejected
    // loudly instead of having its tail bytes published as a bogus Ed25519 key.
    if der.len() != ED25519_SPKI_PREFIX.len() + ED25519_PUBLIC_KEY_LEN
        || der[..ED25519_SPKI_PREFIX.len()] != ED25519_SPKI_PREFIX
    {
        return Err(
            "oidc signing key is not a valid Ed25519 (EdDSA) public key in SPKI form".to_string(),
        );
    }
    let raw = &der[ED25519_SPKI_PREFIX.len()..];
    Ok(json!({
        "kty": "OKP",
        "crv": "Ed25519",
        "use": "sig",
        "alg": "EdDSA",
        "kid": key_id(raw),
        "x": URL_SAFE_NO_PAD.encode(raw),
    }))
}

/// Decode the base64 body of a PEM block.
fn decode_pem_body(pem: &str) -> Result<Vec<u8>, String> {
    let body: String = pem
        .lines()
        .filter(|l| !l.starts_with("-----"))
        .collect::<Vec<_>>()
        .join("");
    STANDARD
        .decode(body.trim())
        .map_err(|e| format!("invalid PEM base64: {e}"))
}

/// Stable key id: the first 16 hex chars of the SHA-256 of the public key.
fn key_id(raw: &[u8]) -> String {
    let digest = Sha256::digest(raw);
    hex16(&digest)
}

/// Render the first 8 bytes of a digest as lowercase hex.
fn hex16(bytes: &[u8]) -> String {
    use std::fmt::Write;
    let mut s = String::with_capacity(16);
    for b in bytes.iter().take(8) {
        let _ = write!(s, "{b:02x}");
    }
    s
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::SigningKeyConfig;

    const TEST_PUB_PEM: &str = "-----BEGIN PUBLIC KEY-----\n\
        MCowBQYDK2VwAyEARCMxEnaM2/dblLuPNgBZpTvSUXO5ir+XQ1nyzJm4CFw=\n\
        -----END PUBLIC KEY-----\n";

    fn write_pub() -> std::path::PathBuf {
        use std::sync::atomic::{AtomicU32, Ordering};
        static N: AtomicU32 = AtomicU32::new(0);
        let p = std::env::temp_dir().join(format!(
            "sp_oidc_{}_{}.pem",
            std::process::id(),
            N.fetch_add(1, Ordering::Relaxed)
        ));
        std::fs::write(&p, TEST_PUB_PEM).unwrap();
        p
    }

    #[test]
    fn discovery_document_includes_configured_endpoints() {
        let cfg = OidcDiscoveryConfig {
            enabled: true,
            issuer: "https://idp.example.com".into(),
            authorization_endpoint: Some("https://idp.example.com/authorize".into()),
            token_endpoint: Some("https://idp.example.com/token".into()),
            userinfo_endpoint: None,
            jwks_uri: None,
            signing_key: None,
        };
        let doc = discovery_document(
            &cfg,
            "EdDSA",
            "https://idp.example.com/.well-known/jwks.json",
        );
        assert_eq!(doc["issuer"], "https://idp.example.com");
        assert_eq!(
            doc["authorization_endpoint"],
            "https://idp.example.com/authorize"
        );
        assert_eq!(doc["token_endpoint"], "https://idp.example.com/token");
        // Absent endpoints are omitted, not null.
        assert!(doc.get("userinfo_endpoint").is_none());
        assert_eq!(
            doc["id_token_signing_alg_values_supported"],
            json!(["EdDSA"])
        );
        assert_eq!(
            doc["jwks_uri"],
            "https://idp.example.com/.well-known/jwks.json"
        );
    }

    #[test]
    fn jwks_uri_path_extracts_path() {
        assert_eq!(
            jwks_uri_path("https://idp.example.com/oauth/keys"),
            "/oauth/keys"
        );
        assert_eq!(jwks_uri_path("/keys.json"), "/keys.json");
        assert_eq!(
            jwks_uri_path("https://idp.example.com"),
            "/.well-known/jwks.json"
        );
    }

    #[test]
    fn ed25519_pem_becomes_okp_jwk() {
        let path = write_pub();
        let jwk = ed25519_jwk(&path, "EdDSA").unwrap();
        assert_eq!(jwk["kty"], "OKP");
        assert_eq!(jwk["crv"], "Ed25519");
        assert_eq!(jwk["alg"], "EdDSA");
        // x is the 32-byte key, base64url without padding (43 chars).
        assert_eq!(jwk["x"].as_str().unwrap().len(), 43);
        assert_eq!(jwk["kid"].as_str().unwrap().len(), 16);
    }

    #[test]
    fn non_eddsa_signing_key_is_rejected() {
        let path = write_pub();
        assert!(ed25519_jwk(&path, "RS256").is_err());
    }

    // An EC P-256 public key (91-byte SPKI) that the loose length check would
    // accept, publishing its last 32 bytes as a bogus Ed25519 key.
    const EC_PUB_PEM: &str = "-----BEGIN PUBLIC KEY-----\n\
        MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEgAJ0pjQcIv5a3YQTu2YHKyl9tYB8\n\
        zxWf7gcS1JeuSRRT6RtezpLXHy5SGMxFCWnJukWOqaLR2lgxTFxQ48HsKA==\n\
        -----END PUBLIC KEY-----\n";

    #[test]
    fn non_ed25519_spki_is_rejected_under_eddsa() {
        use std::sync::atomic::{AtomicU32, Ordering};
        static N: AtomicU32 = AtomicU32::new(1000);
        let p = std::env::temp_dir().join(format!(
            "sp_oidc_ec_{}_{}.pem",
            std::process::id(),
            N.fetch_add(1, Ordering::Relaxed)
        ));
        std::fs::write(&p, EC_PUB_PEM).unwrap();
        // EdDSA configured but the PEM is an EC key: must be a hard error, not a
        // silently-published wrong key.
        assert!(ed25519_jwk(&p, "EdDSA").is_err());
    }

    #[test]
    fn build_serves_jwks_when_signing_key_present() {
        let cfg = OidcDiscoveryConfig {
            enabled: true,
            issuer: "https://idp.example.com".into(),
            authorization_endpoint: None,
            token_endpoint: None,
            userinfo_endpoint: None,
            jwks_uri: Some("https://idp.example.com/keys.json".into()),
            signing_key: Some(SigningKeyConfig {
                algorithm: "EdDSA".into(),
                public_key_pem_file: write_pub(),
            }),
        };
        let oidc = Oidc::build(&cfg).unwrap().unwrap();
        assert_eq!(oidc.jwks_path, "/keys.json");
        assert_eq!(oidc.jwks["keys"][0]["kty"], "OKP");
        assert_eq!(
            oidc.discovery["jwks_uri"],
            "https://idp.example.com/keys.json"
        );
    }

    #[tokio::test]
    async fn routes_serve_discovery_and_jwks() {
        use axum::body::Body;
        use axum::http::Request;
        use tower::ServiceExt;

        let cfg = OidcDiscoveryConfig {
            enabled: true,
            issuer: "https://idp.example.com".into(),
            authorization_endpoint: None,
            token_endpoint: None,
            userinfo_endpoint: None,
            jwks_uri: Some("https://idp.example.com/keys.json".into()),
            signing_key: Some(SigningKeyConfig {
                algorithm: "EdDSA".into(),
                public_key_pem_file: write_pub(),
            }),
        };
        let app: axum::Router = Oidc::build(&cfg).unwrap().unwrap().routes();

        let disc = app
            .clone()
            .oneshot(
                Request::get("/.well-known/openid-configuration")
                    .body(Body::empty())
                    .unwrap(),
            )
            .await
            .unwrap();
        assert_eq!(disc.status(), 200);

        let jwks = app
            .oneshot(Request::get("/keys.json").body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(jwks.status(), 200);
        let body = axum::body::to_bytes(jwks.into_body(), 4096).await.unwrap();
        let v: Value = serde_json::from_slice(&body).unwrap();
        assert_eq!(v["keys"][0]["kty"], "OKP");
    }

    #[tokio::test]
    async fn jwks_uri_is_served_even_without_signing_key() {
        use axum::body::Body;
        use axum::http::Request;
        use tower::ServiceExt;

        // No signing key, but the discovery doc still advertises a local
        // jwks_uri, so that path must resolve (empty set), not 404.
        let cfg = OidcDiscoveryConfig {
            enabled: true,
            issuer: "https://idp.example.com".into(),
            authorization_endpoint: None,
            token_endpoint: None,
            userinfo_endpoint: None,
            jwks_uri: None,
            signing_key: None,
        };
        let oidc = Oidc::build(&cfg).unwrap().unwrap();
        let advertised = oidc.discovery["jwks_uri"].as_str().unwrap().to_string();
        let path = jwks_uri_path(&advertised);
        let app: axum::Router = oidc.routes();
        let resp = app
            .oneshot(Request::get(&path).body(Body::empty()).unwrap())
            .await
            .unwrap();
        assert_eq!(resp.status(), 200);
        assert_eq!(resp.headers()["content-type"], "application/jwk-set+json");
        let body = axum::body::to_bytes(resp.into_body(), 1024).await.unwrap();
        assert_eq!(
            serde_json::from_slice::<Value>(&body).unwrap(),
            json!({ "keys": [] })
        );
    }

    #[test]
    fn disabled_yields_none() {
        let cfg = OidcDiscoveryConfig {
            enabled: false,
            issuer: "x".into(),
            authorization_endpoint: None,
            token_endpoint: None,
            userinfo_endpoint: None,
            jwks_uri: None,
            signing_key: None,
        };
        assert!(Oidc::build(&cfg).unwrap().is_none());
    }
}