using ZB.MOM.WW.Auth.Abstractions.Ldap;
using LibLdapOptions = ZB.MOM.WW.Auth.Abstractions.Ldap.LdapOptions;
namespace ZB.MOM.WW.OtOpcUa.Security.Ldap;
///
/// LDAP + role-mapping configuration for the Admin UI. Bound from appsettings.json
/// Security:Ldap section. The code defaults are localhost:3893, plaintext
/// () and search base dc=zb,dc=local — the GLAuth
/// shape; the shipped overlays and service environment point it at a real directory. See
/// docs/security.md §"LDAP bind flow" and docs/Configuration.md
/// §"Security:Ldap" for setup details.
///
///
/// Carries both the wire fields the shared ZB.MOM.WW.Auth.Ldap directory client needs
/// (///…) AND the app-only concerns
/// the shared library has no notion of ( master switch,
/// dev bypass, appsettings role baseline).
/// The app wrapper (OtOpcUaLdapAuthService) projects this onto the library's
/// at construction; see .
///
public sealed class LdapOptions
{
public const string SectionName = "Security:Ldap";
/// Gets or sets a value indicating whether LDAP authentication is enabled.
public bool Enabled { get; set; } = true;
/// Gets or sets the LDAP server hostname.
public string Server { get; set; } = "localhost";
/// Gets or sets the LDAP server port.
public int Port { get; set; } = 3893;
///
/// Transport security for the LDAP connection — (implicit
/// TLS), (upgrade), or
/// (plaintext, dev/test only — requires ). Replaces the former
/// UseTls bool: true→,
/// false→.
///
public LdapTransport Transport { get; set; } = LdapTransport.None;
/// Dev-only escape hatch — must be false in production. Maps to the shared
/// library's (renamed from AllowInsecureLdap).
public bool AllowInsecure { get; set; }
///
/// Dev-only stub: when true, bypasses the real LDAP
/// bind and accepts any non-empty username/password, returning a single Administrator role
/// so the operator can navigate the full Admin UI. MUST be false in production.
///
public bool DevStubMode { get; set; }
/// Gets or sets the LDAP search base DN.
public string SearchBase { get; set; } = "dc=zb,dc=local";
///
/// Service-account DN used for search-then-bind in
/// mode. There is no direct-bind fallback: when empty, the shared library binds with an empty
/// DN (an anonymous bind), which works against GLAuth's anonymous search but not against
/// Active Directory. Not required at startup (DEV boots on the empty default — lmxopcua#913);
/// unused in mode.
///
public string ServiceAccountDn { get; set; } = string.Empty;
/// Gets or sets the service account password for LDAP authentication.
public string ServiceAccountPassword { get; set; } = string.Empty;
/// Gets or sets the LDAP attribute name for user display name.
public string DisplayNameAttribute { get; set; } = "cn";
/// Gets or sets the LDAP attribute name for group membership.
public string GroupAttribute { get; set; } = "memberOf";
///
/// Attribute the service-account search matches the login name against to resolve the
/// user's DN. cn for GLAuth (the dev default); set sAMAccountName for
/// Active Directory.
///
public string UserNameAttribute { get; set; } = "cn";
///
/// Maps LDAP group name → Admin role. Group match is case-insensitive. A user gets every
/// role whose source group is in their membership list. Values are the canonical control-plane
/// roles. Example dev mapping:
/// "ReadOnly":"Viewer","ReadWrite":"Designer","AlarmAck":"Administrator"
///
public Dictionary GroupToRole { get; set; } = new(StringComparer.OrdinalIgnoreCase);
///
/// Bounds (in milliseconds) the shared-library directory client's socket connect AND each
/// per-operation search inside a single bind flow. Projected into the library's
/// via . The
/// default (10000) matches the library default, so behaviour is unchanged unless configured.
///
///
/// This is now the ONLY directory-outage bound. Its former backstops — AuthTimeoutMs,
/// MaxConcurrentAuthentications, OutageFailureThreshold and
/// OutageCooldownSeconds — were removed with LdapOpcUaUserAuthenticator
/// (see the note on this type). They existed because an LDAP bind ran inline on OPC UA session
/// activation, under a SessionManager-wide lock; nothing on the remaining AdminUI login path has
/// that property, so they had no consumer left. Setting any of them in configuration is now
/// silently ignored rather than an error, which is how options binding treats unknown keys.
///
public int ConnectionTimeoutMs { get; set; } = 10_000;
///
/// Ordered fallback LDAP endpoints (e.g. a second domain controller), tried when the primary
/// fails system-side (connect/TLS, service-account bind, or search). Each entry is
/// "host" (adopts ) or "host:port"; validated at startup.
/// Empty (the default) disables failover. Endpoint preference is sticky. Projected into the
/// library's .
///
public List FallbackServers { get; set; } = [];
///
/// Selects the authentication flow: (default —
/// bind as the service account, search for the user, re-bind as the user) or
/// (bind as the signing-in user via
/// ; no service account). Projected into the library's
/// .
///
public LdapBindMode BindMode { get; set; } = LdapBindMode.ServiceAccount;
///
/// only: the simple-bind identity with {0} replaced by
/// the normalised login name — prefer the down-level form DOMAIN\{0}. Must contain
/// {0} exactly once when is
/// (validated at startup).
///
public string UserBindTemplate { get; set; } = string.Empty;
///
/// only: when true (default) a login typed as
/// DOMAIN\name or name@suffix is reduced to name; when false such
/// input is refused. Has no effect in mode — there,
/// users type the bare login name (sAMAccountName on AD).
///
public bool AcceptDomainQualifiedNames { get; set; } = true;
///
/// Projects the wire fields onto the shared ZB.MOM.WW.Auth.Ldap
/// the directory client consumes. Every settable library property
/// must be mapped here or allow-listed in LdapOptionsProjectionCompletenessTests — a
/// dropped property (as FallbackServers/BindMode once were, lmxopcua#913) is
/// silently ignored config. App-only concerns
/// (, ) have no library counterpart and are
/// handled by the app wrapper around the library service; is carried
/// through so the library's own feature gate stays consistent with the app master switch.
///
/// The projected for the shared LDAP client library.
public LibLdapOptions ToLibraryOptions() => new()
{
Enabled = Enabled,
Server = Server,
Port = Port,
Transport = Transport,
AllowInsecure = AllowInsecure,
SearchBase = SearchBase,
ServiceAccountDn = ServiceAccountDn,
ServiceAccountPassword = ServiceAccountPassword,
UserNameAttribute = UserNameAttribute,
DisplayNameAttribute = DisplayNameAttribute,
GroupAttribute = GroupAttribute,
ConnectionTimeoutMs = ConnectionTimeoutMs,
FallbackServers = [.. FallbackServers],
BindMode = BindMode,
UserBindTemplate = UserBindTemplate,
AcceptDomainQualifiedNames = AcceptDomainQualifiedNames,
// ServerCertificateValidationCallback is intentionally NOT mapped: it is a code-only seam
// in the library (not config-bindable). The completeness test allow-lists it.
};
}