gephyr 1.16.18

Gephyr is a headless local AI relay/proxy API handling OpenAI, Claude, and Gemini-compatible APIs
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use jsonwebtoken::{decode, decode_header, Algorithm, DecodingKey, Validation};
use serde::{Deserialize, Deserializer, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::{Arc, Mutex, OnceLock};

const GOOGLE_JWKS_URL: &str = "https://www.googleapis.com/oauth2/v3/certs";
const JWKS_CACHE_TTL_SECONDS: i64 = 3600;
const ALLOWED_ISSUERS: [&str; 2] = ["accounts.google.com", "https://accounts.google.com"];

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct GoogleIdClaims {
    pub sub: String,
    pub email: String,
    pub email_verified: bool,
    pub name: Option<String>,
    pub hd: Option<String>,
}

#[derive(Debug, Deserialize)]
struct RawGoogleIdClaims {
    #[serde(rename = "iss")]
    _iss: String,
    #[serde(rename = "aud")]
    _aud: serde_json::Value,
    #[serde(rename = "exp")]
    _exp: i64,
    pub sub: String,
    pub email: String,
    #[serde(default, deserialize_with = "deserialize_email_verified")]
    pub email_verified: bool,
    #[serde(default)]
    pub name: Option<String>,
    #[serde(default)]
    pub hd: Option<String>,
}

#[derive(Debug, Deserialize)]
struct GoogleJwks {
    keys: Vec<GoogleJwk>,
}

#[derive(Debug, Deserialize)]
struct GoogleJwk {
    kid: String,
    kty: String,
    #[serde(default)]
    alg: Option<String>,
    #[serde(rename = "use", default)]
    key_use: Option<String>,
    #[serde(default)]
    n: Option<String>,
    #[serde(default)]
    e: Option<String>,
}

#[derive(Clone)]
struct CachedJwks {
    fetched_at_unix: i64,
    keys: HashMap<String, Arc<DecodingKey>>,
}

static JWKS_CACHE: OnceLock<Mutex<Option<CachedJwks>>> = OnceLock::new();

fn cache_state() -> &'static Mutex<Option<CachedJwks>> {
    JWKS_CACHE.get_or_init(|| Mutex::new(None))
}

fn now_unix() -> i64 {
    chrono::Utc::now().timestamp()
}

fn deserialize_email_verified<'de, D>(deserializer: D) -> Result<bool, D::Error>
where
    D: Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(untagged)]
    enum BoolOrString {
        Bool(bool),
        String(String),
    }

    let raw = Option::<BoolOrString>::deserialize(deserializer)?;
    match raw {
        Some(BoolOrString::Bool(v)) => Ok(v),
        Some(BoolOrString::String(v)) => {
            let normalized = v.trim().to_ascii_lowercase();
            Ok(normalized == "true" || normalized == "1")
        }
        None => Ok(false),
    }
}

fn parse_allowed_domains() -> Option<HashSet<String>> {
    let raw = std::env::var("ALLOWED_GOOGLE_DOMAINS").ok()?;
    let set: HashSet<String> = raw
        .split(',')
        .map(|s| s.trim().to_ascii_lowercase())
        .filter(|s| !s.is_empty())
        .collect();
    if set.is_empty() {
        None
    } else {
        Some(set)
    }
}

fn validate_domain_allowlist(hd: Option<&str>) -> Result<(), String> {
    let Some(allowed) = parse_allowed_domains() else {
        return Ok(());
    };
    let Some(domain) = hd.map(|v| v.trim().to_ascii_lowercase()) else {
        return Err(
            "Google hosted domain is required but missing (ALLOWED_GOOGLE_DOMAINS)".to_string(),
        );
    };
    if allowed.contains(&domain) {
        Ok(())
    } else {
        Err(format!(
            "Google hosted domain '{}' is not in ALLOWED_GOOGLE_DOMAINS",
            domain
        ))
    }
}

fn get_cached_keys_if_fresh(now: i64) -> Result<Option<HashMap<String, Arc<DecodingKey>>>, String> {
    let lock = cache_state()
        .lock()
        .map_err(|_| "JWKS cache lock is poisoned".to_string())?;
    let Some(cached) = lock.as_ref() else {
        return Ok(None);
    };
    if now - cached.fetched_at_unix < JWKS_CACHE_TTL_SECONDS {
        Ok(Some(cached.keys.clone()))
    } else {
        Ok(None)
    }
}

