Path: redb-identity/tests/redb.Identity.Tests/FullStack/RefreshTokenReuseDetectionTests.cs Snapshot: redb-identity, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb-identity Page: https://redbase.app/src/dev-2026-12-06/redb-identity/tests/redb.Identity.Tests/FullStack/RefreshTokenReuseDetectionTests.cs ---- using System.Net; using System.Text.Json; using AwesomeAssertions; using redb.Identity.Tests.Infrastructure; using Xunit; namespace redb.Identity.Tests.FullStack; /// /// C3 — Refresh-token rotation reuse detection: tests the production OpenIddict pipeline /// against the redb-backed token store. Verifies that once a refresh token has been /// rotated (A → B), presenting the original token (A) again is rejected, AND the entire /// token family (including the still-fresh B) is revoked so that a stolen rolling /// refresh token cannot be exchanged even once after the legitimate client has rotated. /// [Collection("ProductionHttp")] public sealed class RefreshTokenReuseDetectionTests { private readonly ProductionHttpFixture _fx; private readonly HttpClient _http; public RefreshTokenReuseDetectionTests(ProductionHttpFixture fx) { _fx = fx; _http = fx.Http; } [Fact] public async Task ReusedRefreshToken_RejectsAndRevokesFamily() { // 1. Issue initial refresh token A via auth code flow + offline_access. var (refreshA, _) = await IssueInitialTokensAsync(); // 2. Rotate A → B (legitimate rotation). var rotated1 = await RefreshAsync(refreshA); rotated1.StatusCode.Should().Be(HttpStatusCode.OK, "first rotation must succeed: {0}", await rotated1.Content.ReadAsStringAsync()); var refreshB = (await ParseJsonAsync(rotated1)).GetProperty("refresh_token").GetString()!; refreshB.Should().NotBe(refreshA, "rotation must produce a fresh refresh_token"); // 3. Reuse the now-redeemed A. Per OAuth 2.1 §6.1 the server MUST refuse and // SHOULD revoke the entire token family. OpenIddict surfaces this as either // 400 invalid_grant or 401 (depending on whether it treats redeemed tokens as // a grant failure or an authentication failure) — both are rejections. var reuse = await RefreshAsync(refreshA); reuse.IsSuccessStatusCode.Should().BeFalse( "reusing a redeemed refresh token must be rejected. Got {0}: {1}", reuse.StatusCode, await reuse.Content.ReadAsStringAsync()); // 4. The freshly rotated B must ALSO be invalid now — that is the family revocation // half of the contract. Without this, an attacker who stole A can still use B // (or vice versa) once the legitimate user has already rotated. var attemptUseFamily = await RefreshAsync(refreshB); attemptUseFamily.IsSuccessStatusCode.Should().BeFalse( "after reuse of a redeemed token, every sibling in the family must be revoked. " + "Got {0}: {1}", attemptUseFamily.StatusCode, await attemptUseFamily.Content.ReadAsStringAsync()); } [Fact] public async Task ReusedAncestorAfterMultipleRotations_RevokesEntireChain() { // A → B → C. Reuse B (the middle one). Expect C also revoked. var (refreshA, _) = await IssueInitialTokensAsync(); var resp1 = await RefreshAsync(refreshA); resp1.StatusCode.Should().Be(HttpStatusCode.OK); var refreshB = (await ParseJsonAsync(resp1)).GetProperty("refresh_token").GetString()!; var resp2 = await RefreshAsync(refreshB); resp2.StatusCode.Should().Be(HttpStatusCode.OK); var refreshC = (await ParseJsonAsync(resp2)).GetProperty("refresh_token").GetString()!; // Reuse B (already redeemed when we exchanged it for C). var reuse = await RefreshAsync(refreshB); reuse.IsSuccessStatusCode.Should().BeFalse( "reusing a redeemed refresh token must be rejected. Got {0}: {1}", reuse.StatusCode, await reuse.Content.ReadAsStringAsync()); // The current head of the chain (C) must also be invalidated. var attemptUseC = await RefreshAsync(refreshC); attemptUseC.IsSuccessStatusCode.Should().BeFalse( "ancestor reuse must invalidate the entire chain, including the latest token (C). " + "Got {0}: {1}", attemptUseC.StatusCode, await attemptUseC.Content.ReadAsStringAsync()); } /// /// G7 — RFC 7009 token revocation: posting a refresh_token to /connect/revocation /// must invalidate it so a subsequent refresh_token grant on the same value fails. /// This is the explicit «log out / sign out everywhere» path the spec mandates. /// [Fact] public async Task RevokedRefreshToken_CannotBeUsed_ConfidentialClient() { // Issue refresh token via password grant on the confidential client (RFC 7009 §2.1 // requires client authentication for revocation; public clients cannot revoke). var initial = await _http.PostAsync("/connect/token", new FormUrlEncodedContent( new Dictionary { ["grant_type"] = "password", ["username"] = ProductionHttpFixture.TestUsername, ["password"] = ProductionHttpFixture.TestPassword, ["scope"] = "openid offline_access", ["client_id"] = ProductionHttpFixture.TestClientId, ["client_secret"] = ProductionHttpFixture.TestClientSecret, })); if (!initial.IsSuccessStatusCode) return; // password grant not available in this env — skip silently. var refreshToken = (await ParseJsonAsync(initial)).GetProperty("refresh_token").GetString()!; var revoke = await _http.PostAsync("/connect/revocation", new FormUrlEncodedContent( new Dictionary { ["token"] = refreshToken, ["token_type_hint"] = "refresh_token", ["client_id"] = ProductionHttpFixture.TestClientId, ["client_secret"] = ProductionHttpFixture.TestClientSecret, })); revoke.StatusCode.Should().Be(HttpStatusCode.OK, "RFC 7009 §2.2: revocation must respond 200 — got {0}: {1}", revoke.StatusCode, await revoke.Content.ReadAsStringAsync()); var refresh = await _http.PostAsync("/connect/token", new FormUrlEncodedContent( new Dictionary { ["grant_type"] = "refresh_token", ["refresh_token"] = refreshToken, ["client_id"] = ProductionHttpFixture.TestClientId, ["client_secret"] = ProductionHttpFixture.TestClientSecret, })); refresh.IsSuccessStatusCode.Should().BeFalse( "a revoked refresh_token must no longer be exchangeable. Got {0}: {1}", refresh.StatusCode, await refresh.Content.ReadAsStringAsync()); } // ────────────────── helpers ────────────────── private async Task<(string refreshToken, string accessToken)> IssueInitialTokensAsync() { var (verifier, challenge) = GeneratePkce(); var code = await AuthorizeAsync( ProductionHttpFixture.TestPublicClientId, challenge, "openid offline_access"); var resp = await _http.PostAsync("/connect/token", new FormUrlEncodedContent( new Dictionary { ["grant_type"] = "authorization_code", ["code"] = code!, ["redirect_uri"] = ProductionHttpFixture.TestRedirectUri, ["client_id"] = ProductionHttpFixture.TestPublicClientId, ["code_verifier"] = verifier, })); resp.StatusCode.Should().Be(HttpStatusCode.OK, "initial token exchange failed: {0}", await resp.Content.ReadAsStringAsync()); var json = await ParseJsonAsync(resp); return (json.GetProperty("refresh_token").GetString()!, json.GetProperty("access_token").GetString()!); } private Task RefreshAsync(string refreshToken) => _http.PostAsync("/connect/token", new FormUrlEncodedContent(new Dictionary { ["grant_type"] = "refresh_token", ["refresh_token"] = refreshToken, ["client_id"] = ProductionHttpFixture.TestPublicClientId, })); private async Task AuthorizeAsync(string clientId, string challenge, string scope) { // Reproduces FullStackProtocolTests.AuthorizeViaHttp: login first to obtain a // session cookie, then POST /connect/authorize with that cookie. var cookieJar = new System.Net.CookieContainer(); using var handler = new HttpClientHandler { AllowAutoRedirect = false, CookieContainer = cookieJar, UseCookies = true, }; using var client = new HttpClient(handler) { BaseAddress = new Uri(_fx.BaseUrl) }; var loginResp = await client.PostAsync("/login", new FormUrlEncodedContent( new Dictionary { ["username"] = ProductionHttpFixture.TestUsername, ["password"] = ProductionHttpFixture.TestPassword, })); cookieJar.Count.Should().BeGreaterThan(0, "login response must set a session cookie"); var form = new Dictionary { ["response_type"] = "code", ["client_id"] = clientId, ["redirect_uri"] = ProductionHttpFixture.TestRedirectUri, ["scope"] = scope, ["code_challenge"] = challenge, ["code_challenge_method"] = "S256", }; var resp = await client.PostAsync("/connect/authorize", new FormUrlEncodedContent(form)); if (resp.StatusCode is HttpStatusCode.Redirect or HttpStatusCode.Found) { var location = resp.Headers.Location?.ToString(); location.Should().NotBeNull(); return ExtractQueryParam(location!, "code"); } var json = await ParseJsonAsync(resp); if (json.TryGetProperty("code", out var c)) return c.GetString(); if (json.TryGetProperty("error", out var err)) throw new InvalidOperationException( $"Authorize error: {err.GetString()} — " + (json.TryGetProperty("error_description", out var d) ? d.GetString() : "(no description)")); throw new InvalidOperationException($"Unexpected authorize response: {json.GetRawText()}"); } private static async Task ParseJsonAsync(HttpResponseMessage resp) { var body = await resp.Content.ReadAsStringAsync(); return JsonDocument.Parse(body).RootElement; } private static (string verifier, string challenge) GeneratePkce() { var bytes = new byte[32]; System.Security.Cryptography.RandomNumberGenerator.Fill(bytes); var verifier = Convert.ToBase64String(bytes).TrimEnd('=').Replace('+', '-').Replace('/', '_'); var hash = System.Security.Cryptography.SHA256.HashData(System.Text.Encoding.ASCII.GetBytes(verifier)); var challenge = Convert.ToBase64String(hash).TrimEnd('=').Replace('+', '-').Replace('/', '_'); return (verifier, challenge); } private static string? ExtractQueryParam(string url, string param) { var idx = url.IndexOf('?'); if (idx < 0) return null; foreach (var pair in url[(idx + 1)..].Split('&')) { var kv = pair.Split('=', 2); if (kv[0] == param) return Uri.UnescapeDataString(kv[1]); } return null; } }