AccountManager - jimdroberts/FishMMO GitHub Wiki
AccountManager
maps connections to usernames and usernames to connections. This static helper class manages account and encryption data for server connections in FishMMO. Adding a connection should only be done in your authenticator.
-
public readonly static Dictionary<NetworkConnection, ConnectionEncryptionData> ConnectionEncryptionDatas
Maps network connections to their encryption data.
-
public readonly static Dictionary<NetworkConnection, string> ConnectionAccounts
Maps network connections to account names.
-
public readonly static Dictionary<string, NetworkConnection> AccountConnections
Maps account names to network connections.
-
public readonly static Dictionary<NetworkConnection, AccountData> ConnectionAccountData
Maps network connections to account data.
-
public static void AddConnectionEncryptionData(NetworkConnection connection, byte[] publicKey)
Adds encryption data for a connection using the provided public key. Parameters: - NetworkConnection connection: The network connection. - byte[] publicKey: The public key for encryption.
-
public static bool GetConnectionEncryptionData(NetworkConnection connection, out ConnectionEncryptionData encryptionData)
Retrieves encryption data for a connection. Parameters: - NetworkConnection connection: The network connection. - out ConnectionEncryptionData encryptionData: The retrieved encryption data. Returns: True if found, false otherwise.
-
public static void AddConnectionAccount(NetworkConnection connection, string accountName, string publicClientEphemeral, string salt, string verifier, AccessLevel accessLevel)
Adds account data for a connection, including SRP data and access level. Parameters: - NetworkConnection connection: The network connection. - string accountName: The account name. - string publicClientEphemeral: The client's public ephemeral value. - string salt: The salt for password hashing. - string verifier: The verifier for password validation. - AccessLevel accessLevel: The access level for the account.
-
public static void RemoveConnectionAccount(NetworkConnection connection)
Removes all account and encryption data for a connection. Parameters: - NetworkConnection connection: The network connection.
-
public static void RemoveAccountConnection(string accountName)
Removes all account and encryption data for an account name. Parameters: - string accountName: The account name.
-
public static bool GetConnectionAccountData(NetworkConnection connection, out AccountData accountData)
Retrieves account data for a connection. Parameters: - NetworkConnection connection: The network connection. - out AccountData accountData: The retrieved account data. Returns: True if found, false otherwise.
-
public static bool GetAccountNameByConnection(NetworkConnection connection, out string accountName)
Retrieves the account name for a connection. Parameters: - NetworkConnection connection: The network connection. - out string accountName: The retrieved account name. Returns: True if found, false otherwise.
-
public static bool GetConnectionByAccountName(string accountName, out NetworkConnection connection)
Retrieves the network connection for an account name. Parameters: - string accountName: The account name. - out NetworkConnection connection: The retrieved network connection. Returns: True if found, false otherwise.
-
public static bool TryUpdateSrpState(NetworkConnection connection, SrpState requiredState, SrpState nextState)
Attempts to update the SRP state for a connection if the current state matches the required state. Parameters: - NetworkConnection connection: The network connection. - SrpState requiredState: The required current SRP state. - SrpState nextState: The next SRP state to set. Returns: True if the state was updated, false otherwise.
-
public static bool TryUpdateSrpState(NetworkConnection connection, SrpState requiredState, SrpState nextState, Func<AccountData, bool> onSuccess)
Attempts to update the SRP state for a connection and invokes a callback on success. Parameters: - NetworkConnection connection: The network connection. - SrpState requiredState: The required current SRP state. - SrpState nextState: The next SRP state to set. - Func<AccountData, bool> onSuccess: Callback to invoke on success. Returns: True if the state was updated and callback succeeded, false otherwise.
- Use
AddConnectionEncryptionData
andAddConnectionAccount
to register new connections and account data during authentication. - Use the provided methods to retrieve, update, or remove account and encryption data as needed.
- Use
TryUpdateSrpState
to manage SRP authentication state transitions for connections.
// Example 1: Adding a new connection and account
AccountManager.AddConnectionEncryptionData(connection, publicKey);
AccountManager.AddConnectionAccount(connection, accountName, clientEphemeral, salt, verifier, AccessLevel.Player);
// Example 2: Updating SRP state
AccountManager.TryUpdateSrpState(connection, SrpState.SrpVerify, SrpState.SrpProof);
// Example 3: Removing a connection
AccountManager.RemoveConnectionAccount(connection);
- Always remove account and encryption data when a connection is closed or an account logs out.
- Use the provided dictionaries and methods to efficiently manage connection and account mappings.
- Use
TryUpdateSrpState
to enforce correct SRP authentication flow and prevent state mismatches.