diff --git a/docs/design.md b/docs/design.md index 1e03231..8570dba 100644 --- a/docs/design.md +++ b/docs/design.md @@ -173,7 +173,9 @@ tested with, and the [demo](demo.md) runs it. - **Signing in.** The broker is an OIDC client (`web.clientId`, the `openidconnect` crate, with PKCE), checking the ID token itself rather than trusting a header from the proxy. Only members of `web.group` may sign in: the provider should restrict the client to that group, and the broker - checks the token's `groups` claim too. + checks the token's `groups` claim too. Every sign-in asks the provider to authenticate afresh + (`max_age=0`) and checks the token's `auth_time`, so signing out of the app means the next + sign-in needs the passkey, even while the provider's own session lasts. - **Sessions.** A sign-in gives a session cookie (`tower-sessions`) that lasts seven days without use. Sessions are kept in an SQLite database in the broker's state directory, so a restart doesn't sign phones out. diff --git a/src/web/auth.rs b/src/web/auth.rs index 96f32a9..60075c6 100644 --- a/src/web/auth.rs +++ b/src/web/auth.rs @@ -1,6 +1,6 @@ //! Signing approvers in through an OpenID Connect provider, and confirming single answers by -//! sending the approver back to sign in again (`max_age=0`), which a passkey-only provider turns -//! into a passkey prompt. +//! sending the approver back to sign in again. Both ask the provider to authenticate afresh +//! (`max_age=0`), which a passkey-only provider turns into a passkey prompt. use std::time::{Duration, Instant}; use anyhow::{Context, Result, anyhow, ensure}; @@ -162,14 +162,12 @@ impl Oidc { let client = self.client().await?; let (challenge, verifier) = PkceCodeChallenge::new_random_sha256(); - let mut request = client + let request = client .authorize_url(CoreAuthenticationFlow::AuthorizationCode, CsrfToken::new_random, Nonce::new_random) .add_scope(Scope::new("profile".into())) .add_scope(Scope::new("groups".into())) - .set_pkce_challenge(challenge); - if matches!(purpose, Purpose::Confirm { .. }) { - request = request.set_max_age(Duration::ZERO); - } + .set_pkce_challenge(challenge) + .set_max_age(Duration::ZERO); let (url, state, nonce) = request.url(); let flow = Flow { @@ -206,15 +204,18 @@ impl Oidc { } } +/// Checks that the approver authenticated after the flow started, rather than the provider +/// reusing its own session. +pub fn fresh(flow: &Flow, identity: &Identity) -> Result<()> { + ensure!(identity.auth_time.is_some_and(|t| t >= flow.started), "the sign-in provider didn't ask for the passkey"); + Ok(()) +} + /// Checks that a confirmation came from the signed-in approver, who authenticated after it /// started. pub fn confirmed(flow: &Flow, identity: &Identity, subject: &str) -> Result<()> { ensure!(identity.subject == subject, "the passkey belongs to a different account"); - ensure!( - identity.auth_time.is_some_and(|t| t >= flow.started), - "the sign-in provider didn't ask for the passkey again" - ); - Ok(()) + fresh(flow, identity) } #[cfg(test)] @@ -243,4 +244,11 @@ mod tests { assert!(confirmed(&flow(100), &identity("a", None), "a").is_err()); assert!(confirmed(&flow(100), &identity("b", Some(130)), "a").is_err()); } + + #[test] + fn sign_in_needs_a_fresh_authentication() { + assert!(fresh(&flow(100), &identity("a", Some(100))).is_ok()); + assert!(fresh(&flow(100), &identity("a", Some(99))).is_err()); + assert!(fresh(&flow(100), &identity("a", None)).is_err()); + } } diff --git a/src/web/mod.rs b/src/web/mod.rs index 40cf4eb..4215ce2 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -440,12 +440,14 @@ async fn callback( let identity = web.oidc.finish(&flow, code).await.map_err(|e| Failure(StatusCode::FORBIDDEN, format!("{e:#}")))?; match flow.purpose { - Purpose::SignIn { next } => { + Purpose::SignIn { ref next } => { + auth::fresh(&flow, &identity).map_err(|e| Failure(StatusCode::FORBIDDEN, format!("{e:#}")))?; + // A new session ID, so one planted before signing in is useless. session.cycle_id().await?; let csrf = hex::encode(rand::random::<[u8; 32]>()); session.insert(USER, User { subject: identity.subject, name: identity.name, csrf }).await?; - Ok(Redirect::to(&next).into_response()) + Ok(Redirect::to(next).into_response()) } Purpose::Confirm { ref id, decision, ref shown } => { let user = api_user(&session).await?;