Path: redb-identity/tests/redb.Identity.Tests/Authorization/AdminEndpointAuthMatrixTests.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/Authorization/AdminEndpointAuthMatrixTests.cs ---- using System.Net; using System.Net.Http.Headers; using AwesomeAssertions; using redb.Identity.Tests.Infrastructure; using Xunit; namespace redb.Identity.Tests.Authorization; /// /// G14 — endpoint-matrix smoke for management-API authorization. /// /// The HTTP facade routes management endpoints through /// ManagementBearerAuthProcessor which requires a bearer access token whose /// scopes intersect { identity:manage, identity:account, scim } (see /// ProductionHttpFixture.InitializeAsync wiring). This test asserts the /// invariant per endpoint: /// /// no token → 401 (cannot enumerate without an access token); /// token without the required scope → 403 (e.g. scim token on /// management routes, or identity:manage token on SCIM routes); /// token with the right scope → 2xx. /// /// /// /// Without this matrix, a refactor that drops ManagementBearerAuthProcessor /// from a single endpoint would silently expose admin operations to unauthenticated /// callers — exactly the IDOR-shaped regression that G14 / B8 aims to lock down. /// /// [Collection("ProductionHttp")] public sealed class AdminEndpointAuthMatrixTests { private readonly ProductionHttpFixture _fx; public AdminEndpointAuthMatrixTests(ProductionHttpFixture fx) => _fx = fx; /// Endpoints requiring identity:manage (or identity:account when self-scoped). public static IEnumerable ManagementEndpoints() => new[] { new object[] { "GET", "/api/v1/identity/users" }, new object[] { "GET", "/api/v1/identity/scopes" }, new object[] { "GET", "/api/v1/identity/sessions?userId=1" }, }; /// SCIM endpoints requiring the scim scope. public static IEnumerable ScimEndpoints() => new[] { new object[] { "GET", "/scim/v2/Users" }, new object[] { "GET", "/scim/v2/Groups" }, }; [Theory] [MemberData(nameof(ManagementEndpoints))] public async Task ManagementEndpoint_WithoutBearerToken_Returns401(string method, string path) { using var req = new HttpRequestMessage(new HttpMethod(method), path); var resp = await _fx.Http.SendAsync(req); ((int)resp.StatusCode).Should().Be(401, "{0} {1}: management API must reject anonymous calls — without this guard " + "every Manage* route would be open to the internet (G14 / B8).", method, path); } [Theory] [MemberData(nameof(ManagementEndpoints))] public async Task ManagementEndpoint_WithScimOnlyToken_Returns403(string method, string path) { using var req = new HttpRequestMessage(new HttpMethod(method), path); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ScimToken); var resp = await _fx.Http.SendAsync(req); // 403 (forbidden) is correct; 401 also acceptable if the validator strips // mismatched-scope tokens. The critical assertion is that the call is NOT // allowed (2xx) — that would be cross-scope privilege escalation. var code = (int)resp.StatusCode; (code == 401 || code == 403).Should().BeTrue( "{0} {1}: a SCIM-only token must NOT reach management endpoints (got {2}). " + "Allowing it would let a SCIM provisioner perform admin mutations.", method, path, code); } [Theory] [MemberData(nameof(ManagementEndpoints))] public async Task ManagementEndpoint_WithManagementToken_Returns2xx(string method, string path) { using var req = new HttpRequestMessage(new HttpMethod(method), path); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ManagementToken); var resp = await _fx.Http.SendAsync(req); ((int)resp.StatusCode).Should().BeInRange(200, 299, "{0} {1}: a token bearing identity:manage must be admitted. " + "Status: {2}", method, path, resp.StatusCode); } [Theory] [MemberData(nameof(ScimEndpoints))] public async Task ScimEndpoint_WithoutBearerToken_Returns401(string method, string path) { using var req = new HttpRequestMessage(new HttpMethod(method), path); var resp = await _fx.Http.SendAsync(req); ((int)resp.StatusCode).Should().Be(401, "{0} {1}: SCIM API must reject anonymous calls (RFC 7644 §2).", method, path); } [Theory] [MemberData(nameof(ScimEndpoints))] public async Task ScimEndpoint_WithManagementToken_IsRejected(string method, string path) { // Symmetry: a non-SCIM token must NOT pass the SCIM authorization gate. // Otherwise an admin could side-step provisioning audit by crossing scopes. using var req = new HttpRequestMessage(new HttpMethod(method), path); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ManagementToken); var resp = await _fx.Http.SendAsync(req); var code = (int)resp.StatusCode; (code == 401 || code == 403).Should().BeTrue( "{0} {1}: a management-only token must NOT pass the SCIM scope gate (got {2}). " + "Cross-scope acceptance is a privilege-boundary violation.", method, path, code); } [Theory] [MemberData(nameof(ScimEndpoints))] public async Task ScimEndpoint_WithScimToken_Returns2xx(string method, string path) { using var req = new HttpRequestMessage(new HttpMethod(method), path); req.Headers.Authorization = new AuthenticationHeaderValue("Bearer", _fx.ScimToken); var resp = await _fx.Http.SendAsync(req); ((int)resp.StatusCode).Should().BeInRange(200, 299, "{0} {1}: a token bearing 'scim' must be admitted. Status: {2}", method, path, resp.StatusCode); } }