pas-external 0.14.1

Ppoppo Accounts System (PAS) external SDK — OAuth2 PKCE, JWT verification port, Axum middleware, session liveness
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
//! OIDC RP composition root.
//!
//! [`RelyingParty<S>`] is the deep-module entry point for OAuth + OIDC
//! integration. It hides ALL SDK-internal collaborators behind 3
//! lifecycle methods:
//!
//! - [`new`](RelyingParty::new) — discovery + JWKS bootstrap +
//!   internal `oauth::AuthClient` + [`PasIdTokenVerifier<S>`]
//!   composition. One call at consumer boot.
//! - [`start`](RelyingParty::start) — generate state + nonce + PKCE,
//!   persist in the [`StateStore`], return [`AuthorizationRedirect`].
//! - [`complete`](RelyingParty::complete) — atomic state-take + token
//!   exchange + id_token verify, return [`Completion<S>`].
//!
//! The consumer never directly imports `oauth::AuthClient` or
//! [`PasIdTokenVerifier<S>`]; the only port the consumer implements
//! is [`StateStore`] (the OIDC-specific atomic single-use invariant).
//! Discovery + JWKS + PKCE + nonce + URL building are all in-process
//! composition that earned its place inside this struct because the
//! caller cannot meaningfully customize them.
//!
//! ── 3 methods, 4 hidden collaborators ───────────────────────────────────
//!
//! | Hidden inside `RelyingParty<S>` | Surfaced to consumer |
//! |---------------------------------|----------------------|
//! | [`super::discovery::fetch_discovery`] (boot)         | none |
//! | [`PasIdTokenVerifier`] (boot + complete)             | none |
//! | `oauth::AuthClient` (boot + complete)                | none |
//! | [`crate::pkce`] (every `start`)                      | none |
//! | nonce generation (every `start`)                     | none |
//! | state generation (every `start`)                     | [`State`] (round-trip key only) |
//!
//! ── Scope contract ──────────────────────────────────────────────────────
//!
//! The marker `S` parameter does triple duty:
//!
//! 1. Determines the requested-scope string sent to PAS at `start`
//!    (via [`RequestedScope::SCOPE`]).
//! 2. Carries through to the verifier, narrowing the post-verify
//!    [`super::IdAssertion<S>`] PII surface (Phase 10's marker traits
//!    `HasEmail` / `HasProfile` / `HasPhone` / `HasAddress`).
//! 3. Propagates into [`Completion<S>`] so the consumer's typed
//!    handler signature mirrors the requested scope without runtime
//!    re-derivation.
//!
//! Asking for a scope at construction time and being unable to read
//! claims outside that scope is a single architectural decision
//! enforced at compile time.

use std::sync::Arc;

use ppoppo_clock::ArcClock;
use ppoppo_clock::native::WallClock;
use ppoppo_token::id_token::scopes::{
    Email, EmailProfile, EmailProfilePhone, EmailProfilePhoneAddress, Openid, Profile,
};
use ppoppo_token::id_token::Nonce;
use url::Url;

use super::{
    discovery::{fetch_discovery, Discovery, DiscoveryError},
    port::{IdTokenVerifier, IdVerifyError, ScopePiiReader},
    refresh_outcome::RefreshOutcome,
    state_store::{
        AuthorizationRedirect, CallbackParams, Completion, Config, PendingAuthRequest,
        RelativePath, State, StateStore, StateStoreError,
    },
    verifier::PasIdTokenVerifier,
};
use crate::oauth::{AuthClient, OAuthConfig};
use crate::pkce;
use crate::{JwtVerifier, VerifyConfig};

// ────────────────────────────────────────────────────────────────────────
// RequestedScope — S → scope-string mapping
// ────────────────────────────────────────────────────────────────────────

/// Scope marker → scope-parameter mapping for the OIDC `scope` query
/// parameter sent to PAS at `start`.
///
/// Phase 10's [`ScopePiiReader`] gates which PII fields the verifier
/// hydrates (and therefore which accessors compile on the resulting
/// [`super::IdAssertion<S>`]). [`RequestedScope`] is the *strictly
/// stronger* trait the RP requires: every scope marker also has a
/// canonical string sent on the wire. The two ends meet —
/// `S = scopes::Email` causes both `scope=openid email` to be sent
/// AND `assertion.email()` to compile, with the same single type
/// parameter wiring the contract end-to-end.
///
/// **Adding a scope**: implement [`RequestedScope`] for the engine's
/// new marker type with the exact wire string. The RP does not
/// validate the string against any allowlist — PAS rejects unknown
/// scopes at the authorize endpoint.
pub trait RequestedScope: ScopePiiReader {
    /// The exact `scope` parameter value sent to PAS, space-separated
    /// per RFC 6749 §3.3.
    const SCOPE: &'static str;
}

