diff --git a/crates/ghostlight-dungeon/src/heimdall.rs b/crates/ghostlight-dungeon/src/heimdall.rs index 8816536..671cbe6 100644 --- a/crates/ghostlight-dungeon/src/heimdall.rs +++ b/crates/ghostlight-dungeon/src/heimdall.rs @@ -644,12 +644,7 @@ impl HeimdallClient { else { bail!("Heimdall returned a non-operation response"); }; - if status == "denied" { - return Err(HeimdallDenied { - diagnostics: diagnostics.join("; "), - } - .into()); - } + refuse_denied(&status, &diagnostics)?; if payload_schema != PRIVATE_ENVELOPE_SCHEMA || payload_encoding != "messagepack-base64" { bail!("Heimdall returned the wrong private response contract"); } @@ -659,6 +654,18 @@ impl HeimdallClient { } } +/// A response with status denied is Heimdall's answer, not a transport +/// failure: it becomes the typed `HeimdallDenied`. +fn refuse_denied(status: &str, diagnostics: &[String]) -> anyhow::Result<()> { + if status == "denied" { + return Err(HeimdallDenied { + diagnostics: diagnostics.join("; "), + } + .into()); + } + Ok(()) +} + fn validate_private_response_binding( envelope: &PrivateEnvelope, operation: &str, @@ -1104,6 +1111,15 @@ mod tests { } } + #[test] + fn a_denied_status_is_the_typed_denial_and_any_other_status_passes() { + let diagnostics = vec!["Idempotency key was reused".to_owned()]; + let denial = refuse_denied("denied", &diagnostics).unwrap_err(); + assert!(denial.downcast_ref::().is_some()); + assert!(denial.to_string().contains("Idempotency key was reused")); + refuse_denied("accepted", &diagnostics).unwrap(); + } + #[test] fn private_responses_are_bound_to_the_exact_app_and_request() { let secret = "fixture-shared-secret"; diff --git a/crates/ghostlight-dungeon/src/runtime.rs b/crates/ghostlight-dungeon/src/runtime.rs index 6a2b84c..6de4527 100644 --- a/crates/ghostlight-dungeon/src/runtime.rs +++ b/crates/ghostlight-dungeon/src/runtime.rs @@ -6403,6 +6403,10 @@ mod refresh_loop_tests { Deny, Timeout, Receipt { revision: u64, claim: &'static str }, + /// A receipt for the same claim but another Heimdall session. + OtherSession, + /// A receipt for the same claim but another account. + OtherAccount, } #[derive(Default)] @@ -6442,10 +6446,18 @@ mod refresh_loop_tests { } .into()), Reply::Timeout => bail!("Heimdall private command timed out"), - Reply::Receipt { revision, claim } => { + reply @ (Reply::Receipt { .. } | Reply::OtherSession | Reply::OtherAccount) => { + let (account, session, revision, claim) = match reply { + Reply::OtherSession => ("operator-account", "other-session", 1, "claim-1"), + Reply::OtherAccount => ("other-account", "heimdall-session", 1, "claim-1"), + Reply::Receipt { revision, claim } => { + ("operator-account", "heimdall-session", revision, claim) + } + _ => unreachable!("matched above"), + }; *self.pending.lock().unwrap() = Some(VerifiedSessionRefresh::fixture( - "operator-account", - "heimdall-session", + account, + session, revision, Utc::now() + chrono::Duration::seconds(90), Utc::now() + chrono::Duration::days(1), @@ -6579,6 +6591,16 @@ mod refresh_loop_tests { assert_eq!(heimdall.calls().len(), 1, "a revoked session is not offered again"); } + #[tokio::test] + async fn a_receipt_for_another_session_or_account_revokes_the_local_session() { + for reply in [Reply::OtherSession, Reply::OtherAccount] { + let (_directory, sessions, cookie) = due_session(1); + let heimdall = ScriptedHeimdall::answering(vec![reply]); + refresh_due_sessions(&sessions, &heimdall).await.unwrap(); + assert!(!is_live(&sessions, &cookie).await); + } + } + #[tokio::test] async fn a_heimdall_denial_revokes_the_session_and_it_is_not_offered_again() { let (_directory, sessions, cookie) = due_session(1);