rscale 0.1.0

Self-hosted Rust control plane for operating a single tailnet with Tailscale clients
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
use std::collections::{BTreeMap, BTreeSet};
use std::time::Duration;

use axum::http::header::{AUTHORIZATION, HeaderValue};
use base64::Engine as _;
use base64::engine::general_purpose::{STANDARD, URL_SAFE_NO_PAD};
use graviola::hashing::{Hash, Sha256};
use jsonwebtoken::jwk::{JwkSet, PublicKeyUse};
use jsonwebtoken::{DecodingKey, Validation, decode, decode_header};
use reqx::TlsVersion;
use reqx::prelude::{Client, RetryPolicy};
use serde::{Deserialize, Serialize};

use crate::config::OidcConfig;
use crate::error::{AppError, AppResult};
use crate::infra::db::PendingOidcAuthRequest;

#[derive(Clone)]
pub struct OidcProviderClient {
    client: Client,
    issuer_url: String,
}

#[derive(Clone)]
pub struct OidcRuntime {
    client: OidcProviderClient,
    discovery: OidcDiscoveryDocument,
    public_base_url: String,
    client_id: String,
    client_secret: String,
    scopes: Vec<String>,
    allowed_domains: BTreeSet<String>,
    allowed_users: BTreeSet<String>,
    allowed_groups: BTreeSet<String>,
    extra_params: BTreeMap<String, String>,
    auth_flow_ttl_secs: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Default)]
pub struct OidcDiscoveryDocument {
    pub issuer: String,
    pub authorization_endpoint: String,
    pub token_endpoint: String,
    pub jwks_uri: String,
    pub userinfo_endpoint: Option<String>,
    pub end_session_endpoint: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct OidcPrincipal {
    pub issuer: String,
    pub subject: String,
    pub email: Option<String>,
    pub display_name: Option<String>,
    pub groups: Vec<String>,
}

impl OidcProviderClient {
    pub fn from_config(config: &OidcConfig) -> AppResult<Self> {
        validate(config)?;

        let issuer_url =
            normalize_url(config.issuer_url.as_deref().ok_or_else(|| {
                AppError::InvalidConfig("OIDC issuer_url is missing".to_string())
            })?);

        let client = Client::builder(issuer_url.clone())
            .client_name("rscale-oidc")
            .request_timeout(Duration::from_secs(config.request_timeout_secs))
            .total_timeout(Duration::from_secs(config.total_timeout_secs))
            .retry_policy(
                RetryPolicy::standard()
                    .max_attempts(2)
                    .base_backoff(Duration::from_millis(100))
                    .max_backoff(Duration::from_millis(500)),
            )
            .tls_min_version(TlsVersion::V1_2)
            .build()
            .map_err(|err| {
                AppError::Bootstrap(format!("failed to build OIDC HTTP client: {err}"))
            })?;

        Ok(Self { client, issuer_url })
    }

    pub async fn fetch_discovery(&self) -> AppResult<OidcDiscoveryDocument> {
        let document: OidcDiscoveryDocument = self
            .client
            .get("/.well-known/openid-configuration")
            .send_json()
            .await
            .map_err(|err| {
                AppError::Bootstrap(format!(
                    "failed to fetch OIDC discovery document from {}: {err}",
                    self.issuer_url
                ))
            })?;

        validate_discovery_document(&self.issuer_url, &document)?;

        Ok(document)
    }

    async fn exchange_code(
        &self,
        discovery: &OidcDiscoveryDocument,
        callback_url: &str,
        code: &str,
        client_id: &str,
        client_secret: &str,
        code_verifier: &str,
    ) -> AppResult<OidcTokenResponse> {
        let basic = STANDARD.encode(format!("{client_id}:{client_secret}"));
        let authorization = HeaderValue::from_str(&format!("Basic {basic}")).map_err(|err| {
            AppError::Bootstrap(format!(
                "failed to build OIDC token authorization header: {err}"
            ))
        })?;

        self.client
            .post(&discovery.token_endpoint)
            .header(AUTHORIZATION, authorization)
            .form(&OidcTokenRequest {
                grant_type: "authorization_code",
                code,
                redirect_uri: callback_url,
                client_id,
                code_verifier,
            })
            .map_err(AppError::from)?
            .send_json()
            .await
            .map_err(|err| {
                AppError::Unauthorized(format!("failed to exchange OIDC authorization code: {err}"))
            })
    }

