An open-source, self-hosted system for apps built with web technologies.
Processes, Connections and Sessions take ten-character identities people can type; what is reached by its identity alone keeps a UUID
zohayrslileh9884a1288f9b
diff --git a/source/libs/short-identity.ts b/source/libs/short-identity.tsnew file mode 100644index 0000000..dd238c6--- /dev/null+++ b/source/libs/short-identity.ts@@ -0,0 +1,21 @@+/**+ * Crockford's base32 in lower case: digits and letters without i, l, o and u, so an identity reads+ * and types without confusing one character for another.+ */+const alphabet = "0123456789abcdefghjkmnpqrstvwxyz"++/** Ten characters of 32: fifty random bits. */+const length = 10++/**+ * A short random identity for what people name by hand, such as a Process, a Connection, or a+ * Session typed into the CLI. It names; it grants nothing, so it need not be unguessable. What is+ * reached from outside by its identity alone, such as Program assets or uploads, keeps a UUID.+ */+export default function shortIdentity() {++ const bytes = crypto.getRandomValues(new Uint8Array(length))++ // 256 is a multiple of 32, so the low five bits of each byte are evenly spread.+ return Array.from(bytes, byte => alphabet[byte & 31]).join("")+}diff --git a/source/server/core/authentication/sessions.ts b/source/server/core/authentication/sessions.tsindex a656437..d62593f 100644--- a/source/server/core/authentication/sessions.ts+++ b/source/server/core/authentication/sessions.ts@@ -1,5 +1,6 @@import Keyv from "keyv"-import { createHash, randomBytes, randomUUID } from "node:crypto"+import { createHash, randomBytes } from "node:crypto"+import shortIdentity from "@libs/short-identity"const storagePrefix = "authentication:sessions:"@@ -54,7 +55,7 @@ export default class Sessions {const record: StoredSession = {- identity: randomUUID(),+ identity: shortIdentity(),hash: hashToken(token),diff --git a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts b/source/server/core/link-manager/auth-manager/program-manager/program-manager.tsindex 0a3883d..4144ff3 100644--- a/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts+++ b/source/server/core/link-manager/auth-manager/program-manager/program-manager.ts@@ -1,6 +1,5 @@import { copyFileSync, cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs"import { Subscribe } from "@the-link/core/decorators"-import { randomUUID } from "node:crypto"import { TheLink } from "@the-link/core"import SqliteDatabase from "@libs/sqlite-database"import FileManager from "@libs/file-manager"@@ -28,6 +27,7 @@ import {type PermissionValue,type Permissions} from "@phreshos/core"+import shortIdentity from "@libs/short-identity"const maximumProcessesPerProgram = 20@@ -1457,9 +1457,9 @@ export default class ProgramManager extends TheLink {if (active >= maximumProcessesPerProgram) throw new Error(`This program has reached its limit of ${maximumProcessesPerProgram} active processes`)}- let identity = randomUUID()+ let identity = shortIdentity()- while (this.authManager.processManager.processes.has(identity)) identity = randomUUID()+ while (this.authManager.processManager.processes.has(identity)) identity = shortIdentity()// Kept in this Program's declared storage, whoever is or is not// watching and whether the Program is installed or attached. Eachdiff --git a/source/server/core/link-manager/link-manager.ts b/source/server/core/link-manager/link-manager.tsindex 35b6098..7ddae7e 100644--- a/source/server/core/link-manager/link-manager.ts+++ b/source/server/core/link-manager/link-manager.ts@@ -5,6 +5,7 @@ import Application from "../application"import type { Appearance, ConnectionSnapshot, SessionEndReason, SessionSnapshot } from "@phreshos/core"import { type AuthenticationState, type SignUpError } from "../authentication/authentication"import { AsyncLocalStorage } from "node:async_hooks"+import shortIdentity from "@libs/short-identity"/** Owns live Link boundaries and their browser Connection and Session relationships. */export default class LinkManager extends TheLink {@@ -503,7 +504,7 @@ export type SignUpResponse = { signedUp: true } | { error: SignUpError }/** One internal Link boundary. Only browser boundaries enter the public Connection registry. */export class LinkBoundary {- public readonly identity = crypto.randomUUID()+ public readonly identity = shortIdentity()public session: string | null = nulldiff --git a/tests/short-identity.test.ts b/tests/short-identity.test.tsnew file mode 100644index 0000000..e8505ab--- /dev/null+++ b/tests/short-identity.test.ts@@ -0,0 +1,10 @@+import assert from "node:assert/strict"+import { test } from "vitest"+import shortIdentity from "@libs/short-identity"++test("short identities are ten characters people can type", () => {+ const identities = new Set(Array.from({ length: 10_000 }, shortIdentity))++ assert.equal(identities.size, 10_000)+ for (const identity of identities) assert.match(identity, /^[0-9abcdefghjkmnpqrstvwxyz]{10}$/)+})