mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 23:28:27 +00:00
refactor(auth): dedupe legacy auth store migration
This commit is contained in:
@@ -184,6 +184,42 @@ function mergeOAuthFileIntoStore(store: AuthProfileStore): boolean {
|
|||||||
return mutated;
|
return mutated;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function applyLegacyStore(store: AuthProfileStore, legacy: LegacyAuthStore): void {
|
||||||
|
for (const [provider, cred] of Object.entries(legacy)) {
|
||||||
|
const profileId = `${provider}:default`;
|
||||||
|
if (cred.type === "api_key") {
|
||||||
|
store.profiles[profileId] = {
|
||||||
|
type: "api_key",
|
||||||
|
provider: String(cred.provider ?? provider),
|
||||||
|
key: cred.key,
|
||||||
|
...(cred.email ? { email: cred.email } : {}),
|
||||||
|
};
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (cred.type === "token") {
|
||||||
|
store.profiles[profileId] = {
|
||||||
|
type: "token",
|
||||||
|
provider: String(cred.provider ?? provider),
|
||||||
|
token: cred.token,
|
||||||
|
...(typeof cred.expires === "number" ? { expires: cred.expires } : {}),
|
||||||
|
...(cred.email ? { email: cred.email } : {}),
|
||||||
|
};
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
store.profiles[profileId] = {
|
||||||
|
type: "oauth",
|
||||||
|
provider: String(cred.provider ?? provider),
|
||||||
|
access: cred.access,
|
||||||
|
refresh: cred.refresh,
|
||||||
|
expires: cred.expires,
|
||||||
|
...(cred.enterpriseUrl ? { enterpriseUrl: cred.enterpriseUrl } : {}),
|
||||||
|
...(cred.projectId ? { projectId: cred.projectId } : {}),
|
||||||
|
...(cred.accountId ? { accountId: cred.accountId } : {}),
|
||||||
|
...(cred.email ? { email: cred.email } : {}),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function loadAuthProfileStore(): AuthProfileStore {
|
export function loadAuthProfileStore(): AuthProfileStore {
|
||||||
const authPath = resolveAuthStorePath();
|
const authPath = resolveAuthStorePath();
|
||||||
const raw = loadJsonFile(authPath);
|
const raw = loadJsonFile(authPath);
|
||||||
@@ -204,37 +240,7 @@ export function loadAuthProfileStore(): AuthProfileStore {
|
|||||||
version: AUTH_STORE_VERSION,
|
version: AUTH_STORE_VERSION,
|
||||||
profiles: {},
|
profiles: {},
|
||||||
};
|
};
|
||||||
for (const [provider, cred] of Object.entries(legacy)) {
|
applyLegacyStore(store, legacy);
|
||||||
const profileId = `${provider}:default`;
|
|
||||||
if (cred.type === "api_key") {
|
|
||||||
store.profiles[profileId] = {
|
|
||||||
type: "api_key",
|
|
||||||
provider: String(cred.provider ?? provider),
|
|
||||||
key: cred.key,
|
|
||||||
...(cred.email ? { email: cred.email } : {}),
|
|
||||||
};
|
|
||||||
} else if (cred.type === "token") {
|
|
||||||
store.profiles[profileId] = {
|
|
||||||
type: "token",
|
|
||||||
provider: String(cred.provider ?? provider),
|
|
||||||
token: cred.token,
|
|
||||||
...(typeof cred.expires === "number" ? { expires: cred.expires } : {}),
|
|
||||||
...(cred.email ? { email: cred.email } : {}),
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
store.profiles[profileId] = {
|
|
||||||
type: "oauth",
|
|
||||||
provider: String(cred.provider ?? provider),
|
|
||||||
access: cred.access,
|
|
||||||
refresh: cred.refresh,
|
|
||||||
expires: cred.expires,
|
|
||||||
...(cred.enterpriseUrl ? { enterpriseUrl: cred.enterpriseUrl } : {}),
|
|
||||||
...(cred.projectId ? { projectId: cred.projectId } : {}),
|
|
||||||
...(cred.accountId ? { accountId: cred.accountId } : {}),
|
|
||||||
...(cred.email ? { email: cred.email } : {}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
syncExternalCliCredentials(store);
|
syncExternalCliCredentials(store);
|
||||||
return store;
|
return store;
|
||||||
}
|
}
|
||||||
@@ -280,37 +286,7 @@ function loadAuthProfileStoreForAgent(
|
|||||||
profiles: {},
|
profiles: {},
|
||||||
};
|
};
|
||||||
if (legacy) {
|
if (legacy) {
|
||||||
for (const [provider, cred] of Object.entries(legacy)) {
|
applyLegacyStore(store, legacy);
|
||||||
const profileId = `${provider}:default`;
|
|
||||||
if (cred.type === "api_key") {
|
|
||||||
store.profiles[profileId] = {
|
|
||||||
type: "api_key",
|
|
||||||
provider: String(cred.provider ?? provider),
|
|
||||||
key: cred.key,
|
|
||||||
...(cred.email ? { email: cred.email } : {}),
|
|
||||||
};
|
|
||||||
} else if (cred.type === "token") {
|
|
||||||
store.profiles[profileId] = {
|
|
||||||
type: "token",
|
|
||||||
provider: String(cred.provider ?? provider),
|
|
||||||
token: cred.token,
|
|
||||||
...(typeof cred.expires === "number" ? { expires: cred.expires } : {}),
|
|
||||||
...(cred.email ? { email: cred.email } : {}),
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
store.profiles[profileId] = {
|
|
||||||
type: "oauth",
|
|
||||||
provider: String(cred.provider ?? provider),
|
|
||||||
access: cred.access,
|
|
||||||
refresh: cred.refresh,
|
|
||||||
expires: cred.expires,
|
|
||||||
...(cred.enterpriseUrl ? { enterpriseUrl: cred.enterpriseUrl } : {}),
|
|
||||||
...(cred.projectId ? { projectId: cred.projectId } : {}),
|
|
||||||
...(cred.accountId ? { accountId: cred.accountId } : {}),
|
|
||||||
...(cred.email ? { email: cred.email } : {}),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const mergedOAuth = mergeOAuthFileIntoStore(store);
|
const mergedOAuth = mergeOAuthFileIntoStore(store);
|
||||||
|
|||||||
Reference in New Issue
Block a user