























@@ -11,6 +11,43 @@ export type DeviceAuthStoreAdapter = {
1111writeStore: (store: DeviceAuthStore) => void;
1212};
131314+function isRecord(value: unknown): value is Record<string, unknown> {
15+return !!value && typeof value === "object" && !Array.isArray(value);
16+}
17+18+function coerceDeviceAuthEntry(role: string, value: unknown): DeviceAuthEntry | null {
19+if (!isRecord(value) || typeof value.token !== "string") {
20+return null;
21+}
22+const updatedAtMs =
23+typeof value.updatedAtMs === "number" && Number.isFinite(value.updatedAtMs)
24+ ? value.updatedAtMs
25+ : 0;
26+return {
27+token: value.token,
28+ role,
29+scopes: normalizeDeviceAuthScopes(Array.isArray(value.scopes) ? value.scopes : undefined),
30+ updatedAtMs,
31+};
32+}
33+34+function copyCanonicalDeviceAuthTokens(
35+tokens: Record<string, unknown>,
36+): Record<string, DeviceAuthEntry> {
37+const out: Record<string, DeviceAuthEntry> = {};
38+for (const [rawRole, value] of Object.entries(tokens)) {
39+const role = normalizeDeviceAuthRole(rawRole);
40+if (!role) {
41+continue;
42+}
43+const entry = coerceDeviceAuthEntry(role, value);
44+if (entry) {
45+out[role] = entry;
46+}
47+}
48+return out;
49+}
50+1451export function loadDeviceAuthTokenFromStore(params: {
1552adapter: DeviceAuthStoreAdapter;
1653deviceId: string;
@@ -21,11 +58,7 @@ export function loadDeviceAuthTokenFromStore(params: {
2158return null;
2259}
2360const role = normalizeDeviceAuthRole(params.role);
24-const entry = store.tokens[role];
25-if (!entry || typeof entry.token !== "string") {
26-return null;
27-}
28-return entry;
61+return coerceDeviceAuthEntry(role, store.tokens[role]);
2962}
30633164export function storeDeviceAuthTokenInStore(params: {
@@ -42,7 +75,7 @@ export function storeDeviceAuthTokenInStore(params: {
4275deviceId: params.deviceId,
4376tokens:
4477existing && existing.deviceId === params.deviceId && existing.tokens
45- ? { ...existing.tokens }
78+ ? copyCanonicalDeviceAuthTokens(existing.tokens)
4679 : {},
4780};
4881const entry: DeviceAuthEntry = {
@@ -72,7 +105,7 @@ export function clearDeviceAuthTokenFromStore(params: {
72105const next: DeviceAuthStore = {
73106version: 1,
74107deviceId: store.deviceId,
75-tokens: { ...store.tokens },
108+tokens: copyCanonicalDeviceAuthTokens(store.tokens),
76109};
77110delete next.tokens[role];
78111params.adapter.writeStore(next);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。