Path: redb-identity/tests/redb.Identity.Tests/Http/E2E/UnreadableBasicCredentialsTests.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/Http/E2E/UnreadableBasicCredentialsTests.cs ---- using System.Net; using System.Text.Json; using AwesomeAssertions; using redb.Identity.Tests.Infrastructure; using Xunit; namespace redb.Identity.Tests.Http.E2E; /// /// A client that sends Authorization: Basic attempts client authentication through the header. /// When the value cannot be read (not base64, not its canonical spelling, which .NET 10 decodes and /// .NET 11 refuses, or no client_id:client_secret pair) the attempt failed: every endpoint that /// takes client authentication answers 401 invalid_client with a Basic challenge (RFC 6749 ยง5.2), /// on every runtime. Up to this change the token endpoint answered such a request with a 500. /// [Collection("ProductionHttp")] public sealed class UnreadableBasicCredentialsTests { private const string NotBase64 = "!!!not-base64!!!"; private readonly ProductionHttpFixture _fx; public UnreadableBasicCredentialsTests(ProductionHttpFixture fx) => _fx = fx; public static TheoryData Cases() { var data = new TheoryData(); foreach (var endpoint in new[] { "/connect/token", "/connect/introspect", "/connect/revocation", "/connect/deviceauthorization", "/connect/par", }) { data.Add(endpoint, NotBase64); data.Add(endpoint, NonCanonical(ProductionHttpFixture.TestClientId + ":" + ProductionHttpFixture.TestClientSecret)); data.Add(endpoint, Convert.ToBase64String("no-colon-here"u8)); } return data; } [Theory] [MemberData(nameof(Cases))] public async Task UnreadableBasicValue_AnswersInvalidClient_WithABasicChallenge(string endpoint, string basic) { var (status, error, challenge, body) = await Post(endpoint, basic); status.Should().Be(HttpStatusCode.Unauthorized, body); error.Should().Be("invalid_client", body); challenge.Should().StartWith("Basic ", body); } [Fact] public async Task ReadableBasicCredentials_StillAuthenticate() { var basic = Convert.ToBase64String( System.Text.Encoding.UTF8.GetBytes(ProductionHttpFixture.TestClientId + ":" + ProductionHttpFixture.TestClientSecret)); var (status, error, _, body) = await Post("/connect/token", basic); status.Should().Be(HttpStatusCode.OK, body); error.Should().BeNull(body); } /// /// Device authorization and PAR read Basic credentials as the token endpoint does, so discovery /// names client_secret_basic for them wherever it lists their authentication methods. /// [Fact] public async Task Discovery_NamesClientSecretBasic_ForEveryClientAuthenticatedEndpoint() { using var response = await _fx.Http.GetAsync("/.well-known/openid-configuration"); var body = await response.Content.ReadAsStringAsync(); response.StatusCode.Should().Be(HttpStatusCode.OK, body); var root = JsonDocument.Parse(body).RootElement; foreach (var name in new[] { "token_endpoint_auth_methods_supported", "introspection_endpoint_auth_methods_supported", "revocation_endpoint_auth_methods_supported", "device_authorization_endpoint_auth_methods_supported", "pushed_authorization_request_endpoint_auth_methods_supported", }) { if (!root.TryGetProperty(name, out var methods)) continue; methods.EnumerateArray().Select(m => m.GetString()).Should().Contain("client_secret_basic", $"{name}: {methods}"); } } private async Task<(HttpStatusCode Status, string? Error, string? Challenge, string Body)> Post(string endpoint, string basic) { var form = endpoint switch { "/connect/token" => new Dictionary { ["grant_type"] = "client_credentials" }, "/connect/introspect" or "/connect/revocation" => new Dictionary { ["token"] = "opaque" }, "/connect/deviceauthorization" => new Dictionary { ["scope"] = "openid" }, _ => new Dictionary { ["response_type"] = "code", ["redirect_uri"] = "https://client.example/cb", ["scope"] = "openid", }, }; using var request = new HttpRequestMessage(HttpMethod.Post, endpoint) { Content = new FormUrlEncodedContent(form) }; request.Headers.TryAddWithoutValidation("Authorization", $"Basic {basic}"); using var response = await _fx.Http.SendAsync(request); var body = await response.Content.ReadAsStringAsync(); string? error = null; if (body.TrimStart().StartsWith('{') && JsonDocument.Parse(body).RootElement.TryGetProperty("error", out var e)) error = e.GetString(); var challenge = response.Headers.WwwAuthenticate.Count > 0 ? response.Headers.WwwAuthenticate.ToString() : null; return (response.StatusCode, error, challenge, body); } /// The same bytes spelled with a non-zero unused bit in the last character. private static string NonCanonical(string text) { const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; var canonical = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); var data = canonical.TrimEnd('='); if (data.Length == canonical.Length) throw new InvalidOperationException($"'{text}' encodes without padding; pick a length that is not a multiple of three."); var last = alphabet.IndexOf(data[^1]); return data[..^1] + alphabet[last | 1] + canonical[data.Length..]; } }