BlueGit

timfa questward

Stop depending on auth email for Questward login and display.

Auth.timfalken.com will stop returning email; match accounts by auth
subject only, preserve any previously stored email, and show username
(or auth subject) instead of email in the client.

timfa Tim Falken · pushed by timfa

Authored 2026-10-05 · 6 weeks ago

Hash
f00e220da016685533a634efdfcd41bd124cb123
Parent
266d6d03
7 files changed +57 −9
client/src/editor/main.js +1 −1
@@ -1527,7 +1527,7 @@ async function main ()
1527 1527 return;
1528 1528 }
1529 1529
1530 const accountLabel = authStatus.username ?? authStatus.email ?? authStatus.authSubject ?? "Signed in";
1530 const accountLabel = authStatus.username ?? authStatus.authSubject ?? "Signed in";
1531 1531 $("who").textContent = `${accountLabel} · loading sprites…`;
1532 1532
1533 1533 catalog = await loadSpriteCatalog();
@@ -1527,7 +1527,7 @@ async function main ()
1527 return;
1527 return;
1528 }
1528 }
1529
1529
1530 const accountLabel = authStatus.username ?? authStatus.email ?? authStatus.authSubject ?? "Signed in";
1530 const accountLabel = authStatus.username ?? authStatus.authSubject ?? "Signed in";
1531 $("who").textContent = `${accountLabel} · loading sprites…`;
1531 $("who").textContent = `${accountLabel} · loading sprites…`;
1532
1532
1533 catalog = await loadSpriteCatalog();
1533 catalog = await loadSpriteCatalog();
client/src/editor/settingsModal.js +2 −2
@@ -236,10 +236,10 @@ export function initSettingsModal ({ socket, MessageType, onApplied, onPreviewNi
236 236 const row = document.createElement("div");
237 237 row.className = "game-admin-row";
238 238 const label = document.createElement("span");
239 label.textContent = admin.username ?? admin.email ?? admin.authSubject;
239 label.textContent = admin.username ?? admin.authSubject;
240 240 const meta = document.createElement("span");
241 241 meta.className = "muted small";
242 meta.textContent = admin.email && admin.username ? admin.email : admin.authSubject;
242 meta.textContent = admin.authSubject;
243 243 const remove = document.createElement("button");
244 244 remove.type = "button";
245 245 remove.textContent = "Remove admin";
@@ -236,10 +236,10 @@ export function initSettingsModal ({ socket, MessageType, onApplied, onPreviewNi
236 const row = document.createElement("div");
236 const row = document.createElement("div");
237 row.className = "game-admin-row";
237 row.className = "game-admin-row";
238 const label = document.createElement("span");
238 const label = document.createElement("span");
239 label.textContent = admin.username ?? admin.email ?? admin.authSubject;
239 label.textContent = admin.username ?? admin.authSubject;
240 const meta = document.createElement("span");
240 const meta = document.createElement("span");
241 meta.className = "muted small";
241 meta.className = "muted small";
242 meta.textContent = admin.email && admin.username ? admin.email : admin.authSubject;
242 meta.textContent = admin.authSubject;
243 const remove = document.createElement("button");
243 const remove = document.createElement("button");
244 remove.type = "button";
244 remove.type = "button";
245 remove.textContent = "Remove admin";
245 remove.textContent = "Remove admin";
client/src/net/auth.js +0 −2
@@ -22,7 +22,5 @@ export function formatAccountLabel (status)
22 22 { return "Signed out"; }
23 23 if (status.username)
24 24 { return status.username; }
25 if (status.email)
26 { return status.email; }
27 25 return status.authSubject ?? "Signed in";
28 26 }
@@ -22,7 +22,5 @@ export function formatAccountLabel (status)
22 { return "Signed out"; }
22 { return "Signed out"; }
23 if (status.username)
23 if (status.username)
24 { return status.username; }
24 { return status.username; }
25 if (status.email)
26 { return status.email; }
27 return status.authSubject ?? "Signed in";
25 return status.authSubject ?? "Signed in";
28}
26}
server/src/auth/client.js +7 −2
@@ -104,10 +104,15 @@ export function authSubjectForUser (user)
104 104
105 105 export function identityFromUser (user, accessToken = null)
106 106 {
107 const rawEmail = user?.email;
108 const email = typeof rawEmail === "string" && rawEmail.trim() !== "" ? rawEmail.trim() : null;
109 const rawUsername = user?.username;
110 const username = typeof rawUsername === "string" && rawUsername.trim() !== "" ? rawUsername.trim() : null;
107 111 return {
108 112 authSubject: authSubjectForUser(user),
109 email: user.email ?? null,
110 username: user.username ?? null,
113 // Auth no longer shares email; keep null-tolerant for older tokens / stored sessions.
114 email,
115 username,
111 116 accessToken
112 117 };
113 118 }
@@ -104,10 +104,15 @@ export function authSubjectForUser (user)
104
104
105export function identityFromUser (user, accessToken = null)
105export function identityFromUser (user, accessToken = null)
106{
106{
107 const rawEmail = user?.email;
108 const email = typeof rawEmail === "string" && rawEmail.trim() !== "" ? rawEmail.trim() : null;
109 const rawUsername = user?.username;
110 const username = typeof rawUsername === "string" && rawUsername.trim() !== "" ? rawUsername.trim() : null;
107 return {
111 return {
108 authSubject: authSubjectForUser(user),
112 authSubject: authSubjectForUser(user),
109 email: user.email ?? null,
113 // Auth no longer shares email; keep null-tolerant for older tokens / stored sessions.
110 username: user.username ?? null,
114 email,
115 username,
111 accessToken
116 accessToken
112 };
117 };
113}
118}
server/src/persistence/stateDb.js +3 −2
@@ -484,10 +484,11 @@ export class StateDatabase
484 484 {
485 485 const fields = [];
486 486 const values = [];
487 if (email != null)
487 // Never blank a stored email when auth stops sending one.
488 if (typeof email === "string" && email.trim() !== "")
488 489 {
489 490 fields.push("email = ?");
490 values.push(email);
491 values.push(email.trim());
491 492 }
492 493 if (username != null)
493 494 {
@@ -484,10 +484,11 @@ export class StateDatabase
484 {
484 {
485 const fields = [];
485 const fields = [];
486 const values = [];
486 const values = [];
487 if (email != null)
487 // Never blank a stored email when auth stops sending one.
488 if (typeof email === "string" && email.trim() !== "")
488 {
489 {
489 fields.push("email = ?");
490 fields.push("email = ?");
490 values.push(email);
491 values.push(email.trim());
491 }
492 }
492 if (username != null)
493 if (username != null)
493 {
494 {
server/tests/authAccounts.test.js +34 −0
@@ -48,3 +48,37 @@ test("grant admin by username requires prior sign-in", () =>
48 48 (err) => err.userFacing && /signed in yet/i.test(err.message)
49 49 );
50 50 });
51
52 test("resolveAccountFromAuth works without email and preserves stored email", () =>
53 {
54 const dir = mkdtempSync(join(tmpdir(), "questward-auth-"));
55 const db = new StateDatabase(join(dir, "state.sqlite"));
56 const created = db.resolveAccountFromAuth({
57 authSubject: "auth:99",
58 email: "keep@example.com",
59 username: "keeper"
60 });
61 assert.equal(created.email, "keep@example.com");
62 const again = db.resolveAccountFromAuth({
63 authSubject: "auth:99",
64 email: null,
65 username: "keeper"
66 });
67 assert.equal(again.account_id, created.account_id);
68 assert.equal(again.email, "keep@example.com");
69 assert.equal(again.username, "keeper");
70 });
71
72 test("new account can be created with only auth subject and username", () =>
73 {
74 const dir = mkdtempSync(join(tmpdir(), "questward-auth-"));
75 const db = new StateDatabase(join(dir, "state.sqlite"));
76 const account = db.resolveAccountFromAuth({
77 authSubject: "auth:7",
78 username: "noemail"
79 });
80 assert.equal(account.auth_subject, "auth:7");
81 assert.equal(account.username, "noemail");
82 assert.equal(account.email, null);
83 assert.equal(account.is_admin, 1);
84 });
@@ -48,3 +48,37 @@ test("grant admin by username requires prior sign-in", () =>
48 (err) => err.userFacing && /signed in yet/i.test(err.message)
48 (err) => err.userFacing && /signed in yet/i.test(err.message)
49 );
49 );
50});
50});
51
52test("resolveAccountFromAuth works without email and preserves stored email", () =>
53{
54 const dir = mkdtempSync(join(tmpdir(), "questward-auth-"));
55 const db = new StateDatabase(join(dir, "state.sqlite"));
56 const created = db.resolveAccountFromAuth({
57 authSubject: "auth:99",
58 email: "keep@example.com",
59 username: "keeper"
60 });
61 assert.equal(created.email, "keep@example.com");
62 const again = db.resolveAccountFromAuth({
63 authSubject: "auth:99",
64 email: null,
65 username: "keeper"
66 });
67 assert.equal(again.account_id, created.account_id);
68 assert.equal(again.email, "keep@example.com");
69 assert.equal(again.username, "keeper");
70});
71
72test("new account can be created with only auth subject and username", () =>
73{
74 const dir = mkdtempSync(join(tmpdir(), "questward-auth-"));
75 const db = new StateDatabase(join(dir, "state.sqlite"));
76 const account = db.resolveAccountFromAuth({
77 authSubject: "auth:7",
78 username: "noemail"
79 });
80 assert.equal(account.auth_subject, "auth:7");
81 assert.equal(account.username, "noemail");
82 assert.equal(account.email, null);
83 assert.equal(account.is_admin, 1);
84});
server/tests/authSession.test.js +10 −0
@@ -6,6 +6,7 @@ import { join } from "node:path";
6 6 import { OAuthAuth } from "../src/auth/oauth.js";
7 7 import { authConfig } from "../src/auth/config.js";
8 8 import { StateDatabase } from "../src/persistence/stateDb.js";
9 import { identityFromUser } from "../src/auth/client.js";
9 10
10 11 test("local auth session resolves identity without calling auth API", async () =>
11 12 {
@@ -44,3 +45,12 @@ test("local auth session resolves identity without calling auth API", async () =
44 45 assert.equal(identity?.username, "player");
45 46 assert.equal(identity?.localSession, true);
46 47 });
48
49 test("identityFromUser ignores missing email", () =>
50 {
51 const identity = identityFromUser({ id: 5, username: "player" }, "tok");
52 assert.equal(identity.authSubject, "auth:5");
53 assert.equal(identity.username, "player");
54 assert.equal(identity.email, null);
55 assert.equal(identity.accessToken, "tok");
56 });
@@ -6,6 +6,7 @@ import { join } from "node:path";
6import { OAuthAuth } from "../src/auth/oauth.js";
6import { OAuthAuth } from "../src/auth/oauth.js";
7import { authConfig } from "../src/auth/config.js";
7import { authConfig } from "../src/auth/config.js";
8import { StateDatabase } from "../src/persistence/stateDb.js";
8import { StateDatabase } from "../src/persistence/stateDb.js";
9import { identityFromUser } from "../src/auth/client.js";
9
10
10test("local auth session resolves identity without calling auth API", async () =>
11test("local auth session resolves identity without calling auth API", async () =>
11{
12{
@@ -44,3 +45,12 @@ test("local auth session resolves identity without calling auth API", async () =
44 assert.equal(identity?.username, "player");
45 assert.equal(identity?.username, "player");
45 assert.equal(identity?.localSession, true);
46 assert.equal(identity?.localSession, true);
46});
47});
48
49test("identityFromUser ignores missing email", () =>
50{
51 const identity = identityFromUser({ id: 5, username: "player" }, "tok");
52 assert.equal(identity.authSubject, "auth:5");
53 assert.equal(identity.username, "player");
54 assert.equal(identity.email, null);
55 assert.equal(identity.accessToken, "tok");
56});