Path: redb-route/redb.Route/Abstractions/ExchangePrincipal.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/Abstractions/ExchangePrincipal.cs
----
using System.Security.Claims;
namespace redb.Route.Abstractions;
///
/// The identity of the caller an exchange came from (Apache Camel Exchange.AUTHENTICATION).
/// A consumer that knows who sent the request stores the principal here, and processors, policies and
/// tools read it from here rather than from transport-specific headers.
///
/// It is an exchange property, not a header, on purpose. Headers are filled from the wire, so a
/// caller can send any header name it likes, and producers bridge headers on to the next system.
/// Properties are written only by code running in this process and never travel to a broker. Every
/// exchange copy (child, linked child, clone, snapshot) inherits properties, so a sub-route, a split part
/// or a tool call sees the identity of the request that started it.
///
///
/// The server-side HTTP-family consumers fill it from the shared host's principal resolver or from the
/// transport's own authenticate hook. A route that validates credentials itself — a bearer token checked
/// in a processor — records the outcome the same way, with .
///
///
public static class ExchangePrincipal
{
/// Exchange property key holding the principal (Camel: Exchange.AUTHENTICATION).
public const string PropertyKey = "CamelAuthentication";
/// Returns the caller's principal, or null when the exchange carries no identity.
public static ClaimsPrincipal? Get(IExchange exchange)
{
ArgumentNullException.ThrowIfNull(exchange);
return exchange.Properties.TryGetValue(PropertyKey, out var raw) ? raw as ClaimsPrincipal : null;
}
/// Stores as the caller's identity; null removes it.
public static void Set(IExchange exchange, ClaimsPrincipal? principal)
{
ArgumentNullException.ThrowIfNull(exchange);
if (principal is null)
exchange.Properties.Remove(PropertyKey);
else
exchange.Properties[PropertyKey] = principal;
}
}