Path: redb-identity/tests/redb.Identity.Tests/Security/AnonymousDcrAdminScopeTests.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/Security/AnonymousDcrAdminScopeTests.cs
----
using AwesomeAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NSubstitute;
using OpenIddict.Abstractions;
using redb.Core;
using redb.Core.Models.Entities;
using redb.Identity.Contracts.Configuration;
using redb.Identity.Contracts.Registration;
using redb.Identity.Core.Configuration;
using redb.Identity.Core.Models;
using redb.Identity.Core.Routes.Processors;
using redb.Identity.Http.Processors;
using redb.Identity.Tests.Infrastructure;
using redb.Route.Abstractions;
using redb.Route.Core;
using redb.Route.Http;
using Xunit;
namespace redb.Identity.Tests.Security;
///
/// Nobody registers themselves an administrator.
///
/// Open dynamic client registration (RFC 7591 §1.2) lets an anonymous caller create a client, and
/// the only limit on what that client may ask for is DynamicRegistrationAllowedScopes. The
/// shipped list includes the granular administrative scopes so the demo suite can probe each admin
/// surface — which also meant that anyone able to reach POST /connect/register could mint a
/// client_credentials client carrying identity:users:write, take a token and delete
/// users. No role, no password, no operator involved.
///
///
/// The entitlement gate on user-bound grants cannot reach this: client_credentials has no
/// user to entitle, so the client's own permissions are the whole gate — and those permissions are
/// exactly what registration hands out. The floor therefore sits below deployment policy: while
/// registration is open, administrative scopes are refused whatever the allow-list says.
/// Configuring an initial access token turns registration into the protected mode RFC 7591 defines,
/// and the allow-list governs again.
///
///
public class AnonymousDcrAdminScopeTests
{
private const string InitialAccessToken = "test-initial-access-token";
private readonly IOpenIddictApplicationManager _manager = Substitute.For();
private readonly IRedbService _redb = Substitute.For();
/// A deployment whose allow-list is as permissive as the shipped one.
private static RedbIdentityOptions Permissive(string? initialAccessToken = null) => new()
{
Features = new IdentityFeatureFlags { EnableDynamicRegistration = true },
DynamicRegistrationAllowedGrantTypes = ["authorization_code", "client_credentials"],
DynamicRegistrationAllowedScopes =
["openid", "profile", "identity:account", "identity:users:write", "identity:manage", "scim"],
DynamicRegistrationInitialAccessToken = initialAccessToken,
};
private DynamicRegistrationProcessor CreateProcessor(RedbIdentityOptions options)
{
var services = new ServiceCollection();
services.AddSingleton(_manager);
services.AddSingleton(_redb);
var sp = services.BuildServiceProvider();
var context = Substitute.For();
context.GetServiceProvider().Returns(sp);
context.GetFromRegistry(Arg.Any()).Returns((IServiceScopeFactory?)null);
context.GetService().Returns(_redb);
return new DynamicRegistrationProcessor(context, Options.Create(options));
}
private void SetupManager()
{
_manager.PopulateAsync(Arg.Any