@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,86 @@
|
|
|
1
|
+
import { readLinkedProject } from "./linked-project";
|
|
2
|
+
import { describe, test, expect } from "vitest";
|
|
3
|
+
import * as fs from "node:fs/promises";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import * as os from "node:os";
|
|
6
|
+
|
|
7
|
+
async function withTempDir(fn: (dir: string) => Promise<void>): Promise<void> {
|
|
8
|
+
const dir = await fs.mkdtemp(path.join(os.tmpdir(), "linked-project-test-"));
|
|
9
|
+
try {
|
|
10
|
+
await fn(dir);
|
|
11
|
+
} finally {
|
|
12
|
+
await fs.rm(dir, { recursive: true });
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
describe("readLinkedProject", () => {
|
|
17
|
+
test("returns null when .vercel/project.json does not exist", async () => {
|
|
18
|
+
await withTempDir(async (dir) => {
|
|
19
|
+
const result = await readLinkedProject(dir);
|
|
20
|
+
expect(result).toBeNull();
|
|
21
|
+
});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test("returns null when .vercel directory exists but project.json does not", async () => {
|
|
25
|
+
await withTempDir(async (dir) => {
|
|
26
|
+
await fs.mkdir(path.join(dir, ".vercel"));
|
|
27
|
+
const result = await readLinkedProject(dir);
|
|
28
|
+
expect(result).toBeNull();
|
|
29
|
+
});
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test("returns projectId and teamId when project.json exists", async () => {
|
|
33
|
+
await withTempDir(async (dir) => {
|
|
34
|
+
await fs.mkdir(path.join(dir, ".vercel"));
|
|
35
|
+
await fs.writeFile(
|
|
36
|
+
path.join(dir, ".vercel", "project.json"),
|
|
37
|
+
JSON.stringify({
|
|
38
|
+
projectId: "prj_123",
|
|
39
|
+
orgId: "team_456",
|
|
40
|
+
settings: { framework: null },
|
|
41
|
+
}),
|
|
42
|
+
);
|
|
43
|
+
const result = await readLinkedProject(dir);
|
|
44
|
+
expect(result).toEqual({
|
|
45
|
+
projectId: "prj_123",
|
|
46
|
+
teamId: "team_456",
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
test("returns null when project.json is invalid JSON", async () => {
|
|
52
|
+
await withTempDir(async (dir) => {
|
|
53
|
+
await fs.mkdir(path.join(dir, ".vercel"));
|
|
54
|
+
await fs.writeFile(
|
|
55
|
+
path.join(dir, ".vercel", "project.json"),
|
|
56
|
+
"not valid json",
|
|
57
|
+
);
|
|
58
|
+
const result = await readLinkedProject(dir);
|
|
59
|
+
expect(result).toBeNull();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("returns null when project.json is missing projectId", async () => {
|
|
64
|
+
await withTempDir(async (dir) => {
|
|
65
|
+
await fs.mkdir(path.join(dir, ".vercel"));
|
|
66
|
+
await fs.writeFile(
|
|
67
|
+
path.join(dir, ".vercel", "project.json"),
|
|
68
|
+
JSON.stringify({ orgId: "team_456" }),
|
|
69
|
+
);
|
|
70
|
+
const result = await readLinkedProject(dir);
|
|
71
|
+
expect(result).toBeNull();
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test("returns null when project.json is missing orgId", async () => {
|
|
76
|
+
await withTempDir(async (dir) => {
|
|
77
|
+
await fs.mkdir(path.join(dir, ".vercel"));
|
|
78
|
+
await fs.writeFile(
|
|
79
|
+
path.join(dir, ".vercel", "project.json"),
|
|
80
|
+
JSON.stringify({ projectId: "prj_123" }),
|
|
81
|
+
);
|
|
82
|
+
const result = await readLinkedProject(dir);
|
|
83
|
+
expect(result).toBeNull();
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import * as fs from "node:fs/promises";
|
|
3
|
+
import * as path from "node:path";
|
|
4
|
+
import { json } from "./zod";
|
|
5
|
+
|
|
6
|
+
const LinkedProjectSchema = json.pipe(
|
|
7
|
+
z.object({
|
|
8
|
+
projectId: z.string(),
|
|
9
|
+
orgId: z.string(),
|
|
10
|
+
}),
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Reads the linked project configuration from `.vercel/project.json`.
|
|
15
|
+
*
|
|
16
|
+
* @param cwd - The directory to search for `.vercel/project.json`.
|
|
17
|
+
* @returns The linked project's `projectId` and `teamId`, or `null` if not found.
|
|
18
|
+
*/
|
|
19
|
+
export async function readLinkedProject(
|
|
20
|
+
cwd: string,
|
|
21
|
+
): Promise<{ projectId: string; teamId: string } | null> {
|
|
22
|
+
const projectJsonPath = path.join(cwd, ".vercel", "project.json");
|
|
23
|
+
|
|
24
|
+
let content: string;
|
|
25
|
+
try {
|
|
26
|
+
content = await fs.readFile(projectJsonPath, "utf-8");
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const parsed = LinkedProjectSchema.safeParse(content);
|
|
32
|
+
if (!parsed.success) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
projectId: parsed.data.projectId,
|
|
38
|
+
teamId: parsed.data.orgId,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
import os from "os";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { VERSION } from "../version";
|
|
4
|
+
|
|
5
|
+
const USER_AGENT = `${os.hostname()} @ vercel/sandbox/${VERSION} node-${
|
|
6
|
+
process.version
|
|
7
|
+
} ${os.platform()} (${os.arch()})`;
|
|
8
|
+
|
|
9
|
+
const ISSUER = new URL("https://vercel.com");
|
|
10
|
+
const CLIENT_ID = "cl_HYyOPBNtFMfHhaUn9L4QPfTZz6TP47bp";
|
|
11
|
+
|
|
12
|
+
const AuthorizationServerMetadata = z.object({
|
|
13
|
+
issuer: z.string().url(),
|
|
14
|
+
device_authorization_endpoint: z.string().url(),
|
|
15
|
+
token_endpoint: z.string().url(),
|
|
16
|
+
revocation_endpoint: z.string().url(),
|
|
17
|
+
jwks_uri: z.string().url(),
|
|
18
|
+
introspection_endpoint: z.string().url(),
|
|
19
|
+
});
|
|
20
|
+
type AuthorizationServerMetadata = z.infer<typeof AuthorizationServerMetadata>;
|
|
21
|
+
let _as: AuthorizationServerMetadata;
|
|
22
|
+
|
|
23
|
+
const DeviceAuthorization = z.object({
|
|
24
|
+
device_code: z.string(),
|
|
25
|
+
user_code: z.string(),
|
|
26
|
+
verification_uri: z.string().url(),
|
|
27
|
+
verification_uri_complete: z.string().url(),
|
|
28
|
+
expires_in: z.number(),
|
|
29
|
+
interval: z.number(),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const IntrospectionResponse = z
|
|
33
|
+
.object({
|
|
34
|
+
active: z.literal(true),
|
|
35
|
+
client_id: z.string(),
|
|
36
|
+
session_id: z.string(),
|
|
37
|
+
})
|
|
38
|
+
.or(z.object({ active: z.literal(false) }));
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Returns the Authorization Server Metadata
|
|
42
|
+
*
|
|
43
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest
|
|
44
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse
|
|
45
|
+
*/
|
|
46
|
+
async function authorizationServerMetadata(): Promise<AuthorizationServerMetadata> {
|
|
47
|
+
if (_as) return _as;
|
|
48
|
+
|
|
49
|
+
const response = await fetch(
|
|
50
|
+
new URL(".well-known/openid-configuration", ISSUER),
|
|
51
|
+
{
|
|
52
|
+
headers: { "Content-Type": "application/json", "user-agent": USER_AGENT },
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
_as = AuthorizationServerMetadata.parse(await response.json());
|
|
57
|
+
return _as;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function OAuth() {
|
|
61
|
+
const as = await authorizationServerMetadata();
|
|
62
|
+
return {
|
|
63
|
+
/**
|
|
64
|
+
* Perform the Device Authorization Request
|
|
65
|
+
*
|
|
66
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.1
|
|
67
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.2
|
|
68
|
+
*/
|
|
69
|
+
async deviceAuthorizationRequest(): Promise<DeviceAuthorizationRequest> {
|
|
70
|
+
const response = await fetch(as.device_authorization_endpoint, {
|
|
71
|
+
method: "POST",
|
|
72
|
+
headers: {
|
|
73
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
74
|
+
"user-agent": USER_AGENT,
|
|
75
|
+
},
|
|
76
|
+
body: new URLSearchParams({
|
|
77
|
+
client_id: CLIENT_ID,
|
|
78
|
+
scope: "openid offline_access",
|
|
79
|
+
}),
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const json = await response.json();
|
|
83
|
+
const parsed = DeviceAuthorization.safeParse(json);
|
|
84
|
+
|
|
85
|
+
if (!parsed.success) {
|
|
86
|
+
throw new OAuthError(
|
|
87
|
+
`Failed to parse device authorization response: ${parsed.error.message}`,
|
|
88
|
+
json,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return {
|
|
93
|
+
device_code: parsed.data.device_code,
|
|
94
|
+
user_code: parsed.data.user_code,
|
|
95
|
+
verification_uri: parsed.data.verification_uri,
|
|
96
|
+
verification_uri_complete: parsed.data.verification_uri_complete,
|
|
97
|
+
expiresAt: Date.now() + parsed.data.expires_in * 1000,
|
|
98
|
+
interval: parsed.data.interval,
|
|
99
|
+
};
|
|
100
|
+
},
|
|
101
|
+
/**
|
|
102
|
+
* Perform the Device Access Token Request
|
|
103
|
+
*
|
|
104
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.4
|
|
105
|
+
*/
|
|
106
|
+
async deviceAccessTokenRequest(
|
|
107
|
+
device_code: string,
|
|
108
|
+
): Promise<[Error] | [null, Response]> {
|
|
109
|
+
try {
|
|
110
|
+
return [
|
|
111
|
+
null,
|
|
112
|
+
await fetch(as.token_endpoint, {
|
|
113
|
+
method: "POST",
|
|
114
|
+
headers: {
|
|
115
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
116
|
+
"user-agent": USER_AGENT,
|
|
117
|
+
},
|
|
118
|
+
body: new URLSearchParams({
|
|
119
|
+
client_id: CLIENT_ID,
|
|
120
|
+
grant_type: "urn:ietf:params:oauth:grant-type:device_code",
|
|
121
|
+
device_code,
|
|
122
|
+
}),
|
|
123
|
+
signal: AbortSignal.timeout(10 * 1000),
|
|
124
|
+
}),
|
|
125
|
+
];
|
|
126
|
+
} catch (error) {
|
|
127
|
+
if (error instanceof Error) return [error];
|
|
128
|
+
return [
|
|
129
|
+
new Error("An unknown error occurred. See the logs for details.", {
|
|
130
|
+
cause: error,
|
|
131
|
+
}),
|
|
132
|
+
];
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
/**
|
|
136
|
+
* Process the Token request Response
|
|
137
|
+
*
|
|
138
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.5
|
|
139
|
+
*/
|
|
140
|
+
async processTokenResponse(
|
|
141
|
+
response: Response,
|
|
142
|
+
): Promise<[OAuthError] | [null, TokenSet]> {
|
|
143
|
+
const json = await response.json();
|
|
144
|
+
const processed = TokenSet.safeParse(json);
|
|
145
|
+
|
|
146
|
+
if (!processed.success) {
|
|
147
|
+
return [
|
|
148
|
+
new OAuthError(
|
|
149
|
+
`Failed to parse token response: ${processed.error.message}`,
|
|
150
|
+
json,
|
|
151
|
+
),
|
|
152
|
+
];
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return [null, processed.data];
|
|
156
|
+
},
|
|
157
|
+
/**
|
|
158
|
+
* Perform a Token Revocation Request.
|
|
159
|
+
*
|
|
160
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.1
|
|
161
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.2
|
|
162
|
+
*/
|
|
163
|
+
async revokeToken(token: string): Promise<OAuthError | void> {
|
|
164
|
+
const response = await fetch(as.revocation_endpoint, {
|
|
165
|
+
method: "POST",
|
|
166
|
+
headers: {
|
|
167
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
168
|
+
"user-agent": USER_AGENT,
|
|
169
|
+
},
|
|
170
|
+
body: new URLSearchParams({ token, client_id: CLIENT_ID }),
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
if (response.ok) return;
|
|
174
|
+
const json = await response.json();
|
|
175
|
+
|
|
176
|
+
return new OAuthError("Revocation request failed", json);
|
|
177
|
+
},
|
|
178
|
+
/**
|
|
179
|
+
* Perform Refresh Token Request.
|
|
180
|
+
*
|
|
181
|
+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-6
|
|
182
|
+
*/
|
|
183
|
+
async refreshToken(token: string): Promise<TokenSet> {
|
|
184
|
+
const response = await fetch(as.token_endpoint, {
|
|
185
|
+
method: "POST",
|
|
186
|
+
headers: {
|
|
187
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
188
|
+
"user-agent": USER_AGENT,
|
|
189
|
+
},
|
|
190
|
+
body: new URLSearchParams({
|
|
191
|
+
client_id: CLIENT_ID,
|
|
192
|
+
grant_type: "refresh_token",
|
|
193
|
+
refresh_token: token,
|
|
194
|
+
}),
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
const [tokensError, tokenSet] = await this.processTokenResponse(response);
|
|
198
|
+
if (tokensError) throw tokensError;
|
|
199
|
+
return tokenSet;
|
|
200
|
+
},
|
|
201
|
+
/**
|
|
202
|
+
* Perform Token Introspection Request.
|
|
203
|
+
*
|
|
204
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7662#section-2.1
|
|
205
|
+
*/
|
|
206
|
+
async introspectToken(
|
|
207
|
+
token: string,
|
|
208
|
+
): Promise<z.infer<typeof IntrospectionResponse>> {
|
|
209
|
+
const response = await fetch(as.introspection_endpoint, {
|
|
210
|
+
method: "POST",
|
|
211
|
+
headers: {
|
|
212
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
213
|
+
"user-agent": USER_AGENT,
|
|
214
|
+
},
|
|
215
|
+
body: new URLSearchParams({ token }),
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
const json = await response.json();
|
|
219
|
+
const processed = IntrospectionResponse.safeParse(json);
|
|
220
|
+
if (!processed.success) {
|
|
221
|
+
throw new OAuthError(
|
|
222
|
+
`Failed to parse introspection response: ${processed.error.message}`,
|
|
223
|
+
json,
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return processed.data;
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
export type OAuth = Awaited<ReturnType<typeof OAuth>>;
|
|
233
|
+
|
|
234
|
+
const TokenSet = z.object({
|
|
235
|
+
/** The access token issued by the authorization server. */
|
|
236
|
+
access_token: z.string(),
|
|
237
|
+
/** The type of the token issued */
|
|
238
|
+
token_type: z.literal("Bearer"),
|
|
239
|
+
/** The lifetime in seconds of the access token. */
|
|
240
|
+
expires_in: z.number(),
|
|
241
|
+
/** The refresh token, which can be used to obtain new access tokens. */
|
|
242
|
+
refresh_token: z.string().optional(),
|
|
243
|
+
/** The scope of the access token. */
|
|
244
|
+
scope: z.string().optional(),
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
type TokenSet = z.infer<typeof TokenSet>;
|
|
248
|
+
|
|
249
|
+
const OAuthErrorResponse = z.object({
|
|
250
|
+
error: z.enum([
|
|
251
|
+
"invalid_request",
|
|
252
|
+
"invalid_client",
|
|
253
|
+
"invalid_grant",
|
|
254
|
+
"unauthorized_client",
|
|
255
|
+
"unsupported_grant_type",
|
|
256
|
+
"invalid_scope",
|
|
257
|
+
"server_error",
|
|
258
|
+
// Device Authorization Response Errors
|
|
259
|
+
"authorization_pending",
|
|
260
|
+
"slow_down",
|
|
261
|
+
"access_denied",
|
|
262
|
+
"expired_token",
|
|
263
|
+
// Revocation Response Errors
|
|
264
|
+
"unsupported_token_type",
|
|
265
|
+
]),
|
|
266
|
+
error_description: z.string().optional(),
|
|
267
|
+
error_uri: z.string().optional(),
|
|
268
|
+
});
|
|
269
|
+
|
|
270
|
+
type OAuthErrorResponse = z.infer<typeof OAuthErrorResponse>;
|
|
271
|
+
|
|
272
|
+
function processOAuthErrorResponse(
|
|
273
|
+
json: unknown,
|
|
274
|
+
): OAuthErrorResponse | TypeError {
|
|
275
|
+
try {
|
|
276
|
+
return OAuthErrorResponse.parse(json);
|
|
277
|
+
} catch (error) {
|
|
278
|
+
if (error instanceof z.ZodError) {
|
|
279
|
+
return new TypeError(`Invalid OAuth error response: ${error.message}`);
|
|
280
|
+
}
|
|
281
|
+
return new TypeError("Failed to parse OAuth error response");
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
class OAuthError extends Error {
|
|
286
|
+
name = "OAuthError";
|
|
287
|
+
code: OAuthErrorResponse["error"];
|
|
288
|
+
cause: Error;
|
|
289
|
+
constructor(message: string, response: unknown) {
|
|
290
|
+
super(message);
|
|
291
|
+
const error = processOAuthErrorResponse(response);
|
|
292
|
+
if (error instanceof TypeError) {
|
|
293
|
+
const message = `Unexpected server response: ${JSON.stringify(response)}`;
|
|
294
|
+
this.cause = new Error(message, { cause: error });
|
|
295
|
+
this.code = "server_error";
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
let cause = error.error;
|
|
299
|
+
if (error.error_description) cause += `: ${error.error_description}`;
|
|
300
|
+
if (error.error_uri) cause += ` (${error.error_uri})`;
|
|
301
|
+
|
|
302
|
+
this.cause = new Error(cause);
|
|
303
|
+
this.code = error.error;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function isOAuthError(error: unknown): error is OAuthError {
|
|
308
|
+
return error instanceof OAuthError;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
export interface DeviceAuthorizationRequest {
|
|
312
|
+
/** The device verification code. */
|
|
313
|
+
device_code: string;
|
|
314
|
+
/** The end-user verification code. */
|
|
315
|
+
user_code: string;
|
|
316
|
+
/**
|
|
317
|
+
* The minimum amount of time in seconds that the client
|
|
318
|
+
* SHOULD wait between polling requests to the token endpoint.
|
|
319
|
+
*/
|
|
320
|
+
interval: number;
|
|
321
|
+
/** The end-user verification URI on the authorization server. */
|
|
322
|
+
verification_uri: string;
|
|
323
|
+
/**
|
|
324
|
+
* The end-user verification URI on the authorization server,
|
|
325
|
+
* including the `user_code`, without redirection.
|
|
326
|
+
*/
|
|
327
|
+
verification_uri_complete: string;
|
|
328
|
+
/**
|
|
329
|
+
* The absolute lifetime of the `device_code` and `user_code`.
|
|
330
|
+
* Calculated from `expires_in`.
|
|
331
|
+
*/
|
|
332
|
+
expiresAt: number;
|
|
333
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { setTimeout } from "node:timers/promises";
|
|
2
|
+
import { updateAuthConfig } from "./file";
|
|
3
|
+
import { DeviceAuthorizationRequest, isOAuthError, OAuth } from "./oauth";
|
|
4
|
+
|
|
5
|
+
export type PollTokenItem =
|
|
6
|
+
| { _tag: "Timeout"; newInterval: number }
|
|
7
|
+
| { _tag: "SlowDown"; newInterval: number }
|
|
8
|
+
| { _tag: "Error"; error: Error }
|
|
9
|
+
| {
|
|
10
|
+
_tag: "Response";
|
|
11
|
+
response: { text(): Promise<string> };
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export async function* pollForToken({
|
|
15
|
+
request,
|
|
16
|
+
oauth,
|
|
17
|
+
}: {
|
|
18
|
+
request: DeviceAuthorizationRequest;
|
|
19
|
+
oauth: OAuth;
|
|
20
|
+
}): AsyncGenerator<PollTokenItem, void, void> {
|
|
21
|
+
const controller = new AbortController();
|
|
22
|
+
try {
|
|
23
|
+
let intervalMs = request.interval * 1000;
|
|
24
|
+
while (Date.now() < request.expiresAt) {
|
|
25
|
+
const [tokenResponseError, tokenResponse] =
|
|
26
|
+
await oauth.deviceAccessTokenRequest(request.device_code);
|
|
27
|
+
|
|
28
|
+
if (tokenResponseError) {
|
|
29
|
+
// 2x backoff on connection timeouts per spec https://datatracker.ietf.org/doc/html/rfc8628#section-3.5
|
|
30
|
+
if (tokenResponseError.message.includes("timeout")) {
|
|
31
|
+
intervalMs *= 2;
|
|
32
|
+
yield { _tag: "Timeout" as const, newInterval: intervalMs };
|
|
33
|
+
await setTimeout(intervalMs, { signal: controller.signal });
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
yield { _tag: "Error" as const, error: tokenResponseError };
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
yield {
|
|
41
|
+
_tag: "Response" as const,
|
|
42
|
+
response: tokenResponse.clone() as { text(): Promise<string> },
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const [tokensError, tokens] =
|
|
46
|
+
await oauth.processTokenResponse(tokenResponse);
|
|
47
|
+
|
|
48
|
+
if (isOAuthError(tokensError)) {
|
|
49
|
+
const { code } = tokensError;
|
|
50
|
+
switch (code) {
|
|
51
|
+
case "authorization_pending":
|
|
52
|
+
await setTimeout(intervalMs, { signal: controller.signal });
|
|
53
|
+
continue;
|
|
54
|
+
case "slow_down":
|
|
55
|
+
intervalMs += 5 * 1000;
|
|
56
|
+
yield { _tag: "SlowDown" as const, newInterval: intervalMs };
|
|
57
|
+
await setTimeout(intervalMs, { signal: controller.signal });
|
|
58
|
+
continue;
|
|
59
|
+
default:
|
|
60
|
+
yield { _tag: "Error", error: tokensError.cause };
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (tokensError) {
|
|
66
|
+
yield { _tag: "Error", error: tokensError };
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
updateAuthConfig({
|
|
71
|
+
token: tokens.access_token,
|
|
72
|
+
expiresAt: new Date(Date.now() + tokens.expires_in * 1000),
|
|
73
|
+
refreshToken: tokens.refresh_token,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
yield {
|
|
80
|
+
_tag: "Error" as const,
|
|
81
|
+
error: new Error(
|
|
82
|
+
"Timed out waiting for authentication. Please try again.",
|
|
83
|
+
),
|
|
84
|
+
};
|
|
85
|
+
return;
|
|
86
|
+
} finally {
|
|
87
|
+
controller.abort();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { fetchApi } from "./api";
|
|
3
|
+
import { NotOk } from "./error";
|
|
4
|
+
import { readLinkedProject } from "./linked-project";
|
|
5
|
+
|
|
6
|
+
const TeamsSchema = z.object({
|
|
7
|
+
teams: z
|
|
8
|
+
.array(
|
|
9
|
+
z.object({
|
|
10
|
+
slug: z.string(),
|
|
11
|
+
}),
|
|
12
|
+
)
|
|
13
|
+
.min(1, `No teams found. Please create a team first.`),
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
const DEFAULT_PROJECT_NAME = "vercel-sandbox-default-project";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Resolves the team and project scope for sandbox operations.
|
|
20
|
+
*
|
|
21
|
+
* First checks for a locally linked project in `.vercel/project.json`.
|
|
22
|
+
* If found, uses the `projectId` and `orgId` from there.
|
|
23
|
+
*
|
|
24
|
+
* Otherwise, if `teamId` is not provided, selects the first available team for the account.
|
|
25
|
+
* Ensures a default project exists within the team, creating it if necessary.
|
|
26
|
+
*
|
|
27
|
+
* @param opts.token - Vercel API authentication token.
|
|
28
|
+
* @param opts.teamId - Optional team slug. If omitted, the first team is selected.
|
|
29
|
+
* @param opts.cwd - Optional directory to search for `.vercel/project.json`. Defaults to `process.cwd()`.
|
|
30
|
+
* @returns The resolved scope with `projectId`, `teamId`, and whether the project was `created`.
|
|
31
|
+
*
|
|
32
|
+
* @throws {NotOk} If the API returns an error other than 404 when checking the project.
|
|
33
|
+
* @throws {ZodError} If no teams exist for the account.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* const scope = await inferScope({ token: "vercel_..." });
|
|
38
|
+
* // => { projectId: "vercel-sandbox-default-project", teamId: "my-team", created: false }
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
export async function inferScope(opts: {
|
|
42
|
+
token: string;
|
|
43
|
+
teamId?: string;
|
|
44
|
+
cwd?: string;
|
|
45
|
+
}): Promise<{ projectId: string; teamId: string; created: boolean }> {
|
|
46
|
+
const linkedProject = await readLinkedProject(opts.cwd ?? process.cwd());
|
|
47
|
+
if (linkedProject) {
|
|
48
|
+
return { ...linkedProject, created: false };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const teamId = opts.teamId ?? (await selectTeam(opts.token));
|
|
52
|
+
|
|
53
|
+
let created = false;
|
|
54
|
+
try {
|
|
55
|
+
await fetchApi({
|
|
56
|
+
token: opts.token,
|
|
57
|
+
endpoint: `/v2/projects/${encodeURIComponent(DEFAULT_PROJECT_NAME)}?slug=${encodeURIComponent(teamId)}`,
|
|
58
|
+
});
|
|
59
|
+
} catch (e) {
|
|
60
|
+
if (!(e instanceof NotOk) || e.response.statusCode !== 404) {
|
|
61
|
+
throw e;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
await fetchApi({
|
|
65
|
+
token: opts.token,
|
|
66
|
+
endpoint: `/v11/projects?slug=${encodeURIComponent(teamId)}`,
|
|
67
|
+
method: "POST",
|
|
68
|
+
body: JSON.stringify({
|
|
69
|
+
name: DEFAULT_PROJECT_NAME,
|
|
70
|
+
}),
|
|
71
|
+
});
|
|
72
|
+
created = true;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
return { projectId: DEFAULT_PROJECT_NAME, teamId, created };
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Selects a team for the current token by querying the Teams API and
|
|
80
|
+
* returning the slug of the first team in the result set.
|
|
81
|
+
*
|
|
82
|
+
* @param token - Authentication token used to call the Vercel API.
|
|
83
|
+
* @returns A promise that resolves to the first team's slug.
|
|
84
|
+
*/
|
|
85
|
+
export async function selectTeam(token: string) {
|
|
86
|
+
const {
|
|
87
|
+
teams: [team],
|
|
88
|
+
} = await fetchApi({ token, endpoint: "/v2/teams?limit=1" }).then(
|
|
89
|
+
TeamsSchema.parse,
|
|
90
|
+
);
|
|
91
|
+
return team.slug;
|
|
92
|
+
}
|
package/src/auth/zod.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A Zod codec that serializes and deserializes JSON strings.
|
|
5
|
+
*/
|
|
6
|
+
export const json = z.string().transform((jsonString: string, ctx): unknown => {
|
|
7
|
+
try {
|
|
8
|
+
return JSON.parse(jsonString);
|
|
9
|
+
} catch (err: any) {
|
|
10
|
+
ctx.addIssue({
|
|
11
|
+
code: z.ZodIssueCode.custom,
|
|
12
|
+
message: `Invalid JSON: ${err.message}`,
|
|
13
|
+
});
|
|
14
|
+
return z.NEVER;
|
|
15
|
+
}
|
|
16
|
+
});
|