|
| 1 | +import fs from "node:fs"; |
| 2 | +import os from "node:os"; |
| 3 | +import path from "node:path"; |
| 4 | +import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "openclaw/plugin-sdk/account-id"; |
| 5 | +import { |
| 6 | + requiresExplicitMatrixDefaultAccount, |
| 7 | + resolveMatrixDefaultOrOnlyAccountId, |
| 8 | +} from "../account-selection.js"; |
| 9 | +import { getMatrixRuntime } from "../runtime.js"; |
| 10 | +import { |
| 11 | + resolveMatrixCredentialsDir as resolveSharedMatrixCredentialsDir, |
| 12 | + resolveMatrixCredentialsPath as resolveSharedMatrixCredentialsPath, |
| 13 | +} from "../storage-paths.js"; |
| 14 | + |
| 15 | +export type MatrixStoredCredentials = { |
| 16 | + homeserver: string; |
| 17 | + userId: string; |
| 18 | + accessToken: string; |
| 19 | + deviceId?: string; |
| 20 | + createdAt: string; |
| 21 | + lastUsedAt?: string; |
| 22 | +}; |
| 23 | + |
| 24 | +function resolveStateDir(env: NodeJS.ProcessEnv): string { |
| 25 | + return getMatrixRuntime().state.resolveStateDir(env, os.homedir); |
| 26 | +} |
| 27 | + |
| 28 | +function resolveLegacyMatrixCredentialsPath(env: NodeJS.ProcessEnv): string | null { |
| 29 | + return path.join(resolveMatrixCredentialsDir(env), "credentials.json"); |
| 30 | +} |
| 31 | + |
| 32 | +function shouldReadLegacyCredentialsForAccount(accountId?: string | null): boolean { |
| 33 | + const normalizedAccountId = normalizeAccountId(accountId); |
| 34 | + const cfg = getMatrixRuntime().config.loadConfig(); |
| 35 | + if (!cfg.channels?.matrix || typeof cfg.channels.matrix !== "object") { |
| 36 | + return normalizedAccountId === DEFAULT_ACCOUNT_ID; |
| 37 | + } |
| 38 | + if (requiresExplicitMatrixDefaultAccount(cfg)) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + return normalizeAccountId(resolveMatrixDefaultOrOnlyAccountId(cfg)) === normalizedAccountId; |
| 42 | +} |
| 43 | + |
| 44 | +function resolveLegacyMigrationSourcePath( |
| 45 | + env: NodeJS.ProcessEnv, |
| 46 | + accountId?: string | null, |
| 47 | +): string | null { |
| 48 | + if (!shouldReadLegacyCredentialsForAccount(accountId)) { |
| 49 | + return null; |
| 50 | + } |
| 51 | + const legacyPath = resolveLegacyMatrixCredentialsPath(env); |
| 52 | + return legacyPath === resolveMatrixCredentialsPath(env, accountId) ? null : legacyPath; |
| 53 | +} |
| 54 | + |
| 55 | +function parseMatrixCredentialsFile(filePath: string): MatrixStoredCredentials | null { |
| 56 | + const raw = fs.readFileSync(filePath, "utf-8"); |
| 57 | + const parsed = JSON.parse(raw) as Partial<MatrixStoredCredentials>; |
| 58 | + if ( |
| 59 | + typeof parsed.homeserver !== "string" || |
| 60 | + typeof parsed.userId !== "string" || |
| 61 | + typeof parsed.accessToken !== "string" |
| 62 | + ) { |
| 63 | + return null; |
| 64 | + } |
| 65 | + return parsed as MatrixStoredCredentials; |
| 66 | +} |
| 67 | + |
| 68 | +export function resolveMatrixCredentialsDir( |
| 69 | + env: NodeJS.ProcessEnv = process.env, |
| 70 | + stateDir?: string, |
| 71 | +): string { |
| 72 | + const resolvedStateDir = stateDir ?? resolveStateDir(env); |
| 73 | + return resolveSharedMatrixCredentialsDir(resolvedStateDir); |
| 74 | +} |
| 75 | + |
| 76 | +export function resolveMatrixCredentialsPath( |
| 77 | + env: NodeJS.ProcessEnv = process.env, |
| 78 | + accountId?: string | null, |
| 79 | +): string { |
| 80 | + const resolvedStateDir = resolveStateDir(env); |
| 81 | + return resolveSharedMatrixCredentialsPath({ stateDir: resolvedStateDir, accountId }); |
| 82 | +} |
| 83 | + |
| 84 | +export function loadMatrixCredentials( |
| 85 | + env: NodeJS.ProcessEnv = process.env, |
| 86 | + accountId?: string | null, |
| 87 | +): MatrixStoredCredentials | null { |
| 88 | + const credPath = resolveMatrixCredentialsPath(env, accountId); |
| 89 | + try { |
| 90 | + if (fs.existsSync(credPath)) { |
| 91 | + return parseMatrixCredentialsFile(credPath); |
| 92 | + } |
| 93 | + |
| 94 | + const legacyPath = resolveLegacyMigrationSourcePath(env, accountId); |
| 95 | + if (!legacyPath || !fs.existsSync(legacyPath)) { |
| 96 | + return null; |
| 97 | + } |
| 98 | + |
| 99 | + const parsed = parseMatrixCredentialsFile(legacyPath); |
| 100 | + if (!parsed) { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + try { |
| 105 | + fs.mkdirSync(path.dirname(credPath), { recursive: true }); |
| 106 | + fs.renameSync(legacyPath, credPath); |
| 107 | + } catch { |
| 108 | + // Keep returning the legacy credentials even if migration fails. |
| 109 | + } |
| 110 | + |
| 111 | + return parsed; |
| 112 | + } catch { |
| 113 | + return null; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +export function clearMatrixCredentials( |
| 118 | + env: NodeJS.ProcessEnv = process.env, |
| 119 | + accountId?: string | null, |
| 120 | +): void { |
| 121 | + const paths = [ |
| 122 | + resolveMatrixCredentialsPath(env, accountId), |
| 123 | + resolveLegacyMigrationSourcePath(env, accountId), |
| 124 | + ]; |
| 125 | + for (const filePath of paths) { |
| 126 | + if (!filePath) { |
| 127 | + continue; |
| 128 | + } |
| 129 | + try { |
| 130 | + if (fs.existsSync(filePath)) { |
| 131 | + fs.unlinkSync(filePath); |
| 132 | + } |
| 133 | + } catch { |
| 134 | + // ignore |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +export function credentialsMatchConfig( |
| 140 | + stored: MatrixStoredCredentials, |
| 141 | + config: { homeserver: string; userId: string; accessToken?: string }, |
| 142 | +): boolean { |
| 143 | + if (!config.userId) { |
| 144 | + if (!config.accessToken) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + return stored.homeserver === config.homeserver && stored.accessToken === config.accessToken; |
| 148 | + } |
| 149 | + return stored.homeserver === config.homeserver && stored.userId === config.userId; |
| 150 | +} |
0 commit comments