105 lines
4.4 KiB
C#
105 lines
4.4 KiB
C#
using System.Runtime.CompilerServices;
|
|
using System.Security.Claims;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using ZB.MOM.WW.MxGateway.Server.Dashboard;
|
|
using ZB.MOM.WW.MxGateway.Server.Security.Authentication;
|
|
using ZB.MOM.WW.MxGateway.Server.Security.Authorization;
|
|
internal sealed class StubSnapshotService : IDashboardSnapshotService
|
|
{
|
|
/// <inheritdoc />
|
|
public DashboardSnapshot GetSnapshot() => new(
|
|
GeneratedAt: DateTimeOffset.UnixEpoch,
|
|
GatewayStartedAt: DateTimeOffset.UnixEpoch,
|
|
GatewayUptime: TimeSpan.Zero,
|
|
GatewayStatus: "Healthy",
|
|
GatewayVersion: "test",
|
|
Sessions: [],
|
|
Workers: [],
|
|
Metrics: [],
|
|
Faults: [],
|
|
ApiKeys:
|
|
[
|
|
new DashboardApiKeySummary(
|
|
KeyId: "synthetic-tagged",
|
|
DisplayName: "Tags only",
|
|
Scopes: new HashSet<string>(StringComparer.Ordinal),
|
|
Constraints: ApiKeyConstraints.Empty with { DashboardTags = ["team-a", "team-b"] },
|
|
CreatedUtc: DateTimeOffset.UnixEpoch,
|
|
LastUsedUtc: null,
|
|
RevokedUtc: null),
|
|
new DashboardApiKeySummary(
|
|
KeyId: "synthetic-plain",
|
|
DisplayName: "Unconstrained",
|
|
Scopes: new HashSet<string>(StringComparer.Ordinal),
|
|
Constraints: ApiKeyConstraints.Empty,
|
|
CreatedUtc: DateTimeOffset.UnixEpoch,
|
|
LastUsedUtc: null,
|
|
RevokedUtc: null),
|
|
],
|
|
Configuration: null!,
|
|
Galaxy: null!);
|
|
|
|
/// <inheritdoc />
|
|
public IAsyncEnumerable<DashboardSnapshot> WatchSnapshotsAsync(CancellationToken cancellationToken) =>
|
|
new IdleSnapshotFeed().WatchAsync(cancellationToken);
|
|
}
|
|
|
|
// Parks until the page is disposed, so the base page's watch loop neither spins nor pushes a
|
|
// second snapshot mid-render.
|
|
internal sealed class IdleSnapshotFeed : IDashboardSnapshotFeed
|
|
{
|
|
/// <inheritdoc />
|
|
public async IAsyncEnumerable<DashboardSnapshot> WatchAsync(
|
|
[EnumeratorCancellation] CancellationToken cancellationToken)
|
|
{
|
|
await Task.Delay(Timeout.Infinite, cancellationToken).ConfigureAwait(false);
|
|
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
// Grants management so the create dialog is reachable; no test here exercises a mutation.
|
|
internal sealed class ManagingApiKeyService : IDashboardApiKeyManagementService
|
|
{
|
|
/// <inheritdoc />
|
|
public bool CanManage(ClaimsPrincipal user) => true;
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardApiKeyManagementResult> CreateAsync(
|
|
ClaimsPrincipal user,
|
|
DashboardApiKeyManagementRequest request,
|
|
CancellationToken cancellationToken) =>
|
|
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardApiKeyManagementResult> RevokeAsync(
|
|
ClaimsPrincipal user,
|
|
string keyId,
|
|
CancellationToken cancellationToken) =>
|
|
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardApiKeyManagementResult> RotateAsync(
|
|
ClaimsPrincipal user,
|
|
string keyId,
|
|
CancellationToken cancellationToken) =>
|
|
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
|
|
|
|
/// <inheritdoc />
|
|
public Task<DashboardApiKeyManagementResult> DeleteAsync(
|
|
ClaimsPrincipal user,
|
|
string keyId,
|
|
CancellationToken cancellationToken) =>
|
|
Task.FromResult(DashboardApiKeyManagementResult.Fail("not exercised"));
|
|
}
|
|
|
|
internal sealed class StubAuthenticationStateProvider : AuthenticationStateProvider
|
|
{
|
|
/// <inheritdoc />
|
|
public override Task<AuthenticationState> GetAuthenticationStateAsync() =>
|
|
Task.FromResult(new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(
|
|
[new Claim(ClaimTypes.Name, "admin-user"), new Claim(ClaimTypes.Role, DashboardRoles.Admin)],
|
|
authenticationType: "test",
|
|
nameType: ClaimTypes.Name,
|
|
roleType: ClaimTypes.Role))));
|
|
} |