    async fn fetch_jwks(&self, jwks_uri: &str) -> AppResult<JwkSet> {
        self.client.get(jwks_uri).send_json().await.map_err(|err| {
            AppError::Bootstrap(format!(
                "failed to fetch OIDC JWKS from {}: {err}",
                jwks_uri
            ))
        })
    }

    async fn fetch_userinfo(
        &self,
        userinfo_endpoint: &str,
        access_token: &str,
    ) -> AppResult<OidcUserInfo> {
        let authorization =
            HeaderValue::from_str(&format!("Bearer {access_token}")).map_err(|err| {
                AppError::Bootstrap(format!(
                    "failed to build OIDC userinfo authorization header: {err}"
                ))
            })?;

        self.client
            .get(userinfo_endpoint)
            .header(AUTHORIZATION, authorization)
            .send_json()
            .await
            .map_err(|err| AppError::Unauthorized(format!("failed to fetch OIDC userinfo: {err}")))
    }
}

impl OidcRuntime {
    pub async fn from_config(
        config: &OidcConfig,
        public_base_url: Option<&str>,
    ) -> AppResult<Option<Self>> {
        validate(config)?;

        if !config.enabled {
            return Ok(None);
        }

        let public_base_url = normalize_url(public_base_url.ok_or_else(|| {
            AppError::InvalidConfig(
                "server.public_base_url is required when OIDC is enabled".to_string(),
            )
        })?);
        let client = OidcProviderClient::from_config(config)?;
        let discovery = client.fetch_discovery().await?;

        Ok(Some(Self {
            client,
            discovery,
            public_base_url,
            client_id: config.client_id.clone().ok_or_else(|| {
                AppError::InvalidConfig(
                    "auth.oidc.client_id is required when OIDC is enabled".to_string(),
                )
            })?,
            client_secret: config.client_secret.clone().ok_or_else(|| {
                AppError::InvalidConfig(
                    "auth.oidc.client_secret is required when OIDC is enabled".to_string(),
                )
            })?,
            scopes: config.scopes.clone(),
            allowed_domains: normalized_set(&config.allowed_domains),
            allowed_users: normalized_set(&config.allowed_users),
            allowed_groups: normalized_set(&config.allowed_groups),
            extra_params: config.extra_params.clone(),
            auth_flow_ttl_secs: config.auth_flow_ttl_secs,
        }))
    }

    pub fn auth_flow_ttl_secs(&self) -> u64 {
        self.auth_flow_ttl_secs
    }

    pub fn registration_url(&self, auth_id: &str) -> String {
        format!("{}/register/{auth_id}", self.public_base_url)
    }

    pub fn callback_url(&self) -> String {
        format!("{}/oidc/callback", self.public_base_url)
    }

    pub fn authorization_redirect_url(
        &self,
        pending: &PendingOidcAuthRequest,
    ) -> AppResult<String> {
        self.authorization_redirect_url_for_flow(
            &pending.oidc_state,
            &pending.oidc_nonce,
            &pending.pkce_verifier,
        )
    }

    pub fn authorization_redirect_url_for_flow(
        &self,
        oidc_state: &str,
        oidc_nonce: &str,
        pkce_verifier: &str,
    ) -> AppResult<String> {
        let mut query = BTreeMap::from([
            ("response_type".to_string(), "code".to_string()),
            ("client_id".to_string(), self.client_id.clone()),
            ("redirect_uri".to_string(), self.callback_url()),
            ("scope".to_string(), self.scopes.join(" ")),
            ("state".to_string(), oidc_state.to_string()),
            ("nonce".to_string(), oidc_nonce.to_string()),
            ("code_challenge".to_string(), pkce_challenge(pkce_verifier)),
            ("code_challenge_method".to_string(), "S256".to_string()),
        ]);
        for (key, value) in &self.extra_params {
            query.insert(key.clone(), value.clone());
        }

        let encoded = serde_urlencoded::to_string(query).map_err(|err| {
            AppError::Bootstrap(format!("failed to encode OIDC authorization query: {err}"))
        })?;

        Ok(append_query(
            &self.discovery.authorization_endpoint,
            &encoded,
        ))
    }

