diff --git a/crates/auth-sdk/src/axum/tests/backchannel.rs b/crates/auth-sdk/src/axum/tests/backchannel.rs index de988d507..16a2cb750 100644 --- a/crates/auth-sdk/src/axum/tests/backchannel.rs +++ b/crates/auth-sdk/src/axum/tests/backchannel.rs @@ -57,7 +57,7 @@ struct Down; /// 500 when it fails, so auth retries; an invalid token never reaches it. #[tokio::test] async fn backchannel_logout_hands_tokens_to_the_handler() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let issuer = endpoint.issuer(&clock()); let response = post_logout( &login_app(&issuer), @@ -126,7 +126,7 @@ async fn backchannel_logout_hands_tokens_to_the_handler() { /// refused as invalid without reaching the handler, even around a valid token. #[tokio::test] async fn backchannel_logout_refuses_large_requests() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let handled = Arc::new(Mutex::new(0)); let app = login_app_with(&endpoint.issuer(&clock()), |routes| { let handled = handled.clone(); diff --git a/crates/auth-sdk/src/axum/tests/login.rs b/crates/auth-sdk/src/axum/tests/login.rs index 72411cf5d..d4d8f00a3 100644 --- a/crates/auth-sdk/src/axum/tests/login.rs +++ b/crates/auth-sdk/src/axum/tests/login.rs @@ -21,7 +21,7 @@ fn return_to_vectors() { /// not redeem it: the login routes take confidential clients only. #[tokio::test] async fn login_routes_refuse_public_clients() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let public = endpoint.issuer(&clock()).client(ClientConfig { client_id: CLIENT.parse().expect("client ID"), credentials: Credentials::None, @@ -39,7 +39,7 @@ async fn login_routes_refuse_public_clients() { /// redirect to auth would fail its CORS check. #[tokio::test] async fn signed_in_redirects_only_navigations() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let app = login_app(&endpoint.issuer(&clock())); let response = get_with(&app, "/app/notes?page=2", &[]).await; assert_eq!( @@ -61,7 +61,7 @@ async fn signed_in_redirects_only_navigations() { /// the 401 instead of a redirect. #[tokio::test] async fn step_ups_in_nested_routers_keep_their_prefix() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let issuer = endpoint.issuer(&clock()); let key = CookieKey::generate().expect("cookie key"); let auth = LoginRoutes::new(client(&issuer), key).expect("a confidential client"); @@ -101,7 +101,7 @@ async fn step_ups_in_nested_routers_keep_their_prefix() { /// start at once, a browser holds at most three cookies, one per slot. #[tokio::test] async fn logins_in_several_tabs_complete() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let app = login_app(&endpoint.issuer(&clock())); // Start logins until every slot is used: random `state`s land in all three // within 200 but for a chance of about 1 in 10^35. @@ -154,7 +154,7 @@ async fn logins_in_several_tabs_complete() { /// it returns home instead, and to the root when the home path is too long as well. #[tokio::test] async fn pending_cookies_fit_their_slots() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let issuer = endpoint.issuer(&clock()); // Quotes double in JSON, so 2048 of them would seal to some 6 KB. let quotes = format!("%2F{}", "%22".repeat(2047)); @@ -218,7 +218,7 @@ async fn send_from( /// Logout is accepted only from the app's own pages. #[tokio::test] async fn logout_refuses_other_origins() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let app = login_app(&endpoint.issuer(&clock())); let session = sign_in(&app, USER).await; for (fetch_site, origin, own) in ORIGINS { @@ -242,7 +242,7 @@ async fn logout_refuses_other_origins() { /// [`CrossOrigin`] skip the check. #[tokio::test] async fn signed_in_refuses_unsafe_requests_from_other_origins() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let app = login_app(&endpoint.issuer(&clock())); let cookie = sign_in(&app, USER).await; let signed_in = @@ -286,7 +286,7 @@ const OTHER: &str = "01936f3e-8c00-7000-8000-000000000009"; /// accounts. #[tokio::test] async fn step_ups_stay_on_the_signed_in_account() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let app = login_app(&endpoint.issuer(&clock())); let session = sign_in(&app, USER).await; let step_up = "%2F&step_up=true"; @@ -350,7 +350,7 @@ async fn step_ups_stay_on_the_signed_in_account() { /// names; another user's grant is revoked as it is refused. #[tokio::test] async fn hinted_logins_complete_only_for_the_hinted_user() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let client = client(&endpoint.issuer(&clock())); let claims = serde_json::from_value(id_claims(USER)).expect("ID token claims"); let hint = crate::IdToken::unchecked(claims, "id-0"); diff --git a/crates/auth-sdk/src/axum/tests/mod.rs b/crates/auth-sdk/src/axum/tests/mod.rs index 52b6d376b..21292e280 100644 --- a/crates/auth-sdk/src/axum/tests/mod.rs +++ b/crates/auth-sdk/src/axum/tests/mod.rs @@ -27,12 +27,9 @@ use ::axum::{ use chrono::Utc; use serde_json::json; use sourceorigin_common::protocol::jwt::TokenType; -use std::{ - sync::{ - Arc, Mutex, - atomic::{AtomicBool, AtomicUsize, Ordering}, - }, - time::Duration, +use std::sync::{ + Arc, Mutex, + atomic::{AtomicBool, AtomicUsize, Ordering}, }; use tower::ServiceExt; use url::Url; @@ -122,7 +119,7 @@ struct TokenEndpoint { revocations_fail: Arc, /// Tokens whose revocation fails, while others' succeeds. refused: Arc>>, - /// Whether revocations hang for an hour before answering, as a stuck auth would. + /// Whether revocations never answer, as a stuck auth would. revocations_hang: Arc, } @@ -130,11 +127,10 @@ struct TokenEndpoint { type Form = std::collections::HashMap; impl TokenEndpoint { - /// Serves endpoints whose `n`th refresh answers, after `delay`, `access-n` and no - /// refresh token, as auth answers a confidential client. - async fn start(delay: Duration) -> Self { + /// Serves endpoints whose `n`th refresh answers `access-n` and no refresh + /// token, as auth answers a confidential client. + async fn start() -> Self { Self::refreshing(move |call| async move { - tokio::time::sleep(delay).await; ::axum::Json(Self::tokens(&format!("access-{call}"))).into_response() }) .await @@ -199,7 +195,7 @@ impl TokenEndpoint { let revoke = move |::axum::Form(form): ::axum::Form
| async move { received.fetch_add(1, Ordering::SeqCst); if hanging.load(Ordering::SeqCst) { - tokio::time::sleep(Duration::from_secs(3600)).await; + std::future::pending::<()>().await; } let token = &form["token"]; if failing.load(Ordering::SeqCst) || refusing.lock().expect("refused").contains(token) { diff --git a/crates/auth-sdk/src/axum/tests/sessions.rs b/crates/auth-sdk/src/axum/tests/sessions.rs index f9540a656..8c8fe7185 100644 --- a/crates/auth-sdk/src/axum/tests/sessions.rs +++ b/crates/auth-sdk/src/axum/tests/sessions.rs @@ -13,7 +13,7 @@ use chrono::TimeDelta; /// the shape they write, or none. #[tokio::test] async fn session_cookie_vectors() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; for case in Case::all("sessions") { let client = endpoint.issuer(&case.clock()).client(ClientConfig { client_id: case.text("client_id").parse().expect("client ID"), @@ -96,7 +96,21 @@ async fn home(app: &Router, session: &str) -> (StatusCode, String, Option= 2) + .await + .expect("the endpoint holds a sender"); + ::axum::Json(TokenEndpoint::tokens(&format!("access-{call}"))).into_response() + } + }) + .await; let clock = clock(); let app = login_app(&endpoint.issuer(&clock)); let session = sign_in(&app, USER).await; @@ -164,7 +178,7 @@ async fn refresh_failures_end_the_session_only_when_auth_refuses() { /// response clears it. #[tokio::test] async fn ended_sessions_are_cleared() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let clock = clock(); let app = login_app(&endpoint.issuer(&clock)); let session = sign_in(&app, USER).await; @@ -186,7 +200,7 @@ async fn ended_sessions_are_cleared() { /// cookie stays, for the user to try again. #[tokio::test] async fn logout_revokes_the_refresh_token() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let app = login_app(&endpoint.issuer(&clock())); let session = sign_in(&app, USER).await; let logout = || async { @@ -232,7 +246,7 @@ async fn logout_revokes_the_refresh_token() { /// sign in again. #[tokio::test] async fn sign_ins_revoke_the_session_they_replace() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let clock = clock(); let app = login_app(&endpoint.issuer(&clock)); let first = sign_in(&app, USER).await; @@ -329,7 +343,7 @@ fn worst_case_tokens(access_bytes: usize) -> Tokens { /// would drop. #[tokio::test] async fn sessions_fit_in_a_cookie() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; let routes = LoginRoutes::new( client(&endpoint.issuer(&clock())), CookieKey::generate().expect("cookie key"), @@ -368,14 +382,14 @@ fn sessions_without_a_refresh_token_end_with_their_access_token() { /// replacing the session answers 502 too, so neither hangs. #[tokio::test] async fn revocations_give_up_after_their_deadline() { - let endpoint = TokenEndpoint::start(Duration::ZERO).await; + let endpoint = TokenEndpoint::start().await; // An injected client without a timeout of its own. let http = Http::plain(reqwest::Client::builder()); let app = login_app(&endpoint.issuer_with(&clock(), http)); let session = sign_in(&app, USER).await; endpoint.revocations_hang.store(true, Ordering::SeqCst); - // Waiting out the hour the revocation hangs takes no time, but the deadline - // must end it first. + // The revocation never answers: the SDK's deadline ends it, which paused + // time reaches at once. tokio::time::pause(); let logout = Request::post("/auth/logout") .header(header::COOKIE, &session) diff --git a/crates/auth-sdk/src/tests.rs b/crates/auth-sdk/src/tests.rs index af054b752..f48eea07b 100644 --- a/crates/auth-sdk/src/tests.rs +++ b/crates/auth-sdk/src/tests.rs @@ -800,9 +800,11 @@ async fn cancelled_fetches_leave_keys_unavailable() { Arc::new(ManualClock::at(1_767_225_600)), ); let token = vector_token("access-valid"); - let cancelled = - tokio::time::timeout(std::time::Duration::from_millis(100), verify(&keys, &token)).await; - assert!(cancelled.is_err(), "the fetch never completes"); + // Cancelled once the server accepts its connection. + tokio::select! { + verified = verify(&keys, &token) => panic!("the fetch never completes: {verified:?}"), + accepted = silent.accept() => drop(accepted.expect("the fetch connects")), + } assert!(matches!( verify(&keys, &token).await, Err(TokenError::Keys(KeyError::Unavailable))