@zapier/zapier-sdk-cli 0.42.1 → 0.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/README.md +4 -4
- package/dist/cli.cjs +619 -34
- package/dist/cli.d.mts +0 -1
- package/dist/cli.d.ts +0 -1
- package/dist/cli.mjs +601 -19
- package/dist/index.cjs +607 -23
- package/dist/index.mjs +592 -11
- package/dist/login.cjs +599 -7
- package/dist/login.d.mts +144 -1
- package/dist/login.d.ts +144 -1
- package/dist/login.mjs +565 -1
- package/dist/package.json +6 -3
- package/dist/src/login/filesystem-cache.d.ts +25 -0
- package/dist/src/login/filesystem-cache.js +195 -0
- package/dist/src/login/index.d.ts +115 -0
- package/dist/src/login/index.js +442 -0
- package/dist/src/login/keychain.d.ts +18 -0
- package/dist/src/login/keychain.js +74 -0
- package/dist/src/login.d.ts +10 -1
- package/dist/src/login.js +10 -1
- package/dist/src/plugins/feedback/index.js +1 -1
- package/dist/src/plugins/getLoginConfigPath/index.js +1 -1
- package/dist/src/plugins/login/index.js +1 -1
- package/dist/src/plugins/logout/index.js +1 -1
- package/dist/src/sdk.js +1 -1
- package/dist/src/utils/auth/login.d.ts +1 -1
- package/dist/src/utils/auth/login.js +1 -1
- package/dist/src/utils/constants.d.ts +1 -1
- package/dist/src/utils/constants.js +1 -1
- package/dist/src/utils/version-checker.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -5
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import Conf from 'conf';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import { existsSync, mkdirSync, writeFileSync, promises, createWriteStream, readdirSync, rmSync, copyFileSync, readFileSync } from 'fs';
|
|
4
|
+
import * as jwt from 'jsonwebtoken';
|
|
5
|
+
import { deletePassword, setPassword, getPassword, getKeyring } from 'cross-keychain';
|
|
6
|
+
import crypto, { createHash } from 'crypto';
|
|
7
|
+
import * as path from 'path';
|
|
8
|
+
import { dirname, resolve, join, basename, relative, extname } from 'path';
|
|
9
|
+
import * as lockfile from 'proper-lockfile';
|
|
3
10
|
import { createFunction, OutputPropertySchema, DEFAULT_CONFIG_PATH, injectCliLogin, createZapierSdk, getOsInfo, getPlatformVersions, getCiPlatform, isCi, ZapierValidationError, ZapierUnknownError, getReleaseId, getCurrentTimestamp, generateEventId, batch, toSnakeCase, buildApplicationLifecycleEvent, ZapierError, isCredentialsObject } from '@zapier/zapier-sdk';
|
|
4
11
|
import open from 'open';
|
|
5
|
-
import crypto, { createHash } from 'crypto';
|
|
6
12
|
import express from 'express';
|
|
7
13
|
import pkceChallenge from 'pkce-challenge';
|
|
8
14
|
import ora from 'ora';
|
|
@@ -11,10 +17,6 @@ import inquirer from 'inquirer';
|
|
|
11
17
|
import { z } from 'zod';
|
|
12
18
|
import { startMcpServer } from '@zapier/zapier-sdk-mcp';
|
|
13
19
|
import { buildSync } from 'esbuild';
|
|
14
|
-
import * as fs from 'fs';
|
|
15
|
-
import { promises, createWriteStream, existsSync, readdirSync, rmSync, mkdirSync, writeFileSync, copyFileSync, readFileSync } from 'fs';
|
|
16
|
-
import * as path from 'path';
|
|
17
|
-
import { resolve, join, dirname, basename, relative, extname } from 'path';
|
|
18
20
|
import { mkdir, writeFile, access } from 'fs/promises';
|
|
19
21
|
import * as ts from 'typescript';
|
|
20
22
|
import 'is-installed-globally';
|
|
@@ -22,7 +24,586 @@ import { execSync } from 'child_process';
|
|
|
22
24
|
import Handlebars from 'handlebars';
|
|
23
25
|
import { fileURLToPath } from 'url';
|
|
24
26
|
|
|
25
|
-
|
|
27
|
+
var __defProp = Object.defineProperty;
|
|
28
|
+
var __export = (target, all) => {
|
|
29
|
+
for (var name in all)
|
|
30
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// src/login.ts
|
|
34
|
+
var login_exports = {};
|
|
35
|
+
__export(login_exports, {
|
|
36
|
+
AUTH_MODE_HEADER: () => AUTH_MODE_HEADER,
|
|
37
|
+
ZapierAuthenticationError: () => ZapierAuthenticationError,
|
|
38
|
+
createCache: () => createCache,
|
|
39
|
+
getAuthAuthorizeUrl: () => getAuthAuthorizeUrl,
|
|
40
|
+
getAuthTokenUrl: () => getAuthTokenUrl,
|
|
41
|
+
getConfig: () => getConfig,
|
|
42
|
+
getConfigPath: () => getConfigPath,
|
|
43
|
+
getLoggedInUser: () => getLoggedInUser,
|
|
44
|
+
getLoginStorageMode: () => getLoginStorageMode,
|
|
45
|
+
getPkceLoginConfig: () => getPkceLoginConfig,
|
|
46
|
+
getToken: () => getToken,
|
|
47
|
+
logout: () => logout,
|
|
48
|
+
unloadConfig: () => unloadConfig,
|
|
49
|
+
updateLogin: () => updateLogin
|
|
50
|
+
});
|
|
51
|
+
var SERVICE = "zapier-sdk-cli";
|
|
52
|
+
var ACCOUNT = "login";
|
|
53
|
+
var cachedBackendInfo;
|
|
54
|
+
async function getBackendInfo() {
|
|
55
|
+
if (!cachedBackendInfo) {
|
|
56
|
+
const keyring = await getKeyring();
|
|
57
|
+
cachedBackendInfo = `${keyring.name} (${keyring.id})`;
|
|
58
|
+
}
|
|
59
|
+
return cachedBackendInfo;
|
|
60
|
+
}
|
|
61
|
+
var keychainQueue = Promise.resolve();
|
|
62
|
+
function enqueue(fn) {
|
|
63
|
+
const result = keychainQueue.then(fn, fn);
|
|
64
|
+
keychainQueue = result.then(
|
|
65
|
+
() => {
|
|
66
|
+
},
|
|
67
|
+
() => {
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
async function getTokensFromKeychain({
|
|
73
|
+
debugLog
|
|
74
|
+
} = {}) {
|
|
75
|
+
return enqueue(async () => {
|
|
76
|
+
const backendInfo = await getBackendInfo();
|
|
77
|
+
debugLog?.(`Keychain read via ${backendInfo}`);
|
|
78
|
+
const startTime = Date.now();
|
|
79
|
+
const raw = await getPassword(SERVICE, ACCOUNT);
|
|
80
|
+
debugLog?.(`Keychain read completed in ${Date.now() - startTime}ms`);
|
|
81
|
+
if (!raw) {
|
|
82
|
+
debugLog?.("Keychain returned no data");
|
|
83
|
+
return void 0;
|
|
84
|
+
}
|
|
85
|
+
let parsed;
|
|
86
|
+
try {
|
|
87
|
+
parsed = JSON.parse(raw);
|
|
88
|
+
} catch {
|
|
89
|
+
debugLog?.("Keychain data is not valid JSON");
|
|
90
|
+
return void 0;
|
|
91
|
+
}
|
|
92
|
+
if (typeof parsed.login_jwt === "string" && typeof parsed.login_refresh_token === "string") {
|
|
93
|
+
return {
|
|
94
|
+
login_jwt: parsed.login_jwt,
|
|
95
|
+
login_refresh_token: parsed.login_refresh_token
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
debugLog?.("Keychain data has invalid shape", parsed);
|
|
99
|
+
return void 0;
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
async function setTokensInKeychain({
|
|
103
|
+
data,
|
|
104
|
+
debugLog
|
|
105
|
+
}) {
|
|
106
|
+
return enqueue(async () => {
|
|
107
|
+
const backendInfo = await getBackendInfo();
|
|
108
|
+
debugLog?.(`Keychain write via ${backendInfo}`);
|
|
109
|
+
const startTime = Date.now();
|
|
110
|
+
await setPassword(SERVICE, ACCOUNT, JSON.stringify(data));
|
|
111
|
+
debugLog?.(`Keychain write completed in ${Date.now() - startTime}ms`);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
async function clearTokensFromKeychain({
|
|
115
|
+
debugLog
|
|
116
|
+
} = {}) {
|
|
117
|
+
return enqueue(async () => {
|
|
118
|
+
try {
|
|
119
|
+
const backendInfo = await getBackendInfo();
|
|
120
|
+
debugLog?.(`Keychain clear via ${backendInfo}`);
|
|
121
|
+
await deletePassword(SERVICE, ACCOUNT);
|
|
122
|
+
} catch {
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
var SERVICE2 = "zapier-sdk-cache";
|
|
127
|
+
var CONFIG_KEY = "cache";
|
|
128
|
+
var LOCK_UPDATE_MS = 5e3;
|
|
129
|
+
var LOCK_STALE_MS = 1e4;
|
|
130
|
+
var LOCK_RETRY_WAIT_MS = 100;
|
|
131
|
+
var LOCK_RETRY_MAX_WAIT_MS = 1e3;
|
|
132
|
+
var LOCK_RETRY_COUNT = 120;
|
|
133
|
+
function keychainAccount(key) {
|
|
134
|
+
return createHash("sha256").update(key).digest("hex");
|
|
135
|
+
}
|
|
136
|
+
function readConfigMap() {
|
|
137
|
+
const cfg = getConfig();
|
|
138
|
+
const stored = cfg.get(CONFIG_KEY);
|
|
139
|
+
if (stored && typeof stored === "object") {
|
|
140
|
+
return stored;
|
|
141
|
+
}
|
|
142
|
+
return {};
|
|
143
|
+
}
|
|
144
|
+
function writeConfigMap(map) {
|
|
145
|
+
getConfig().set(CONFIG_KEY, map);
|
|
146
|
+
}
|
|
147
|
+
function entryIsExpired(entry) {
|
|
148
|
+
return entry.expires_at !== void 0 && entry.expires_at <= Date.now();
|
|
149
|
+
}
|
|
150
|
+
function createCache() {
|
|
151
|
+
return {
|
|
152
|
+
async get(key) {
|
|
153
|
+
const entry = readConfigMap()[key];
|
|
154
|
+
if (!entry) return void 0;
|
|
155
|
+
if (entryIsExpired(entry)) return void 0;
|
|
156
|
+
if (entry.secret) {
|
|
157
|
+
const stored = await enqueue(async () => {
|
|
158
|
+
await getBackendInfo();
|
|
159
|
+
return getPassword(SERVICE2, keychainAccount(key));
|
|
160
|
+
});
|
|
161
|
+
if (!stored) {
|
|
162
|
+
return void 0;
|
|
163
|
+
}
|
|
164
|
+
return { value: stored, expiresAt: entry.expires_at };
|
|
165
|
+
}
|
|
166
|
+
if (entry.value === void 0) return void 0;
|
|
167
|
+
return { value: entry.value, expiresAt: entry.expires_at };
|
|
168
|
+
},
|
|
169
|
+
async set(key, value, options) {
|
|
170
|
+
const secret = options?.secret ?? false;
|
|
171
|
+
const expiresAt = options?.ttl ? Date.now() + options.ttl * 1e3 : void 0;
|
|
172
|
+
if (secret) {
|
|
173
|
+
try {
|
|
174
|
+
await enqueue(async () => {
|
|
175
|
+
await getBackendInfo();
|
|
176
|
+
await setPassword(SERVICE2, keychainAccount(key), value);
|
|
177
|
+
});
|
|
178
|
+
} catch {
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const map = readConfigMap();
|
|
182
|
+
map[key] = { secret: true, expires_at: expiresAt };
|
|
183
|
+
try {
|
|
184
|
+
writeConfigMap(map);
|
|
185
|
+
} catch {
|
|
186
|
+
}
|
|
187
|
+
} else {
|
|
188
|
+
const map = readConfigMap();
|
|
189
|
+
map[key] = { secret: false, value, expires_at: expiresAt };
|
|
190
|
+
try {
|
|
191
|
+
writeConfigMap(map);
|
|
192
|
+
} catch {
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
async delete(key) {
|
|
197
|
+
const map = readConfigMap();
|
|
198
|
+
const entry = map[key];
|
|
199
|
+
if (entry) {
|
|
200
|
+
delete map[key];
|
|
201
|
+
try {
|
|
202
|
+
writeConfigMap(map);
|
|
203
|
+
} catch {
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (entry?.secret) {
|
|
207
|
+
try {
|
|
208
|
+
await enqueue(async () => {
|
|
209
|
+
await getBackendInfo();
|
|
210
|
+
await deletePassword(SERVICE2, keychainAccount(key));
|
|
211
|
+
});
|
|
212
|
+
} catch {
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
async withLock(_key, fn) {
|
|
217
|
+
const cfg = getConfig();
|
|
218
|
+
const lockTarget = `${cfg.path}.cache-lock`;
|
|
219
|
+
try {
|
|
220
|
+
mkdirSync(dirname(lockTarget), { recursive: true });
|
|
221
|
+
if (!existsSync(lockTarget)) {
|
|
222
|
+
writeFileSync(lockTarget, "");
|
|
223
|
+
}
|
|
224
|
+
} catch {
|
|
225
|
+
return fn();
|
|
226
|
+
}
|
|
227
|
+
let release = null;
|
|
228
|
+
try {
|
|
229
|
+
release = await lockfile.lock(lockTarget, {
|
|
230
|
+
stale: LOCK_STALE_MS,
|
|
231
|
+
update: LOCK_UPDATE_MS,
|
|
232
|
+
retries: {
|
|
233
|
+
retries: LOCK_RETRY_COUNT,
|
|
234
|
+
factor: 1.2,
|
|
235
|
+
minTimeout: LOCK_RETRY_WAIT_MS,
|
|
236
|
+
maxTimeout: LOCK_RETRY_MAX_WAIT_MS
|
|
237
|
+
}
|
|
238
|
+
});
|
|
239
|
+
} catch {
|
|
240
|
+
return fn();
|
|
241
|
+
}
|
|
242
|
+
try {
|
|
243
|
+
return await fn();
|
|
244
|
+
} finally {
|
|
245
|
+
try {
|
|
246
|
+
await release();
|
|
247
|
+
} catch {
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// src/login/index.ts
|
|
255
|
+
var ZapierAuthenticationError = class extends Error {
|
|
256
|
+
constructor(message) {
|
|
257
|
+
super(message);
|
|
258
|
+
this.name = "ZapierAuthenticationError";
|
|
259
|
+
}
|
|
260
|
+
};
|
|
261
|
+
var config = null;
|
|
262
|
+
var DEFAULT_AUTH_CLIENT_ID = "grwWZD5hUWGvb4V8ODBuOtXer3h0DBEZ2HR8aay6";
|
|
263
|
+
var TOKEN_REFRESH_BUFFER_MS = 5 * 60 * 1e3;
|
|
264
|
+
function createDebugLog(enabled) {
|
|
265
|
+
if (!enabled) {
|
|
266
|
+
return () => {
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return (message, data) => {
|
|
270
|
+
if (data === void 0) {
|
|
271
|
+
console.log(`[Zapier SDK CLI Login] ${message}`);
|
|
272
|
+
} else {
|
|
273
|
+
console.log(`[Zapier SDK CLI Login] ${message}`, data);
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
function censorHeaderValue(value) {
|
|
278
|
+
if (value.length > 12) {
|
|
279
|
+
return `${value.substring(0, 4)}...${value.substring(value.length - 4)}`;
|
|
280
|
+
}
|
|
281
|
+
return `${value.charAt(0)}...`;
|
|
282
|
+
}
|
|
283
|
+
function getAuthClientId(clientId) {
|
|
284
|
+
return clientId || DEFAULT_AUTH_CLIENT_ID;
|
|
285
|
+
}
|
|
286
|
+
var AUTH_MODE_HEADER = "X-Auth";
|
|
287
|
+
var DEFAULT_AUTH_BASE_URL = "https://zapier.com";
|
|
288
|
+
function getAuthTokenUrl(options) {
|
|
289
|
+
const authBaseUrl = options?.baseUrl || DEFAULT_AUTH_BASE_URL;
|
|
290
|
+
return `${authBaseUrl}/oauth/token/`;
|
|
291
|
+
}
|
|
292
|
+
function getAuthAuthorizeUrl(options) {
|
|
293
|
+
const authBaseUrl = options?.baseUrl || DEFAULT_AUTH_BASE_URL;
|
|
294
|
+
return `${authBaseUrl}/oauth/authorize/`;
|
|
295
|
+
}
|
|
296
|
+
function getPkceLoginConfig(options) {
|
|
297
|
+
return {
|
|
298
|
+
clientId: getAuthClientId(options?.credentials?.clientId),
|
|
299
|
+
tokenUrl: getAuthTokenUrl({ baseUrl: options?.credentials?.baseUrl }),
|
|
300
|
+
authorizeUrl: getAuthAuthorizeUrl({
|
|
301
|
+
baseUrl: options?.credentials?.baseUrl
|
|
302
|
+
})
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
var cachedLogin;
|
|
306
|
+
function getConfig() {
|
|
307
|
+
if (!config) {
|
|
308
|
+
config = new Conf({ projectName: "zapier-sdk-cli" });
|
|
309
|
+
if (!config.has("login_storage_mode")) {
|
|
310
|
+
config.set(
|
|
311
|
+
"login_storage_mode",
|
|
312
|
+
existsSync(config.path) ? "config" : "keychain"
|
|
313
|
+
);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
return config;
|
|
317
|
+
}
|
|
318
|
+
function unloadConfig() {
|
|
319
|
+
config = null;
|
|
320
|
+
cachedLogin = void 0;
|
|
321
|
+
}
|
|
322
|
+
async function updateLogin(loginData, options = {}) {
|
|
323
|
+
const debugLog = createDebugLog(options.debug ?? false);
|
|
324
|
+
const storage = options.storage ?? cachedLogin?.storage ?? "keychain";
|
|
325
|
+
const expiresAt = Date.now() + loginData.expires_in * 1e3;
|
|
326
|
+
const cfg = getConfig();
|
|
327
|
+
cfg.set("login_storage_mode", storage);
|
|
328
|
+
if (storage === "keychain") {
|
|
329
|
+
await setTokensInKeychain({
|
|
330
|
+
data: {
|
|
331
|
+
login_jwt: loginData.access_token,
|
|
332
|
+
login_refresh_token: loginData.refresh_token
|
|
333
|
+
},
|
|
334
|
+
debugLog
|
|
335
|
+
});
|
|
336
|
+
cfg.set("login_expires_at", expiresAt);
|
|
337
|
+
cfg.delete("login_jwt");
|
|
338
|
+
cfg.delete("login_refresh_token");
|
|
339
|
+
} else {
|
|
340
|
+
cfg.set("login_jwt", loginData.access_token);
|
|
341
|
+
cfg.set("login_refresh_token", loginData.refresh_token);
|
|
342
|
+
cfg.set("login_expires_at", expiresAt);
|
|
343
|
+
await clearTokensFromKeychain({ debugLog });
|
|
344
|
+
}
|
|
345
|
+
cachedLogin = {
|
|
346
|
+
jwt: loginData.access_token,
|
|
347
|
+
refreshToken: loginData.refresh_token,
|
|
348
|
+
expiresAt,
|
|
349
|
+
storage
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
function decodeJwtOrThrow(token) {
|
|
353
|
+
if (typeof token !== "string") {
|
|
354
|
+
throw new Error("Expected JWT to be a string");
|
|
355
|
+
}
|
|
356
|
+
const decodedJwt = jwt.decode(token, { complete: true });
|
|
357
|
+
if (!decodedJwt) {
|
|
358
|
+
throw new Error("Could not decode JWT");
|
|
359
|
+
}
|
|
360
|
+
if (typeof decodedJwt.payload === "string") {
|
|
361
|
+
throw new Error("Did not expect JWT payload to be a string");
|
|
362
|
+
}
|
|
363
|
+
return decodedJwt;
|
|
364
|
+
}
|
|
365
|
+
async function refreshJwt(refreshToken, options = {}) {
|
|
366
|
+
const {
|
|
367
|
+
onEvent,
|
|
368
|
+
fetch: fetch2 = globalThis.fetch,
|
|
369
|
+
credentials,
|
|
370
|
+
debug = false
|
|
371
|
+
} = options;
|
|
372
|
+
const debugLog = createDebugLog(debug);
|
|
373
|
+
const tokenUrl = getAuthTokenUrl({ baseUrl: credentials?.baseUrl });
|
|
374
|
+
const clientId = getAuthClientId(credentials?.clientId);
|
|
375
|
+
const startTime = Date.now();
|
|
376
|
+
try {
|
|
377
|
+
onEvent?.({
|
|
378
|
+
type: "auth_refreshing",
|
|
379
|
+
payload: {
|
|
380
|
+
message: "Refreshing your token...",
|
|
381
|
+
operation: "token_refresh"
|
|
382
|
+
},
|
|
383
|
+
timestamp: Date.now()
|
|
384
|
+
});
|
|
385
|
+
debugLog(`\u2192 POST ${tokenUrl}`, {
|
|
386
|
+
headers: {
|
|
387
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
388
|
+
[AUTH_MODE_HEADER]: "no"
|
|
389
|
+
},
|
|
390
|
+
body: {
|
|
391
|
+
client_id: clientId,
|
|
392
|
+
refresh_token: censorHeaderValue(refreshToken),
|
|
393
|
+
grant_type: "refresh_token"
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
const response = await fetch2(tokenUrl, {
|
|
397
|
+
method: "POST",
|
|
398
|
+
headers: {
|
|
399
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
400
|
+
[AUTH_MODE_HEADER]: "no"
|
|
401
|
+
},
|
|
402
|
+
body: new URLSearchParams({
|
|
403
|
+
client_id: clientId,
|
|
404
|
+
refresh_token: refreshToken,
|
|
405
|
+
grant_type: "refresh_token"
|
|
406
|
+
})
|
|
407
|
+
});
|
|
408
|
+
const duration = Date.now() - startTime;
|
|
409
|
+
if (!response.ok) {
|
|
410
|
+
debugLog(`\u2190 ${response.status} ${response.statusText} (${duration}ms)`);
|
|
411
|
+
throw new Error(
|
|
412
|
+
`Token refresh failed: ${response.status} ${response.statusText}`
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
debugLog(`\u2190 ${response.status} ${response.statusText} (${duration}ms)`);
|
|
416
|
+
const data = await response.json();
|
|
417
|
+
await updateLogin(data, { debug });
|
|
418
|
+
debugLog(
|
|
419
|
+
`Token refreshed and saved to ${cachedLogin?.storage ?? "keychain"}`
|
|
420
|
+
);
|
|
421
|
+
onEvent?.({
|
|
422
|
+
type: "auth_success",
|
|
423
|
+
payload: {
|
|
424
|
+
message: "Token refreshed successfully",
|
|
425
|
+
operation: "token_refresh"
|
|
426
|
+
},
|
|
427
|
+
timestamp: Date.now()
|
|
428
|
+
});
|
|
429
|
+
return data.access_token;
|
|
430
|
+
} catch (error) {
|
|
431
|
+
const duration = Date.now() - startTime;
|
|
432
|
+
debugLog(`\u2716 Token refresh failed (${duration}ms)`, {
|
|
433
|
+
error: error instanceof Error ? error.message : error
|
|
434
|
+
});
|
|
435
|
+
cachedLogin = void 0;
|
|
436
|
+
const errorMessage = `Token refresh failed: ${error instanceof Error ? error.message : "Unknown error"}`;
|
|
437
|
+
onEvent?.({
|
|
438
|
+
type: "auth_error",
|
|
439
|
+
payload: {
|
|
440
|
+
message: errorMessage,
|
|
441
|
+
error: errorMessage,
|
|
442
|
+
operation: "token_refresh"
|
|
443
|
+
},
|
|
444
|
+
timestamp: Date.now()
|
|
445
|
+
});
|
|
446
|
+
throw error;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
var pendingRefresh = null;
|
|
450
|
+
var pendingResolve = null;
|
|
451
|
+
async function resolveStoredLogin(debugLog) {
|
|
452
|
+
if (cachedLogin) {
|
|
453
|
+
debugLog("Using in-memory cached credentials");
|
|
454
|
+
return cachedLogin;
|
|
455
|
+
}
|
|
456
|
+
if (pendingResolve) {
|
|
457
|
+
debugLog("Waiting for existing keychain read to complete");
|
|
458
|
+
return pendingResolve;
|
|
459
|
+
}
|
|
460
|
+
pendingResolve = resolveStoredLoginFromStorage(debugLog).finally(() => {
|
|
461
|
+
pendingResolve = null;
|
|
462
|
+
});
|
|
463
|
+
return pendingResolve;
|
|
464
|
+
}
|
|
465
|
+
async function resolveStoredLoginFromStorage(debugLog) {
|
|
466
|
+
let cfg;
|
|
467
|
+
try {
|
|
468
|
+
cfg = getConfig();
|
|
469
|
+
} catch (error) {
|
|
470
|
+
debugLog("Failed to load config", {
|
|
471
|
+
error: error instanceof Error ? error.message : error
|
|
472
|
+
});
|
|
473
|
+
return void 0;
|
|
474
|
+
}
|
|
475
|
+
const expiresAt = cfg.get("login_expires_at");
|
|
476
|
+
const configJwt = cfg.get("login_jwt");
|
|
477
|
+
const configRefresh = cfg.get("login_refresh_token");
|
|
478
|
+
if (configJwt && configRefresh && typeof expiresAt === "number") {
|
|
479
|
+
debugLog("Loaded credentials from config (legacy format)");
|
|
480
|
+
cachedLogin = {
|
|
481
|
+
jwt: configJwt,
|
|
482
|
+
refreshToken: configRefresh,
|
|
483
|
+
expiresAt,
|
|
484
|
+
storage: "config"
|
|
485
|
+
};
|
|
486
|
+
return cachedLogin;
|
|
487
|
+
}
|
|
488
|
+
if (typeof expiresAt !== "number") {
|
|
489
|
+
debugLog("No stored login credentials found");
|
|
490
|
+
return void 0;
|
|
491
|
+
}
|
|
492
|
+
const keychainData = await getTokensFromKeychain({ debugLog });
|
|
493
|
+
if (!keychainData) {
|
|
494
|
+
debugLog("No tokens found in keychain");
|
|
495
|
+
return void 0;
|
|
496
|
+
}
|
|
497
|
+
debugLog("Loaded credentials from keychain");
|
|
498
|
+
cachedLogin = {
|
|
499
|
+
jwt: keychainData.login_jwt,
|
|
500
|
+
refreshToken: keychainData.login_refresh_token,
|
|
501
|
+
expiresAt,
|
|
502
|
+
storage: "keychain"
|
|
503
|
+
};
|
|
504
|
+
return cachedLogin;
|
|
505
|
+
}
|
|
506
|
+
async function resolveOrRefreshToken(options = {}) {
|
|
507
|
+
const { debug = false } = options;
|
|
508
|
+
const debugLog = createDebugLog(debug);
|
|
509
|
+
const stored = await resolveStoredLogin(debugLog);
|
|
510
|
+
if (!stored) {
|
|
511
|
+
return void 0;
|
|
512
|
+
}
|
|
513
|
+
const { jwt: storedJwt, refreshToken, expiresAt } = stored;
|
|
514
|
+
if (expiresAt > Date.now() + TOKEN_REFRESH_BUFFER_MS) {
|
|
515
|
+
debugLog("Using cached token (still valid)");
|
|
516
|
+
return storedJwt;
|
|
517
|
+
}
|
|
518
|
+
debugLog("Token expired, refreshing...");
|
|
519
|
+
if (pendingRefresh) {
|
|
520
|
+
debugLog("Waiting for existing refresh to complete");
|
|
521
|
+
return pendingRefresh;
|
|
522
|
+
}
|
|
523
|
+
pendingRefresh = refreshJwt(refreshToken, options).finally(() => {
|
|
524
|
+
pendingRefresh = null;
|
|
525
|
+
});
|
|
526
|
+
return await pendingRefresh;
|
|
527
|
+
}
|
|
528
|
+
async function getToken(options = {}) {
|
|
529
|
+
try {
|
|
530
|
+
return await resolveOrRefreshToken(options);
|
|
531
|
+
} catch (error) {
|
|
532
|
+
const message = error instanceof Error ? error.message : "Token refresh failed";
|
|
533
|
+
throw new ZapierAuthenticationError(
|
|
534
|
+
`${message}
|
|
535
|
+
Please run 'login' to authenticate again.`
|
|
536
|
+
);
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
async function getLoggedInUser(options = {}) {
|
|
540
|
+
const jwt2 = await getToken(options).catch(() => void 0);
|
|
541
|
+
if (!jwt2) {
|
|
542
|
+
throw new Error(
|
|
543
|
+
"No valid authentication token available. Please login first."
|
|
544
|
+
);
|
|
545
|
+
}
|
|
546
|
+
let decodedJwt = decodeJwtOrThrow(jwt2);
|
|
547
|
+
if (decodedJwt.payload["sub_type"] == "service") {
|
|
548
|
+
decodedJwt = decodeJwtOrThrow(decodedJwt.payload["njwt"]);
|
|
549
|
+
}
|
|
550
|
+
if (typeof decodedJwt.payload["zap:acc"] !== "string") {
|
|
551
|
+
throw new Error("JWT payload does not contain accountId");
|
|
552
|
+
}
|
|
553
|
+
const accountId = parseInt(decodedJwt.payload["zap:acc"], 10);
|
|
554
|
+
if (isNaN(accountId)) {
|
|
555
|
+
throw new Error("JWT accountId is not a number");
|
|
556
|
+
}
|
|
557
|
+
if (decodedJwt.payload["sub_type"] !== "customuser" || typeof decodedJwt.payload["sub"] !== "string") {
|
|
558
|
+
throw new Error("JWT payload does not contain customUserId");
|
|
559
|
+
}
|
|
560
|
+
const customUserId = parseInt(decodedJwt.payload["sub"], 10);
|
|
561
|
+
if (isNaN(customUserId)) {
|
|
562
|
+
throw new Error("JWT customUserId is not a number");
|
|
563
|
+
}
|
|
564
|
+
const email = decodedJwt.payload["zap:uname"];
|
|
565
|
+
if (typeof email !== "string") {
|
|
566
|
+
throw new Error("JWT payload does not contain email");
|
|
567
|
+
}
|
|
568
|
+
return {
|
|
569
|
+
accountId,
|
|
570
|
+
customUserId,
|
|
571
|
+
email
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
function getLoginStorageMode() {
|
|
575
|
+
const cfg = getConfig();
|
|
576
|
+
if (typeof cfg.get("login_jwt") === "string") {
|
|
577
|
+
return "config";
|
|
578
|
+
}
|
|
579
|
+
const explicitMode = cfg.get("login_storage_mode");
|
|
580
|
+
if (explicitMode === "keychain" || explicitMode === "config") {
|
|
581
|
+
return explicitMode;
|
|
582
|
+
}
|
|
583
|
+
return "keychain";
|
|
584
|
+
}
|
|
585
|
+
async function logout(options = {}) {
|
|
586
|
+
const { onEvent } = options;
|
|
587
|
+
const mode = getLoginStorageMode();
|
|
588
|
+
cachedLogin = void 0;
|
|
589
|
+
await clearTokensFromKeychain();
|
|
590
|
+
const cfg = getConfig();
|
|
591
|
+
cfg.set("login_storage_mode", mode);
|
|
592
|
+
cfg.delete("login_expires_at");
|
|
593
|
+
cfg.delete("login_jwt");
|
|
594
|
+
cfg.delete("login_refresh_token");
|
|
595
|
+
onEvent?.({
|
|
596
|
+
type: "auth_logout",
|
|
597
|
+
payload: { message: "Logged out successfully", operation: "logout" },
|
|
598
|
+
timestamp: Date.now()
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
function getConfigPath() {
|
|
602
|
+
const cfg = getConfig();
|
|
603
|
+
return cfg.path;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// src/utils/constants.ts
|
|
26
607
|
var LOGIN_PORTS = [49505, 50575, 52804, 55981, 61010, 63851];
|
|
27
608
|
var LOGIN_TIMEOUT_MS = 3e5;
|
|
28
609
|
var ZapierCliError = class extends ZapierError {
|
|
@@ -2469,10 +3050,10 @@ var initPlugin = () => {
|
|
|
2469
3050
|
// package.json with { type: 'json' }
|
|
2470
3051
|
var package_default = {
|
|
2471
3052
|
name: "@zapier/zapier-sdk-cli",
|
|
2472
|
-
version: "0.
|
|
3053
|
+
version: "0.43.0"};
|
|
2473
3054
|
|
|
2474
3055
|
// src/sdk.ts
|
|
2475
|
-
injectCliLogin(
|
|
3056
|
+
injectCliLogin(login_exports);
|
|
2476
3057
|
function createZapierCliSdk(options = {}) {
|
|
2477
3058
|
return createZapierSdk({
|
|
2478
3059
|
...options,
|
|
@@ -2483,7 +3064,7 @@ function createZapierCliSdk(options = {}) {
|
|
|
2483
3064
|
|
|
2484
3065
|
// package.json
|
|
2485
3066
|
var package_default2 = {
|
|
2486
|
-
version: "0.
|
|
3067
|
+
version: "0.43.0"};
|
|
2487
3068
|
|
|
2488
3069
|
// src/telemetry/builders.ts
|
|
2489
3070
|
function createCliBaseEvent(context = {}) {
|