Class ConnectionTokenKeyService
Service for managing connection token verification keys. LoginServers use this to discover verification keys at startup and to periodically poll for new keys from new regions.
public sealed class ConnectionTokenKeyService : BaseService<ConnectionTokenKeyEntity>, IConnectionTokenKeyService
- Inheritance
-
ConnectionTokenKeyService
- Implements
- Inherited Members
Constructors
ConnectionTokenKeyService(INpgsqlDbContextFactory)
Initializes a new instance of ConnectionTokenKeyService.
public ConnectionTokenKeyService(INpgsqlDbContextFactory dbContextFactory)
Parameters
dbContextFactoryINpgsqlDbContextFactoryDbContext factory for creating contexts.
Exceptions
- ArgumentNullException
Thrown when dbContextFactory is null.
Methods
FetchAllActiveAsync(CancellationToken)
Fetches all active connection token keys.
Called periodically by game servers to refresh their in-memory key map.
Results are ordered by TimeCreated descending (newest first).
public Task<DatabaseResult<ConnectionTokenKeyData[]>> FetchAllActiveAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<ConnectionTokenKeyData[]>>
A DatabaseResult<T> containing an array of active key data.
FetchByKeyIdAsync(string, CancellationToken)
Fetches a single connection token key by its logical key identifier.
public Task<DatabaseResult<ConnectionTokenKeyData>> FetchByKeyIdAsync(string keyId, CancellationToken cancellationToken = default)
Parameters
keyIdstringThe logical key identifier to look up.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<ConnectionTokenKeyData>>
A DatabaseResult<T> containing the key data if found; otherwise a failure with ENTITY_NOT_FOUND.
GetConnectionTokenKeyMapAsync(CancellationToken)
Returns a dictionary mapping key ID to raw HMAC key bytes for all active keys. Each value is the base64-decoded HMAC key material. Convenience method for game servers that need the key material in byte form.
public Task<DatabaseResult<Dictionary<string, byte[]>>> GetConnectionTokenKeyMapAsync(CancellationToken cancellationToken = default)
Parameters
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<Dictionary<string, byte[]>>>
A DatabaseResult<T> containing the key ID to raw key bytes map.
UpsertAsync(string, byte[], CancellationToken)
Inserts or updates the active HMAC key for the given keyId.
On insert, a new row is created with IsActive = true.
On update (existing keyId), the key material is replaced,
the key is marked active, and DeactivatedAt is cleared.
public Task<DatabaseResult<ConnectionTokenKeyData>> UpsertAsync(string keyId, byte[] hmacKey, CancellationToken cancellationToken = default)
Parameters
keyIdstringLogical key identifier (e.g., region code). Must be non-empty and at most 255 characters.
hmacKeybyte[]HMAC-SHA256 key material. Must be at least 32 bytes.
cancellationTokenCancellationTokenCancellation token.
Returns
- Task<DatabaseResult<ConnectionTokenKeyData>>
A DatabaseResult<T> containing the upserted key data on success.