@vercel/cli-auth 0.1.0-canary.20260211174907.cdd2da6 → 0.1.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/credentials-store.d.ts +59 -0
- package/credentials-store.js +450 -0
- package/credentials-store.js.map +1 -0
- package/credentials-store.ts +494 -16
- package/oauth.d.ts +207 -0
- package/oauth.js +311 -0
- package/oauth.js.map +1 -0
- package/package.json +2 -1
- package/sso.d.ts +24 -0
- package/sso.js +136 -0
- package/sso.js.map +1 -0
- package/user-agent.d.ts +13 -0
- package/user-agent.js +20 -0
- package/user-agent.js.map +1 -0
package/oauth.d.ts
ADDED
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import * as z from 'zod/mini';
|
|
2
|
+
declare const AuthorizationServerMetadata: z.ZodMiniObject<{
|
|
3
|
+
issuer: z.ZodMiniURL;
|
|
4
|
+
device_authorization_endpoint: z.ZodMiniURL;
|
|
5
|
+
token_endpoint: z.ZodMiniURL;
|
|
6
|
+
revocation_endpoint: z.ZodMiniURL;
|
|
7
|
+
jwks_uri: z.ZodMiniURL;
|
|
8
|
+
introspection_endpoint: z.ZodMiniURL;
|
|
9
|
+
}, z.core.$strip>;
|
|
10
|
+
export type AuthorizationServerMetadata = z.infer<typeof AuthorizationServerMetadata>;
|
|
11
|
+
declare const IntrospectionResponse: z.ZodMiniObject<{
|
|
12
|
+
active: z.ZodMiniBoolean<boolean>;
|
|
13
|
+
client_id: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
14
|
+
session_id: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
15
|
+
}, z.core.$strip>;
|
|
16
|
+
export declare function OAuth(config: {
|
|
17
|
+
issuer: URL;
|
|
18
|
+
clientId: string;
|
|
19
|
+
userAgent: string;
|
|
20
|
+
}): {
|
|
21
|
+
init(): Promise<{
|
|
22
|
+
as: {
|
|
23
|
+
issuer: string;
|
|
24
|
+
device_authorization_endpoint: string;
|
|
25
|
+
token_endpoint: string;
|
|
26
|
+
revocation_endpoint: string;
|
|
27
|
+
jwks_uri: string;
|
|
28
|
+
introspection_endpoint: string;
|
|
29
|
+
};
|
|
30
|
+
init(): Promise</*elided*/ any>;
|
|
31
|
+
/**
|
|
32
|
+
* Returns the Authorization Server Metadata
|
|
33
|
+
*
|
|
34
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest
|
|
35
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse
|
|
36
|
+
*/
|
|
37
|
+
authorizationServerMetadata(): Promise<AuthorizationServerMetadata>;
|
|
38
|
+
/**
|
|
39
|
+
* Perform the Device Authorization Request
|
|
40
|
+
*
|
|
41
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.1
|
|
42
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.2
|
|
43
|
+
*/
|
|
44
|
+
deviceAuthorizationRequest(): Promise<{
|
|
45
|
+
/** The device verification code. */
|
|
46
|
+
device_code: string;
|
|
47
|
+
/** The end-user verification code. */
|
|
48
|
+
user_code: string;
|
|
49
|
+
/**
|
|
50
|
+
* The minimum amount of time in seconds that the client
|
|
51
|
+
* SHOULD wait between polling requests to the token endpoint.
|
|
52
|
+
*/
|
|
53
|
+
interval: number;
|
|
54
|
+
/** The end-user verification URI on the authorization server. */
|
|
55
|
+
verification_uri: string;
|
|
56
|
+
/**
|
|
57
|
+
* The end-user verification URI on the authorization server,
|
|
58
|
+
* including the `user_code`, without redirection.
|
|
59
|
+
*/
|
|
60
|
+
verification_uri_complete: string;
|
|
61
|
+
/**
|
|
62
|
+
* The absolute lifetime of the `device_code` and `user_code`.
|
|
63
|
+
* Calculated from `expires_in`.
|
|
64
|
+
*/
|
|
65
|
+
expiresAt: number;
|
|
66
|
+
}>;
|
|
67
|
+
/**
|
|
68
|
+
* Perform the Device Access Token Request
|
|
69
|
+
*
|
|
70
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.4
|
|
71
|
+
*/
|
|
72
|
+
deviceAccessTokenRequest(device_code: string): Promise<[Error] | [null, Response]>;
|
|
73
|
+
/**
|
|
74
|
+
* Process the Token request Response
|
|
75
|
+
*
|
|
76
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.5
|
|
77
|
+
*/
|
|
78
|
+
processTokenResponse(response: Response): Promise<[OAuthError] | [null, TokenSet]>;
|
|
79
|
+
/**
|
|
80
|
+
* Perform a Token Revocation Request.
|
|
81
|
+
*
|
|
82
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.1
|
|
83
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.2
|
|
84
|
+
*/
|
|
85
|
+
revokeToken(token: string): Promise<OAuthError | undefined>;
|
|
86
|
+
/**
|
|
87
|
+
* Perform Refresh Token Request.
|
|
88
|
+
*
|
|
89
|
+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-6
|
|
90
|
+
*/
|
|
91
|
+
refreshToken(token: string): Promise<TokenSet>;
|
|
92
|
+
/**
|
|
93
|
+
* Perform Token Introspection Request.
|
|
94
|
+
*
|
|
95
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7662#section-2.1
|
|
96
|
+
*/
|
|
97
|
+
introspectToken(token: string): Promise<z.infer<typeof IntrospectionResponse>>;
|
|
98
|
+
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Returns the Authorization Server Metadata
|
|
101
|
+
*
|
|
102
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest
|
|
103
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse
|
|
104
|
+
*/
|
|
105
|
+
authorizationServerMetadata(): Promise<AuthorizationServerMetadata>;
|
|
106
|
+
/**
|
|
107
|
+
* Perform the Device Authorization Request
|
|
108
|
+
*
|
|
109
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.1
|
|
110
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.2
|
|
111
|
+
*/
|
|
112
|
+
deviceAuthorizationRequest(): Promise<{
|
|
113
|
+
/** The device verification code. */
|
|
114
|
+
device_code: string;
|
|
115
|
+
/** The end-user verification code. */
|
|
116
|
+
user_code: string;
|
|
117
|
+
/**
|
|
118
|
+
* The minimum amount of time in seconds that the client
|
|
119
|
+
* SHOULD wait between polling requests to the token endpoint.
|
|
120
|
+
*/
|
|
121
|
+
interval: number;
|
|
122
|
+
/** The end-user verification URI on the authorization server. */
|
|
123
|
+
verification_uri: string;
|
|
124
|
+
/**
|
|
125
|
+
* The end-user verification URI on the authorization server,
|
|
126
|
+
* including the `user_code`, without redirection.
|
|
127
|
+
*/
|
|
128
|
+
verification_uri_complete: string;
|
|
129
|
+
/**
|
|
130
|
+
* The absolute lifetime of the `device_code` and `user_code`.
|
|
131
|
+
* Calculated from `expires_in`.
|
|
132
|
+
*/
|
|
133
|
+
expiresAt: number;
|
|
134
|
+
}>;
|
|
135
|
+
/**
|
|
136
|
+
* Perform the Device Access Token Request
|
|
137
|
+
*
|
|
138
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.4
|
|
139
|
+
*/
|
|
140
|
+
deviceAccessTokenRequest(device_code: string): Promise<[Error] | [null, Response]>;
|
|
141
|
+
/**
|
|
142
|
+
* Process the Token request Response
|
|
143
|
+
*
|
|
144
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.5
|
|
145
|
+
*/
|
|
146
|
+
processTokenResponse(response: Response): Promise<[OAuthError] | [null, TokenSet]>;
|
|
147
|
+
/**
|
|
148
|
+
* Perform a Token Revocation Request.
|
|
149
|
+
*
|
|
150
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.1
|
|
151
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.2
|
|
152
|
+
*/
|
|
153
|
+
revokeToken(token: string): Promise<OAuthError | undefined>;
|
|
154
|
+
/**
|
|
155
|
+
* Perform Refresh Token Request.
|
|
156
|
+
*
|
|
157
|
+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-6
|
|
158
|
+
*/
|
|
159
|
+
refreshToken(token: string): Promise<TokenSet>;
|
|
160
|
+
/**
|
|
161
|
+
* Perform Token Introspection Request.
|
|
162
|
+
*
|
|
163
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7662#section-2.1
|
|
164
|
+
*/
|
|
165
|
+
introspectToken(token: string): Promise<z.infer<typeof IntrospectionResponse>>;
|
|
166
|
+
};
|
|
167
|
+
export type OAuth = ReturnType<typeof OAuth>;
|
|
168
|
+
declare const TokenSet: z.ZodMiniObject<{
|
|
169
|
+
/** The access token issued by the authorization server. */
|
|
170
|
+
access_token: z.ZodMiniString<string>;
|
|
171
|
+
/** The type of the token issued */
|
|
172
|
+
token_type: z.ZodMiniLiteral<"Bearer">;
|
|
173
|
+
/** The lifetime in seconds of the access token. */
|
|
174
|
+
expires_in: z.ZodMiniNumber<number>;
|
|
175
|
+
/** The refresh token, which can be used to obtain new access tokens. */
|
|
176
|
+
refresh_token: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
177
|
+
/** The scope of the access token. */
|
|
178
|
+
scope: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
179
|
+
}, z.core.$strip>;
|
|
180
|
+
type TokenSet = z.infer<typeof TokenSet>;
|
|
181
|
+
declare const OAuthErrorResponse: z.ZodMiniObject<{
|
|
182
|
+
error: z.ZodMiniEnum<{
|
|
183
|
+
invalid_request: "invalid_request";
|
|
184
|
+
invalid_client: "invalid_client";
|
|
185
|
+
invalid_grant: "invalid_grant";
|
|
186
|
+
unauthorized_client: "unauthorized_client";
|
|
187
|
+
unsupported_grant_type: "unsupported_grant_type";
|
|
188
|
+
invalid_scope: "invalid_scope";
|
|
189
|
+
server_error: "server_error";
|
|
190
|
+
authorization_pending: "authorization_pending";
|
|
191
|
+
slow_down: "slow_down";
|
|
192
|
+
access_denied: "access_denied";
|
|
193
|
+
expired_token: "expired_token";
|
|
194
|
+
unsupported_token_type: "unsupported_token_type";
|
|
195
|
+
}>;
|
|
196
|
+
error_description: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
197
|
+
error_uri: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
198
|
+
}, z.core.$strip>;
|
|
199
|
+
type OAuthErrorResponse = z.infer<typeof OAuthErrorResponse>;
|
|
200
|
+
declare class OAuthError extends Error {
|
|
201
|
+
name: string;
|
|
202
|
+
code: OAuthErrorResponse['error'];
|
|
203
|
+
cause: Error;
|
|
204
|
+
constructor(message: string, response: unknown);
|
|
205
|
+
}
|
|
206
|
+
export declare function isOAuthError(error: unknown): error is OAuthError;
|
|
207
|
+
export {};
|
package/oauth.js
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
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.OAuth = OAuth;
|
|
37
|
+
exports.isOAuthError = isOAuthError;
|
|
38
|
+
const z = __importStar(require("zod/mini"));
|
|
39
|
+
const AuthorizationServerMetadata = z.object({
|
|
40
|
+
issuer: z.url(),
|
|
41
|
+
device_authorization_endpoint: z.url(),
|
|
42
|
+
token_endpoint: z.url(),
|
|
43
|
+
revocation_endpoint: z.url(),
|
|
44
|
+
jwks_uri: z.url(),
|
|
45
|
+
introspection_endpoint: z.url(),
|
|
46
|
+
});
|
|
47
|
+
const DeviceAuthorization = z.object({
|
|
48
|
+
device_code: z.string(),
|
|
49
|
+
user_code: z.string(),
|
|
50
|
+
verification_uri: z.url(),
|
|
51
|
+
verification_uri_complete: z.url(),
|
|
52
|
+
expires_in: z.number(),
|
|
53
|
+
interval: z.number(),
|
|
54
|
+
});
|
|
55
|
+
const IntrospectionResponse = z.object({
|
|
56
|
+
active: z.boolean(),
|
|
57
|
+
client_id: z.optional(z.string()),
|
|
58
|
+
session_id: z.optional(z.string()),
|
|
59
|
+
});
|
|
60
|
+
function OAuth(config) {
|
|
61
|
+
const { issuer, clientId, userAgent } = config;
|
|
62
|
+
let as;
|
|
63
|
+
function assertAs(as) {
|
|
64
|
+
if (as)
|
|
65
|
+
return;
|
|
66
|
+
throw new Error('Authorization Server Metadata not initialized. Call init() first.');
|
|
67
|
+
}
|
|
68
|
+
return {
|
|
69
|
+
async init() {
|
|
70
|
+
as ??= await this.authorizationServerMetadata();
|
|
71
|
+
return { ...this, as };
|
|
72
|
+
},
|
|
73
|
+
/**
|
|
74
|
+
* Returns the Authorization Server Metadata
|
|
75
|
+
*
|
|
76
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationRequest
|
|
77
|
+
* @see https://openid.net/specs/openid-connect-discovery-1_0.html#ProviderConfigurationResponse
|
|
78
|
+
*/
|
|
79
|
+
async authorizationServerMetadata() {
|
|
80
|
+
const response = await fetch(new URL('.well-known/openid-configuration', issuer), {
|
|
81
|
+
headers: {
|
|
82
|
+
'Content-Type': 'application/json',
|
|
83
|
+
'user-agent': userAgent,
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
const json = await response.json();
|
|
87
|
+
as = AuthorizationServerMetadata.parse(json);
|
|
88
|
+
if (as.issuer !== issuer.origin) {
|
|
89
|
+
throw new OAuthError(`Issuer mismatch: expected ${issuer}, got ${as.issuer}`, json);
|
|
90
|
+
}
|
|
91
|
+
return as;
|
|
92
|
+
},
|
|
93
|
+
/**
|
|
94
|
+
* Perform the Device Authorization Request
|
|
95
|
+
*
|
|
96
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.1
|
|
97
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.2
|
|
98
|
+
*/
|
|
99
|
+
async deviceAuthorizationRequest() {
|
|
100
|
+
assertAs(as);
|
|
101
|
+
const response = await fetch(as.device_authorization_endpoint, {
|
|
102
|
+
method: 'POST',
|
|
103
|
+
headers: {
|
|
104
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
105
|
+
'user-agent': userAgent,
|
|
106
|
+
},
|
|
107
|
+
body: new URLSearchParams({
|
|
108
|
+
client_id: clientId,
|
|
109
|
+
scope: 'openid offline_access',
|
|
110
|
+
}),
|
|
111
|
+
});
|
|
112
|
+
const json = await response.json();
|
|
113
|
+
const parsed = DeviceAuthorization.safeParse(json);
|
|
114
|
+
if (!parsed.success) {
|
|
115
|
+
throw new OAuthError(`Failed to parse device authorization response: ${parsed.error.message}`, json);
|
|
116
|
+
}
|
|
117
|
+
return {
|
|
118
|
+
device_code: parsed.data.device_code,
|
|
119
|
+
user_code: parsed.data.user_code,
|
|
120
|
+
verification_uri: parsed.data.verification_uri,
|
|
121
|
+
verification_uri_complete: parsed.data.verification_uri_complete,
|
|
122
|
+
expiresAt: Date.now() + parsed.data.expires_in * 1000,
|
|
123
|
+
interval: parsed.data.interval,
|
|
124
|
+
};
|
|
125
|
+
},
|
|
126
|
+
/**
|
|
127
|
+
* Perform the Device Access Token Request
|
|
128
|
+
*
|
|
129
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.4
|
|
130
|
+
*/
|
|
131
|
+
async deviceAccessTokenRequest(device_code) {
|
|
132
|
+
assertAs(as);
|
|
133
|
+
try {
|
|
134
|
+
return [
|
|
135
|
+
null,
|
|
136
|
+
await fetch(as.token_endpoint, {
|
|
137
|
+
method: 'POST',
|
|
138
|
+
headers: {
|
|
139
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
140
|
+
'user-agent': userAgent,
|
|
141
|
+
},
|
|
142
|
+
body: new URLSearchParams({
|
|
143
|
+
client_id: clientId,
|
|
144
|
+
grant_type: 'urn:ietf:params:oauth:grant-type:device_code',
|
|
145
|
+
device_code,
|
|
146
|
+
}),
|
|
147
|
+
signal: AbortSignal.timeout(10 * 1000),
|
|
148
|
+
}),
|
|
149
|
+
];
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (error instanceof Error)
|
|
153
|
+
return [error];
|
|
154
|
+
return [
|
|
155
|
+
new Error('An unknown error occurred. See the logs for details.', {
|
|
156
|
+
cause: error,
|
|
157
|
+
}),
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
},
|
|
161
|
+
/**
|
|
162
|
+
* Process the Token request Response
|
|
163
|
+
*
|
|
164
|
+
* @see https://datatracker.ietf.org/doc/html/rfc8628#section-3.5
|
|
165
|
+
*/
|
|
166
|
+
async processTokenResponse(response) {
|
|
167
|
+
const json = await response.json();
|
|
168
|
+
const processed = TokenSet.safeParse(json);
|
|
169
|
+
if (!processed.success) {
|
|
170
|
+
return [
|
|
171
|
+
new OAuthError(`Failed to parse token response: ${processed.error.message}`, json),
|
|
172
|
+
];
|
|
173
|
+
}
|
|
174
|
+
return [null, processed.data];
|
|
175
|
+
},
|
|
176
|
+
/**
|
|
177
|
+
* Perform a Token Revocation Request.
|
|
178
|
+
*
|
|
179
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.1
|
|
180
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7009#section-2.2
|
|
181
|
+
*/
|
|
182
|
+
async revokeToken(token) {
|
|
183
|
+
assertAs(as);
|
|
184
|
+
const response = await fetch(as.revocation_endpoint, {
|
|
185
|
+
method: 'POST',
|
|
186
|
+
headers: {
|
|
187
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
188
|
+
'user-agent': userAgent,
|
|
189
|
+
},
|
|
190
|
+
body: new URLSearchParams({ token, client_id: clientId }),
|
|
191
|
+
});
|
|
192
|
+
if (response.ok)
|
|
193
|
+
return;
|
|
194
|
+
const json = await response.json();
|
|
195
|
+
return new OAuthError('Revocation request failed', json);
|
|
196
|
+
},
|
|
197
|
+
/**
|
|
198
|
+
* Perform Refresh Token Request.
|
|
199
|
+
*
|
|
200
|
+
* @see https://datatracker.ietf.org/doc/html/rfc6749#section-6
|
|
201
|
+
*/
|
|
202
|
+
async refreshToken(token) {
|
|
203
|
+
assertAs(as);
|
|
204
|
+
const response = await fetch(as.token_endpoint, {
|
|
205
|
+
method: 'POST',
|
|
206
|
+
headers: {
|
|
207
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
208
|
+
'user-agent': userAgent,
|
|
209
|
+
},
|
|
210
|
+
body: new URLSearchParams({
|
|
211
|
+
client_id: clientId,
|
|
212
|
+
grant_type: 'refresh_token',
|
|
213
|
+
refresh_token: token,
|
|
214
|
+
}),
|
|
215
|
+
});
|
|
216
|
+
const [tokensError, tokenSet] = await this.processTokenResponse(response);
|
|
217
|
+
if (tokensError)
|
|
218
|
+
throw tokensError;
|
|
219
|
+
return tokenSet;
|
|
220
|
+
},
|
|
221
|
+
/**
|
|
222
|
+
* Perform Token Introspection Request.
|
|
223
|
+
*
|
|
224
|
+
* @see https://datatracker.ietf.org/doc/html/rfc7662#section-2.1
|
|
225
|
+
*/
|
|
226
|
+
async introspectToken(token) {
|
|
227
|
+
assertAs(as);
|
|
228
|
+
const response = await fetch(as.introspection_endpoint, {
|
|
229
|
+
method: 'POST',
|
|
230
|
+
headers: {
|
|
231
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
232
|
+
'user-agent': userAgent,
|
|
233
|
+
},
|
|
234
|
+
body: new URLSearchParams({ token }),
|
|
235
|
+
});
|
|
236
|
+
const json = await response.json();
|
|
237
|
+
const processed = IntrospectionResponse.safeParse(json);
|
|
238
|
+
if (!processed.success) {
|
|
239
|
+
throw new OAuthError(`Failed to parse introspection response: ${processed.error.message}`, json);
|
|
240
|
+
}
|
|
241
|
+
return processed.data;
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
const TokenSet = z.object({
|
|
246
|
+
/** The access token issued by the authorization server. */
|
|
247
|
+
access_token: z.string(),
|
|
248
|
+
/** The type of the token issued */
|
|
249
|
+
token_type: z.literal('Bearer'),
|
|
250
|
+
/** The lifetime in seconds of the access token. */
|
|
251
|
+
expires_in: z.number(),
|
|
252
|
+
/** The refresh token, which can be used to obtain new access tokens. */
|
|
253
|
+
refresh_token: z.optional(z.string()),
|
|
254
|
+
/** The scope of the access token. */
|
|
255
|
+
scope: z.optional(z.string()),
|
|
256
|
+
});
|
|
257
|
+
const OAuthErrorResponse = z.object({
|
|
258
|
+
error: z.enum([
|
|
259
|
+
'invalid_request',
|
|
260
|
+
'invalid_client',
|
|
261
|
+
'invalid_grant',
|
|
262
|
+
'unauthorized_client',
|
|
263
|
+
'unsupported_grant_type',
|
|
264
|
+
'invalid_scope',
|
|
265
|
+
'server_error',
|
|
266
|
+
// Device Authorization Response Errors
|
|
267
|
+
'authorization_pending',
|
|
268
|
+
'slow_down',
|
|
269
|
+
'access_denied',
|
|
270
|
+
'expired_token',
|
|
271
|
+
// Revocation Response Errors
|
|
272
|
+
'unsupported_token_type',
|
|
273
|
+
]),
|
|
274
|
+
error_description: z.optional(z.string()),
|
|
275
|
+
error_uri: z.optional(z.string()),
|
|
276
|
+
});
|
|
277
|
+
function processOAuthErrorResponse(json) {
|
|
278
|
+
try {
|
|
279
|
+
return OAuthErrorResponse.parse(json);
|
|
280
|
+
}
|
|
281
|
+
catch (error) {
|
|
282
|
+
if (error instanceof z.core.$ZodError) {
|
|
283
|
+
return new TypeError(`Invalid OAuth error response: ${error.message}`);
|
|
284
|
+
}
|
|
285
|
+
return new TypeError('Failed to parse OAuth error response');
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
class OAuthError extends Error {
|
|
289
|
+
constructor(message, response) {
|
|
290
|
+
super(message);
|
|
291
|
+
this.name = 'OAuthError';
|
|
292
|
+
const error = processOAuthErrorResponse(response);
|
|
293
|
+
if (error instanceof TypeError) {
|
|
294
|
+
const message = `Unexpected server response: ${JSON.stringify(response)}`;
|
|
295
|
+
this.cause = new Error(message, { cause: error });
|
|
296
|
+
this.code = 'server_error';
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
let cause = error.error;
|
|
300
|
+
if (error.error_description)
|
|
301
|
+
cause += `: ${error.error_description}`;
|
|
302
|
+
if (error.error_uri)
|
|
303
|
+
cause += ` (${error.error_uri})`;
|
|
304
|
+
this.cause = new Error(cause.toString());
|
|
305
|
+
this.code = error.error;
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function isOAuthError(error) {
|
|
309
|
+
return error instanceof OAuthError;
|
|
310
|
+
}
|
|
311
|
+
//# sourceMapp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;AAED,MAAM,UAAW,SAAQ,KAAK;IAI5B,YAAY,OAAe,EAAE,QAAiB;QAC5C,KAAK,CAAC,OAAO,CAAC,CAAC;QAJjB,SAAI,GAAG,YAAY,CAAC;QAKlB,MAAM,KAAK,GAAG,yBAAyB,CAAC,QAAQ,CAAC,CAAC;QAClD,IAAI,KAAK,YAAY,SAAS,EAAE,CAAC;YAC/B,MAAM,OAAO,GAAG,+BAA+B,IAAI,CAAC,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,QAAQ,EAAE,CAAC,CAAC;QACzC,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,KAAK,CAAC;IAC1B,CAAC;CACF;AAED,SAAgB,YAAY,CAAC,KAAc;IACzC,OAAO,KAAK,YAAY,UAAU,CAAC;AACrC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vercel/cli-auth",
|
|
3
3
|
"description": "Used by Vercel's CLIs to handle authentication",
|
|
4
|
-
"version": "0.1.0
|
|
4
|
+
"version": "0.1.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -19,6 +19,7 @@
|
|
|
19
19
|
"!**/*.test.ts"
|
|
20
20
|
],
|
|
21
21
|
"dependencies": {
|
|
22
|
+
"@napi-rs/keyring": "1.2.0",
|
|
22
23
|
"async-listen": "3.0.0",
|
|
23
24
|
"open": "8.4.0",
|
|
24
25
|
"xdg-app-paths": "5",
|
package/sso.d.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import * as z from 'zod/mini';
|
|
2
|
+
import type { OAuth } from './oauth';
|
|
3
|
+
import type { Credentials } from './credentials-store';
|
|
4
|
+
/**
|
|
5
|
+
* Re-authorize a team's SSO session by opening the browser and waiting for the user to complete
|
|
6
|
+
* the SSO flow. Use when the user's SSO session has expired or missing.
|
|
7
|
+
*
|
|
8
|
+
*/
|
|
9
|
+
export declare function reauthorizeTeam(params: {
|
|
10
|
+
/** The team's slug that requires re-authorization */
|
|
11
|
+
team: string;
|
|
12
|
+
/** The current access token that needs to be re-authorized */
|
|
13
|
+
token: Credentials['token'];
|
|
14
|
+
oauth: OAuth;
|
|
15
|
+
}): Promise<void>;
|
|
16
|
+
declare const SSOError: z.ZodMiniObject<{
|
|
17
|
+
code: z.ZodMiniString<string>;
|
|
18
|
+
message: z.ZodMiniString<string>;
|
|
19
|
+
scope: z.ZodMiniOptional<z.ZodMiniString<string>>;
|
|
20
|
+
enforced: z.ZodMiniOptional<z.ZodMiniBoolean<boolean>>;
|
|
21
|
+
}, z.core.$strip>;
|
|
22
|
+
/** Parse an error-like object to determine if SSO re-authorization is required. */
|
|
23
|
+
export declare function parseSSOError(error: unknown): z.infer<typeof SSOError> | undefined;
|
|
24
|
+
export {};
|