Class TokenAuthenticatorCore<TConnection>
- Namespace
- FishMMO.Auth.Implementation
- Assembly
- FishMMO-ServerAuth.dll
Engine-independent token-based authenticator core for World/Scene servers. Extends BaseAuthenticatorCore<TConnection> with a bounded-channel token auth worker that decrypts, verifies, and revocation-checks client-supplied auth tokens issued by the LoginServer.
Subclasses supply transport-specific callbacks (broadcast, database validation).
public abstract class TokenAuthenticatorCore<TConnection> : BaseAuthenticatorCore<TConnection>
Type Parameters
TConnectionThe type representing a network connection.
- Inheritance
-
BaseAuthenticatorCore<TConnection>TokenAuthenticatorCore<TConnection>
- Inherited Members
Constructors
TokenAuthenticatorCore(ITokenAccountManager<TConnection>)
Initializes the token authenticator core.
protected TokenAuthenticatorCore(ITokenAccountManager<TConnection> accountManager)
Parameters
accountManagerITokenAccountManager<TConnection>Token account manager instance.
Properties
IsWorkerIdle
Returns true when no async worker operations are in-flight. Subclasses with bounded-channel workers should override to check channel emptiness. Default returns true.
public override bool IsWorkerIdle { get; }
Property Value
RequiresRealIp
Whether this authenticator requires the auth token to carry a verified real IP.
protected virtual bool RequiresRealIp { get; }
Property Value
Remarks
True by default, because that is the setting that cannot be wrong by accident:
behind an L4 proxy GetConnectionAddress returns the proxy's loopback for
every client, so the token-embedded IP is the only trustworthy one and the handshake
rate limiter has nothing else to key off.
Override to false only where clients reach this authenticator directly. The IP is put into the token by whoever issued it, and an issuer that never saw a real IP cannot supply one — so in a deployment without the proxy this requirement rejects tokens that are otherwise entirely valid, and no player can enter the world.
TokenChannelCapacity
Default maximum pending token auth requests in the bounded channel. Configurable via .cfg AuthTokenChannelCapacity.
public int TokenChannelCapacity { get; set; }
Property Value
TokenWorkerCount
Default number of concurrent token auth worker tasks. Configurable via .cfg AuthTokenWorkerCount.
public int TokenWorkerCount { get; set; }
Property Value
Methods
CheckTokenRevocationAsync(string)
Checks whether a token has been revoked, using its SHA-256 hex hash.
protected abstract Task<bool> CheckTokenRevocationAsync(string tokenHash)
Parameters
tokenHashstringSHA-256 hex hash of the raw token.
Returns
FetchSigningKeyAsync(long, long)
Fetches the HMAC signing key for the given login server ID from the database. Returns null if the key is not found, too short, or a database error occurred; the caller will substitute a random dummy key for timing equalization when null is returned.
protected abstract Task<byte[]> FetchSigningKeyAsync(long loginServerId, long signingKeyId)
Parameters
Returns
InitializeWorkersCore(CancellationToken)
Subclass-specific worker initialization: create channels and start worker tasks. Called after the base generates the cookie key.
protected override void InitializeWorkersCore(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenToken for signalling worker shutdown.
IsConnectionActive(TConnection)
Returns whether the connection is currently active (connected and not disposed).
protected abstract bool IsConnectionActive(TConnection conn)
Parameters
connTConnectionThe connection to check.
Returns
- bool
trueif the connection is still active; otherwise,false.
OnAuthenticationResult(TConnection, bool)
Called when authentication completes (success or failure). Implementations should call their engine's authenticate/reject methods here.
protected abstract void OnAuthenticationResult(TConnection conn, bool authenticated)
Parameters
connTConnectionThe authenticated (or rejected) connection.
authenticatedboolTrue if authentication succeeded.
OnTokenAuthReceived(TConnection, byte[], uint)
Gate for an incoming token auth broadcast. Validates connection state and enqueues for async processing. No decryption or database work occurs here.
public void OnTokenAuthReceived(TConnection conn, byte[] encryptedToken, uint seq)
Parameters
connTConnectionThe network connection.
encryptedTokenbyte[]AES-GCM encrypted auth token from client.
sequintBroadcast sequence number.
OnTokenAuthSuccessAsync(TConnection, string, AccessLevel, long)
Invoked once after a successful token authentication (and the resulting auth-result broadcast). Override to mint and push a fresh auth token to the client over the existing AES-GCM session channel, extending the effective session lifetime past the original LoginServer-issued token's expiration.
Exceptions thrown here are caught by the caller and logged as warnings; they do not invalidate the just-completed authentication.
protected virtual Task OnTokenAuthSuccessAsync(TConnection conn, string accountName, AccessLevel accessLevel, long loginServerId)
Parameters
connTConnectionThe newly authenticated connection.
accountNamestringAccount name extracted from the verified token.
accessLevelAccessLevelAccess level extracted from the verified token.
loginServerIdlongLogin server ID extracted from the verified token (used to look up the HMAC signing key for the renewal).
Returns
ShutdownWorkersCore()
Subclass-specific worker shutdown: complete channel writers, null channel references, and clear subclass-specific state. Called BEFORE the base zeroes the cookie key.
protected override void ShutdownWorkersCore()
StoreClientRealIp(TConnection, string)
Stores the real client IP recovered from a verified auth token (v4+). Override in server-specific subclasses to write to ConnectionIpCache.
protected virtual void StoreClientRealIp(TConnection conn, string realIp)
Parameters
connTConnectionrealIpstring
TryLoginAsync(ClientAuthenticationResult, string)
Attempts to complete login for a newly authenticated token connection. Override to apply server-type-specific login logic.
protected virtual Task<ClientAuthenticationResult> TryLoginAsync(ClientAuthenticationResult defaultResult, string username)
Parameters
defaultResultClientAuthenticationResultThe result to return if no override logic modifies it.
usernamestringAccount name being authenticated.
Returns
- Task<ClientAuthenticationResult>
The final ClientAuthenticationResult to send to the client.