MCPcopy Create free account
hub / github.com/OpenHands/OpenHands / createCredentialStore

Function createCredentialStore

scripts/mars-credentials.mjs:102–403  ·  view source on GitHub ↗
({
  userDataPath = null,
  safeStorage = null,
  secrets = null,
  log = (message) => console.warn(`[mars-credentials] ${message}`),
} = {})

Source from the content-addressed store, hash-verified

100 * @param {(message: string) => void} [options.log]
101 */
102export function createCredentialStore({
103 userDataPath = null,
104 safeStorage = null,
105 secrets = null,
106 log = (message) => console.warn(`[mars-credentials] ${message}`),
107} = {}) {
108 const filePath = userDataPath
109 ? path.join(userDataPath, STORE_FILENAME)
110 : null;
111
112 // When the OS keychain is unavailable (common on bare Linux desktops),
113 // safeStorage credentials live only for this process rather than being
114 // written to disk in the clear.
115 const canEncrypt = Boolean(
116 !secrets && filePath && safeStorage?.isEncryptionAvailable?.(),
117 );
118 /**
119 * Plaintext token per connection id. The only copy when nothing persists
120 * it; otherwise a cache, so the keychain is not hit on every dial.
121 */
122 const memoryTokens = new Map();
123
124 let state = load();
125
126 function load() {
127 memoryTokens.clear();
128 if (!filePath) return emptyState();
129 let loaded;
130 try {
131 const parsed = JSON.parse(readFileSync(filePath, "utf8"));
132 if (parsed?.version !== STORE_VERSION) return emptyState();
133 loaded = {
134 ...emptyState(),
135 ...parsed,
136 connections: Array.isArray(parsed.connections)
137 ? parsed.connections
138 : [],
139 };
140 } catch {
141 // Missing or corrupt store — start clean rather than blocking sign-in.
142 return emptyState();
143 }
144 if (secrets) {
145 for (const connection of loaded.connections) {
146 try {
147 const secret = secrets.read(secretAccountFor(connection.id));
148 const token = secret ? tokenFromSecret(secret) : null;
149 if (token) memoryTokens.set(connection.id, token);
150 } catch (error) {
151 // A locked keychain: the connection stays listed, needing sign-in.
152 log(error instanceof Error ? error.message : String(error));
153 }
154 }
155 }
156 return loaded;
157 }
158
159 function persist() {

Callers 6

createMarsTunnelBridgeFunction · 0.90
setupFunction · 0.90
newStoreFunction · 0.90
serverStoreFunction · 0.90

Calls 1

loadFunction · 0.70

Tested by 3

setupFunction · 0.72
newStoreFunction · 0.72
serverStoreFunction · 0.72