diff --git a/README.md b/README.md index 9f9dc0d..2405643 100644 --- a/README.md +++ b/README.md @@ -234,6 +234,25 @@ cookie cannot be read. Any other failure keeps the cookies, so the next request | gets a 2xx other than 200, a 200 with no session, or a session that fails verification | 502 `upstream_unavailable` | | does not settle within 30 seconds, or its task panics | 502 `upstream_unavailable` | +#### Running several instances + +The sharing above lives in one process. Several adapter instances (replicas, or a rolling deploy) +share nothing with each other, so two of them can present the same refresh token to the auth API at +once. The auth API covers this with a short replay window: for 5 seconds after a refresh token +rotates (`REFRESH_REPLAY_WINDOW_SECONDS` on the API, 0 disables it, capped at 60), up to 3 further +presentations of that token each get a 200 with the same successor refresh token, session id and +claims, and an access token of their own for the same session. Every instance then sets the same +refresh cookie, so no instance loses the race and no user is logged out. + +No shared lock or cache between instances is needed while the spread between the requests and any +clock skew between the hosts stay well under the window. A refresh token presented after the window, +past the replay count, or after it has been rotated twice is `refresh_token_reused`: the API revokes +the chain and answers 401, and the adapter clears the session cookies and never retries it. + +This needs the auth API release that ships the replay window (seamless-auth-api#613). Against an +earlier API, the instance that loses the rotation race gets `refresh_token_reused` and its user is +logged out. + ### Errors and logout An error from the auth API goes back with its status, its JSON body and its `Retry-After` header, diff --git a/src/tests/refresh.rs b/src/tests/refresh.rs index 1ff3438..93c68ba 100644 --- a/src/tests/refresh.rs +++ b/src/tests/refresh.rs @@ -414,3 +414,103 @@ async fn a_refresh_whose_task_panics_keeps_the_session() { assert_unavailable("silent", &silent_refresh(&broken_adapter(true)).await); assert_unavailable("cookie", &cookie_refresh(&broken_adapter(true)).await); } + +/// The auth API's replay window: every presentation of `r1` inside it answers +/// 200 with one successor, and a fresh access token carrying the same session. +fn replay_window(api: &FakeApi, delay: Duration) -> Arc { + let calls = Arc::new(AtomicUsize::new(0)); + let counter = calls.clone(); + let url = api.url.clone(); + api.on( + "POST", + "/refresh", + Arc::new(move |_| { + if !delay.is_zero() { + std::thread::sleep(delay); + } + let n = counter.fetch_add(1, Ordering::SeqCst); + json_reply( + 200, + json!({ + "message": "Success", + "sub": "u1", + "token": sign_rs256(json!({ + "sub": "u1", "sid": "s-1", "typ": "access", + "iss": url, "aud": url, "jti": format!("call-{n}"), + })), + "refreshToken": "r2", + "sessionId": "s-1", + "refreshTtl": 3600, + "ttl": 900, + "roles": ["user"], + "organizationId": "org-1", + }), + ) + }), + ); + calls +} + +#[tokio::test] +async fn two_adapter_instances_refreshing_one_token_both_succeed_with_the_same_successor() { + let api = FakeApi::start().await; + let calls = replay_window(&api, Duration::from_millis(100)); + let (one, two) = (api.adapter(|b| b), api.adapter(|b| b)); + + let (a, b) = tokio::join!(cookie_refresh(&one), cookie_refresh(&two)); + + assert_eq!(a.status, 200, "{}", a.text); + assert_eq!(b.status, 200, "{}", b.text); + assert_eq!( + calls.load(Ordering::SeqCst), + 2, + "separate instances share nothing, so each reaches the API" + ); + let refresh_a = verify_hs256(&a.cookie_value("seamless-refresh"), TEST_SECRET).unwrap(); + let refresh_b = verify_hs256(&b.cookie_value("seamless-refresh"), TEST_SECRET).unwrap(); + assert_eq!(str_claim(&refresh_a, "refreshToken"), "r2"); + assert_eq!(str_claim(&refresh_b, "refreshToken"), "r2"); + for claim in ["sub", "sessionId", "refreshToken"] { + assert_eq!( + str_claim(&refresh_a, claim), + str_claim(&refresh_b, claim), + "{claim}" + ); + } + let access_a = verify_hs256(&a.cookie_value("seamless-access"), TEST_SECRET).unwrap(); + let access_b = verify_hs256(&b.cookie_value("seamless-access"), TEST_SECRET).unwrap(); + assert_eq!(str_claim(&access_a, "sessionId"), "s-1"); + assert_eq!(str_claim(&access_b, "sessionId"), "s-1"); + assert_ne!( + str_claim(&access_a, "token"), + str_claim(&access_b, "token"), + "the API mints an access token per call" + ); + assert_eq!(a.body, b.body); +} + +#[tokio::test] +async fn refresh_token_reused_is_terminal_and_never_retried() { + let api = FakeApi::start().await; + let calls = Arc::new(AtomicUsize::new(0)); + let counter = calls.clone(); + api.on( + "POST", + "/refresh", + Arc::new(move |_| { + counter.fetch_add(1, Ordering::SeqCst); + json_reply(401, json!({ "error": "refresh_token_reused" })) + }), + ); + let adapter = api.adapter(|b| b); + + let refreshed = cookie_refresh(&adapter).await; + assert_eq!(refreshed.status, 401, "{}", refreshed.text); + assert!(refreshed.cleared("seamless-access") && refreshed.cleared("seamless-refresh")); + assert_eq!(calls.load(Ordering::SeqCst), 1, "the refresh was retried"); + + let silent = silent_refresh(&api.adapter(|b| b)).await; + assert_eq!(silent.status, 401, "{}", silent.text); + assert!(silent.cleared("seamless-access") && silent.cleared("seamless-refresh")); + assert_eq!(calls.load(Ordering::SeqCst), 2, "the refresh was retried"); +}