Path: redb-route/redb.Route.Ldap/LdapConnectionFactory.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.Ldap/LdapConnectionFactory.cs
----
using redb.Route.Abstractions;
using redb.Route.Core;
namespace redb.Route.Ldap;
///
/// Named connection factory for LDAP. Register it in the route registry and reference it
/// by name from the endpoint URI (connectionFactory=my-ldap) so that service-account
/// credentials never have to appear in the URI itself — and therefore can never reach logs,
/// telemetry, or the Tsak dashboard.
///
/// Example:
///
/// context.AddToRegistry("corp-ldap", new LdapConnectionFactory
/// {
/// Server = "ldap.corp.local",
/// Port = 636,
/// Ssl = true,
/// BindDn = "cn=svc-reader,dc=corp,dc=local",
/// BindPassword = Environment.GetEnvironmentVariable("LDAP_BIND_PASSWORD")
/// });
///
/// // route carries no credentials at all:
/// r.From("ldap://SEARCH:dc=corp,dc=local?connectionFactory=corp-ldap&filter=(objectClass=user)")
///
///
///
public sealed class LdapConnectionFactory
{
// ── Connection ──
/// LDAP server hostname or IP.
public string Server { get; set; } = "localhost";
/// LDAP port (389 for plain, 636 for LDAPS).
public int Port { get; set; } = 389;
/// Use LDAPS (SSL/TLS from the start, typically port 636).
public bool Ssl { get; set; }
/// Use STARTTLS upgrade on the plain port (389).
public bool StartTls { get; set; }
/// Connection timeout in milliseconds.
public int ConnectTimeout { get; set; } = 5000;
/// Operation timeout in milliseconds.
public int OperationTimeout { get; set; } = 30000;
// ── Auth (service account) ──
/// Bind DN for service account authentication.
public string? BindDn { get; set; }
/// Password for service account authentication.
[Sensitive]
public string? BindPassword { get; set; }
// ── Protocol / pool ──
/// LDAP protocol version (default 3).
public int ProtocolVersion { get; set; } = 3;
/// Follow LDAP referrals automatically.
public bool FollowReferrals { get; set; } = true;
/// Maximum number of connections in the pool.
public int MaxConnections { get; set; } = 10;
// ── TLS ──
/// Skip server certificate validation (development only!).
public bool TrustAllCertificates { get; set; }
/// Path to client certificate for mutual TLS.
public string? ClientCertPath { get; set; }
/// Password for client certificate.
[Sensitive]
public string? ClientCertPassword { get; set; }
///
/// Copies this factory's connection and credential settings onto the endpoint options,
/// but only for parameters the endpoint URI did not set explicitly — an inline URI value
/// always wins, so existing routes keep their behaviour.
///
/// Endpoint options already bound from the URI.
/// The parsed endpoint URI, used to detect explicitly-supplied parameters.
internal void ApplyTo(LdapEndpointOptions options, EndpointUri uri)
{
var supplied = uri.RawParameters;
if (!supplied.ContainsKey(nameof(options.Server))) options.Server = Server;
if (!supplied.ContainsKey(nameof(options.Port))) options.Port = Port;
if (!supplied.ContainsKey(nameof(options.Ssl))) options.Ssl = Ssl;
if (!supplied.ContainsKey(nameof(options.StartTls))) options.StartTls = StartTls;
if (!supplied.ContainsKey(nameof(options.ConnectTimeout))) options.ConnectTimeout = ConnectTimeout;
if (!supplied.ContainsKey(nameof(options.OperationTimeout))) options.OperationTimeout = OperationTimeout;
if (!supplied.ContainsKey(nameof(options.BindDn))) options.BindDn = BindDn;
if (!supplied.ContainsKey(nameof(options.BindPassword))) options.BindPassword = BindPassword;
if (!supplied.ContainsKey(nameof(options.ProtocolVersion))) options.ProtocolVersion = ProtocolVersion;
if (!supplied.ContainsKey(nameof(options.FollowReferrals))) options.FollowReferrals = FollowReferrals;
if (!supplied.ContainsKey(nameof(options.MaxConnections))) options.MaxConnections = MaxConnections;
if (!supplied.ContainsKey(nameof(options.TrustAllCertificates)))
options.TrustAllCertificates = TrustAllCertificates;
if (!supplied.ContainsKey(nameof(options.ClientCertPath))) options.ClientCertPath = ClientCertPath;
if (!supplied.ContainsKey(nameof(options.ClientCertPassword)))
options.ClientCertPassword = ClientCertPassword;
}
}