Open the local signup page in the game's embedded browser before sign-in. Return the existing login error for unknown accounts or invalid passwords. BREAKING CHANGE: First-time players must register in the game's signup window or create a save in the launcher before signing in. Existing accounts can sign in with their saved credentials.
136 lines
5.2 KiB
TypeScript
136 lines
5.2 KiB
TypeScript
/**
|
|
* Local accounts behind the login service.
|
|
*
|
|
* Accounts are created through explicit registration. Sign-in verifies the credentials of
|
|
* an existing account. Accounts are kept in <data>/accounts.json.
|
|
*/
|
|
import { existsSync, readFileSync } from 'node:fs';
|
|
import { join } from 'node:path';
|
|
import { writeFileAtomic } from '../storage/atomic-file.ts';
|
|
import { hashPassword, verifyPassword } from './password.ts';
|
|
|
|
export interface Account {
|
|
/** What the player types into the login window; matched without regard to case. */
|
|
readonly login: string;
|
|
/** Decimal user id, as the login service and the char service exchange it. */
|
|
readonly userId: string;
|
|
readonly nick: string;
|
|
readonly passwordHash: string;
|
|
readonly created: string;
|
|
}
|
|
|
|
export type SignInResult =
|
|
| { readonly kind: 'signed-in'; readonly account: Account }
|
|
| { readonly kind: 'unknown-login' | 'wrong-password' };
|
|
|
|
export type RegistrationResult =
|
|
| { readonly kind: 'registered'; readonly account: Account }
|
|
| { readonly kind: 'invalid-login' | 'invalid-password' | 'invalid-nick' | 'already-registered' };
|
|
|
|
interface AccountFile {
|
|
formatVersion: 1;
|
|
nextUserId: number;
|
|
accounts: Account[];
|
|
}
|
|
|
|
const FIRST_USER_ID = 100000001;
|
|
const NICK_MAX_LENGTH = 16;
|
|
|
|
export class AccountStore {
|
|
readonly #path: string;
|
|
#file: AccountFile;
|
|
#lastSignedInUserId: string | undefined;
|
|
|
|
constructor(dataFolder: string) {
|
|
this.#path = join(dataFolder, 'accounts.json');
|
|
this.#file = existsSync(this.#path) ? readAccountFile(this.#path) : { formatVersion: 1, nextUserId: FIRST_USER_ID, accounts: [] };
|
|
}
|
|
|
|
signIn(login: string, password: string): SignInResult {
|
|
const account = this.#find(login);
|
|
if (!account) return { kind: 'unknown-login' };
|
|
if (!verifyPassword(password, account.passwordHash)) return { kind: 'wrong-password' };
|
|
this.#lastSignedInUserId = account.userId;
|
|
return { kind: 'signed-in', account };
|
|
}
|
|
|
|
register(login: string, password: string, nick: string): RegistrationResult {
|
|
login = login.trim();
|
|
const displayName = normalizedNickname(nick);
|
|
if (!login || /[\s\p{Cc}]/u.test(login)) return { kind: 'invalid-login' };
|
|
if (!password) return { kind: 'invalid-password' };
|
|
if (displayName === undefined) return { kind: 'invalid-nick' };
|
|
if (this.#find(login)) return { kind: 'already-registered' };
|
|
return { kind: 'registered', account: this.#create(login, password, displayName) };
|
|
}
|
|
|
|
#create(login: string, password: string, nick: string): Account {
|
|
const userId = String(this.#file.nextUserId);
|
|
const account: Account = { login, userId, nick, passwordHash: hashPassword(password), created: new Date().toISOString() };
|
|
const file = { ...this.#file, nextUserId: this.#file.nextUserId + 1, accounts: [...this.#file.accounts, account] };
|
|
writeFileAtomic(this.#path, JSON.stringify(file, null, 2));
|
|
this.#file = file;
|
|
return account;
|
|
}
|
|
|
|
rename(userId: string, nick: string): 'updated' | 'invalid-nick' | 'not-found' {
|
|
const displayName = normalizedNickname(nick);
|
|
if (displayName === undefined) return 'invalid-nick';
|
|
const prior = this.byUserId(userId);
|
|
if (!prior) return 'not-found';
|
|
if (prior.nick === displayName) return 'updated';
|
|
const accounts = this.#file.accounts.map((account) => account.userId === userId ? { ...account, nick: displayName } : account);
|
|
const file = { ...this.#file, accounts };
|
|
writeFileAtomic(this.#path, JSON.stringify(file, null, 2));
|
|
this.#file = file;
|
|
return 'updated';
|
|
}
|
|
|
|
remove(userId: string): boolean {
|
|
const accounts = this.#file.accounts.filter((account) => account.userId !== userId);
|
|
if (accounts.length === this.#file.accounts.length) return false;
|
|
const file = { ...this.#file, accounts };
|
|
writeFileAtomic(this.#path, JSON.stringify(file, null, 2));
|
|
this.#file = file;
|
|
if (this.#lastSignedInUserId === userId) this.#lastSignedInUserId = undefined;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* The account that most recently signed in during this service run (for defaulting the initial
|
|
* read-only account dropdown in the built-in browser; never used as a fallback on mutations).
|
|
*/
|
|
lastSignedIn(): Account | undefined {
|
|
return this.#lastSignedInUserId ? this.byUserId(this.#lastSignedInUserId) : undefined;
|
|
}
|
|
|
|
all(): readonly Account[] {
|
|
return this.#file.accounts;
|
|
}
|
|
|
|
byUserId(userId: string): Account | undefined {
|
|
return this.#file.accounts.find((account) => account.userId === userId);
|
|
}
|
|
|
|
/** The account with this login, compared without regard to case. */
|
|
byLogin(login: string): Account | undefined {
|
|
return this.#find(login);
|
|
}
|
|
|
|
#find(login: string): Account | undefined {
|
|
const key = login.toLowerCase();
|
|
return this.#file.accounts.find((account) => account.login.toLowerCase() === key);
|
|
}
|
|
}
|
|
|
|
export function normalizedNickname(nick: string): string | undefined {
|
|
const value = nick.trim();
|
|
return value && [...value].length <= NICK_MAX_LENGTH && !/\p{Cc}/u.test(value) ? value : undefined;
|
|
}
|
|
|
|
function readAccountFile(path: string): AccountFile {
|
|
const file = JSON.parse(readFileSync(path, 'utf8')) as AccountFile;
|
|
if (file.formatVersion !== 1) throw new Error(`${path}: unsupported account file version ${String(file.formatVersion)}`);
|
|
return file;
|
|
}
|