mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-10 00:23:29 +00:00
refactor(device-pairing): share token update context
This commit is contained in:
@@ -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,6 +459,29 @@ export async function ensureDeviceToken(params: {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function resolveDeviceTokenUpdateContext(params: {
|
||||||
|
state: DevicePairingStateFile;
|
||||||
|
deviceId: string;
|
||||||
|
role: string;
|
||||||
|
}): {
|
||||||
|
device: PairedDevice;
|
||||||
|
role: string;
|
||||||
|
tokens: Record<string, DeviceAuthToken>;
|
||||||
|
existing: DeviceAuthToken | undefined;
|
||||||
|
} | null {
|
||||||
|
const device = getPairedDeviceFromState(params.state, params.deviceId);
|
||||||
|
if (!device) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const role = normalizeRole(params.role);
|
||||||
|
if (!role) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const tokens = cloneDeviceTokens(device);
|
||||||
|
const existing = tokens[role];
|
||||||
|
return { device, role, tokens, existing };
|
||||||
|
}
|
||||||
|
|
||||||
export async function rotateDeviceToken(params: {
|
export async function rotateDeviceToken(params: {
|
||||||
deviceId: string;
|
deviceId: string;
|
||||||
role: string;
|
role: string;
|
||||||
@@ -468,16 +490,15 @@ export async function rotateDeviceToken(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);
|
const context = resolveDeviceTokenUpdateContext({
|
||||||
if (!device) {
|
state,
|
||||||
|
deviceId: params.deviceId,
|
||||||
|
role: params.role,
|
||||||
|
});
|
||||||
|
if (!context) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const role = normalizeRole(params.role);
|
const { device, role, tokens, existing } = context;
|
||||||
if (!role) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
const tokens = cloneDeviceTokens(device);
|
|
||||||
const existing = tokens[role];
|
|
||||||
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({
|
||||||
|
|||||||
Reference in New Issue
Block a user