81cf44cb90
AdminSessionRevalidator re-mapped the groups frozen into the cookie at sign-in,
so removing a user from a directory group changed nothing until
MaxSessionMinutes ended the session.
Bump ZB.MOM.WW.Auth.{Abstractions,Ldap,AspNetCore} 0.4.1 -> 0.5.0 (additive
ILdapGroupLookup; 0.4.2-0.4.4 changes only affect BindMode=User / API-key SQL
stores, unused here; solution builds clean). OtOpcUaLdapAuthService now
implements ILdapGroupLookup with the same Enabled / DevStubMode / insecure-
transport rules as sign-in, registered as the same singleton (a stub
ILdapAuthService gets a NotSupported lookup).
At each re-validation interval the revalidator looks the user up: Found ->
fresh groups replace the session's otopcua:group claims and are mapped;
UserNotFound / UserDisabled / AmbiguousUser / Found-with-no-groups -> reject;
DirectoryUnavailable (or a lookup fault) -> fail-soft on the last known groups
(still re-mapped), bounded by MaxSessionMinutes; NotSupported / ProviderDisabled
-> previous behaviour. Never queried under DisableLogin.
Tests (directory rows failed first): group removal / addition picked up after
the window, unchanged membership kept (positive control), four end-the-session
statuses, outage keeps the session until the absolute cap, DisableLogin issues
no lookup (control: one lookup otherwise); wrapper lookup rules + DI
registration. docs/security.md drops the 'no password-less lookup' claim.
Co-Authored-By: Claude <noreply@anthropic.com>
127 lines
5.7 KiB
C#
127 lines
5.7 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Logging.Abstractions;
|
|
using Shouldly;
|
|
using Xunit;
|
|
using ZB.MOM.WW.Auth.Abstractions.Ldap;
|
|
using ZB.MOM.WW.OtOpcUa.Configuration;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AppLdapOptions = ZB.MOM.WW.OtOpcUa.Security.Ldap.LdapOptions;
|
|
using AppLdapAuthService = ZB.MOM.WW.OtOpcUa.Security.Ldap.OtOpcUaLdapAuthService;
|
|
using LibILdapAuthService = ZB.MOM.WW.Auth.Abstractions.Ldap.ILdapAuthService;
|
|
using LibLdapAuthResult = ZB.MOM.WW.Auth.Abstractions.Ldap.LdapAuthResult;
|
|
|
|
namespace ZB.MOM.WW.OtOpcUa.Security.Tests;
|
|
|
|
/// <summary>
|
|
/// lmxopcua#975 (review 2026-12-02): the app's LDAP wrapper exposes the shared library's 0.5.0 passwordless
|
|
/// <see cref="ILdapGroupLookup"/> so a live AdminUI session can re-read its directory groups. The wrapper's own
|
|
/// concerns apply to the lookup exactly as to sign-in: <c>Enabled</c> is the master switch, <c>DevStubMode</c>
|
|
/// answers the stub's group set without touching the network, and a plaintext transport without
|
|
/// <c>AllowInsecure</c> is refused.
|
|
/// </summary>
|
|
public sealed class OtOpcUaLdapGroupLookupTests
|
|
{
|
|
private static ILdapGroupLookup Build(AppLdapOptions options, LibILdapAuthService inner) =>
|
|
new AppLdapAuthService(options, inner, NullLogger<AppLdapAuthService>.Instance);
|
|
|
|
[Fact]
|
|
public async Task Real_path_delegates_to_the_library_lookup()
|
|
{
|
|
var inner = new LookupLib(LdapGroupLookupResult.Found("alice", "Alice", ["ReadOnly"]));
|
|
var sut = Build(new AppLdapOptions { Enabled = true, Transport = LdapTransport.Ldaps }, inner);
|
|
|
|
var result = await sut.GetUserGroupsAsync("alice", CancellationToken.None);
|
|
|
|
result.Status.ShouldBe(LdapGroupLookupStatus.Found);
|
|
result.Groups.ShouldBe(["ReadOnly"]);
|
|
inner.LookedUp.ShouldBe("alice");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Disabled_provider_answers_ProviderDisabled_without_the_library()
|
|
{
|
|
var inner = new LookupLib(LdapGroupLookupResult.Found("alice", "Alice", ["ReadOnly"]));
|
|
var sut = Build(new AppLdapOptions { Enabled = false, DevStubMode = true }, inner);
|
|
|
|
(await sut.GetUserGroupsAsync("alice", CancellationToken.None)).Status.ShouldBe(LdapGroupLookupStatus.ProviderDisabled);
|
|
inner.LookedUp.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task DevStub_answers_the_stub_group_without_the_library()
|
|
{
|
|
var inner = new LookupLib(LdapGroupLookupResult.Of(LdapGroupLookupStatus.UserNotFound));
|
|
var sut = Build(new AppLdapOptions { Enabled = true, DevStubMode = true }, inner);
|
|
|
|
var result = await sut.GetUserGroupsAsync("anyone", CancellationToken.None);
|
|
|
|
result.Status.ShouldBe(LdapGroupLookupStatus.Found);
|
|
result.Groups.ShouldBe(["dev"]);
|
|
inner.LookedUp.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Insecure_transport_without_opt_in_is_NotSupported()
|
|
{
|
|
var inner = new LookupLib(LdapGroupLookupResult.Found("alice", "Alice", ["ReadOnly"]));
|
|
var sut = Build(new AppLdapOptions { Enabled = true, Transport = LdapTransport.None, AllowInsecure = false }, inner);
|
|
|
|
(await sut.GetUserGroupsAsync("alice", CancellationToken.None)).Status.ShouldBe(LdapGroupLookupStatus.NotSupported);
|
|
inner.LookedUp.ShouldBeNull();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task A_library_without_lookup_answers_NotSupported()
|
|
{
|
|
var sut = Build(new AppLdapOptions { Enabled = true, Transport = LdapTransport.Ldaps }, new AuthOnlyLib());
|
|
|
|
(await sut.GetUserGroupsAsync("alice", CancellationToken.None)).Status.ShouldBe(LdapGroupLookupStatus.NotSupported);
|
|
}
|
|
|
|
/// <summary>AddOtOpcUaAuth registers the lookup as the app's LDAP service — or NotSupported for a stub service.</summary>
|
|
[Fact]
|
|
public async Task AddOtOpcUaAuth_registers_the_group_lookup()
|
|
{
|
|
var services = new ServiceCollection();
|
|
services.AddLogging();
|
|
services.AddDbContext<OtOpcUaConfigDbContext>(o => o.UseInMemoryDatabase($"lookup-{Guid.NewGuid():N}"));
|
|
services.AddOtOpcUaAuth(new ConfigurationBuilder().Build());
|
|
using (var provider = services.BuildServiceProvider())
|
|
provider.GetRequiredService<ILdapGroupLookup>().ShouldBeOfType<AppLdapAuthService>();
|
|
|
|
services.AddSingleton<ZB.MOM.WW.OtOpcUa.Security.Ldap.ILdapAuthService, AuthOnlyAppService>();
|
|
using (var provider = services.BuildServiceProvider())
|
|
{
|
|
var lookup = provider.GetRequiredService<ILdapGroupLookup>();
|
|
(await lookup.GetUserGroupsAsync("x", CancellationToken.None)).Status.ShouldBe(LdapGroupLookupStatus.NotSupported);
|
|
}
|
|
}
|
|
|
|
private sealed class LookupLib(LdapGroupLookupResult result) : LibILdapAuthService, ILdapGroupLookup
|
|
{
|
|
public string? LookedUp { get; private set; }
|
|
|
|
public Task<LibLdapAuthResult> AuthenticateAsync(string username, string password, CancellationToken ct) =>
|
|
throw new NotSupportedException();
|
|
|
|
public Task<LdapGroupLookupResult> GetUserGroupsAsync(string username, CancellationToken ct)
|
|
{
|
|
LookedUp = username;
|
|
return Task.FromResult(result);
|
|
}
|
|
}
|
|
|
|
private sealed class AuthOnlyLib : LibILdapAuthService
|
|
{
|
|
public Task<LibLdapAuthResult> AuthenticateAsync(string username, string password, CancellationToken ct) =>
|
|
throw new NotSupportedException();
|
|
}
|
|
|
|
private sealed class AuthOnlyAppService : ZB.MOM.WW.OtOpcUa.Security.Ldap.ILdapAuthService
|
|
{
|
|
public Task<ZB.MOM.WW.OtOpcUa.Security.Ldap.LdapAuthResult> AuthenticateAsync(
|
|
string username, string password, CancellationToken ct = default) => throw new NotSupportedException();
|
|
}
|
|
}
|