Repository navigation
Expand file tree
/
Copy pathproviderSignIn.ts
More file actions
607 lines (549 loc) · 20.5 KB
/
Copy pathproviderSignIn.ts
File metadata and controls
607 lines (549 loc) · 20.5 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
import {
SESSION_EXCHANGE_GRANT_TYPE,
SESSION_EXCHANGE_ISSUED_TOKEN_TYPE,
SESSION_EXCHANGE_SCOPE,
SESSION_EXCHANGE_SUBJECT_TOKEN_TYPE,
SessionExchangeRequestSchema,
SessionExchangeResponseSchema,
} from "@seamless-auth/types";
import { createHash, randomBytes } from "crypto";
import { createServer, type Server } from "http";
import { createLocalJWKSet, jwtVerify, type JSONWebKeySet } from "jose";
import type { AddressInfo } from "net";
import { z } from "zod";
import { apiRequest, joinUrl, jsonBody, safeJson } from "./http.js";
import type { TokenBundle } from "./keychain.js";
import { openBrowser as defaultOpenBrowser } from "./openBrowser.js";
import { tokensFromAuthResponse } from "./tokenResponse.js";
export const PROVIDER_SCOPE = `openid ${SESSION_EXCHANGE_SCOPE}`;
export const LOOPBACK_TIMEOUT_MS = 5 * 60 * 1000;
const DEVICE_GRANT = "urn:ietf:params:oauth:grant-type:device_code";
const DEFAULT_POLL_SECONDS = 5;
const SLOW_DOWN_SECONDS = 5;
const CALLBACK_PATH = "/callback";
const CLIENT_SETTINGS_HINT =
"The client must be a first-party public client with a loopback redirect, with sessionExchangeEnabled on (and the device grant, for --device).";
export class ProviderSignInError extends Error {
constructor(
message: string,
readonly replayed = false,
) {
super(message);
this.name = "ProviderSignInError";
}
}
export interface ProviderMetadata {
issuer: string;
authorizationEndpoint: string;
tokenEndpoint: string;
deviceAuthorizationEndpoint?: string;
jwksUri: string;
issParameterSupported: boolean;
}
/** What a provider sign-in hands to the exchange. The access token goes no further. */
export interface ProviderSubject {
accessToken: string;
sub: string;
}
export interface ExchangedSession {
tokens: TokenBundle;
sub?: string;
email?: string;
}
export function normalizeIssuer(value: string): string {
let url: URL;
try {
url = new URL(value);
} catch {
throw new ProviderSignInError(`"${printable(value)}" is not a valid issuer URL.`);
}
if (url.protocol !== "https:") {
throw new ProviderSignInError(`The issuer must use https, not ${url.protocol}`);
}
if (url.search || url.hash) {
throw new ProviderSignInError("The issuer cannot have a query or a fragment.");
}
// The issuer gets printed for the user to check, and these make a URL read as another site.
if (url.username || url.password) {
throw new ProviderSignInError("The issuer cannot have a username or a password.");
}
// The parsed form, because the input can carry control characters that URL escapes.
return url.href.replace(/\/+$/, "");
}
export function builtInResource(issuer: string): string {
return `${issuer}/seamless`;
}
const httpsUrl = z.url().refine((value) => value.startsWith("https://"), "must be https");
// scopes_supported is deliberately not read: session:exchange is expected to be absent
// from it, because the scope belongs to one resource rather than to the provider.
const DiscoverySchema = z
.object({
issuer: z.string(),
authorization_endpoint: httpsUrl,
token_endpoint: httpsUrl,
device_authorization_endpoint: httpsUrl.optional(),
jwks_uri: httpsUrl,
code_challenge_methods_supported: z.array(z.string()).optional(),
authorization_response_iss_parameter_supported: z.boolean().optional(),
})
.loose();
export async function discoverProvider(issuer: string): Promise<ProviderMetadata> {
const url = `${issuer}/.well-known/openid-configuration`;
let res;
try {
res = await apiRequest<unknown>(url, { redirect: "error" });
} catch {
throw new ProviderSignInError(`Could not read the provider's discovery document at ${url}.`);
}
if (!res.ok) {
throw new ProviderSignInError(`The provider's discovery document at ${url} answered ${res.status}.`);
}
const parsed = DiscoverySchema.safeParse(res.data);
if (!parsed.success) {
throw new ProviderSignInError(`The discovery document at ${url} is not one this CLI can use.`);
}
const doc = parsed.data;
if (doc.issuer !== issuer) {
throw new ProviderSignInError(
`The discovery document at ${url} names issuer ${printable(doc.issuer)}, not ${issuer}.`,
);
}
if (!doc.code_challenge_methods_supported?.includes("S256")) {
throw new ProviderSignInError(`The provider at ${issuer} does not offer PKCE S256.`);
}
return {
issuer: doc.issuer,
authorizationEndpoint: doc.authorization_endpoint,
tokenEndpoint: doc.token_endpoint,
...(doc.device_authorization_endpoint
? { deviceAuthorizationEndpoint: doc.device_authorization_endpoint }
: {}),
jwksUri: doc.jwks_uri,
issParameterSupported: doc.authorization_response_iss_parameter_supported === true,
};
}
// Local until the types package has it. Older APIs omit the field, and anything unreadable
// reads as absent: an instance that offers no provider keeps the email code.
const AdvertisementSchema = z
.object({
oidcProvider: z
.object({ issuer: z.string().min(1), cliClientId: z.string().min(1) })
.loose(),
})
.loose();
export interface AdvertisedProvider {
issuer: string;
cliClientId: string;
}
/** What the instance says about its provider. Never reason to stop: see loginToInstance. */
export async function readAdvertisedProvider(
instanceUrl: string,
): Promise<AdvertisedProvider | undefined> {
try {
const res = await apiRequest<unknown>(joinUrl(instanceUrl, "/system-config/public"));
if (!res.ok) return undefined;
const provider = AdvertisementSchema.safeParse(res.data).data?.oidcProvider;
return provider && { issuer: provider.issuer, cliClientId: provider.cliClientId };
} catch {
return undefined;
}
}
function randomToken(): string {
return randomBytes(32).toString("base64url");
}
// What the provider or a local program put in a query string reaches a terminal.
function printable(value: string | null | undefined): string {
return (value ?? "").replace(/[^\x20-\x7e]/g, "").slice(0, 200);
}
const TokenResponseSchema = z
.object({ access_token: z.string().optional(), id_token: z.string().optional() })
.loose();
const OAuthErrorSchema = z
.object({ error: z.string(), error_description: z.string().optional() })
.loose();
function describeOAuthError(data: unknown): { error?: string; text: string } {
const parsed = OAuthErrorSchema.safeParse(data);
if (!parsed.success) return { text: "" };
const error = printable(parsed.data.error);
const description = printable(parsed.data.error_description);
return { error, text: description ? `${error}: ${description}` : error };
}
async function verifyIdToken(
idToken: string | undefined,
metadata: ProviderMetadata,
clientId: string,
nonce?: string,
): Promise<string> {
if (!idToken) throw new ProviderSignInError("The provider did not return an ID token.");
let keys;
try {
const res = await apiRequest<JSONWebKeySet>(metadata.jwksUri, { redirect: "error" });
if (!res.ok || !res.data) throw new Error(String(res.status));
keys = createLocalJWKSet(res.data);
} catch {
throw new ProviderSignInError(`Could not read the provider's signing keys at ${metadata.jwksUri}.`);
}
let claims;
try {
({ payload: claims } = await jwtVerify(idToken, keys, {
issuer: metadata.issuer,
audience: clientId,
requiredClaims: ["sub", "exp", "iat"],
}));
} catch (err) {
throw new ProviderSignInError(`The ID token did not verify: ${printable((err as Error).message)}.`);
}
if (nonce !== undefined && claims.nonce !== nonce) {
throw new ProviderSignInError("The ID token's nonce does not match this sign-in.");
}
return claims.sub as string;
}
async function subjectFromTokens(
data: unknown,
metadata: ProviderMetadata,
clientId: string,
nonce?: string,
): Promise<ProviderSubject> {
const parsed = TokenResponseSchema.safeParse(data);
const accessToken = parsed.data?.access_token;
if (!accessToken) {
throw new ProviderSignInError("The provider did not return an access token.");
}
const sub = await verifyIdToken(parsed.data?.id_token, metadata, clientId, nonce);
return { accessToken, sub };
}
function postForm(url: string, form: Record<string, string>): Promise<Response> {
return fetch(url, {
method: "POST",
redirect: "error",
headers: { "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json" },
body: new URLSearchParams(form).toString(),
});
}
interface CallbackListener {
redirectUri: string;
callback: Promise<URLSearchParams>;
close(): void;
}
const CALLBACK_PAGE = `<!doctype html><meta charset="utf-8"><title>Seamless CLI</title>
<p>You can close this tab and go back to your terminal.</p>`;
// RFC 8252: the loopback IP only, not "localhost" and not every interface. The first GET
// to the callback path settles the sign-in, whatever it says: state decides below whether
// it was the real one, and the port closes either way.
function listenForCallback(timeoutMs: number): Promise<CallbackListener> {
return new Promise((resolve, reject) => {
let settle: (params: URLSearchParams) => void = () => {};
let refuse: (err: Error) => void = () => {};
const callback = new Promise<URLSearchParams>((res, rej) => {
settle = res;
refuse = rej;
});
// The caller may not be awaiting yet when the timer fires.
callback.catch(() => {});
let timer: NodeJS.Timeout | undefined;
const server: Server = createServer((req, res) => {
// Thrown here it would be uncaught, and any local program could end the sign-in.
let url: URL;
try {
url = new URL(req.url ?? "/", "http://127.0.0.1");
} catch {
res.writeHead(400).end();
return;
}
if (req.method !== "GET" || url.pathname !== CALLBACK_PATH) {
res.writeHead(404).end();
return;
}
res.writeHead(200, {
"Content-Type": "text/html; charset=utf-8",
"Cache-Control": "no-store",
"Referrer-Policy": "no-referrer",
"Content-Security-Policy": "default-src 'none'",
Connection: "close",
});
res.end(CALLBACK_PAGE, () => close());
settle(url.searchParams);
});
const close = (): void => {
if (timer) clearTimeout(timer);
server.close();
server.closeAllConnections();
};
server.once("error", reject);
server.listen(0, "127.0.0.1", () => {
timer = setTimeout(() => {
close();
refuse(new ProviderSignInError("Timed out waiting for the browser sign-in."));
}, timeoutMs);
const { port } = server.address() as AddressInfo;
resolve({
redirectUri: `http://127.0.0.1:${port}${CALLBACK_PATH}`,
callback,
close,
});
});
});
}
function checkCallback(
params: URLSearchParams,
state: string,
metadata: ProviderMetadata,
): string {
if (params.get("state") !== state) {
throw new ProviderSignInError(
"The browser returned a sign-in with the wrong state, so it was refused. Sign in again.",
);
}
const iss = params.get("iss");
if (iss !== metadata.issuer && (iss !== null || metadata.issParameterSupported)) {
throw new ProviderSignInError(
"The browser returned a sign-in whose iss does not name this provider, so it was refused.",
);
}
const error = params.get("error");
if (error) {
const description = printable(params.get("error_description"));
const safe = printable(error);
const hint =
safe === "invalid_target" || safe === "invalid_scope" || safe === "unauthorized_client"
? ` ${CLIENT_SETTINGS_HINT}`
: "";
throw new ProviderSignInError(
`The provider refused the sign-in: ${safe}${description ? `: ${description}` : ""}.${hint}`,
);
}
const code = params.get("code");
if (!code) throw new ProviderSignInError("The browser returned no authorization code.");
return code;
}
export interface LoopbackOptions {
metadata: ProviderMetadata;
clientId: string;
say: (line: string) => void;
openBrowser?: (url: string) => Promise<boolean>;
acrValues?: string;
maxAge?: number;
timeoutMs?: number;
}
/** RFC 8252: authorization code with PKCE, through the system browser, to a loopback redirect. */
export async function signInWithLoopback(opts: LoopbackOptions): Promise<ProviderSubject> {
const { metadata, clientId, say } = opts;
const open = opts.openBrowser ?? defaultOpenBrowser;
const state = randomToken();
const nonce = randomToken();
const verifier = randomToken();
const listener = await listenForCallback(opts.timeoutMs ?? LOOPBACK_TIMEOUT_MS);
try {
const resource = builtInResource(metadata.issuer);
const authorize = new URL(metadata.authorizationEndpoint);
authorize.search = new URLSearchParams({
client_id: clientId,
response_type: "code",
scope: PROVIDER_SCOPE,
redirect_uri: listener.redirectUri,
resource,
state,
nonce,
code_challenge: createHash("sha256").update(verifier).digest("base64url"),
code_challenge_method: "S256",
...(opts.acrValues ? { acr_values: opts.acrValues } : {}),
...(opts.maxAge !== undefined ? { max_age: String(opts.maxAge) } : {}),
}).toString();
say(
'Opening your browser to sign in. It will ask "Continue to <app>?": check that it names this CLI and your account, then continue.',
);
if (!(await open(authorize.href))) {
say(`Could not open a browser. Open this URL yourself:\n\n ${authorize.href}\n`);
say("On a machine with no browser, run this again with --device.");
}
const code = checkCallback(await listener.callback, state, metadata);
let res;
try {
res = await postForm(metadata.tokenEndpoint, {
client_id: clientId,
grant_type: "authorization_code",
code,
redirect_uri: listener.redirectUri,
code_verifier: verifier,
resource,
});
} catch {
throw new ProviderSignInError(`Could not reach the provider's token endpoint at ${metadata.tokenEndpoint}.`);
}
const data = await safeJson<unknown>(res);
if (!res.ok) {
const { text } = describeOAuthError(data);
throw new ProviderSignInError(
`The provider's token endpoint refused the sign-in (${res.status})${text ? `: ${text}` : ""}.`,
);
}
return await subjectFromTokens(data, metadata, clientId, nonce);
} finally {
listener.close();
}
}
const DeviceAuthorizationSchema = z
.object({
device_code: z.string(),
user_code: z.string(),
verification_uri: httpsUrl,
expires_in: z.number().positive(),
interval: z.number().positive().optional(),
})
.loose();
export interface DeviceOptions {
metadata: ProviderMetadata;
clientId: string;
say: (line: string) => void;
sleep?: (ms: number) => Promise<void>;
now?: () => number;
}
const defaultSleep = (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms));
/**
* RFC 8628. Takes no acr_values or max_age: the provider refuses them here, since an
* approval made on another machine cannot speak for whoever holds this terminal.
*/
export async function signInWithDevice(opts: DeviceOptions): Promise<ProviderSubject> {
const { metadata, clientId, say } = opts;
const sleep = opts.sleep ?? defaultSleep;
const now = opts.now ?? Date.now;
const endpoint = metadata.deviceAuthorizationEndpoint;
if (!endpoint) {
throw new ProviderSignInError(`The provider at ${metadata.issuer} does not offer device sign-in.`);
}
let started;
try {
started = await postForm(endpoint, {
client_id: clientId,
scope: PROVIDER_SCOPE,
resource: builtInResource(metadata.issuer),
});
} catch {
throw new ProviderSignInError(`Could not reach the provider's device endpoint at ${endpoint}.`);
}
const startedData = await safeJson<unknown>(started);
if (!started.ok) {
const { text } = describeOAuthError(startedData);
throw new ProviderSignInError(
`The provider refused the device sign-in (${started.status})${text ? `: ${text}` : ""}. ${CLIENT_SETTINGS_HINT}`,
);
}
const parsed = DeviceAuthorizationSchema.safeParse(startedData);
if (!parsed.success) {
throw new ProviderSignInError("The provider's device response is not one this CLI can read.");
}
const device = parsed.data;
say(`On any device with a browser, open:\n\n ${printable(device.verification_uri)}\n`);
say(`and enter this code:\n\n ${printable(device.user_code)}\n`);
say("Only continue if you started this sign-in yourself. Waiting for approval...");
const deadline = now() + device.expires_in * 1000;
const expired = (): ProviderSignInError =>
new ProviderSignInError("The device code expired before it was approved. Sign in again.");
let interval = device.interval ?? DEFAULT_POLL_SECONDS;
for (;;) {
await sleep(interval * 1000);
if (now() >= deadline) throw expired();
let res;
try {
res = await postForm(metadata.tokenEndpoint, {
client_id: clientId,
grant_type: DEVICE_GRANT,
device_code: device.device_code,
});
} catch {
// RFC 8628 section 3.5: a connection timeout backs off. The code stays good until
// it expires, and a response lost here shows up as the next poll's invalid_grant.
interval *= 2;
continue;
}
const data = await safeJson<unknown>(res);
if (res.ok) return subjectFromTokens(data, metadata, clientId);
const { error, text } = describeOAuthError(data);
if (error === "authorization_pending") continue;
if (error === "slow_down") {
interval += SLOW_DOWN_SECONDS;
continue;
}
if (error === "expired_token") throw expired();
if (error === "access_denied") {
throw new ProviderSignInError("The sign-in was denied in the browser.");
}
throw new ProviderSignInError(
`The provider refused the device sign-in (${res.status})${text ? `: ${text}` : ""}. Sign in again.`,
);
}
}
const SIGN_IN_AGAIN = "Sign in again: seamless profile login.";
/**
* The token works once. Nothing here may send it twice, on any failure: a retry after a
* lost response is a replay, and a replay revokes the session the first attempt created.
*/
export async function exchangeForSession(
instanceUrl: string,
subjectToken: string,
): Promise<ExchangedSession> {
const body = SessionExchangeRequestSchema.parse({
grant_type: SESSION_EXCHANGE_GRANT_TYPE,
subject_token: subjectToken,
subject_token_type: SESSION_EXCHANGE_SUBJECT_TOKEN_TYPE,
requested_token_type: SESSION_EXCHANGE_ISSUED_TOKEN_TYPE,
});
let res;
try {
res = await apiRequest<Record<string, unknown>>(
joinUrl(instanceUrl, "/sessions/exchange"),
jsonBody("POST", body),
);
} catch {
throw new ProviderSignInError(
`Could not reach ${instanceUrl} to finish signing in. The token works only once, so it is not sent again. ${SIGN_IN_AGAIN}`,
);
}
if (res.ok) {
const parsed = SessionExchangeResponseSchema.loose().safeParse(res.data);
const tokens = parsed.success ? tokensFromAuthResponse(res.data) : null;
if (!parsed.success || !tokens) {
throw new ProviderSignInError(
`The instance returned a session this CLI cannot read. ${SIGN_IN_AGAIN}`,
);
}
return { tokens, sub: parsed.data.sub, email: parsed.data.email };
}
if (res.status === 429) {
throw new ProviderSignInError(
"The instance is rate limiting sign-ins from this address. Wait a few minutes, then sign in again.",
);
}
if (res.status === 404) {
throw new ProviderSignInError(
`${instanceUrl} has no session exchange route, so it needs auth server v0.23 or later. Use --email-code until then.`,
);
}
if (res.status === 400 && res.data?.error_description === "token_already_used") {
throw new ProviderSignInError(
`This sign-in's token had already been used, so the instance revoked the session it created. Your sign-in may have been intercepted. Check your sessions with seamless sessions, then sign in again.`,
true,
);
}
if (res.status === 400) {
throw new ProviderSignInError(
`The instance refused the sign-in. The token lasts 60 seconds, and the client needs sessionExchangeEnabled on. ${SIGN_IN_AGAIN}`,
);
}
throw new ProviderSignInError(
`The instance failed while finishing the sign-in (${res.status}). The token works only once, so it is not sent again. ${SIGN_IN_AGAIN}`,
);
}
/** Best effort: for a session this CLI got and will not keep. */
export async function endSession(instanceUrl: string, accessToken: string): Promise<boolean> {
try {
const res = await apiRequest(joinUrl(instanceUrl, "/logout"), {
method: "DELETE",
headers: { Authorization: `Bearer ${accessToken}` },
});
return res.ok;
} catch {
return false;
}
}