use super::{AppState, response::*}; use axum::http::{HeaderName, StatusCode}; use axum::response::Response; use jiff::Timestamp; use crate::clients::{EmailPolicy, RegisteredClient}; use crate::flows::{AuthorizationCode, BrowserSession, CODE_LIFETIME}; use crate::identity::Identity; use crate::oidc::{AuthorizationRequest, AuthorizationTarget, EmailClaim, Scope}; pub(super) struct Authenticated { pub(super) identity: Identity, pub(super) email: Option, pub(super) auth_time: Timestamp, } impl From for Authenticated { fn from(session: BrowserSession) -> Self { Self { identity: Identity { did: session.did, handle: session.handle, }, email: session.email, auth_time: session.auth_time, } } } pub(super) fn issue_code_for( state: &AppState, client: &RegisteredClient, target: AuthorizationTarget, authenticated: Authenticated, now: Timestamp, extra_headers: Vec<(HeaderName, String)>, status: StatusCode, ) -> Result { let Authenticated { identity, email, auth_time, } = authenticated; let refuse = |description: &str, extra_headers| { error_redirect( state, status, target.redirect_uri.clone(), target.client_state.as_deref(), "access_denied", description, extra_headers, ) }; if !client.permits(&identity) { tracing::info!( client = target.client_id.as_str(), did = identity.did.as_str(), handle = identity.handle.as_str(), "authorization refused: the account is not on the client's admission list" ); return Ok(refuse( "this account is not admitted to this application", extra_headers, )); } let policy = client.email_policy(); if policy == EmailPolicy::Required && email.is_none() { tracing::info!( client = target.client_id.as_str(), did = identity.did.as_str(), "authorization refused: the client requires an email address the PDS did not supply" ); return Ok(refuse( "this application requires an email address from your PDS", extra_headers, )); } let email = email.filter(|_| policy.discloses() && target.scopes.contains(&Scope::Email)); tracing::info!( client = target.client_id.as_str(), did = identity.did.as_str(), handle = identity.handle.as_str(), remembered = auth_time != now, email = email.is_some(), "authorization code issued" ); let code = state.flows.issue_code(AuthorizationCode { client_id: target.client_id, redirect_uri: target.redirect_uri.clone(), nonce: target.nonce, challenge: target.challenge, identity, auth_time, expires_at: now + CODE_LIFETIME, email, scopes: target.scopes, })?; Ok(redirect_to( state.config.issuer(), status, target.redirect_uri, target.client_state.as_deref(), &[("code", &code)], extra_headers, )) } pub(super) fn plaintext_form_action(request: &AuthorizationRequest) -> Option { if !request.client.insecure_redirects() { return None; } let target = &request.target.redirect_uri; match target.scheme() { "https" => None, "http" => Some(target.origin().ascii_serialization()), scheme => Some(format!("{scheme}:")), } }