Class TokenService
- Namespace
- FishMMO.Auth.Implementation
- Assembly
- FishMMO-AuthShared.dll
Transport-agnostic service for authentication token operations. Provides token generation, encryption, decryption, and verification for both server-side issuance and client/server token authentication.
public static class TokenService
- Inheritance
-
TokenService
- Inherited Members
Methods
BuildToken(string, long, long, DateTime, byte[], AccessLevel, string?)
Builds a raw HMAC-signed auth token for the specified account. This is a thin wrapper around BuildAuthToken(string, long, long, DateTime, byte[], AccessLevel, string?) that validates the signing key before use.
public static byte[]? BuildToken(string username, long loginServerId, long signingKeyId, DateTime expiresUtc, byte[] signingKey, AccessLevel accessLevel, string? realIp = null)
Parameters
usernamestringAccount name to embed in the token.
loginServerIdlongID of the issuing login server.
signingKeyIdlongDatabase ID of the signing key used for this token.
expiresUtcDateTimeToken expiration timestamp (UTC).
signingKeybyte[]HMAC-SHA256 signing key. Must be at least HmacKeyLength bytes.
accessLevelAccessLevelAccount access level to embed.
realIpstring
Returns
- byte[]
Raw signed token bytes, or
nullif the signing key is invalid.
ClientEncryptToken(byte[], byte[], GcmNonceContext, ushort, out byte[], out uint)
Encrypts a stored auth token for transmission to a World/Scene server using AES-GCM with TokenAuth AAD type.
public static void ClientEncryptToken(byte[] rawToken, byte[] clientToServerKey, CryptoHelper.GcmNonceContext sendNonceCtx, ushort agreedVersion, out byte[] encryptedToken, out uint seq)
Parameters
rawTokenbyte[]Raw auth token bytes to encrypt.
clientToServerKeybyte[]AES-256 key for client→server direction.
sendNonceCtxCryptoHelper.GcmNonceContextClient's send nonce context.
agreedVersionushortNegotiated protocol version.
encryptedTokenbyte[]Encrypted token output.
sequintSequence number used (for broadcast).
EncryptTokenForSend(byte[], ConnectionEncryptionData)
Encrypts a raw token for transmission using AES-GCM with TokenTransfer AAD type.
public static byte[] EncryptTokenForSend(byte[] rawToken, ConnectionEncryptionData encryptionData)
Parameters
rawTokenbyte[]Raw token bytes.
encryptionDataConnectionEncryptionDataConnection encryption state.
Returns
- byte[]
AES-GCM encrypted token bytes.
GenerateAndEncryptToken(ConnectionEncryptionData, string, long, long, int, byte[], AccessLevel, out byte[]?, string?)
Builds, signs, and encrypts an auth token for transmission to the client over an AES-GCM encrypted channel. Used by login servers after successful SRP authentication.
public static byte[]? GenerateAndEncryptToken(ConnectionEncryptionData encryptionData, string username, long loginServerId, long signingKeyId, int tokenExpirationMinutes, byte[] signingKey, AccessLevel accessLevel, out byte[]? rawTokenForHashing, string? realIp = null)
Parameters
encryptionDataConnectionEncryptionDataConnection encryption state (keys, nonces, version).
usernamestringAccount name to embed in the token.
loginServerIdlongID of the issuing login server.
signingKeyIdlongDatabase ID of the signing key used for this token.
tokenExpirationMinutesintToken validity duration in minutes.
signingKeybyte[]HMAC-SHA256 signing key.
accessLevelAccessLevelAccount access level to embed.
rawTokenForHashingbyte[]Raw token bytes (caller must hash for DB storage, then zero).
realIpstring
Returns
- byte[]
Encrypted token bytes for transmission, or
nullif generation failed.
HashToken(byte[])
Computes the SHA-256 hex hash of a raw token for revocation tracking.
public static string HashToken(byte[] rawToken)
Parameters
rawTokenbyte[]Raw token bytes to hash.
Returns
- string
Lowercase hex-encoded SHA-256 hash string.
TryDecryptAndPartialParse(byte[], ConnectionEncryptionData, uint, out byte[]?, out long, out long)
Decrypts a token from an AES-GCM encrypted payload and performs partial parsing to extract the login server ID for signing key lookup. Does NOT verify the HMAC. Call VerifyToken(byte[], byte[], bool, long, long) after obtaining the signing key.
public static bool TryDecryptAndPartialParse(byte[] encryptedToken, ConnectionEncryptionData encryptionData, uint seq, out byte[]? rawToken, out long loginServerId, out long signingKeyId)
Parameters
encryptedTokenbyte[]AES-GCM encrypted token bytes from client.
encryptionDataConnectionEncryptionDataConnection encryption state.
sequintBroadcast sequence number.
rawTokenbyte[]Decrypted raw token bytes (caller must zero after use).
loginServerIdlongExtracted login server ID for signing key ownership checks.
signingKeyIdlongExtracted signing-key ID for signing key lookup.
Returns
- bool
trueif decryption and partial parse succeeded.
Remarks
Pre-decryption size gate: No explicit ciphertext length check is performed here because DecryptAES(byte[], byte[], byte[], byte[]) enforces a hard cap of MaxAesCiphertextSize (64 KiB) and a minimum of AesGcmTagLengthBytes (16 B). Since GCM decryption is authenticated, oversized or malformed payloads are rejected before any plaintext is exposed, making an additional pre-check redundant.
VerifyToken(byte[], byte[], bool, long, long)
Verifies the HMAC on a raw token and parses all fields. Equalizes timing regardless of whether the signing key is real or dummy.
public static TokenService.TokenVerifyResult VerifyToken(byte[] rawToken, byte[] hmacKey, bool signingKeyFound, long preParseLoginServerId, long preParseSigningKeyId)
Parameters
rawTokenbyte[]Raw decrypted token bytes.
hmacKeybyte[]HMAC signing key. If the key was not found, pass a random dummy key of HmacKeyLength bytes to equalize timing.
signingKeyFoundboolWhether the signing key was actually found in the database.
preParseLoginServerIdlongLogin server ID from partial parse. Cross-checked against the HMAC-verified value.
preParseSigningKeyIdlongSigning-key ID from partial parse. Cross-checked against the HMAC-verified value.
Returns
- TokenService.TokenVerifyResult
Verification result with parsed token fields.