- Introduced LtdFeatureTests to validate BACnet and OPC UA point updates, trend logging, and alarm rule functionality. - Added OpcUaClientTests to ensure proper OPC UA client configuration, point value synchronization, and session management. - Created OpcUaFeatureTests to test OPC UA server capabilities, including browsing, reading, writing, and subscription notifications. - Implemented native_bacnet_test.py for isolated BACnet wire testing, covering discovery, read/write operations, and command processing. - Added ui-theme-tabs.test.cjs to verify theme switching, tab interactions, and protocol form accessibility in the UI.
72 lines
2.4 KiB
C#
72 lines
2.4 KiB
C#
using System.Collections.Concurrent;
|
|
using System.Security.Cryptography;
|
|
using Gateway.Api.Domain;
|
|
|
|
namespace Gateway.Api.Services;
|
|
|
|
public sealed class SessionService(GatewayStore store)
|
|
{
|
|
private readonly ConcurrentDictionary<string, SessionEntry> _sessions = new(StringComparer.Ordinal);
|
|
|
|
public string Create(AuthenticatedGatewayUser user)
|
|
{
|
|
PruneExpired();
|
|
var token = Convert.ToHexString(RandomNumberGenerator.GetBytes(32));
|
|
_sessions[token] = new SessionEntry(user, DateTimeOffset.UtcNow.AddHours(12));
|
|
return token;
|
|
}
|
|
|
|
public bool IsValid(string? authorizationHeader)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(authorizationHeader) || !authorizationHeader.StartsWith("Bearer ", StringComparison.Ordinal))
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return GetUser(authorizationHeader) is not null;
|
|
}
|
|
|
|
public AuthenticatedGatewayUser? GetUser(string? authorizationHeader)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(authorizationHeader) || !authorizationHeader.StartsWith("Bearer ", StringComparison.Ordinal))
|
|
{
|
|
return null;
|
|
}
|
|
|
|
var token = authorizationHeader[7..];
|
|
if (!_sessions.TryGetValue(token, out var entry) || entry.ExpiresAtUtc <= DateTimeOffset.UtcNow) return null;
|
|
var current = store.GetAuthenticatedUser(entry.User.User.Id);
|
|
if (current is null || current.User.UpdatedAtUtc != entry.User.User.UpdatedAtUtc)
|
|
{
|
|
_sessions.TryRemove(token, out _);
|
|
return null;
|
|
}
|
|
return current;
|
|
}
|
|
|
|
public bool HasPermission(string? authorizationHeader, string permission) =>
|
|
GetUser(authorizationHeader)?.Permissions.Contains(permission, StringComparer.Ordinal) == true;
|
|
|
|
public void Remove(string? authorizationHeader)
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(authorizationHeader) && authorizationHeader.StartsWith("Bearer ", StringComparison.Ordinal))
|
|
{
|
|
_sessions.TryRemove(authorizationHeader[7..], out _);
|
|
}
|
|
}
|
|
|
|
private void PruneExpired()
|
|
{
|
|
var now = DateTimeOffset.UtcNow;
|
|
foreach (var (token, entry) in _sessions)
|
|
{
|
|
if (entry.ExpiresAtUtc <= now)
|
|
{
|
|
_sessions.TryRemove(token, out _);
|
|
}
|
|
}
|
|
}
|
|
|
|
private sealed record SessionEntry(AuthenticatedGatewayUser User, DateTimeOffset ExpiresAtUtc);
|
|
}
|