async fn fetch_google_jwks() -> Result<HashMap<String, Arc<DecodingKey>>, String> {
    let client = if let Some(pool) = crate::proxy::proxy_pool::get_global_proxy_pool() {
        pool.get_effective_client(None, 60)
            .await
            .map_err(|e| format!("Failed to prepare JWKS client: {}", e))?
    } else {
        crate::utils::http::get_long_client()
    };
    let response = client
        .get(GOOGLE_JWKS_URL)
        .header(
            reqwest::header::USER_AGENT,
            crate::constants::USER_AGENT.as_str(),
        )
        .send()
        .await
        .map_err(|e| format!("Failed to fetch Google JWKS: {}", e))?;

    if !response.status().is_success() {
        let status = response.status();
        let body = response.text().await.unwrap_or_default();
        return Err(format!(
            "Google JWKS fetch failed with status {}: {}",
            status, body
        ));
    }

    let jwks = response
        .json::<GoogleJwks>()
        .await
        .map_err(|e| format!("Failed to parse Google JWKS response: {}", e))?;

    let mut keys = HashMap::new();
    for jwk in jwks.keys {
        if jwk.kty != "RSA" {
            continue;
        }
        if let Some(key_use) = jwk.key_use.as_deref() {
            if key_use != "sig" {
                continue;
            }
        }
        if let Some(alg) = jwk.alg.as_deref() {
            if alg != "RS256" {
                continue;
            }
        }
        let (Some(n), Some(e)) = (jwk.n.as_deref(), jwk.e.as_deref()) else {
            continue;
        };
        match DecodingKey::from_rsa_components(n, e) {
            Ok(key) => {
                keys.insert(jwk.kid, Arc::new(key));
            }
            Err(err) => {
                tracing::warn!("Skipping invalid Google JWK: {}", err);
            }
        }
    }

    if keys.is_empty() {
        return Err("Google JWKS did not contain any usable RS256 keys".to_string());
    }

    let mut lock = cache_state()
        .lock()
        .map_err(|_| "JWKS cache lock is poisoned".to_string())?;
    *lock = Some(CachedJwks {
        fetched_at_unix: now_unix(),
        keys: keys.clone(),
    });

    Ok(keys)
}

async fn get_jwks_keys(force_refresh: bool) -> Result<HashMap<String, Arc<DecodingKey>>, String> {
    if !force_refresh {
        if let Some(keys) = get_cached_keys_if_fresh(now_unix())? {
            return Ok(keys);
        }
    }
    fetch_google_jwks().await
}

pub async fn validate_id_token(raw_jwt: &str) -> Result<GoogleIdClaims, String> {
    let header = decode_header(raw_jwt).map_err(|e| format!("Invalid id_token header: {}", e))?;
    if header.alg != Algorithm::RS256 {
        return Err(format!(
            "Unsupported id_token algorithm: {:?} (expected RS256)",
            header.alg
        ));
    }
    let kid = header
        .kid
        .ok_or_else(|| "id_token missing 'kid' header".to_string())?;

    let mut keys = get_jwks_keys(false).await?;
    let key = if let Some(k) = keys.get(&kid) {
        k.clone()
    } else {
        keys = get_jwks_keys(true).await?;
        keys.get(&kid)
            .cloned()
            .ok_or_else(|| format!("No matching Google JWK for kid '{}'", kid))?
    };

    let audience = crate::modules::auth::oauth::client_id()?;
    let mut validation = Validation::new(Algorithm::RS256);
    validation.set_issuer(&ALLOWED_ISSUERS);
    validation.set_audience(&[audience.as_str()]);
    validation.validate_exp = true;
    validation.leeway = 0;

    let token_data = decode::<RawGoogleIdClaims>(raw_jwt, key.as_ref(), &validation)
        .map_err(|e| format!("id_token validation failed: {}", e))?;
    let claims = token_data.claims;

    if !claims.email_verified {
        return Err("Google id_token rejected: email is not verified".to_string());
    }
    validate_domain_allowlist(claims.hd.as_deref())?;

    Ok(GoogleIdClaims {
        sub: claims.sub,
        email: claims.email,
        email_verified: claims.email_verified,
        name: claims.name,
        hd: claims.hd,
    })
}

#[cfg(test)]
fn set_jwks_cache_for_tests(keys: HashMap<String, Arc<DecodingKey>>) {
    let mut lock = cache_state().lock().expect("set_jwks_cache_for_tests lock");
    *lock = Some(CachedJwks {
        fetched_at_unix: now_unix(),
        keys,
    });
}

