pas-external 0.12.0

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
//! 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::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(
            discovery.issuer.as_str(),
            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),
        })
    }

    #[must_use]
    pub fn with_clock(mut self, clock: ArcClock) -> Self {
        self.clock = clock;
        self
    }

    /// 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_utc(),
        };
        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,
        })
    }

    /// 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)),
        }
    }
}

// ────────────────────────────────────────────────────────────────────────
// 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
}