Path: redb/redb.Templates/templates/redb-bff/RedbBff.Backend/ServiceKeyValidator.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-bff/RedbBff.Backend/ServiceKeyValidator.cs
----
using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using redb.Route.Http;
namespace RedbBff.Backend;
///
/// The backend has one caller: the web server. It sends the shared service key as a bearer token; any
/// other request gets 401 before a controller runs. The users sign in to the web server, not here.
///
public sealed class ServiceKeyValidator(string serviceKey) : IHttpTokenValidator
{
public const string RegistryName = "service-key";
private readonly byte[] _expected = Encoding.UTF8.GetBytes(serviceKey);
public Task ValidateAsync(string token, CancellationToken ct)
{
// Constant-time comparison, so the answer time does not tell how much of the key was right.
var accepted = CryptographicOperations.FixedTimeEquals(_expected, Encoding.UTF8.GetBytes(token));
return Task.FromResult(accepted
? new ClaimsPrincipal(new ClaimsIdentity([new Claim(ClaimTypes.Name, "web")], authenticationType: "ServiceKey"))
: null);
}
}