    pub async fn complete_authorization(
        &self,
        pending: &PendingOidcAuthRequest,
        code: &str,
    ) -> AppResult<OidcPrincipal> {
        self.complete_authorization_for_flow(&pending.oidc_nonce, &pending.pkce_verifier, code)
            .await
    }

    pub async fn complete_authorization_for_flow(
        &self,
        oidc_nonce: &str,
        pkce_verifier: &str,
        code: &str,
    ) -> AppResult<OidcPrincipal> {
        let tokens = self
            .client
            .exchange_code(
                &self.discovery,
                &self.callback_url(),
                code,
                &self.client_id,
                &self.client_secret,
                pkce_verifier,
            )
            .await?;
        let jwks = self.client.fetch_jwks(&self.discovery.jwks_uri).await?;
        let claims = verify_id_token(
            &self.discovery,
            &self.client_id,
            &tokens.id_token,
            oidc_nonce,
            &jwks,
        )?;

        let userinfo = match (
            self.discovery.userinfo_endpoint.as_deref(),
            tokens.access_token.as_deref(),
        ) {
            (Some(endpoint), Some(access_token)) if !access_token.is_empty() => Some(
                self.client
                    .fetch_userinfo(endpoint, access_token)
                    .await
                    .map_err(|err| {
                        AppError::Unauthorized(format!(
                            "OIDC authentication succeeded but userinfo lookup failed: {err}"
                        ))
                    })?,
            ),
            _ => None,
        };

        let principal = merged_principal(&self.discovery.issuer, claims, userinfo);
        self.authorize_principal(&principal)?;
        Ok(principal)
    }