#[cfg(test)]
fn clear_jwks_cache_for_tests() {
    let mut lock = cache_state()
        .lock()
        .expect("clear_jwks_cache_for_tests lock");
    *lock = None;
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_utils::ScopedEnvVar;
    use jsonwebtoken::{encode, EncodingKey, Header};
    use serde::Serialize;

    const TEST_KID: &str = "test-kid-1";
    const TEST_CLIENT_ID: &str = "test-client.apps.googleusercontent.com";
    const TEST_ISSUER: &str = "https://accounts.google.com";
    const TEST_PRIVATE_KEY_PEM: &str = r#"-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC65tN/qAE+tTZS
7RR+L0vpkMzkNv/JTk9d9lAAKAi/1Hq3R4T5fMbEBrGtMNR3HbyUwCkOYdv5XYqK
k4546wUfvkcIuYdfRLwvlqXS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M
+uB2n3/F8f1YAxPoG6gvfRMtXb0TJ+JuC+WUcU5RvoQhG37F7YByCNIpsQKBgFz4
ozsJOBf7mTSuhSnUtbArHtl5X2fGF7B9oE1YvqnKb/VCoVtgqlKjKRIsmNAixjk0
x7m+KkXzpQ/BIsT2v3ovz/DIlbHZdTMlipPWnnRvK4wbtKBMsu37GvT8XemovPU+
286NNGXI16SpUzhYDxUYsXZtx1N1Bms9BLdwMmjhAoGANBM2/Fd4Zm4w0l27XHiJ
yyimSFrbqZkXdASfNAC1GTu0J457pWj8Z/bogEguZ4lK8yflCX7AJZJUfsoUysti
qXAeL4sDAiTEWTED3vuPuG72BN+yQ1CtmNMY+KfN80T584gcOoT+u30jM7ucn5fJ
I5B6EmW+tn/EROqBlqI6yIQ=
-----END PRIVATE KEY-----"#;
    const TEST_PUBLIC_KEY_PEM: &str = r#"-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuubTf6gBPrU2Uu0Ufi9L
6ZDM5Db/yU5PXfZQACgIv9R6t0eE+XzGxAaxrTDUdx28lMApDmHb+V2KipOOeOsF
H75HCLmHX0S8L5al0oyf5778gl2Rggas8YmAqzZ1ljhAVedi/PaswSgS+Oyh0gH/
sS+EsI9uUKErWc+Phv0N7kuKX63o7K6BgefnVyUILYqu5MpQuAv41LCofsTL4FTn
5bowJ76LmHa8qLMShnHgacQIbK68nW0w/t+6sA/Vup3Jfygg5Xm4RwYvv1UI03dP
wAMT1t6aoujR6HCqZfClABJ3dkuEc5WRbSDgFyDu3/sgF7sdyXv6jgSzSOOWzrkw
BQIDAQAB
-----END PUBLIC KEY-----"#;

    #[derive(Serialize)]
    struct TestClaims<'a> {
        iss: &'a str,
        aud: &'a str,
        exp: i64,
        sub: &'a str,
        email: &'a str,
        email_verified: bool,
        hd: Option<&'a str>,
        name: Option<&'a str>,
    }

    fn setup_test_env() -> (ScopedEnvVar, ScopedEnvVar) {
        let decoding =
            DecodingKey::from_rsa_pem(TEST_PUBLIC_KEY_PEM.as_bytes()).expect("test decoding key");
        let mut keys = HashMap::new();
        keys.insert(TEST_KID.to_string(), Arc::new(decoding));
        set_jwks_cache_for_tests(keys);
        let client_id = ScopedEnvVar::set("GOOGLE_OAUTH_CLIENT_ID", TEST_CLIENT_ID);
        let allow_domains = ScopedEnvVar::unset("ALLOWED_GOOGLE_DOMAINS");
        (client_id, allow_domains)
    }

    fn teardown_test_env() {
        clear_jwks_cache_for_tests();
    }

    fn sign_test_jwt(claims: &TestClaims<'_>) -> String {
        let mut header = Header::new(Algorithm::RS256);
        header.kid = Some(TEST_KID.to_string());
        let encoding_key =
            EncodingKey::from_rsa_pem(TEST_PRIVATE_KEY_PEM.as_bytes()).expect("test encoding key");
        encode(&header, claims, &encoding_key).expect("encode test jwt")
    }

    #[tokio::test]
    async fn test_validate_valid_id_token() {
        let _guard = crate::test_utils::lock_env();
        let (_client_id, _allow_domains) = setup_test_env();
        let now = now_unix();
        let token = sign_test_jwt(&TestClaims {
            iss: TEST_ISSUER,
            aud: TEST_CLIENT_ID,
            exp: now + 3600,
            sub: "google-sub-1",
            email: "valid@example.com",
            email_verified: true,
            hd: Some("example.com"),
            name: Some("Valid User"),
        });

        let result = validate_id_token(&token).await.expect("valid id_token");
        assert_eq!(result.sub, "google-sub-1");
        assert_eq!(result.email, "valid@example.com");
        assert!(result.email_verified);
        teardown_test_env();
    }

    #[tokio::test]
    async fn test_reject_expired_token() {
        let _guard = crate::test_utils::lock_env();
        let (_client_id, _allow_domains) = setup_test_env();
        let now = now_unix();
        let token = sign_test_jwt(&TestClaims {
            iss: TEST_ISSUER,
            aud: TEST_CLIENT_ID,
            exp: now - 10,
            sub: "google-sub-2",
            email: "expired@example.com",
            email_verified: true,
            hd: None,
            name: None,
        });

        let err = validate_id_token(&token)
            .await
            .expect_err("expired token must fail");
        assert!(err.contains("id_token validation failed"));
        teardown_test_env();
    }

    #[tokio::test]
    async fn test_reject_wrong_audience() {
        let _guard = crate::test_utils::lock_env();
        let (_client_id, _allow_domains) = setup_test_env();
        let now = now_unix();
        let token = sign_test_jwt(&TestClaims {
            iss: TEST_ISSUER,
            aud: "another-client.apps.googleusercontent.com",
            exp: now + 3600,
            sub: "google-sub-3",
            email: "aud@example.com",
            email_verified: true,
            hd: None,
            name: None,
        });

        let err = validate_id_token(&token)
            .await
            .expect_err("wrong audience must fail");
        assert!(err.contains("id_token validation failed"));
        teardown_test_env();
    }

    #[tokio::test]
    async fn test_reject_wrong_issuer() {
        let _guard = crate::test_utils::lock_env();
        let (_client_id, _allow_domains) = setup_test_env();
        let now = now_unix();
        let token = sign_test_jwt(&TestClaims {
            iss: "https://malicious.example",
            aud: TEST_CLIENT_ID,
            exp: now + 3600,
            sub: "google-sub-4",
            email: "issuer@example.com",
            email_verified: true,
            hd: None,
            name: None,
        });

        let err = validate_id_token(&token)
            .await
            .expect_err("wrong issuer must fail");
        assert!(err.contains("id_token validation failed"));
        teardown_test_env();
    }

    #[tokio::test]
    async fn test_reject_unverified_email() {
        let _guard = crate::test_utils::lock_env();
        let (_client_id, _allow_domains) = setup_test_env();
        let now = now_unix();
        let token = sign_test_jwt(&TestClaims {
            iss: TEST_ISSUER,
            aud: TEST_CLIENT_ID,
            exp: now + 3600,
            sub: "google-sub-5",
            email: "unverified@example.com",
            email_verified: false,
            hd: None,
            name: None,
        });

        let err = validate_id_token(&token)
            .await
            .expect_err("unverified email must fail");
        assert!(err.contains("email is not verified"));
        teardown_test_env();
    }

    #[tokio::test]
    async fn test_domain_allowlist_rejects_unknown_domain() {
        let _guard = crate::test_utils::lock_env();
        let (_client_id, _allow_domains) = setup_test_env();
        let _allowed_domains = ScopedEnvVar::set("ALLOWED_GOOGLE_DOMAINS", "corp.example.com");
        let now = now_unix();
        let token = sign_test_jwt(&TestClaims {
            iss: TEST_ISSUER,
            aud: TEST_CLIENT_ID,
            exp: now + 3600,
            sub: "google-sub-6",
            email: "domain@example.com",
            email_verified: true,
            hd: Some("gmail.com"),
            name: None,
        });

        let err = validate_id_token(&token)
            .await
            .expect_err("disallowed domain must fail");
        assert!(err.contains("not in ALLOWED_GOOGLE_DOMAINS"));
        teardown_test_env();
    }
}