refactor(device-pairing): share token update context

This commit is contained in:
Peter Steinberger
2026-02-16 02:19:45 +00:00
parent a7cbce1b3d
commit 60ad2c2e96

View File

@@ -428,17 +428,16 @@ export async function ensureDeviceToken(params: {
}): Promise<DeviceAuthToken | null> { }): Promise<DeviceAuthToken | null> {
return await withLock(async () => { return await withLock(async () => {
const state = await loadState(params.baseDir); const state = await loadState(params.baseDir);
const device = getPairedDeviceFromState(state, params.deviceId);
if (!device) {
return null;
}
const role = normalizeRole(params.role);
if (!role) {
return null;
}
const requestedScopes = normalizeScopes(params.scopes); const requestedScopes = normalizeScopes(params.scopes);
const tokens = cloneDeviceTokens(device); const context = resolveDeviceTokenUpdateContext({
const existing = tokens[role]; state,
deviceId: params.deviceId,
role: params.role,
});
if (!context) {
return null;
}
const { device, role, tokens, existing } = context;
if (existing && !existing.revokedAtMs) { if (existing && !existing.revokedAtMs) {
if (scopesAllow(requestedScopes, existing.scopes)) { if (scopesAllow(requestedScopes, existing.scopes)) {
return existing; return existing;
@@ -460,15 +459,17 @@ export async function ensureDeviceToken(params: {
}); });
} }
export async function rotateDeviceToken(params: { function resolveDeviceTokenUpdateContext(params: {
state: DevicePairingStateFile;
deviceId: string; deviceId: string;
role: string; role: string;
scopes?: string[]; }): {
baseDir?: string; device: PairedDevice;
}): Promise<DeviceAuthToken | null> { role: string;
return await withLock(async () => { tokens: Record<string, DeviceAuthToken>;
const state = await loadState(params.baseDir); existing: DeviceAuthToken | undefined;
const device = getPairedDeviceFromState(state, params.deviceId); } | null {
const device = getPairedDeviceFromState(params.state, params.deviceId);
if (!device) { if (!device) {
return null; return null;
} }
@@ -478,6 +479,26 @@ export async function rotateDeviceToken(params: {
} }
const tokens = cloneDeviceTokens(device); const tokens = cloneDeviceTokens(device);
const existing = tokens[role]; const existing = tokens[role];
return { device, role, tokens, existing };
}
export async function rotateDeviceToken(params: {
deviceId: string;
role: string;
scopes?: string[];
baseDir?: string;
}): Promise<DeviceAuthToken | null> {
return await withLock(async () => {
const state = await loadState(params.baseDir);
const context = resolveDeviceTokenUpdateContext({
state,
deviceId: params.deviceId,
role: params.role,
});
if (!context) {
return null;
}
const { device, role, tokens, existing } = context;
const requestedScopes = normalizeScopes(params.scopes ?? existing?.scopes ?? device.scopes); const requestedScopes = normalizeScopes(params.scopes ?? existing?.scopes ?? device.scopes);
const now = Date.now(); const now = Date.now();
const next = buildDeviceAuthToken({ const next = buildDeviceAuthToken({