    fn authorize_principal(&self, principal: &OidcPrincipal) -> AppResult<()> {
        if !self.allowed_domains.is_empty() {
            let email = principal.email.as_deref().ok_or_else(|| {
                AppError::Unauthorized(
                    "OIDC login is missing an email claim required by allowed_domains".to_string(),
                )
            })?;
            let Some((_, domain)) = email.rsplit_once('@') else {
                return Err(AppError::Unauthorized(
                    "OIDC login email claim is malformed".to_string(),
                ));
            };
            if !self.allowed_domains.contains(&domain.to_ascii_lowercase()) {
                return Err(AppError::Unauthorized(
                    "OIDC login email domain is not allowed".to_string(),
                ));
            }
        }

        if !self.allowed_users.is_empty() {
            let mut candidates = BTreeSet::from([principal.subject.to_ascii_lowercase()]);
            if let Some(email) = &principal.email {
                candidates.insert(email.to_ascii_lowercase());
            }
            if !candidates
                .iter()
                .any(|candidate| self.allowed_users.contains(candidate))
            {
                return Err(AppError::Unauthorized(
                    "OIDC login subject is not in the allowed_users policy".to_string(),
                ));
            }
        }

        if !self.allowed_groups.is_empty()
            && !principal
                .groups
                .iter()
                .map(|group| group.to_ascii_lowercase())
                .any(|group| self.allowed_groups.contains(&group))
        {
            return Err(AppError::Unauthorized(
                "OIDC login does not satisfy the allowed_groups policy".to_string(),
            ));
        }

        Ok(())
    }
}

pub async fn bootstrap(config: &OidcConfig) -> AppResult<Option<OidcDiscoveryDocument>> {
    validate(config)?;

    if !config.enabled || !config.validate_discovery_on_startup {
        return Ok(None);
    }

    let client = OidcProviderClient::from_config(config)?;
    Ok(Some(client.fetch_discovery().await?))
}

pub fn validate(config: &OidcConfig) -> AppResult<()> {
    if !config.enabled {
        return Ok(());
    }

    let issuer_url = config
        .issuer_url
        .as_deref()
        .ok_or_else(|| AppError::InvalidConfig("OIDC issuer_url is missing".to_string()))?;

    if issuer_url.is_empty() {
        return Err(AppError::InvalidConfig(
            "OIDC issuer_url must not be empty".to_string(),
        ));
    }

    if config.client_id.as_deref().is_none_or(str::is_empty) {
        return Err(AppError::InvalidConfig(
            "OIDC client_id must not be empty when OIDC is enabled".to_string(),
        ));
    }

    if config.client_secret.as_deref().is_none_or(str::is_empty) {
        return Err(AppError::InvalidConfig(
            "OIDC client_secret must not be empty when OIDC is enabled".to_string(),
        ));
    }

    if config.request_timeout_secs == 0 {
        return Err(AppError::InvalidConfig(
            "OIDC request_timeout_secs must be greater than zero".to_string(),
        ));
    }

    if config.total_timeout_secs == 0 {
        return Err(AppError::InvalidConfig(
            "OIDC total_timeout_secs must be greater than zero".to_string(),
        ));
    }

    if config.total_timeout_secs < config.request_timeout_secs {
        return Err(AppError::InvalidConfig(
            "OIDC total_timeout_secs must be greater than or equal to request_timeout_secs"
                .to_string(),
        ));
    }

    if config.auth_flow_ttl_secs == 0 {
        return Err(AppError::InvalidConfig(
            "OIDC auth_flow_ttl_secs must be greater than zero".to_string(),
        ));
    }

    if config.scopes.is_empty() || config.scopes.iter().any(|scope| scope.trim().is_empty()) {
        return Err(AppError::InvalidConfig(
            "OIDC scopes must contain at least one non-empty scope".to_string(),
        ));
    }

    let normalized = normalize_url(issuer_url);
    if !is_secure_or_local(&normalized) {
        return Err(AppError::InvalidConfig(
            "OIDC issuer_url must use https unless it points to a local development endpoint"
                .to_string(),
        ));
    }

    Ok(())
}

fn verify_id_token(
    discovery: &OidcDiscoveryDocument,
    client_id: &str,
    id_token: &str,
    expected_nonce: &str,
    jwks: &JwkSet,
) -> AppResult<OidcIdTokenClaims> {
    let header = decode_header(id_token).map_err(|err| {
        AppError::Unauthorized(format!("failed to decode OIDC ID token header: {err}"))
    })?;
    let jwk = select_verification_key(jwks, header.kid.as_deref(), header.alg)?;
    let decoding_key = DecodingKey::from_jwk(jwk).map_err(|err| {
        AppError::Unauthorized(format!("failed to construct OIDC verification key: {err}"))
    })?;

    let mut validation = Validation::new(header.alg);
    validation.leeway = 30;
    validation.validate_nbf = true;
    validation.set_required_spec_claims(&["exp", "iss", "aud", "sub"]);
    validation.set_issuer(&[normalize_url(&discovery.issuer)]);
    validation.set_audience(&[client_id]);

    let claims = decode::<OidcIdTokenClaims>(id_token, &decoding_key, &validation)
        .map_err(|err| AppError::Unauthorized(format!("failed to validate OIDC ID token: {err}")))?
        .claims;

    if claims.nonce.as_deref() != Some(expected_nonce) {
        return Err(AppError::Unauthorized(
            "OIDC ID token nonce mismatch".to_string(),
        ));
    }

    if claims.email.is_some() && claims.email_verified == Some(false) {
        return Err(AppError::Unauthorized(
            "OIDC email claim is present but not verified".to_string(),
        ));
    }

    Ok(claims)
}

fn select_verification_key<'a>(
    jwks: &'a JwkSet,
    kid: Option<&str>,
    algorithm: jsonwebtoken::Algorithm,
) -> AppResult<&'a jsonwebtoken::jwk::Jwk> {
    let candidates = jwks
        .keys
        .iter()
        .filter(|jwk| {
            jwk.common
                .public_key_use
                .as_ref()
                .is_none_or(|value| *value == PublicKeyUse::Signature)
        })
        .collect::<Vec<_>>();

    let selected = if let Some(kid) = kid {
        candidates
            .into_iter()
            .find(|jwk| jwk.common.key_id.as_deref() == Some(kid))
    } else {
        candidates.into_iter().next()
    };

    selected.ok_or_else(|| {
        AppError::Unauthorized(format!(
            "OIDC provider JWKS does not contain a usable signing key for {algorithm:?}"
        ))
    })
}

