Path: redb-identity/tests/redb.Identity.Tests/Management/OneTimeSecretsTests.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/Management/OneTimeSecretsTests.cs
----
using System.Reflection;
using System.Text.Json.Serialization;
using System.Text.RegularExpressions;
using AwesomeAssertions;
using redb.Identity.Contracts.Applications;
using redb.Identity.Contracts.Common;
using redb.Identity.Contracts.Mfa;
using redb.Identity.Contracts.Registration;
using redb.Identity.Contracts.Scim;
using redb.Identity.Contracts.Webhooks;
using redb.Identity.Core.Routes.Processors;
using Xunit;
namespace redb.Identity.Tests.Management;
///
/// The declaration that keeps one-time secrets out of the idempotency cache: the
/// marker on result members, and , which reads it.
///
public class OneTimeSecretsTests
{
public static TheoryData SecretBearing() => new()
{
{ "webhook created", new WebhookSubscriptionResponse { Id = 1, Url = "http://x", HmacSecret = "s" } },
{ "webhook secret rotated", new WebhookSecretRotatedResponse { HmacSecret = "s" } },
{ "application secret rotated", new ApplicationResponse { ClientId = "c", NewSecret = "s" } },
{ "client registered", new DynamicRegistrationResponse { ClientId = "c", ClientSecret = "s" } },
{ "client registered, public", new DynamicRegistrationResponse { ClientId = "c", RegistrationAccessToken = "t" } },
{ "mfa setup", new MfaSetupResponse { Method = "totp", SecretBase32 = "ABC" } },
{ "mfa setup, otp method", new MfaSetupResponse { Method = "email", SetupToken = "t" } },
{ "mfa confirmed", new MfaConfirmedResponse { RecoveryCodes = new[] { "r1" } } },
{ "recovery codes", new MfaRecoveryCodesResponse { RecoveryCodes = new[] { "r1" } } },
{ "scim user with password", new ScimUser { UserName = "u", Password = "p" } },
{ "in a page", new PagedResult { Items = new List { new() { Url = "http://x", HmacSecret = "s" } } } },
};
public static TheoryData SecretFree() => new()
{
{ "nothing", null },
{ "webhook read back", new WebhookSubscriptionResponse { Id = 1, Url = "http://x", HmacSecret = null, HasHmacSecret = true } },
{ "webhook with an empty secret", new WebhookSubscriptionResponse { Url = "http://x", HmacSecret = "" } },
{ "application read back", new ApplicationResponse { ClientId = "c" } },
{ "recovery codes, none", new MfaRecoveryCodesResponse { RecoveryCodes = Array.Empty() } },
{ "scim user", new ScimUser { UserName = "u" } },
{ "a page of webhooks", new PagedResult { Items = new List { new() { Url = "http://x" } } } },
{ "success", new SuccessResponse() },
{ "a dictionary", new Dictionary { ["hmacSecret"] = "s" } },
{ "a string", "s" },
};
[Theory, MemberData(nameof(SecretBearing))]
public void A_set_marked_member_is_found(string @case, object result)
=> OneTimeSecrets.Carries(result).Should().BeTrue(@case);
[Theory, MemberData(nameof(SecretFree))]
public void An_answer_without_a_set_marked_member_is_kept(string @case, object? result)
=> OneTimeSecrets.Carries(result).Should().BeFalse(@case);
// Member names that look like a credential or a one-time value. A Contracts result member whose name matches
// must carry [OneTimeSecret] or be listed below with the reason it is not one.
private static readonly Regex SecretLooking = new(
"secret|token|password|passcode|recovery|otp|qr|private_?key|api_?key|(^|_)code(s)?$",
RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
private static readonly Dictionary NotSecrets = new()
{
// Flags that say whether a secret exists; the secret itself is never in these answers.
["WebhookSubscriptionResponse.HasHmacSecret"] = "flag",
["FederationProviderResponse.HasSecret"] = "flag",
["SecretWithheldReplayResponse.SecretWithheld"] = "flag of the replay answer itself",
// Kinds, methods, settings and addresses.
["IntrospectionResponse.TokenType"] = "token type name (RFC 7662)",
["TokenResponse.TokenType"] = "token type name (Bearer)",
["DynamicRegistrationResponse.TokenEndpointAuthMethod"] = "authentication method name",
["DynamicRegistrationResponse.ClientSecretExpiresAt"] = "expiry time of the secret",
["DynamicRegistrationResponse.PasswordResetUris"] = "redirect addresses",
["ApplicationResponse.PasswordResetUris"] = "redirect addresses",
["OAuth2EndpointOverrides.TokenEndpoint"] = "endpoint address",
["ScimServiceProviderConfig.ChangePassword"] = "SCIM capability flag",
["ClaimDefinitionResponse.EmitOnIdToken"] = "claim placement flag",
["ClaimDefinitionResponse.EmitOnAccessToken"] = "claim placement flag",
["ApplicationResponse.AccessTokenLifetimeSeconds"] = "lifetime setting",
["ApplicationResponse.RefreshTokenLifetimeSeconds"] = "lifetime setting",
["ApplicationResponse.IdentityTokenLifetimeSeconds"] = "lifetime setting",
["ApplicationResponse.IdTokenAudiences"] = "audience names",
["ApplicationResponse.AccessTokenAudiences"] = "audience names",
// Counts.
["TokenPrunePreview.WouldPruneTokens"] = "count",
["TokenPruneResult.PrunedTokens"] = "count",
["MfaOtpPruneResult.PrunedMfaOtp"] = "count",
["MfaStatusResponse.RecoveryCodesRemaining"] = "count",
// Optimistic concurrency stamps, sent back on update; they grant nothing.
["ScopeResponse.ConcurrencyToken"] = "concurrency stamp",
["ApplicationResponse.ConcurrencyToken"] = "concurrency stamp",
// Configuration bound from settings, never an answer of a route.
["FederationProviderConfig.ClientSecret"] = "configuration, not a result",
};
[Fact]
public void Every_secret_looking_result_member_is_marked_or_explained()
{
var contracts = typeof(OneTimeSecretAttribute).Assembly;
var unmarked = new List();
foreach (var type in contracts.GetExportedTypes())
{
if (!type.IsClass || type.IsAbstract || typeof(Attribute).IsAssignableFrom(type)) continue;
// Requests are what callers send; they are not answers and never reach the cache.
if (type.Name.EndsWith("Request", StringComparison.Ordinal)) continue;
foreach (var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
var json = property.GetCustomAttribute()?.Name ?? property.Name;
if (!SecretLooking.IsMatch(property.Name) && !SecretLooking.IsMatch(json)) continue;
if (property.IsDefined(typeof(OneTimeSecretAttribute), inherit: true)) continue;
var name = $"{type.Name}.{property.Name}";
if (!NotSecrets.ContainsKey(name)) unmarked.Add(name);
}
}
string.Join(", ", unmarked).Should().BeEmpty(
"a result member that looks like a secret is marked [OneTimeSecret], so the idempotency cache never keeps it, "
+ "or it is listed in NotSecrets with the reason it is not one");
}
}