Path: redb-identity/tests/redb.Identity.Tests/TokenFlow/IntrospectionRevocationTests.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/TokenFlow/IntrospectionRevocationTests.cs ---- using System.Security.Claims; using AwesomeAssertions; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using OpenIddict.Abstractions; using OpenIddict.Server; using redb.Identity.Core.OpenIddict; using redb.Identity.Core.Routes.Processors; using redb.Identity.Tests.Infrastructure; using Xunit; using static OpenIddict.Abstractions.OpenIddictConstants; using static OpenIddict.Server.OpenIddictServerEvents; namespace redb.Identity.Tests.TokenFlow; /// /// Integration tests for Introspection and Revocation endpoints. /// Uses degraded mode — token validation is limited, but pipeline path is verified. /// public class IntrospectionRevocationTests { private static ServiceProvider BuildServiceProvider() { var services = new ServiceCollection(); services.AddLogging(b => b.SetMinimumLevel(LogLevel.Warning)); services.AddOpenIddict() .AddServer(options => { options.EnableDegradedMode(); options.SetIssuer(new Uri("https://identity.test.local/")); options.SetTokenEndpointUris("/connect/token"); options.SetIntrospectionEndpointUris("/connect/introspect"); options.SetRevocationEndpointUris("/connect/revocation"); options.AllowClientCredentialsFlow(); options.AddEphemeralEncryptionKey(); options.AddEphemeralSigningKey(); options.DisableAccessTokenEncryption(); options.UseRedbRoute(); // Degraded mode handlers options.AddEventHandler(builder => builder.UseInlineHandler(context => default) .SetOrder(int.MaxValue - 100_000).Build()); options.AddEventHandler(builder => builder.UseInlineHandler(context => { if (!context.Request.IsClientCredentialsGrantType()) return default; var identity = new ClaimsIdentity( authenticationType: "OpenIddict.Server", nameType: Claims.Name, roleType: Claims.Role); identity.SetClaim(Claims.Subject, context.Request.ClientId ?? "test-sub"); identity.SetScopes(context.Request.GetScopes()); context.Principal = new ClaimsPrincipal(identity); return default; }).SetOrder(OpenIddictServerHandlers.Exchange.AttachPrincipal .Descriptor.Order + 100).Build()); // Degraded mode: skip introspection client validation options.AddEventHandler(builder => builder.UseInlineHandler(context => default) .SetOrder(int.MaxValue - 100_000).Build()); // Degraded mode: skip revocation client validation options.AddEventHandler(builder => builder.UseInlineHandler(context => default) .SetOrder(int.MaxValue - 100_000).Build()); }); return services.BuildServiceProvider(); } private static async Task ObtainAccessToken(IServiceScope scope) { var handler = scope.ServiceProvider.GetRequiredService(); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["grant_type"] = "client_credentials", ["client_id"] = "test-client", ["client_secret"] = "test-secret" }; await new TokenEndpointProcessor(handler).Process(exchange); var body = (Dictionary)exchange.Out!.Body!; return body["access_token"]!.ToString()!; } // ── Introspection ── [Fact] public async Task Introspect_ValidToken_ReturnsClaims() { await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var token = await ObtainAccessToken(scope); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["token"] = token }; exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new IntrospectionEndpointProcessor(handler).Process(exchange); exchange.Out.Should().NotBeNull(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; // In degraded mode OpenIddict validates the self-contained JWT directly. // It should return active=true and include the subject claim. body.Should().ContainKey("active"); body["active"].Should().Be(true); body.Should().ContainKey("sub"); body["sub"]!.ToString().Should().Be("test-client"); } [Fact] public async Task Introspect_ValidToken_ContainsIssuer() { await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var token = await ObtainAccessToken(scope); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["token"] = token }; exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new IntrospectionEndpointProcessor(handler).Process(exchange); var body = (Dictionary)exchange.Out!.Body!; body.Should().ContainKey("iss"); body["iss"]!.ToString().Should().Contain("identity.test.local"); } [Fact] public async Task Introspect_InvalidToken_ReturnsInactive() { await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["token"] = "completely-invalid-token" }; exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new IntrospectionEndpointProcessor(handler).Process(exchange); exchange.Out.Should().NotBeNull(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; // OpenIddict returns error for completely invalid tokens (not RFC active:false) body.Should().ContainKey("error"); } [Fact] public async Task Introspect_MissingToken_ReturnsError() { await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var exchange = new TestExchange(); exchange.In.Body = new Dictionary(); exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new IntrospectionEndpointProcessor(handler).Process(exchange); exchange.Out.Should().NotBeNull(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; body.Should().ContainKey("error"); body["error"].Should().Be("invalid_request"); } // ── Revocation ── [Fact] public async Task Revoke_ValidToken_ReturnsSuccess() { await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var token = await ObtainAccessToken(scope); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["token"] = token }; exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new RevocationEndpointProcessor(handler).Process(exchange); // RFC 7009: always returns 200 (no error means success) exchange.Out.Should().NotBeNull(); var body = exchange.Out!.Body as Dictionary; if (body != null) { body.Should().NotContainKey("error", "revocation should not return an error for a valid token"); } } [Fact] public async Task Revoke_SetsEventMetadata() { await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var token = await ObtainAccessToken(scope); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["token"] = token }; exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new RevocationEndpointProcessor(handler).Process(exchange); exchange.Properties.Should().ContainKey("identity-event-type"); exchange.Properties["identity-event-type"].Should().Be("TokenRevoked"); } [Fact] public async Task Revoke_InvalidToken_ReturnsSuccess() { // RFC 7009 §2.1: The server responds with HTTP 200 even if the token is invalid/unknown. await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["token"] = "not-a-real-token-at-all" }; exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new RevocationEndpointProcessor(handler).Process(exchange); // Revocation should not blow up; handler always sets event metadata exchange.Out.Should().NotBeNull(); exchange.Properties.Should().ContainKey("identity-event-type"); exchange.Properties["identity-event-type"].Should().Be("TokenRevoked"); } [Fact] public async Task Revoke_MissingToken_ReturnsError() { await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var exchange = new TestExchange(); exchange.In.Body = new Dictionary(); exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new RevocationEndpointProcessor(handler).Process(exchange); exchange.Out.Should().NotBeNull(); var body = exchange.Out!.Body.Should().BeOfType>().Subject; body.Should().ContainKey("error"); body["error"].Should().Be("invalid_request"); } [Fact] public async Task Revoke_WithTokenTypeHint_ReturnsSuccess() { await using var sp = BuildServiceProvider(); using var scope = sp.CreateScope(); var handler = scope.ServiceProvider.GetRequiredService(); var token = await ObtainAccessToken(scope); var exchange = new TestExchange(); exchange.In.Body = new Dictionary { ["token"] = token, ["token_type_hint"] = "access_token" }; exchange.In.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes("test-client:test-secret")); await new RevocationEndpointProcessor(handler).Process(exchange); exchange.Out.Should().NotBeNull(); var body = exchange.Out!.Body as Dictionary; if (body != null) { body.Should().NotContainKey("error", "revocation with valid token_type_hint should succeed"); } } }