fn merged_principal(
    issuer: &str,
    claims: OidcIdTokenClaims,
    userinfo: Option<OidcUserInfo>,
) -> OidcPrincipal {
    let email = userinfo
        .as_ref()
        .and_then(|value| value.email.clone())
        .or(claims.email)
        .map(normalize_email);
    let display_name = userinfo
        .as_ref()
        .and_then(|value| value.name.clone())
        .or(claims.name)
        .or(claims.preferred_username);
    let groups = userinfo
        .as_ref()
        .and_then(|value| value.groups.clone())
        .unwrap_or(claims.groups)
        .into_iter()
        .filter(|group| !group.trim().is_empty())
        .collect();

    OidcPrincipal {
        issuer: normalize_url(issuer),
        subject: claims.sub,
        email,
        display_name,
        groups,
    }
}

fn validate_discovery_document(
    expected_issuer: &str,
    document: &OidcDiscoveryDocument,
) -> AppResult<()> {
    let actual_issuer = normalize_url(&document.issuer);
    if actual_issuer != normalize_url(expected_issuer) {
        return Err(AppError::Bootstrap(format!(
            "OIDC discovery issuer mismatch: expected {expected_issuer}, got {}",
            document.issuer
        )));
    }

    if document.authorization_endpoint.is_empty() {
        return Err(AppError::Bootstrap(
            "OIDC discovery authorization_endpoint must not be empty".to_string(),
        ));
    }

    if document.token_endpoint.is_empty() {
        return Err(AppError::Bootstrap(
            "OIDC discovery token_endpoint must not be empty".to_string(),
        ));
    }

    if document.jwks_uri.is_empty() {
        return Err(AppError::Bootstrap(
            "OIDC discovery jwks_uri must not be empty".to_string(),
        ));
    }

    Ok(())
}

fn normalize_url(value: &str) -> String {
    value.trim().trim_end_matches('/').to_string()
}

fn normalize_email(value: String) -> String {
    value.trim().to_ascii_lowercase()
}

fn normalized_set(values: &[String]) -> BTreeSet<String> {
    values
        .iter()
        .map(|value| value.trim().to_ascii_lowercase())
        .filter(|value| !value.is_empty())
        .collect()
}

fn is_secure_or_local(value: &str) -> bool {
    value.starts_with("https://")
        || value.starts_with("http://127.0.0.1")
        || value.starts_with("http://localhost")
        || value.starts_with("http://[::1]")
}

fn pkce_challenge(verifier: &str) -> String {
    let digest = Sha256::hash(verifier.as_bytes());
    URL_SAFE_NO_PAD.encode(digest.as_ref())
}

fn append_query(base: &str, encoded_query: &str) -> String {
    if base.contains('?') {
        format!("{base}&{encoded_query}")
    } else {
        format!("{base}?{encoded_query}")
    }
}

#[derive(Debug, Serialize)]
struct OidcTokenRequest<'a> {
    grant_type: &'a str,
    code: &'a str,
    redirect_uri: &'a str,
    client_id: &'a str,
    code_verifier: &'a str,
}

#[derive(Debug, Clone, Deserialize)]
struct OidcTokenResponse {
    access_token: Option<String>,
    id_token: String,
}

#[derive(Debug, Clone, Deserialize)]
struct OidcIdTokenClaims {
    sub: String,
    #[serde(default)]
    nonce: Option<String>,
    #[serde(default)]
    email: Option<String>,
    #[serde(default)]
    email_verified: Option<bool>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    preferred_username: Option<String>,
    #[serde(default)]
    groups: Vec<String>,
}

#[derive(Debug, Clone, Deserialize)]
struct OidcUserInfo {
    #[serde(default)]
    email: Option<String>,
    #[serde(default)]
    name: Option<String>,
    #[serde(default)]
    groups: Option<Vec<String>>,
}

#[cfg(test)]
mod tests {
    use std::error::Error;

    use axum::Json;
    use axum::routing::get;
    use tokio::net::TcpListener;

