DataHandler.AccountsWithSessions
A more robust account system with token verification, session checks, expiries, etc.
Types
ActiveSession
Represents a user’s active session record.
-
Properties:
- string Username — User’s name.
- string SessionID — Unique session identifier.
- string SessionKey — Encrypted session key.
- string Expiry — UTC expiry timestamp in ISO-8601 ("o") format.
- string IsTrusted — Encrypted payload of Expiry|IsTrustedFlag.
- string ChecksAndLastTry — Encrypted payload of FailedRecoveryCount|LastAttemptUtc.
-
Constructors:
ActiveSession() { }
ActiveSession(string username,
string sessionID,
string sessionKey,
string expiry,
string isTrusted,
string checksAndLastTry)
ConnectedSessionReturn
Encapsulates the decrypted session details returned to the caller.
-
Properties (all SecureData):
- Username
- SessionKey
- SessionID
- Directory — Path to the user-data directory.
-
Constructors:
ConnectedSessionReturn() { }
ConnectedSessionReturn(string username,
string sessionKey,
string sessionID,
SecureData directory)
ReturnCreateUser
Holds both the initial session info and recovery key when creating a new user.
-
Properties:
- ConnectedSessionReturn sessionReturn
- SecureData RecoveryKey
-
Constructors:
ReturnCreateUser() { }
ReturnCreateUser(ConnectedSessionReturn sessionReturn,
SecureData recoveryKey)
SecuritySettings
Global configuration for session lifetimes and recovery policies.
-
Properties (static):
- SecureData PublicKey — Key used to encrypt session store.
- double ExpiryDuration — Untrusted session lifetime in minutes (default 540).
- double TrustedExpiryDuration — Trusted session lifetime in minutes (default 20160).
- int FailRecoveryCheck — Max allowed recovery failures before lockout (default 5).
- double TimeToNextRecovery — Lockout duration in minutes (default 20).
-
Methods (static):
- SetPublicKey(string newKey, bool makeReadOnly = true)
- SetExpiryDuration(double minutes)
- SetTrustedExpiryDuration(double minutes)
- SetFailRecoveryCheck(int count)
- SetTimeToNextRecovery(double minutes)
AccountData
Same as in Accounts, stores per-user credentials and encryption keys.
-
Properties:
- string Username
- PasswordCheckData Password
- string DataEncryptionKey
- string RecoveryDataKey
-
Constructors:
AccountData() { }
AccountData(string username,
PasswordCheckData password,
string dataEncryptionKey,
string recoveryDataKey)
Methods
SetupFiles(string directory)
Initializes Users.json with empty AccountsList and empty Sessions arrays.
-
Parameters:
- directory: Path to the folder for Users.json.
-
Returns: Task
CreateUser(string username, SecureData password, string Directory)
Registers a new user and returns a recovery key.
-
Steps:
- Load existing AccountsList.
- Ensure username is unique.
- Hash password (PasswordHandler).
- Generate a random data-encryption key & recovery key.
- Encrypt data key under password, and recovery key under data key.
- Append new AccountData, save JSON.
-
Returns: Task — The recovery key.
-
Exceptions: If username already exists.
LoginUser(string username, string Directory, SecureData password, bool IsTrusted)
Authenticates credentials and creates a new session.
-
Parameters:
- username, Directory, password, IsTrusted flag.
-
Process:
- Validate via private LoginCore.
- Generate sessionKey & sessionID.
- Encrypt sessionKey under data key.
- Compute expiry based on IsTrusted.
- Encrypt IsTrusted|expiry and initial ChecksAndLastTry.
- Append new ActiveSession to Sessions array, save JSON.
-
Returns: Task<(SecureData dataKey, ConnectedSessionReturn sessionInfo)>
-
Exceptions: On invalid credentials or I/O errors.
ValidateSession(ConnectedSessionReturn connSession, SecureData decryptKey)
Checks session validity, expiry, and integrity; extends expiry if valid.
-
Parameters:
- connSession: Decrypted session info.
- decryptKey: Data-encryption key from login.
-
Process:
- Load Sessions and locate matching record.
- If expired, remove it, save JSON, return false.
- Decrypt and verify SessionKey and IsTrusted|expiry consistency.
- If tampered, remove and throw exception.
- Extend expiry, update encrypted session record, save JSON.
- Return true.
-
Exceptions: On tampering or parsing errors.
LogoutUser(ConnectedSessionReturn connSession, SecureData decryptKey)
Invalidates a session immediately.
-
Process: Calls ValidateSession (throws if invalid), then removes the session record and saves JSON.
-
Returns: Task
RemoveAccount(ConnectedSessionReturn connSession, SecureData decryptKey)
Deletes a user account and all its sessions.
-
Process:
- Validate session and logout.
- Remove the matching AccountData from AccountsList.
- Save JSON.
-
Returns: Task
ResetPassword(ConnectedSessionReturn connSession, SecureData decryptKey, SecureData NewPassword, SecureData RecoveryPass)
Allows password reset from within an active session, enforcing recovery-policy limits.
-
Process:
- Validate session.
- Load AccountData and matching ActiveSession.
- Decrypt ChecksAndLastTry and RecoveryDataKey.
- Enforce failure count & lockout timings.
- Generate new password hash, encryption key & recovery key.
- Update AccountData, reset ChecksAndLastTry.
- Invalidate existing session(s), save JSON.
-
Returns: Task
-
Exceptions: On recovery key failures or policy triggers.
GetAllUsernames(ConnectedSessionReturn connSession, SecureData decryptKey)
Returns the list of all registered usernames.
-
Parameters:
- connSession, decryptKey.
-
Returns: Task<List>