@vercel/sandbox 1.1.4 → 1.1.6
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/.turbo/turbo-build.log +1 -1
- package/.turbo/turbo-test.log +14 -8
- package/.turbo/turbo-typecheck.log +1 -1
- package/CHANGELOG.md +12 -0
- package/__mocks__/picocolors.ts +13 -0
- package/dist/api-client/api-client.d.ts +2 -2
- package/dist/api-client/api-client.js +3 -1
- package/dist/api-client/api-client.js.map +1 -1
- package/dist/api-client/api-error.d.ts +4 -1
- package/dist/api-client/api-error.js +3 -1
- package/dist/api-client/api-error.js.map +1 -1
- package/dist/api-client/base-client.js +13 -0
- package/dist/api-client/base-client.js.map +1 -1
- package/dist/api-client/validators.d.ts +10 -10
- package/dist/api-client/with-retry.js +1 -1
- package/dist/api-client/with-retry.js.map +1 -1
- package/dist/auth/api.d.ts +6 -0
- package/dist/auth/api.js +28 -0
- package/dist/auth/api.js.map +1 -0
- package/dist/auth/error.d.ts +11 -0
- package/dist/auth/error.js +12 -0
- package/dist/auth/error.js.map +1 -0
- package/dist/auth/file.d.ts +22 -0
- package/dist/auth/file.js +66 -0
- package/dist/auth/file.js.map +1 -0
- package/dist/auth/index.d.ts +6 -0
- package/dist/auth/index.js +27 -0
- package/dist/auth/index.js.map +1 -0
- package/dist/auth/linked-project.d.ts +10 -0
- package/dist/auth/linked-project.js +69 -0
- package/dist/auth/linked-project.js.map +1 -0
- package/dist/auth/oauth.d.ts +131 -0
- package/dist/auth/oauth.js +269 -0
- package/dist/auth/oauth.js.map +1 -0
- package/dist/auth/poll-for-token.d.ts +20 -0
- package/dist/auth/poll-for-token.js +66 -0
- package/dist/auth/poll-for-token.js.map +1 -0
- package/dist/auth/project.d.ts +40 -0
- package/dist/auth/project.js +80 -0
- package/dist/auth/project.js.map +1 -0
- package/dist/auth/zod.d.ts +5 -0
- package/dist/auth/zod.js +20 -0
- package/dist/auth/zod.js.map +1 -0
- package/dist/command.d.ts +7 -0
- package/dist/command.js +39 -7
- package/dist/command.js.map +1 -1
- package/dist/sandbox.js +1 -1
- package/dist/sandbox.js.map +1 -1
- package/dist/utils/dev-credentials.d.ts +37 -0
- package/dist/utils/dev-credentials.js +191 -0
- package/dist/utils/dev-credentials.js.map +1 -0
- package/dist/utils/get-credentials.d.ts +16 -0
- package/dist/utils/get-credentials.js +66 -7
- package/dist/utils/get-credentials.js.map +1 -1
- package/dist/utils/log.d.ts +2 -0
- package/dist/utils/log.js +24 -0
- package/dist/utils/log.js.map +1 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +4 -1
- package/src/api-client/api-client.test.ts +176 -0
- package/src/api-client/api-client.ts +7 -1
- package/src/api-client/api-error.ts +6 -1
- package/src/api-client/base-client.ts +15 -0
- package/src/api-client/with-retry.ts +1 -1
- package/src/auth/api.ts +31 -0
- package/src/auth/error.ts +8 -0
- package/src/auth/file.ts +69 -0
- package/src/auth/index.ts +9 -0
- package/src/auth/infer-scope.test.ts +178 -0
- package/src/auth/linked-project.test.ts +86 -0
- package/src/auth/linked-project.ts +40 -0
- package/src/auth/oauth.ts +333 -0
- package/src/auth/poll-for-token.ts +89 -0
- package/src/auth/project.ts +92 -0
- package/src/auth/zod.ts +16 -0
- package/src/command.ts +50 -7
- package/src/sandbox.ts +1 -1
- package/src/utils/dev-credentials.test.ts +217 -0
- package/src/utils/dev-credentials.ts +189 -0
- package/src/utils/get-credentials.test.ts +20 -0
- package/src/utils/get-credentials.ts +72 -8
- package/src/utils/log.ts +20 -0
- package/src/version.ts +1 -1
- package/test-utils/mock-response.ts +12 -0
- package/vitest.config.ts +1 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":";AAAA,2FAA2F;AAC3F,kFAAkF;;;;;;;;;;;;;;;;;AAElF,yCAAuB;AAEvB,0CAAwB;AAExB,mDAAgD;AAAvC,8GAAA,YAAY,OAAA;AACrB,qCAAmD;AAA1C,qGAAA,UAAU,OAAA;AAAE,qGAAA,UAAU,OAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reads the linked project configuration from `.vercel/project.json`.
|
|
3
|
+
*
|
|
4
|
+
* @param cwd - The directory to search for `.vercel/project.json`.
|
|
5
|
+
* @returns The linked project's `projectId` and `teamId`, or `null` if not found.
|
|
6
|
+
*/
|
|
7
|
+
export declare function readLinkedProject(cwd: string): Promise<{
|
|
8
|
+
projectId: string;
|
|
9
|
+
teamId: string;
|
|
10
|
+
} | null>;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.readLinkedProject = readLinkedProject;
|
|
37
|
+
const zod_1 = require("zod");
|
|
38
|
+
const fs = __importStar(require("node:fs/promises"));
|
|
39
|
+
const path = __importStar(require("node:path"));
|
|
40
|
+
const zod_2 = require("./zod");
|
|
41
|
+
const LinkedProjectSchema = zod_2.json.pipe(zod_1.z.object({
|
|
42
|
+
projectId: zod_1.z.string(),
|
|
43
|
+
orgId: zod_1.z.string(),
|
|
44
|
+
}));
|
|
45
|
+
/**
|
|
46
|
+
* Reads the linked project configuration from `.vercel/project.json`.
|
|
47
|
+
*
|
|
48
|
+
* @param cwd - The directory to search for `.vercel/project.json`.
|
|
49
|
+
* @returns The linked project's `projectId` and `teamId`, or `null` if not found.
|
|
50
|
+
*/
|
|
51
|
+
async function readLinkedProject(cwd) {
|
|
52
|
+
const projectJsonPath = path.join(cwd, ".vercel", "project.json");
|
|
53
|
+
let content;
|
|
54
|
+
try {
|
|
55
|
+
content = await fs.readFile(projectJsonPath, "utf-8");
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const parsed = LinkedProjectSchema.safeParse(content);
|
|
61
|
+
if (!parsed.success) {
|
|
62
|
+
return null;
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
projectId: parsed.data.projectId,
|
|
66
|
+
teamId: parsed.data.orgId,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=linked-project.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"linked-project.js","sourceRoot":"","sources":["../../src/auth/linked-project.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAkBA,8CAqBC;AAvCD,6BAAwB;AACxB,qDAAuC;AACvC,gDAAkC;AAClC,+BAA6B;AAE7B,MAAM,mBAAmB,GAAG,UAAI,CAAC,IAAI,CACnC,OAAC,CAAC,MAAM,CAAC;IACP,SAAS,EAAE,OAAC,CAAC,MAAM,EAAE;IACrB,KAAK,EAAE,OAAC,CAAC,MAAM,EAAE;CAClB,CAAC,CACH,CAAC;AAEF;;;;;GAKG;AACI,KAAK,UAAU,iBAAiB,CACrC,GAAW;IAEX,MAAM,eAAe,GAAG,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,EAAE,cAAc,CAAC,CAAC;IAElE,IAAI,OAAe,CAAC;IACpB,IAAI,CAAC;QACH,OAAO,GAAG,MAAM,EAAE,CAAC,QAAQ,CAAC,eAAe,EAAE,OAAO,CAAC,CAAC;IACxD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,MAAM,GAAG,mBAAmB,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACtD,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC;IAED,OAAO;QACL,SAAS,EAAE,MAAM,CAAC,IAAI,CAAC,SAAS;QAChC,MAAM,EAAE,MAAM,CAAC,IAAI,CAAC,KAAK;KAC1B,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
declare const IntrospectionResponse: z.ZodUnion<[z.ZodObject<{
|
|
3
|
+
active: z.ZodLiteral<true>;
|
|
4
|
+
client_id: z.ZodString;
|
|
5
|
+
session_id: z.ZodString;
|
|
6
|
+
}, "strip", z.ZodTypeAny, {
|
|
7
|
+
active: true;
|
|
8
|
+
client_id: string;
|
|
9
|
+
session_id: string;
|
|
10
|
+
}, {
|
|
11
|
+
active: true;
|
|
12
|
+
client_id: string;
|
|
13
|
+
session_id: string;
|
|
14
|
+
}>, z.ZodObject<{
|
|
15
|
+
active: z.ZodLiteral<false>;
|
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
|
17
|
+
active: false;
|
|
18
|
+
}, {
|
|
19
|
+
active: false;
|
|
20
|
+
}>]>;
|
|
21
|
+
export declare function OAuth(): Promise<{
|
|
22
|
+
/**
|
|
23
|
+
* Perform the Device Authorization Request
|
|
24
|
+
*
|
|
25
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.1
|
|
26
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.2
|
|
27
|
+
*/
|
|
28
|
+
deviceAuthorizationRequest(): Promise<DeviceAuthorizationRequest>;
|
|
29
|
+
/**
|
|
30
|
+
* Perform the Device Access Token Request
|
|
31
|
+
*
|
|
32
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.4
|
|
33
|
+
*/
|
|
34
|
+
deviceAccessTokenRequest(device_code: string): Promise<[Error] | [null, Response]>;
|
|
35
|
+
/**
|
|
36
|
+
* Process the Token request Response
|
|
37
|
+
*
|
|
38
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.5
|
|
39
|
+
*/
|
|
40
|
+
processTokenResponse(response: Response): Promise<[OAuthError] | [null, TokenSet]>;
|
|
41
|
+
/**
|
|
42
|
+
* Perform a Token Revocation Request.
|
|
43
|
+
*
|
|
44
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.1
|
|
45
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.2
|
|
46
|
+
*/
|
|
47
|
+
revokeToken(token: string): Promise<OAuthError | void>;
|
|
48
|
+
/**
|
|
49
|
+
* Perform Refresh Token Request.
|
|
50
|
+
*
|
|
51
|
+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-6
|
|
52
|
+
*/
|
|
53
|
+
refreshToken(token: string): Promise<TokenSet>;
|
|
54
|
+
/**
|
|
55
|
+
* Perform Token Introspection Request.
|
|
56
|
+
*
|
|
57
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7662#section-2.1
|
|
58
|
+
*/
|
|
59
|
+
introspectToken(token: string): Promise<z.infer<typeof IntrospectionResponse>>;
|
|
60
|
+
}>;
|
|
61
|
+
export type OAuth = Awaited<ReturnType<typeof OAuth>>;
|
|
62
|
+
declare const TokenSet: z.ZodObject<{
|
|
63
|
+
/** The access token issued by the authorization server. */
|
|
64
|
+
access_token: z.ZodString;
|
|
65
|
+
/** The type of the token issued */
|
|
66
|
+
token_type: z.ZodLiteral<"Bearer">;
|
|
67
|
+
/** The lifetime in seconds of the access token. */
|
|
68
|
+
expires_in: z.ZodNumber;
|
|
69
|
+
/** The refresh token, which can be used to obtain new access tokens. */
|
|
70
|
+
refresh_token: z.ZodOptional<z.ZodString>;
|
|
71
|
+
/** The scope of the access token. */
|
|
72
|
+
scope: z.ZodOptional<z.ZodString>;
|
|
73
|
+
}, "strip", z.ZodTypeAny, {
|
|
74
|
+
expires_in: number;
|
|
75
|
+
access_token: string;
|
|
76
|
+
token_type: "Bearer";
|
|
77
|
+
refresh_token?: string | undefined;
|
|
78
|
+
scope?: string | undefined;
|
|
79
|
+
}, {
|
|
80
|
+
expires_in: number;
|
|
81
|
+
access_token: string;
|
|
82
|
+
token_type: "Bearer";
|
|
83
|
+
refresh_token?: string | undefined;
|
|
84
|
+
scope?: string | undefined;
|
|
85
|
+
}>;
|
|
86
|
+
type TokenSet = z.infer<typeof TokenSet>;
|
|
87
|
+
declare const OAuthErrorResponse: z.ZodObject<{
|
|
88
|
+
error: z.ZodEnum<["invalid_request", "invalid_client", "invalid_grant", "unauthorized_client", "unsupported_grant_type", "invalid_scope", "server_error", "authorization_pending", "slow_down", "access_denied", "expired_token", "unsupported_token_type"]>;
|
|
89
|
+
error_description: z.ZodOptional<z.ZodString>;
|
|
90
|
+
error_uri: z.ZodOptional<z.ZodString>;
|
|
91
|
+
}, "strip", z.ZodTypeAny, {
|
|
92
|
+
error: "invalid_request" | "invalid_client" | "invalid_grant" | "unauthorized_client" | "unsupported_grant_type" | "invalid_scope" | "server_error" | "authorization_pending" | "slow_down" | "access_denied" | "expired_token" | "unsupported_token_type";
|
|
93
|
+
error_description?: string | undefined;
|
|
94
|
+
error_uri?: string | undefined;
|
|
95
|
+
}, {
|
|
96
|
+
error: "invalid_request" | "invalid_client" | "invalid_grant" | "unauthorized_client" | "unsupported_grant_type" | "invalid_scope" | "server_error" | "authorization_pending" | "slow_down" | "access_denied" | "expired_token" | "unsupported_token_type";
|
|
97
|
+
error_description?: string | undefined;
|
|
98
|
+
error_uri?: string | undefined;
|
|
99
|
+
}>;
|
|
100
|
+
type OAuthErrorResponse = z.infer<typeof OAuthErrorResponse>;
|
|
101
|
+
declare class OAuthError extends Error {
|
|
102
|
+
name: string;
|
|
103
|
+
code: OAuthErrorResponse["error"];
|
|
104
|
+
cause: Error;
|
|
105
|
+
constructor(message: string, response: unknown);
|
|
106
|
+
}
|
|
107
|
+
export declare function isOAuthError(error: unknown): error is OAuthError;
|
|
108
|
+
export interface DeviceAuthorizationRequest {
|
|
109
|
+
/** The device verification code. */
|
|
110
|
+
device_code: string;
|
|
111
|
+
/** The end-user verification code. */
|
|
112
|
+
user_code: string;
|
|
113
|
+
/**
|
|
114
|
+
* The minimum amount of time in seconds that the client
|
|
115
|
+
* SHOULD wait between polling requests to the token endpoint.
|
|
116
|
+
*/
|
|
117
|
+
interval: number;
|
|
118
|
+
/** The end-user verification URI on the authorization server. */
|
|
119
|
+
verification_uri: string;
|
|
120
|
+
/**
|
|
121
|
+
* The end-user verification URI on the authorization server,
|
|
122
|
+
* including the `user_code`, without redirection.
|
|
123
|
+
*/
|
|
124
|
+
verification_uri_complete: string;
|
|
125
|
+
/**
|
|
126
|
+
* The absolute lifetime of the `device_code` and `user_code`.
|
|
127
|
+
* Calculated from `expires_in`.
|
|
128
|
+
*/
|
|
129
|
+
expiresAt: number;
|
|
130
|
+
}
|
|
131
|
+
export {};
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.OAuth = OAuth;
|
|
7
|
+
exports.isOAuthError = isOAuthError;
|
|
8
|
+
const os_1 = __importDefault(require("os"));
|
|
9
|
+
const zod_1 = require("zod");
|
|
10
|
+
const version_1 = require("../version");
|
|
11
|
+
const USER_AGENT = `${os_1.default.hostname()} @ vercel/sandbox/${version_1.VERSION} node-${process.version} ${os_1.default.platform()} (${os_1.default.arch()})`;
|
|
12
|
+
const ISSUER = new URL("https://vercel.com");
|
|
13
|
+
const CLIENT_ID = "cl_HYyOPBNtFMfHhaUn9L4QPfTZz6TP47bp";
|
|
14
|
+
const AuthorizationServerMetadata = zod_1.z.object({
|
|
15
|
+
issuer: zod_1.z.string().url(),
|
|
16
|
+
device_authorization_endpoint: zod_1.z.string().url(),
|
|
17
|
+
token_endpoint: zod_1.z.string().url(),
|
|
18
|
+
revocation_endpoint: zod_1.z.string().url(),
|
|
19
|
+
jwks_uri: zod_1.z.string().url(),
|
|
20
|
+
introspection_endpoint: zod_1.z.string().url(),
|
|
21
|
+
});
|
|
22
|
+
let _as;
|
|
23
|
+
const DeviceAuthorization = zod_1.z.object({
|
|
24
|
+
device_code: zod_1.z.string(),
|
|
25
|
+
user_code: zod_1.z.string(),
|
|
26
|
+
verification_uri: zod_1.z.string().url(),
|
|
27
|
+
verification_uri_complete: zod_1.z.string().url(),
|
|
28
|
+
expires_in: zod_1.z.number(),
|
|
29
|
+
interval: zod_1.z.number(),
|
|
30
|
+
});
|
|
31
|
+
const IntrospectionResponse = zod_1.z
|
|
32
|
+
.object({
|
|
33
|
+
active: zod_1.z.literal(true),
|
|
34
|
+
client_id: zod_1.z.string(),
|
|
35
|
+
session_id: zod_1.z.string(),
|
|
36
|
+
})
|
|
37
|
+
.or(zod_1.z.object({ active: zod_1.z.literal(false) }));
|
|
38
|
+
/**
|
|
39
|
+
* Returns the Authorization Server Metadata
|
|
40
|
+
*
|
|
41
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest
|
|
42
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse
|
|
43
|
+
*/
|
|
44
|
+
async function authorizationServerMetadata() {
|
|
45
|
+
if (_as)
|
|
46
|
+
return _as;
|
|
47
|
+
const response = await fetch(new URL(".well-known/openid-configuration", ISSUER), {
|
|
48
|
+
headers: { "Content-Type": "application/json", "user-agent": USER_AGENT },
|
|
49
|
+
});
|
|
50
|
+
_as = AuthorizationServerMetadata.parse(await response.json());
|
|
51
|
+
return _as;
|
|
52
|
+
}
|
|
53
|
+
async function OAuth() {
|
|
54
|
+
const as = await authorizationServerMetadata();
|
|
55
|
+
return {
|
|
56
|
+
/**
|
|
57
|
+
* Perform the Device Authorization Request
|
|
58
|
+
*
|
|
59
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.1
|
|
60
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.2
|
|
61
|
+
*/
|
|
62
|
+
async deviceAuthorizationRequest() {
|
|
63
|
+
const response = await fetch(as.device_authorization_endpoint, {
|
|
64
|
+
method: "POST",
|
|
65
|
+
headers: {
|
|
66
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
67
|
+
"user-agent": USER_AGENT,
|
|
68
|
+
},
|
|
69
|
+
body: new URLSearchParams({
|
|
70
|
+
client_id: CLIENT_ID,
|
|
71
|
+
scope: "openid offline_access",
|
|
72
|
+
}),
|
|
73
|
+
});
|
|
74
|
+
const json = await response.json();
|
|
75
|
+
const parsed = DeviceAuthorization.safeParse(json);
|
|
76
|
+
if (!parsed.success) {
|
|
77
|
+
throw new OAuthError(`Failed to parse device authorization response: ${parsed.error.message}`, json);
|
|
78
|
+
}
|
|
79
|
+
return {
|
|
80
|
+
device_code: parsed.data.device_code,
|
|
81
|
+
user_code: parsed.data.user_code,
|
|
82
|
+
verification_uri: parsed.data.verification_uri,
|
|
83
|
+
verification_uri_complete: parsed.data.verification_uri_complete,
|
|
84
|
+
expiresAt: Date.now() + parsed.data.expires_in * 1000,
|
|
85
|
+
interval: parsed.data.interval,
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
/**
|
|
89
|
+
* Perform the Device Access Token Request
|
|
90
|
+
*
|
|
91
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.4
|
|
92
|
+
*/
|
|
93
|
+
async deviceAccessTokenRequest(device_code) {
|
|
94
|
+
try {
|
|
95
|
+
return [
|
|
96
|
+
null,
|
|
97
|
+
await fetch(as.token_endpoint, {
|
|
98
|
+
method: "POST",
|
|
99
|
+
headers: {
|
|
100
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
101
|
+
"user-agent": USER_AGENT,
|
|
102
|
+
},
|
|
103
|
+
body: new URLSearchParams({
|
|
104
|
+
client_id: CLIENT_ID,
|
|
105
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
106
|
+
device_code,
|
|
107
|
+
}),
|
|
108
|
+
signal: AbortSignal.timeout(10 * 1000),
|
|
109
|
+
}),
|
|
110
|
+
];
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error instanceof Error)
|
|
114
|
+
return [error];
|
|
115
|
+
return [
|
|
116
|
+
new Error("An unknown error occurred. See the logs for details.", {
|
|
117
|
+
cause: error,
|
|
118
|
+
}),
|
|
119
|
+
];
|
|
120
|
+
}
|
|
121
|
+
},
|
|
122
|
+
/**
|
|
123
|
+
* Process the Token request Response
|
|
124
|
+
*
|
|
125
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.5
|
|
126
|
+
*/
|
|
127
|
+
async processTokenResponse(response) {
|
|
128
|
+
const json = await response.json();
|
|
129
|
+
const processed = TokenSet.safeParse(json);
|
|
130
|
+
if (!processed.success) {
|
|
131
|
+
return [
|
|
132
|
+
new OAuthError(`Failed to parse token response: ${processed.error.message}`, json),
|
|
133
|
+
];
|
|
134
|
+
}
|
|
135
|
+
return [null, processed.data];
|
|
136
|
+
},
|
|
137
|
+
/**
|
|
138
|
+
* Perform a Token Revocation Request.
|
|
139
|
+
*
|
|
140
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.1
|
|
141
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.2
|
|
142
|
+
*/
|
|
143
|
+
async revokeToken(token) {
|
|
144
|
+
const response = await fetch(as.revocation_endpoint, {
|
|
145
|
+
method: "POST",
|
|
146
|
+
headers: {
|
|
147
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
148
|
+
"user-agent": USER_AGENT,
|
|
149
|
+
},
|
|
150
|
+
body: new URLSearchParams({ token, client_id: CLIENT_ID }),
|
|
151
|
+
});
|
|
152
|
+
if (response.ok)
|
|
153
|
+
return;
|
|
154
|
+
const json = await response.json();
|
|
155
|
+
return new OAuthError("Revocation request failed", json);
|
|
156
|
+
},
|
|
157
|
+
/**
|
|
158
|
+
* Perform Refresh Token Request.
|
|
159
|
+
*
|
|
160
|
+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-6
|
|
161
|
+
*/
|
|
162
|
+
async refreshToken(token) {
|
|
163
|
+
const response = await fetch(as.token_endpoint, {
|
|
164
|
+
method: "POST",
|
|
165
|
+
headers: {
|
|
166
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
167
|
+
"user-agent": USER_AGENT,
|
|
168
|
+
},
|
|
169
|
+
body: new URLSearchParams({
|
|
170
|
+
client_id: CLIENT_ID,
|
|
171
|
+
grant_type: "refresh_token",
|
|
172
|
+
refresh_token: token,
|
|
173
|
+
}),
|
|
174
|
+
});
|
|
175
|
+
const [tokensError, tokenSet] = await this.processTokenResponse(response);
|
|
176
|
+
if (tokensError)
|
|
177
|
+
throw tokensError;
|
|
178
|
+
return tokenSet;
|
|
179
|
+
},
|
|
180
|
+
/**
|
|
181
|
+
* Perform Token Introspection Request.
|
|
182
|
+
*
|
|
183
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7662#section-2.1
|
|
184
|
+
*/
|
|
185
|
+
async introspectToken(token) {
|
|
186
|
+
const response = await fetch(as.introspection_endpoint, {
|
|
187
|
+
method: "POST",
|
|
188
|
+
headers: {
|
|
189
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
190
|
+
"user-agent": USER_AGENT,
|
|
191
|
+
},
|
|
192
|
+
body: new URLSearchParams({ token }),
|
|
193
|
+
});
|
|
194
|
+
const json = await response.json();
|
|
195
|
+
const processed = IntrospectionResponse.safeParse(json);
|
|
196
|
+
if (!processed.success) {
|
|
197
|
+
throw new OAuthError(`Failed to parse introspection response: ${processed.error.message}`, json);
|
|
198
|
+
}
|
|
199
|
+
return processed.data;
|
|
200
|
+
},
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
const TokenSet = zod_1.z.object({
|
|
204
|
+
/** The access token issued by the authorization server. */
|
|
205
|
+
access_token: zod_1.z.string(),
|
|
206
|
+
/** The type of the token issued */
|
|
207
|
+
token_type: zod_1.z.literal("Bearer"),
|
|
208
|
+
/** The lifetime in seconds of the access token. */
|
|
209
|
+
expires_in: zod_1.z.number(),
|
|
210
|
+
/** The refresh token, which can be used to obtain new access tokens. */
|
|
211
|
+
refresh_token: zod_1.z.string().optional(),
|
|
212
|
+
/** The scope of the access token. */
|
|
213
|
+
scope: zod_1.z.string().optional(),
|
|
214
|
+
});
|
|
215
|
+
const OAuthErrorResponse = zod_1.z.object({
|
|
216
|
+
error: zod_1.z.enum([
|
|
217
|
+
"invalid_request",
|
|
218
|
+
"invalid_client",
|
|
219
|
+
"invalid_grant",
|
|
220
|
+
"unauthorized_client",
|
|
221
|
+
"unsupported_grant_type",
|
|
222
|
+
"invalid_scope",
|
|
223
|
+
"server_error",
|
|
224
|
+
// Device Authorization Response Errors
|
|
225
|
+
"authorization_pending",
|
|
226
|
+
"slow_down",
|
|
227
|
+
"access_denied",
|
|
228
|
+
"expired_token",
|
|
229
|
+
// Revocation Response Errors
|
|
230
|
+
"unsupported_token_type",
|
|
231
|
+
]),
|
|
232
|
+
error_description: zod_1.z.string().optional(),
|
|
233
|
+
error_uri: zod_1.z.string().optional(),
|
|
234
|
+
});
|
|
235
|
+
function processOAuthErrorResponse(json) {
|
|
236
|
+
try {
|
|
237
|
+
return OAuthErrorResponse.parse(json);
|
|
238
|
+
}
|
|
239
|
+
catch (error) {
|
|
240
|
+
if (error instanceof zod_1.z.ZodError) {
|
|
241
|
+
return new TypeError(`Invalid OAuth error response: ${error.message}`);
|
|
242
|
+
}
|
|
243
|
+
return new TypeError("Failed to parse OAuth error response");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
class OAuthError extends Error {
|
|
247
|
+
constructor(message, response) {
|
|
248
|
+
super(message);
|
|
249
|
+
this.name = "OAuthError";
|
|
250
|
+
const error = processOAuthErrorResponse(response);
|
|
251
|
+
if (error instanceof TypeError) {
|
|
252
|
+
const message = `Unexpected server response: ${JSON.stringify(response)}`;
|
|
253
|
+
this.cause = new Error(message, { cause: error });
|
|
254
|
+
this.code = "server_error";
|
|
255
|
+
return;
|
|
256
|
+
}
|
|
257
|
+
let cause = error.error;
|
|
258
|
+
if (error.error_description)
|
|
259
|
+
cause += `: ${error.error_description}`;
|
|
260
|
+
if (error.error_uri)
|
|
261
|
+
cause += ` (${error.error_uri})`;
|
|
262
|
+
this.cause = new Error(cause);
|
|
263
|
+
this.code = error.error;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
function isOAuthError(error) {
|
|
267
|
+
return error instanceof OAuthError;
|
|
268
|
+
}
|
|
269
|
+
//# sourceMappingURL=oauth.js.map
|
|
@@ -0,0 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,SAAS,CAAC,QAAQ,CAAC,EAAE,CAAC;YAC1E,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,OAAO,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC;YAClD,IAAI,CAAC,IAAI,GAAG,cAAc,CAAC;YAC3B,OAAO;QACT,CAAC;QACD,IAAI,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC;QACxB,IAAI,KAAK,CAAC,iBAAiB;YAAE,KAAK,IAAI,KAAK,KAAK,CAAC,iBAAiB,EAAE,CAAC;QACrE,IAAI,KAAK,CAAC,SAAS;YAAE,KAAK,IAAI,KAAK,KAAK,CAAC,SAAS,GAAG,CAAC;QAEtD,IAAI,CAAC,KAAK,GAAG,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;QAC9B,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC;IAC1B,CAAC;CACF;AAED,SAAgB,YAAY,CAAC,KAAc;IACzC,OAAO,KAAK,YAAY,UAAU,CAAC;AACrC,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { DeviceAuthorizationRequest, OAuth } from "./oauth";
|
|
2
|
+
export type PollTokenItem = {
|
|
3
|
+
_tag: "Timeout";
|
|
4
|
+
newInterval: number;
|
|
5
|
+
} | {
|
|
6
|
+
_tag: "SlowDown";
|
|
7
|
+
newInterval: number;
|
|
8
|
+
} | {
|
|
9
|
+
_tag: "Error";
|
|
10
|
+
error: Error;
|
|
11
|
+
} | {
|
|
12
|
+
_tag: "Response";
|
|
13
|
+
response: {
|
|
14
|
+
text(): Promise<string>;
|
|
15
|
+
};
|
|
16
|
+
};
|
|
17
|
+
export declare function pollForToken({ request, oauth, }: {
|
|
18
|
+
request: DeviceAuthorizationRequest;
|
|
19
|
+
oauth: OAuth;
|
|
20
|
+
}): AsyncGenerator<PollTokenItem, void, void>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pollForToken = pollForToken;
|
|
4
|
+
const promises_1 = require("node:timers/promises");
|
|
5
|
+
const file_1 = require("./file");
|
|
6
|
+
const oauth_1 = require("./oauth");
|
|
7
|
+
async function* pollForToken({ request, oauth, }) {
|
|
8
|
+
const controller = new AbortController();
|
|
9
|
+
try {
|
|
10
|
+
let intervalMs = request.interval * 1000;
|
|
11
|
+
while (Date.now() < request.expiresAt) {
|
|
12
|
+
const [tokenResponseError, tokenResponse] = await oauth.deviceAccessTokenRequest(request.device_code);
|
|
13
|
+
if (tokenResponseError) {
|
|
14
|
+
// 2x backoff on connection timeouts per spec https://datatracker.ietf.org/doc/html/rfc8628#section-3.5
|
|
15
|
+
if (tokenResponseError.message.includes("timeout")) {
|
|
16
|
+
intervalMs *= 2;
|
|
17
|
+
yield { _tag: "Timeout", newInterval: intervalMs };
|
|
18
|
+
await (0, promises_1.setTimeout)(intervalMs, { signal: controller.signal });
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
yield { _tag: "Error", error: tokenResponseError };
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
yield {
|
|
25
|
+
_tag: "Response",
|
|
26
|
+
response: tokenResponse.clone(),
|
|
27
|
+
};
|
|
28
|
+
const [tokensError, tokens] = await oauth.processTokenResponse(tokenResponse);
|
|
29
|
+
if ((0, oauth_1.isOAuthError)(tokensError)) {
|
|
30
|
+
const { code } = tokensError;
|
|
31
|
+
switch (code) {
|
|
32
|
+
case "authorization_pending":
|
|
33
|
+
await (0, promises_1.setTimeout)(intervalMs, { signal: controller.signal });
|
|
34
|
+
continue;
|
|
35
|
+
case "slow_down":
|
|
36
|
+
intervalMs += 5 * 1000;
|
|
37
|
+
yield { _tag: "SlowDown", newInterval: intervalMs };
|
|
38
|
+
await (0, promises_1.setTimeout)(intervalMs, { signal: controller.signal });
|
|
39
|
+
continue;
|
|
40
|
+
default:
|
|
41
|
+
yield { _tag: "Error", error: tokensError.cause };
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (tokensError) {
|
|
46
|
+
yield { _tag: "Error", error: tokensError };
|
|
47
|
+
return;
|
|
48
|
+
}
|
|
49
|
+
(0, file_1.updateAuthConfig)({
|
|
50
|
+
token: tokens.access_token,
|
|
51
|
+
expiresAt: new Date(Date.now() + tokens.expires_in * 1000),
|
|
52
|
+
refreshToken: tokens.refresh_token,
|
|
53
|
+
});
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
yield {
|
|
57
|
+
_tag: "Error",
|
|
58
|
+
error: new Error("Timed out waiting for authentication. Please try again."),
|
|
59
|
+
};
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
finally {
|
|
63
|
+
controller.abort();
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=poll-for-token.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"poll-for-token.js","sourceRoot":"","sources":["../../src/auth/poll-for-token.ts"],"names":[],"mappings":";;AAaA,oCA2EC;AAxFD,mDAAkD;AAClD,iCAA0C;AAC1C,mCAA0E;AAWnE,KAAK,SAAS,CAAC,CAAC,YAAY,CAAC,EAClC,OAAO,EACP,KAAK,GAIN;IACC,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,IAAI,CAAC;QACH,IAAI,UAAU,GAAG,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC;QACzC,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,OAAO,CAAC,SAAS,EAAE,CAAC;YACtC,MAAM,CAAC,kBAAkB,EAAE,aAAa,CAAC,GACvC,MAAM,KAAK,CAAC,wBAAwB,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;YAE5D,IAAI,kBAAkB,EAAE,CAAC;gBACvB,uGAAuG;gBACvG,IAAI,kBAAkB,CAAC,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAA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I,EAAE,OAAO,EAAE,KAAK,EAAE,WAAW,EAAE,CAAC;gBAC5C,OAAO;YACT,CAAC;YAED,IAAA,uBAAgB,EAAC;gBACf,KAAK,EAAE,MAAM,CAAC,YAAY;gBAC1B,SAAS,EAAE,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,MAAM,CAAC,UAAU,GAAG,IAAI,CAAC;gBAC1D,YAAY,EAAE,MAAM,CAAC,aAAa;aACnC,CAAC,CAAC;YAEH,OAAO;QACT,CAAC;QAED,MAAM;YACJ,IAAI,EAAE,OAAgB;YACtB,KAAK,EAAE,IAAI,KAAK,CACd,yDAAyD,CAC1D;SACF,CAAC;QACF,OAAO;IACT,CAAC;YAAS,CAAC;QACT,UAAU,CAAC,KAAK,EAAE,CAAC;IACrB,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the team and project scope for sandbox operations.
|
|
3
|
+
*
|
|
4
|
+
* First checks for a locally linked project in `.vercel/project.json`.
|
|
5
|
+
* If found, uses the `projectId` and `orgId` from there.
|
|
6
|
+
*
|
|
7
|
+
* Otherwise, if `teamId` is not provided, selects the first available team for the account.
|
|
8
|
+
* Ensures a default project exists within the team, creating it if necessary.
|
|
9
|
+
*
|
|
10
|
+
* @param opts.token - Vercel API authentication token.
|
|
11
|
+
* @param opts.teamId - Optional team slug. If omitted, the first team is selected.
|
|
12
|
+
* @param opts.cwd - Optional directory to search for `.vercel/project.json`. Defaults to `process.cwd()`.
|
|
13
|
+
* @returns The resolved scope with `projectId`, `teamId`, and whether the project was `created`.
|
|
14
|
+
*
|
|
15
|
+
* @throws {NotOk} If the API returns an error other than 404 when checking the project.
|
|
16
|
+
* @throws {ZodError} If no teams exist for the account.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* const scope = await inferScope({ token: "vercel_..." });
|
|
21
|
+
* // => { projectId: "vercel-sandbox-default-project", teamId: "my-team", created: false }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export declare function inferScope(opts: {
|
|
25
|
+
token: string;
|
|
26
|
+
teamId?: string;
|
|
27
|
+
cwd?: string;
|
|
28
|
+
}): Promise<{
|
|
29
|
+
projectId: string;
|
|
30
|
+
teamId: string;
|
|
31
|
+
created: boolean;
|
|
32
|
+
}>;
|
|
33
|
+
/**
|
|
34
|
+
* Selects a team for the current token by querying the Teams API and
|
|
35
|
+
* returning the slug of the first team in the result set.
|
|
36
|
+
*
|
|
37
|
+
* @param token - Authentication token used to call the Vercel API.
|
|
38
|
+
* @returns A promise that resolves to the first team's slug.
|
|
39
|
+
*/
|
|
40
|
+
export declare function selectTeam(token: string): Promise<string>;
|