    use super::*;

    type TestResult<T = ()> = Result<T, Box<dyn Error>>;

    fn test_config(base_url: String) -> OidcConfig {
        OidcConfig {
            enabled: true,
            issuer_url: Some(base_url),
            client_id: Some("rscale".to_string()),
            client_secret: Some("secret".to_string()),
            scopes: vec!["openid".to_string(), "email".to_string()],
            allowed_domains: Vec::new(),
            allowed_users: Vec::new(),
            allowed_groups: Vec::new(),
            extra_params: BTreeMap::new(),
            request_timeout_secs: 2,
            total_timeout_secs: 5,
            auth_flow_ttl_secs: 300,
            validate_discovery_on_startup: true,
        }
    }

    #[tokio::test]
    async fn bootstrap_fetches_discovery_document() -> TestResult {
        let listener = TcpListener::bind("127.0.0.1:0").await?;
        let addr = listener.local_addr()?;
        let issuer = format!("http://{addr}");

        let app = axum::Router::new().route(
            "/.well-known/openid-configuration",
            get({
                let issuer = issuer.clone();
                move || async move {
                    Json(OidcDiscoveryDocument {
                        issuer: issuer.clone(),
                        authorization_endpoint: format!("{issuer}/authorize"),
                        token_endpoint: format!("{issuer}/token"),
                        jwks_uri: format!("{issuer}/jwks.json"),
                        userinfo_endpoint: Some(format!("{issuer}/userinfo")),
                        end_session_endpoint: None,
                    })
                }
            }),
        );

        let server = tokio::spawn(async move {
            let _ = axum::serve(listener, app).await;
        });

        let config = test_config(issuer.clone());
        let discovery = bootstrap(&config)
            .await?
            .ok_or_else(|| std::io::Error::other("discovery should be returned"))?;

        assert_eq!(discovery.issuer, issuer);

        server.abort();
        Ok(())
    }

    #[test]
    fn validate_rejects_non_secure_non_local_issuer() {
        let config = test_config("http://example.com".to_string());
        assert!(validate(&config).is_err());
    }

    #[test]
    fn authorization_redirect_includes_state_nonce_and_pkce() -> TestResult {
        let runtime = OidcRuntime {
            client: OidcProviderClient {
                client: Client::builder("https://issuer.example.com").build()?,
                issuer_url: "https://issuer.example.com".to_string(),
            },
            discovery: OidcDiscoveryDocument {
                issuer: "https://issuer.example.com".to_string(),
                authorization_endpoint: "https://issuer.example.com/authorize".to_string(),
                token_endpoint: "https://issuer.example.com/token".to_string(),
                jwks_uri: "https://issuer.example.com/jwks.json".to_string(),
                userinfo_endpoint: None,
                end_session_endpoint: None,
            },
            public_base_url: "https://rscale.example.com".to_string(),
            client_id: "rscale".to_string(),
            client_secret: "secret".to_string(),
            scopes: vec!["openid".to_string(), "email".to_string()],
            allowed_domains: BTreeSet::new(),
            allowed_users: BTreeSet::new(),
            allowed_groups: BTreeSet::new(),
            extra_params: BTreeMap::from([("prompt".to_string(), "login".to_string())]),
            auth_flow_ttl_secs: 300,
        };
        let pending = PendingOidcAuthRequest {
            auth_id: "auth-1".to_string(),
            machine_key: "mkey:1".to_string(),
            node_key: "nodekey:1".to_string(),
            oidc_state: "state".to_string(),
            oidc_nonce: "nonce".to_string(),
            pkce_verifier: "verifier".to_string(),
            principal_issuer: None,
            principal_sub: None,
            principal_email: None,
            principal_name: None,
            principal_groups: Vec::new(),
            node_id: None,
            expires_at_unix_secs: 1,
            completed_at_unix_secs: None,
        };

        let redirect = runtime.authorization_redirect_url(&pending)?;

        assert!(redirect.contains("state=state"));
        assert!(redirect.contains("nonce=nonce"));
        assert!(redirect.contains("code_challenge_method=S256"));
        assert!(redirect.contains("prompt=login"));

        Ok(())
    }
}