-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path749.js
More file actions
380 lines (380 loc) · 18.8 KB
/
749.js
File metadata and controls
380 lines (380 loc) · 18.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
"use strict";
exports.ids = [
"749"
];
exports.modules = {
"./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/auth-config.js": function(module, __unused_webpack_exports, __webpack_require__) {
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all)=>{
for(var name in all)__defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc)=>{
if (from && "object" == typeof from || "function" == typeof from) {
for (let key of __getOwnPropNames(from))if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ()=>from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target)=>(target = null != mod ? __create(__getProtoOf(mod)) : {}, __copyProps(!isNodeMode && mod && mod.__esModule ? target : __defProp(target, "default", {
value: mod,
enumerable: true
}), mod));
var __toCommonJS = (mod)=>__copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var auth_config_exports = {};
__export(auth_config_exports, {
isValidAccessToken: ()=>isValidAccessToken,
readAuthConfig: ()=>readAuthConfig,
writeAuthConfig: ()=>writeAuthConfig
});
module.exports = __toCommonJS(auth_config_exports);
var fs = __toESM(__webpack_require__("fs"));
var path = __toESM(__webpack_require__("path"));
var import_token_util = __webpack_require__("./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/token-util.js");
function getAuthConfigPath() {
const dataDir = (0, import_token_util.getVercelDataDir)();
if (!dataDir) throw new Error(`Unable to find Vercel CLI data directory. Your platform: ${process.platform}. Supported: darwin, linux, win32.`);
return path.join(dataDir, "auth.json");
}
function readAuthConfig() {
try {
const authPath = getAuthConfigPath();
if (!fs.existsSync(authPath)) return null;
const content = fs.readFileSync(authPath, "utf8");
if (!content) return null;
return JSON.parse(content);
} catch (error) {
return null;
}
}
function writeAuthConfig(config) {
const authPath = getAuthConfigPath();
const authDir = path.dirname(authPath);
if (!fs.existsSync(authDir)) fs.mkdirSync(authDir, {
mode: 504,
recursive: true
});
fs.writeFileSync(authPath, JSON.stringify(config, null, 2), {
mode: 384
});
}
function isValidAccessToken(authConfig) {
if (!authConfig.token) return false;
if ("number" != typeof authConfig.expiresAt) return true;
const nowInSeconds = Math.floor(Date.now() / 1e3);
return authConfig.expiresAt >= nowInSeconds;
}
},
"./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/oauth.js": function(module, __unused_webpack_exports, __webpack_require__) {
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all)=>{
for(var name in all)__defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc)=>{
if (from && "object" == typeof from || "function" == typeof from) {
for (let key of __getOwnPropNames(from))if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ()=>from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toCommonJS = (mod)=>__copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var oauth_exports = {};
__export(oauth_exports, {
processTokenResponse: ()=>processTokenResponse,
refreshTokenRequest: ()=>refreshTokenRequest
});
module.exports = __toCommonJS(oauth_exports);
var import_os = __webpack_require__("os");
const VERCEL_ISSUER = "https://vercel.com";
const VERCEL_CLI_CLIENT_ID = "cl_HYyOPBNtFMfHhaUn9L4QPfTZz6TP47bp";
const userAgent = `@vercel/oidc node-${process.version} ${(0, import_os.platform)()} (${(0, import_os.arch)()}) ${(0, import_os.hostname)()}`;
let _tokenEndpoint = null;
async function getTokenEndpoint() {
if (_tokenEndpoint) return _tokenEndpoint;
const discoveryUrl = `${VERCEL_ISSUER}/.well-known/openid-configuration`;
const response = await fetch(discoveryUrl, {
headers: {
"user-agent": userAgent
}
});
if (!response.ok) throw new Error("Failed to discover OAuth endpoints");
const metadata = await response.json();
if (!metadata || "string" != typeof metadata.token_endpoint) throw new Error("Invalid OAuth discovery response");
const endpoint = metadata.token_endpoint;
_tokenEndpoint = endpoint;
return endpoint;
}
async function refreshTokenRequest(options) {
const tokenEndpoint = await getTokenEndpoint();
return await fetch(tokenEndpoint, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"user-agent": userAgent
},
body: new URLSearchParams({
client_id: VERCEL_CLI_CLIENT_ID,
grant_type: "refresh_token",
...options
})
});
}
async function processTokenResponse(response) {
const json = await response.json();
if (!response.ok) {
const errorMsg = "object" == typeof json && json && "error" in json ? String(json.error) : "Token refresh failed";
return [
new Error(errorMsg)
];
}
if ("object" != typeof json || null === json) return [
new Error("Invalid token response")
];
if ("string" != typeof json.access_token) return [
new Error("Missing access_token in response")
];
if ("Bearer" !== json.token_type) return [
new Error("Invalid token_type in response")
];
if ("number" != typeof json.expires_in) return [
new Error("Missing expires_in in response")
];
return [
null,
json
];
}
},
"./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/token-io.js": function(module, __unused_webpack_exports, __webpack_require__) {
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all)=>{
for(var name in all)__defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc)=>{
if (from && "object" == typeof from || "function" == typeof from) {
for (let key of __getOwnPropNames(from))if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ()=>from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target)=>(target = null != mod ? __create(__getProtoOf(mod)) : {}, __copyProps(!isNodeMode && mod && mod.__esModule ? target : __defProp(target, "default", {
value: mod,
enumerable: true
}), mod));
var __toCommonJS = (mod)=>__copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var token_io_exports = {};
__export(token_io_exports, {
findRootDir: ()=>findRootDir,
getUserDataDir: ()=>getUserDataDir
});
module.exports = __toCommonJS(token_io_exports);
var import_path = __toESM(__webpack_require__("path"));
var import_fs = __toESM(__webpack_require__("fs"));
var import_os = __toESM(__webpack_require__("os"));
var import_token_error = __webpack_require__("./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/token-error.js");
function findRootDir() {
try {
let dir = process.cwd();
while(dir !== import_path.default.dirname(dir)){
const pkgPath = import_path.default.join(dir, ".vercel");
if (import_fs.default.existsSync(pkgPath)) return dir;
dir = import_path.default.dirname(dir);
}
} catch (e) {
throw new import_token_error.VercelOidcTokenError("Token refresh only supported in node server environments");
}
return null;
}
function getUserDataDir() {
if (process.env.XDG_DATA_HOME) return process.env.XDG_DATA_HOME;
switch(import_os.default.platform()){
case "darwin":
return import_path.default.join(import_os.default.homedir(), "Library/Application Support");
case "linux":
return import_path.default.join(import_os.default.homedir(), ".local/share");
case "win32":
if (process.env.LOCALAPPDATA) return process.env.LOCALAPPDATA;
return null;
default:
return null;
}
}
},
"./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/token-util.js": function(module, __unused_webpack_exports, __webpack_require__) {
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all)=>{
for(var name in all)__defProp(target, name, {
get: all[name],
enumerable: true
});
};
var __copyProps = (to, from, except, desc)=>{
if (from && "object" == typeof from || "function" == typeof from) {
for (let key of __getOwnPropNames(from))if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
get: ()=>from[key],
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
});
}
return to;
};
var __toESM = (mod, isNodeMode, target)=>(target = null != mod ? __create(__getProtoOf(mod)) : {}, __copyProps(!isNodeMode && mod && mod.__esModule ? target : __defProp(target, "default", {
value: mod,
enumerable: true
}), mod));
var __toCommonJS = (mod)=>__copyProps(__defProp({}, "__esModule", {
value: true
}), mod);
var token_util_exports = {};
__export(token_util_exports, {
assertVercelOidcTokenResponse: ()=>assertVercelOidcTokenResponse,
findProjectInfo: ()=>findProjectInfo,
getTokenPayload: ()=>getTokenPayload,
getVercelCliToken: ()=>getVercelCliToken,
getVercelDataDir: ()=>getVercelDataDir,
getVercelOidcToken: ()=>getVercelOidcToken,
isExpired: ()=>isExpired,
loadToken: ()=>loadToken,
saveToken: ()=>saveToken
});
module.exports = __toCommonJS(token_util_exports);
var path = __toESM(__webpack_require__("path"));
var fs = __toESM(__webpack_require__("fs"));
var import_token_error = __webpack_require__("./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/token-error.js");
var import_token_io = __webpack_require__("./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/token-io.js");
var import_auth_config = __webpack_require__("./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/auth-config.js");
var import_oauth = __webpack_require__("./node_modules/.pnpm/@vercel+oidc@3.1.0/node_modules/@vercel/oidc/dist/oauth.js");
function getVercelDataDir() {
const vercelFolder = "com.vercel.cli";
const dataDir = (0, import_token_io.getUserDataDir)();
if (!dataDir) return null;
return path.join(dataDir, vercelFolder);
}
async function getVercelCliToken() {
const authConfig = (0, import_auth_config.readAuthConfig)();
if (!authConfig) return null;
if ((0, import_auth_config.isValidAccessToken)(authConfig)) return authConfig.token || null;
if (!authConfig.refreshToken) {
(0, import_auth_config.writeAuthConfig)({});
return null;
}
try {
const tokenResponse = await (0, import_oauth.refreshTokenRequest)({
refresh_token: authConfig.refreshToken
});
const [tokensError, tokens] = await (0, import_oauth.processTokenResponse)(tokenResponse);
if (tokensError || !tokens) {
(0, import_auth_config.writeAuthConfig)({});
return null;
}
const updatedConfig = {
token: tokens.access_token,
expiresAt: Math.floor(Date.now() / 1e3) + tokens.expires_in
};
if (tokens.refresh_token) updatedConfig.refreshToken = tokens.refresh_token;
(0, import_auth_config.writeAuthConfig)(updatedConfig);
return updatedConfig.token ?? null;
} catch (error) {
(0, import_auth_config.writeAuthConfig)({});
return null;
}
}
async function getVercelOidcToken(authToken, projectId, teamId) {
const url = `https://api.vercel.com/v1/projects/${projectId}/token?source=vercel-oidc-refresh${teamId ? `&teamId=${teamId}` : ""}`;
const res = await fetch(url, {
method: "POST",
headers: {
Authorization: `Bearer ${authToken}`
}
});
if (!res.ok) throw new import_token_error.VercelOidcTokenError(`Failed to refresh OIDC token: ${res.statusText}`);
const tokenRes = await res.json();
assertVercelOidcTokenResponse(tokenRes);
return tokenRes;
}
function assertVercelOidcTokenResponse(res) {
if (!res || "object" != typeof res) throw new TypeError("Vercel OIDC token is malformed. Expected an object. Please run `vc env pull` and try again");
if (!("token" in res) || "string" != typeof res.token) throw new TypeError("Vercel OIDC token is malformed. Expected a string-valued token property. Please run `vc env pull` and try again");
}
function findProjectInfo() {
const dir = (0, import_token_io.findRootDir)();
if (!dir) throw new import_token_error.VercelOidcTokenError("Unable to find project root directory. Have you linked your project with `vc link?`");
const prjPath = path.join(dir, ".vercel", "project.json");
if (!fs.existsSync(prjPath)) throw new import_token_error.VercelOidcTokenError("project.json not found, have you linked your project with `vc link?`");
const prj = JSON.parse(fs.readFileSync(prjPath, "utf8"));
if ("string" != typeof prj.projectId && "string" != typeof prj.orgId) throw new TypeError("Expected a string-valued projectId property. Try running `vc link` to re-link your project.");
return {
projectId: prj.projectId,
teamId: prj.orgId
};
}
function saveToken(token, projectId) {
const dir = (0, import_token_io.getUserDataDir)();
if (!dir) throw new import_token_error.VercelOidcTokenError("Unable to find user data directory. Please reach out to Vercel support.");
const tokenPath = path.join(dir, "com.vercel.token", `${projectId}.json`);
const tokenJson = JSON.stringify(token);
fs.mkdirSync(path.dirname(tokenPath), {
mode: 504,
recursive: true
});
fs.writeFileSync(tokenPath, tokenJson);
fs.chmodSync(tokenPath, 432);
}
function loadToken(projectId) {
const dir = (0, import_token_io.getUserDataDir)();
if (!dir) throw new import_token_error.VercelOidcTokenError("Unable to find user data directory. Please reach out to Vercel support.");
const tokenPath = path.join(dir, "com.vercel.token", `${projectId}.json`);
if (!fs.existsSync(tokenPath)) return null;
const token = JSON.parse(fs.readFileSync(tokenPath, "utf8"));
assertVercelOidcTokenResponse(token);
return token;
}
function getTokenPayload(token) {
const tokenParts = token.split(".");
if (3 !== tokenParts.length) throw new import_token_error.VercelOidcTokenError("Invalid token. Please run `vc env pull` and try again");
const base64 = tokenParts[1].replace(/-/g, "+").replace(/_/g, "/");
const padded = base64.padEnd(base64.length + (4 - base64.length % 4) % 4, "=");
return JSON.parse(Buffer.from(padded, "base64").toString("utf8"));
}
function isExpired(token) {
return 1e3 * token.exp < Date.now();
}
}
};