Path: redb/redb.Templates/templates/redb-identity-app/RedbIdentityApp.Module/Controllers/ApiController.cs Snapshot: redb, 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 Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-identity-app/RedbIdentityApp.Module/Controllers/ApiController.cs ---- using System.ComponentModel.DataAnnotations; using System.Security.Claims; using redb.Core; using redb.Route.Abstractions; using redb.Route.Controllers; using redb.Route.Http; using redb.Route.RedbCore.Extensions; using RedbIdentityApp.Contracts; using RedbIdentityApp.Module.Identity; namespace RedbIdentityApp.Module.Controllers; /// /// What the controllers of the module share. A controller is created for each request, without DI: what /// it needs comes from Context (the route context, whose registry holds the module's services) /// and Exchange (the request, whose principal is the checked bearer token). /// public abstract class ApiController : RedbController { /// The redb service of this request, scoped to the exchange. The empty name is the worker's default redb. protected IRedbService Redb => Context.GetRedbService("", Exchange); /// The caller: what Identity said about the token (). protected ClaimsPrincipal Caller => ExchangePrincipal.Get(Exchange) ?? throw new InvalidOperationException("The API route runs without a bearer check."); /// The token's subject: a stable, public id of the user (or of the client, for a service token). protected string Subject => Caller.FindFirst(BearerTokenValidator.SubjectClaim)!.Value; /// True for a user's token; a client-credentials token has no user. protected bool IsUser => Caller.HasClaim(c => c.Type == BearerTokenValidator.UserIdClaim); protected ModuleSettings Settings => Service(); protected T Service() where T : class => Context.GetFromRegistry(typeof(T).FullName!) ?? throw new InvalidOperationException($"{typeof(T).Name} is not registered."); /// /// Null when the token holds ; otherwise sets 403 with /// error="insufficient_scope" and returns the body to return. /// protected ApiError? RequireScope(string scope) { if (Caller.HasClaim(BearerTokenValidator.ScopeClaim, scope)) return null; IdentityGate.InsufficientScope(Exchange, Context.ContextId, scope); return new ApiError { Message = $"The token lacks the scope {scope}." }; } /// Sets the status code of the answer; the returned object is still the body. protected T Status(int code, T body) { Exchange.Out ??= Exchange.In.Clone(); Exchange.Out.Headers[HttpHeaders.ResponseCode] = code; return body; } protected ApiError NotFound(string message) => Status(404, new ApiError { Message = message }); /// The web server validates the form, but the module cannot trust that: the same attributes are checked here. protected ApiError? Invalid(object dto) { var errors = new List(); return Validator.TryValidateObject(dto, new ValidationContext(dto), errors, validateAllProperties: true) ? null : Status(400, new ApiError { Message = string.Join(" ", errors.Select(r => r.ErrorMessage)) }); } }