impl RequestedScope for Openid {
    const SCOPE: &'static str = "openid";
}
impl RequestedScope for Email {
    const SCOPE: &'static str = "openid email";
}
impl RequestedScope for Profile {
    const SCOPE: &'static str = "openid profile";
}
impl RequestedScope for EmailProfile {
    const SCOPE: &'static str = "openid email profile";
}
impl RequestedScope for EmailProfilePhone {
    const SCOPE: &'static str = "openid email profile phone";
}
impl RequestedScope for EmailProfilePhoneAddress {
    const SCOPE: &'static str = "openid email profile phone address";
}

// ────────────────────────────────────────────────────────────────────────
// RelyingParty
// ────────────────────────────────────────────────────────────────────────

/// OAuth + OIDC Relying Party composition root.
///
/// `S: RequestedScope` propagates the scope contract from the
/// requested-scope wire parameter through Phase 10's
/// [`PasIdTokenVerifier<S>`] into the post-verify
/// [`Completion<S>`]. Constructing
/// `RelyingParty::<scopes::Email>` narrows the scope-bounded PII
/// available on the post-login assertion to the email scope claims.
///
/// Wrap in `Arc` for axum state — internals are mostly `Arc`-shared
/// (JWKS cache, reqwest client). The consumer typically constructs
/// once at boot and stores `Arc<RelyingParty<S>>` in app state.
pub struct RelyingParty<S: RequestedScope> {
    config: Config,
    state_store: Arc<dyn StateStore>,
    auth_client: AuthClient,
    verifier: Arc<dyn IdTokenVerifier<S>>,
    discovery: Discovery,
    clock: ArcClock,
}

// `Arc<dyn StateStore>` / `AuthClient` / `Arc<dyn IdTokenVerifier<S>>`
// don't trivially `Debug`. Manual impl shows only the non-sensitive
// boot-time configuration so consumers can log RP state without
// risking credential exposure.
impl<S: RequestedScope> std::fmt::Debug for RelyingParty<S> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RelyingParty")
            .field("config", &self.config)
            .field("discovery", &self.discovery)
            .finish_non_exhaustive()
    }
}

/// Construction-time failure surface.
#[derive(Debug, thiserror::Error)]
pub enum RelyingPartyInitError {
    #[error("OIDC discovery fetch failed: {0}")]
    Discovery(#[from] DiscoveryError),
    #[error("JWKS fetch failed: {0}")]
    Jwks(IdVerifyError),
    #[error("OAuth client construction failed: {0}")]
    OAuthClient(String),
}

/// `start` failure surface.
#[derive(Debug, thiserror::Error)]
pub enum StartError {
    #[error("state store failure: {0}")]
    StateStore(#[from] StateStoreError),
    #[error("authorize URL construction failed: {0}")]
    UrlBuild(String),
}

/// `refresh` failure surface.
///
/// Mirrors [`CallbackError`]'s split between credential-rejection and
/// substrate-failure: 4xx responses indicate a dead refresh_token (the
/// consumer should clear the session cookies and force re-auth); 5xx
/// or transport failures are transient (the consumer may retry).
#[derive(Debug, thiserror::Error)]
pub enum RefreshError {
    /// PAS rejected the refresh_token (4xx). Dead credential — the
    /// consumer must clear the session and start a new authorization
    /// flow. Token rotation is a no-op when there is no live token to
    /// rotate.
    #[error("refresh_token rejected by PAS: {0}")]
    Rejected(String),

