feat(auth): 0.5.0 passwordless group lookup ILdapGroupLookup (ScadaBridge#46)

ILdapGroupLookup.GetUserGroupsAsync(username, ct) re-reads a known user's
groups with the service account: same extraction, canonicalisation, endpoint
walk (FallbackServers), timeouts and TLS policy as AuthenticateAsync, via one
shared WalkEndpoints<T>. Explicit statuses: Found, UserNotFound, UserDisabled
(AD userAccountControl), AmbiguousUser, DirectoryUnavailable, NotSupported
(BindMode=User / no service account / foreign ILdapAuthService),
ProviderDisabled. AddZbLdapAuth registers it as the same singleton.

0.5.0 is the first published version carrying 0.4.2-0.4.4; scadaproj#51 was
live-verified against the NAM AD (24/24, identity check mutation-proven).

Claude-Session: https://claude.ai/code/session_01GT67ACmpMR7b9LGRPLijMi
This commit is contained in:
Joseph Doherty
2026-11-12 20:10:40 -05:00
parent 3ca7e1936f
commit f5ef0efedd
10 changed files with 828 additions and 41 deletions
@@ -116,3 +116,80 @@ public interface ILdapAuthService
/// </remarks>
Task<LdapAuthResult> AuthenticateAsync(string username, string password, CancellationToken ct);
}
/// <summary>Outcome of a passwordless <see cref="ILdapGroupLookup.GetUserGroupsAsync"/> call.</summary>
public enum LdapGroupLookupStatus
{
/// <summary>Exactly one entry matched; <see cref="LdapGroupLookupResult.Groups"/> is its group set (may be empty).</summary>
Found,
/// <summary>A healthy directory answered and no entry matched the name — the account was deleted or renamed.</summary>
UserNotFound,
/// <summary>Exactly one entry matched but the directory marks it disabled (AD <c>userAccountControl</c> ACCOUNTDISABLE).</summary>
UserDisabled,
/// <summary>Two or more entries matched; no group set can be attributed to the name.</summary>
AmbiguousUser,
/// <summary>
/// No endpoint gave an answer: connect/TLS, the service-account bind or the search failed on every
/// endpoint (primary and <see cref="LdapOptions.FallbackServers"/>). Says nothing about the user.
/// </summary>
DirectoryUnavailable,
/// <summary>
/// The configuration cannot search without the user's password: <see cref="LdapOptions.BindMode"/> is
/// <see cref="LdapBindMode.User"/> (there is no service account), no
/// <see cref="LdapOptions.ServiceAccountDn"/> is configured, or the registered provider does not implement lookup.
/// </summary>
NotSupported,
/// <summary><see cref="LdapOptions.Enabled"/> is <see langword="false"/>; the network was not touched.</summary>
ProviderDisabled,
}
/// <summary>
/// Result of a passwordless group lookup. <see cref="Username"/>, <see cref="DisplayName"/> and
/// <see cref="Groups"/> are populated only for <see cref="LdapGroupLookupStatus.Found"/>; for every other
/// status they are empty.
/// </summary>
public sealed record LdapGroupLookupResult(
LdapGroupLookupStatus Status,
string Username,
string DisplayName,
IReadOnlyList<string> Groups)
{
/// <summary><see langword="true"/> when <see cref="Status"/> is <see cref="LdapGroupLookupStatus.Found"/>.</summary>
public bool IsFound => Status == LdapGroupLookupStatus.Found;
public static LdapGroupLookupResult Found(string username, string displayName, IReadOnlyList<string> groups)
=> new(LdapGroupLookupStatus.Found, username, displayName, groups);
/// <summary>Any non-<see cref="LdapGroupLookupStatus.Found"/> outcome, with empty identity fields.</summary>
public static LdapGroupLookupResult Of(LdapGroupLookupStatus status)
=> status == LdapGroupLookupStatus.Found
? throw new ArgumentException("Use Found(...) for a successful lookup.", nameof(status))
: new(status, "", "", Array.Empty<string>());
}
/// <summary>
/// Passwordless directory group lookup (since 0.5.0): re-reads a known user's group membership without
/// their credentials, e.g. to refresh an active session's roles when the directory changes.
/// </summary>
public interface ILdapGroupLookup
{
/// <summary>
/// Binds as the configured service account, finds <paramref name="username"/> by
/// <see cref="LdapOptions.UserNameAttribute"/> under <see cref="LdapOptions.SearchBase"/>, and returns the
/// same group set (and canonical username / display name) <see cref="ILdapAuthService.AuthenticateAsync"/>
/// would return for that user in <see cref="LdapBindMode.ServiceAccount"/> mode.
/// </summary>
/// <remarks>
/// Never throws except for cancellation observed at entry; every fault maps to a
/// <see cref="LdapGroupLookupStatus"/>. Endpoint failover, timeouts and TLS policy are the same as
/// <see cref="ILdapAuthService.AuthenticateAsync"/>. Unlike authentication, a <see cref="LdapGroupLookupStatus.Found"/>
/// result may carry zero groups (sign-in would refuse such a user); the caller decides what that means.
/// </remarks>
Task<LdapGroupLookupResult> GetUserGroupsAsync(string username, CancellationToken ct);
}