Path: redb-route/redb.Route.Llm.Abstractions/Tools/ExchangePrincipalClaimsSource.cs
Snapshot: redb-route, 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-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Llm.Abstractions/Tools/ExchangePrincipalClaimsSource.cs
----
using System.Security.Claims;
using redb.Route.Abstractions;
namespace redb.Route.Llm.Abstractions.Tools;
///
/// Default : the claims of the caller an exchange came from.
///
/// The principal is read from , i.e. a property of the exchange
/// that only in-process code can write — a caller cannot send it as a header, and it does not travel
/// to a broker. That makes it the only identity a tool requirement may be checked against: a header
/// naming a user (such as llm.user.id) is audit information, not proof.
///
///
/// Values are collected from the scope claim types issuers actually use — scope (OAuth 2.0 /
/// OIDC), scp and http://schemas.microsoft.com/identity/claims/scope (Microsoft Entra
/// ID) — split on whitespace only and de-duplicated with ordinal comparison, because one logical
/// set can arrive as several claims with padded values. The delimiter is space by specification
/// (RFC 6749 §3.3) and a comma is a legal character inside a scope token: splitting on it would
/// turn one granted scope into two and widen the caller's rights, which is the one direction a security
/// check must never fail in. A deployment whose issuer really emits comma-separated lists registers its
/// own . Claim values are compared as-is; scope names are case-sensitive.
///
///
/// A principal whose identity is not authenticated counts as no principal (null), not as
/// an empty set: an anonymous caller cannot satisfy a requirement. This puts a requirement on hosts: a
/// resolver must return an identity carrying an authentication type — new ClaimsIdentity(claims)
/// without one reports IsAuthenticated == false and would leave claim-required tools denied
/// (fail closed, but for a reason the log must make visible). A run that has no inbound request — a
/// scheduler-fired one, or one born after a broker hop, where properties do not survive — likewise has
/// no principal. A consumer that establishes identity by itself (an AS2 signature check, a bearer token
/// validated in a processor) must store the outcome the same way, with
/// — a property, never a report header the caller could have written.
///
///
/// Deployments with a different vocabulary register their own ; it
/// replaces this one.
///
///
public sealed class ExchangePrincipalClaimsSource : IToolClaimsSource
{
private static readonly string[] ScopeClaimTypes =
[
"scope", // OAuth 2.0 / OIDC
"scp", // Microsoft Entra ID (v2)
"http://schemas.microsoft.com/identity/claims/scope" // Microsoft Entra ID (v1)
];
private static readonly char[] Separators = [' ', '\t', '\r', '\n'];
///
public IReadOnlyCollection? GetClaims(IExchange exchange)
{
ArgumentNullException.ThrowIfNull(exchange);
var principal = ExchangePrincipal.Get(exchange);
if (principal?.Identity is not { IsAuthenticated: true }) return null;
var claims = new HashSet(StringComparer.Ordinal);
foreach (var claimType in ScopeClaimTypes)
{
foreach (var claim in principal.FindAll(claimType))
{
foreach (var value in claim.Value.Split(
Separators, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries))
{
claims.Add(value);
}
}
}
return claims;
}
}