    /// PAS service is degraded (5xx) or transport-level failure
    /// (timeout, TLS, DNS). The session may still be live; the
    /// consumer should fail-soft (e.g., return 503 to the browser
    /// rather than clearing cookies).
    #[error("refresh transient failure: {0}")]
    Transient(String),
}

/// `complete` failure surface.
///
/// `StateNotFoundOrConsumed` is the load-bearing CSRF / state-replay
/// defense: state-store substrate atomicity guarantees that a second
/// `complete` call with the same `state` lands here regardless of
/// whether the first call succeeded or failed late.
#[derive(Debug, thiserror::Error)]
pub enum CallbackError {
    /// State key absent from substrate at callback. Indistinguishable
    /// across "never existed", "already consumed", "TTL-expired" — all
    /// three are CSRF-equivalent and intentionally collapse into one
    /// variant.
    #[error("state not found or already consumed (CSRF defense triggered)")]
    StateNotFoundOrConsumed,

    #[error("state store failure: {0}")]
    StateStore(#[from] StateStoreError),

    #[error("token exchange failed: {0}")]
    TokenExchange(String),

    #[error("id_token verification failed: {0}")]
    IdToken(#[from] IdVerifyError),
}

impl<S: RequestedScope> RelyingParty<S> {
    /// Construct a fully-composed RP.
    ///
    /// At boot:
    /// 1. Fetch the OIDC discovery document from
    ///    `<config.issuer>/.well-known/openid-configuration`.
    /// 2. Fetch the JWKS from the discovery's `jwks_uri` and seed
    ///    [`PasIdTokenVerifier<S>`] with it.
    /// 3. Construct the internal `oauth::AuthClient` using the
    ///    discovery's `authorization_endpoint` + `token_endpoint`.
    /// 4. Store all components for the lifetime of the RP.
    ///
    /// # Errors
    ///
    /// - [`RelyingPartyInitError::Discovery`] — discovery fetch failed
    /// - [`RelyingPartyInitError::Jwks`] — JWKS fetch failed
    /// - [`RelyingPartyInitError::OAuthClient`] — reqwest client build failed
    pub async fn new(
        config: Config,
        state_store: Arc<dyn StateStore>,
    ) -> Result<Self, RelyingPartyInitError> {
        let discovery = fetch_discovery(&config.issuer).await?;

        let expectations = VerifyConfig::new(
            // Trim the trailing slash the `url` crate re-adds to a host-only
            // `Url`: PAS signs id_tokens — like access tokens — under the
            // BARE issuer. id_token verify does not compare `iss` YET (Phase
            // 10.5-10.7), so this trim is a no-op today; doing it now keeps
            // the OAuth callback correct the moment iss-enforcement lands.
            // Mirror of the access-token perimeter trim in
            // `access_token_verifier` — same issuer, same reason.
            discovery.issuer.as_str().trim_end_matches('/'),
            config.client_id.clone(),
        );
        let verifier_concrete: PasIdTokenVerifier<S> =
            PasIdTokenVerifier::from_jwks_url(discovery.jwks_uri.to_string(), expectations)
                .await
                .map_err(RelyingPartyInitError::Jwks)?;
        let verifier: Arc<dyn IdTokenVerifier<S>> = Arc::new(verifier_concrete);

        let oauth_config = OAuthConfig::new(
            config.client_id.clone(),
            config.redirect_uri.clone(),
        )
        .with_auth_url(discovery.authorization_endpoint.clone())
        .with_token_url(discovery.token_endpoint.clone());
        let auth_client = AuthClient::try_new(oauth_config)
            .map_err(|e| RelyingPartyInitError::OAuthClient(e.to_string()))?;

        Ok(Self {
            config,
            state_store,
            auth_client,
            verifier,
            discovery,
            clock: Arc::new(WallClock),
        })
    }

    /// Begin an authorization flow.
    ///
    /// Generates fresh state + nonce + PKCE, persists the
    /// [`PendingAuthRequest`] under the state key (TTL =
    /// `config.state_ttl`), and returns the authorize URL the
    /// consumer should redirect the browser to.
    ///
    /// `after_login` is the post-login redirect target; the
    /// [`RelativePath`] newtype prevents open-redirect (RFC 9700
    /// §4.1.5) at the SDK boundary.
    ///
    /// # Errors
    ///
    /// - [`StartError::StateStore`] — substrate failure during `put`
    /// - [`StartError::UrlBuild`] — authorize URL serialization failed
    pub async fn start(
        &self,
        after_login: RelativePath,
    ) -> Result<AuthorizationRedirect, StartError> {
        let state_str = pkce::generate_state();
        let code_verifier = pkce::generate_code_verifier();
        let code_challenge = pkce::generate_code_challenge(&code_verifier);
        let nonce = pkce::generate_state();

        let state = State::from_string(state_str.clone());
        let pending = PendingAuthRequest {
            code_verifier: code_verifier.clone(),
            nonce: nonce.clone(),
            after_login,
            created_at: self.clock.now(),
        };
        self.state_store
            .put(&state, pending, self.config.state_ttl)
            .await?;

        let url = build_authorize_url(
            &self.discovery.authorization_endpoint,
            &self.config.client_id,
            &self.config.redirect_uri,
            &state_str,
            &code_challenge,
            S::SCOPE,
            &nonce,
        );

        Ok(AuthorizationRedirect { url, state })
    }

    /// Complete a callback.
    ///
    /// Atomically `take`s the pending state, exchanges the
    /// authorization `code` for tokens, verifies the returned
    /// id_token against the stored nonce, and returns the verified
    /// [`Completion<S>`].
    ///
    /// # Errors
    ///
    /// - [`CallbackError::StateNotFoundOrConsumed`] — CSRF defense
    ///   (state absent or already consumed)
    /// - [`CallbackError::StateStore`] — substrate failure during
    ///   `take`
    /// - [`CallbackError::TokenExchange`] — PAS token endpoint
    ///   rejected
    /// - [`CallbackError::IdToken`] — id_token verification failed
    pub async fn complete(
        &self,
        params: CallbackParams,
    ) -> Result<Completion<S>, CallbackError> {
        // 1. Atomic state-take — single-use enforcement
        let pending = self
            .state_store
            .take(&params.state)
            .await?
            .ok_or(CallbackError::StateNotFoundOrConsumed)?;

        // 2. Token exchange
        let tokens = self
            .auth_client
            .exchange_code(&params.code, &pending.code_verifier)
            .await
            .map_err(|e| CallbackError::TokenExchange(e.to_string()))?;

        // 3. Read id_token from response (OIDC Core §3.1.3.3 — required
        //    when scope includes `openid`, which RequestedScope's
        //    `SCOPE` const always does)
        let id_token = tokens.id_token.as_deref().ok_or_else(|| {
            CallbackError::TokenExchange("token response missing id_token".to_owned())
        })?;

        // 4. Verify id_token against the stored nonce
        let nonce = Nonce::new(pending.nonce.as_str())
            .map_err(|_| CallbackError::IdToken(IdVerifyError::NonceMismatch))?;
        let id_assertion = self.verifier.verify(id_token, &nonce).await?;

        Ok(Completion {
            id_assertion,
            tokens,
            redirect_to: pending.after_login,
        })
    }

    /// Build the request-perimeter **access-token** verifier from this
    /// RP's own OIDC discovery metadata.
    ///
    /// The login flow ([`Self::complete`]) verifies *id_tokens*; a
    /// consumer's request perimeter must separately verify the *access
    /// tokens* PAS issues (RFC 9068). Both are signed by the same PAS
    /// keyset under the same issuer — values this RP already fetched at
    /// [`Self::new`]. This factory returns a ready [`JwtVerifier`] wired
    /// to the discovered `jwks_uri` and `issuer`, so the consumer never
    /// re-derives them.
    ///
    /// **Why it exists.** Before this method every integrator
    /// hand-assembled the perimeter verifier — string-formatting the
    /// JWKS URL and re-normalizing the issuer. [`Discovery::issuer`] is a
    /// [`Url`] whose `as_str()` carries the trailing slash the `url`
    /// crate normalizes onto a host-only URL, while PAS emits the *bare*
    /// issuer in the access-token `iss` claim and the engine matches it
    /// exactly. Getting that trim wrong produced a successful OAuth
    /// callback followed by a silent 401 on every subsequent request
    /// (RCW + CTW, 2026-05-27). Trimming **once here, in the SDK**, makes
    /// that bug class unrepresentable at the call site.
    ///
    /// The returned verifier owns a fresh, independent JWKS cache (the
    /// current per-verifier `ε1` invariant — see
    /// [`PasIdTokenVerifier`](super::verifier::PasIdTokenVerifier)); the
    /// `async fn` signature lets a future shared-cache optimization (ε2)
    /// swap the internals without a breaking change. Chain
    /// [`JwtVerifier::with_session_liveness`] /
    /// [`with_epoch_revocation`](JwtVerifier::with_epoch_revocation) /
    /// [`with_audit`](JwtVerifier::with_audit) on the result before
    /// storing it behind `Arc<dyn BearerVerifier>`.
    ///
    /// # Errors
    ///
    /// Returns [`crate::TokenVerifyError::KeysetUnavailable`] if the JWKS
    /// fetch fails — the perimeter cannot verify without at least one
    /// usable key snapshot.
    pub async fn access_token_verifier(&self) -> Result<JwtVerifier, crate::TokenVerifyError> {
        JwtVerifier::from_jwks_url(
            self.discovery.jwks_uri.to_string(),
            VerifyConfig::new(
                // `discovery.issuer` is a `Url`; `.as_str()` re-adds the
                // trailing slash the `url` crate normalizes onto a
                // host-only URL. PAS emits the bare issuer on the
                // access-token `iss` claim and the engine matches it
                // exactly (M23) — so trim here, once, so no consumer has to.
                //
                // SYMMETRY (both configs trim): the sibling id_token
                // `VerifyConfig` in `new` above trims identically, and that
                // is deliberate — PAS signs both token classes' `iss` from
                // the same bare-issuer config (`ppoppo-token`'s
                // `engine::encode`); the matching no-trailing-slash invariant
                // on the published metadata is pinned in
                // `accounts-api::rest::well_known::discovery`. Access-token
                // verify does an exact `iss` match; id_token verify does not
                // check `iss` YET (Phase 10.5-10.7), so a slash-carrying
                // id_token config "works" today only by omission. Trimming
                // both keeps this perimeter correct now AND keeps the OAuth
                // callback correct the moment id_token iss-enforcement lands
                // — at which point a `…/` id_token config would reject every
                // real callback.
                self.discovery.issuer.as_str().trim_end_matches('/'),
                self.config.client_id.clone(),
            ),
        )
        .await
    }

    /// Refresh an existing PAS session.
    ///
    /// Exchanges a `refresh_token` for a fresh
    /// [`oauth::TokenResponse`] (new access_token, possibly rotated
    /// refresh_token, possibly new id_token). The consumer typically
    /// invokes this from a dedicated refresh endpoint — it is the
    /// SSOT for the OAuth refresh-grant exchange so that
    /// `chat-auth::rp` (and equivalent consumers) never reach into
    /// `oauth::AuthClient` directly.
    ///
    /// **No id_token verification is performed here.** Refresh
    /// responses MAY include an id_token (OIDC Core §12), but the
    /// consumer treats refresh as a session-extension mechanism, not a
    /// re-authentication event — the original
    /// [`Self::complete`] verified the user identity, and the cookie
    /// flow that calls this method already trusts the refresh_token
    /// it just decrypted.
    ///
    /// # Errors
    ///
    /// - [`RefreshError::Rejected`] — PAS returned 4xx; refresh_token
    ///   is dead, clear session and force re-auth.
    /// - [`RefreshError::Transient`] — 5xx or transport failure;
    ///   session may still be live, fail-soft.
    pub async fn refresh(
        &self,
        refresh_token: &str,
    ) -> Result<RefreshOutcome, RefreshError> {
        use crate::pas_port::{PasAuthPort, PasFailure};
        match self.auth_client.refresh(refresh_token).await {
            Ok(t) => Ok(RefreshOutcome::from(t)),
            Err(PasFailure::Rejected { detail, .. }) => Err(RefreshError::Rejected(detail)),
            Err(PasFailure::ServerError { detail, .. })
            | Err(PasFailure::Transport { detail }) => Err(RefreshError::Transient(detail)),
        }
    }

    /// Build the OIDC RP-Initiated Logout URL — the OP `end_session_endpoint`
    /// (OIDC RP-Initiated Logout 1.0 §2). The browser navigates here to end
    /// its OP session: pass the `id_token` the RP holds as the hint, the
    /// pre-registered `post_logout_redirect_uri` to land on afterward, and an
    /// optional opaque `state`. Pure — no IO; the caller performs the redirect.
    ///
    /// # Errors
    /// - [`EndSessionError::NotSupported`] — the OP's discovery document
    ///   advertises no `end_session_endpoint`.
    pub fn end_session_url(
        &self,
        id_token: &str,
        post_logout_redirect_uri: &Url,
        state: Option<&str>,
    ) -> Result<Url, EndSessionError> {
        let endpoint = self
            .discovery
            .end_session_endpoint
            .as_ref()
            .ok_or(EndSessionError::NotSupported)?;
        Ok(build_end_session_url(
            endpoint,
            id_token,
            post_logout_redirect_uri,
            &self.config.client_id,
            state,
        ))
    }
}

/// Error from [`RelyingParty::end_session_url`].
#[derive(Debug, thiserror::Error)]
pub enum EndSessionError {
    /// The OP's discovery document advertises no `end_session_endpoint`, so
    /// RP-Initiated Logout is unavailable against this provider.
    #[error("the OP advertises no end_session_endpoint (RP-Initiated Logout unsupported)")]
    NotSupported,
}

// ────────────────────────────────────────────────────────────────────────
// URL builder — extracted for boundary-test introspection
// ────────────────────────────────────────────────────────────────────────

/// Build the OIDC authorize URL.
///
/// Pulled out as a free function so the URL-shape boundary test can
/// re-derive the expected URL deterministically (given fixed inputs)
/// without round-tripping through `RelyingParty::start` (which
/// generates fresh randomness each call).
///
/// Order of query params is stable to ease test assertions, but
/// PAS does not depend on order (RFC 6749 §3.1).
fn build_authorize_url(
    authorization_endpoint: &Url,
    client_id: &str,
    redirect_uri: &Url,
    state: &str,
    code_challenge: &str,
    scope: &str,
    nonce: &str,
) -> Url {
    let mut url = authorization_endpoint.clone();
    url.query_pairs_mut()
        .append_pair("response_type", "code")
        .append_pair("client_id", client_id)
        .append_pair("redirect_uri", redirect_uri.as_str())
        .append_pair("state", state)
        .append_pair("code_challenge", code_challenge)
        .append_pair("code_challenge_method", "S256")
        .append_pair("scope", scope)
        .append_pair("nonce", nonce);
    url
}

/// Build the OIDC RP-Initiated Logout URL. Free function for the same
/// boundary-test reason as [`build_authorize_url`]. `query_pairs_mut`
/// percent-encodes every value (so `state` / the redirect URI are safe).
fn build_end_session_url(
    end_session_endpoint: &Url,
    id_token_hint: &str,
    post_logout_redirect_uri: &Url,
    client_id: &str,
    state: Option<&str>,
) -> Url {
    let mut url = end_session_endpoint.clone();
    {
        let mut pairs = url.query_pairs_mut();
        pairs
            .append_pair("id_token_hint", id_token_hint)
            .append_pair("post_logout_redirect_uri", post_logout_redirect_uri.as_str())
            .append_pair("client_id", client_id);
        if let Some(s) = state {
            pairs.append_pair("state", s);
        }
    }
    url
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    use super::*;

    #[test]
    fn end_session_url_carries_hint_redirect_client_and_state() {
        let endpoint: Url = "https://accounts.ppoppo.com/oauth/logout".parse().unwrap();
        let redirect: Url = "https://ppoppo.com/".parse().unwrap();
        let url = build_end_session_url(&endpoint, "the.id.token", &redirect, "cwc", Some("a b"));
        let s = url.as_str();
        assert!(s.starts_with("https://accounts.ppoppo.com/oauth/logout?"), "{s}");
        assert!(s.contains("id_token_hint=the.id.token"), "{s}");
        // post_logout_redirect_uri is percent-encoded by query_pairs_mut.
        assert!(s.contains("post_logout_redirect_uri=https%3A%2F%2Fppoppo.com%2F"), "{s}");
        assert!(s.contains("client_id=cwc"), "{s}");
        // form-encoding: space → `+`.
        assert!(s.contains("state=a+b"), "{s}");
    }

    #[test]
    fn end_session_url_omits_state_when_absent() {
        let endpoint: Url = "https://accounts.ppoppo.com/oauth/logout".parse().unwrap();
        let redirect: Url = "https://ppoppo.com/".parse().unwrap();
        let url = build_end_session_url(&endpoint, "tok", &redirect, "cwc", None);
        assert!(!url.as_str().contains("state="), "{}", url.as_str());
    }
}