-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauthClient.ts
More file actions
282 lines (250 loc) · 8.98 KB
/
Copy pathauthClient.ts
File metadata and controls
282 lines (250 loc) · 8.98 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
import {
getActiveProfile,
getPortalSession,
type Profile,
} from "./config.js";
import {
deleteTokens,
getTokens,
headlessRefreshToken,
KeychainUnavailableError,
saveTokens,
type TokenBundle,
} from "./keychain.js";
import { apiRequest, joinUrl, type ApiResponse } from "./http.js";
export class ReauthRequiredError extends Error {
constructor(message: string) {
super(message);
this.name = "ReauthRequiredError";
}
}
export interface AuthClient {
profile: Profile;
request<T = unknown>(path: string, init?: RequestInit): Promise<ApiResponse<T>>;
get<T = unknown>(path: string, init?: RequestInit): Promise<ApiResponse<T>>;
post<T = unknown>(
path: string,
body?: unknown,
init?: RequestInit,
): Promise<ApiResponse<T>>;
}
export function tokensFromAuthResponse(
data: Record<string, unknown> | null,
): TokenBundle | null {
if (!data) return null;
const accessToken = typeof data.token === "string" ? data.token : undefined;
const refreshToken =
typeof data.refreshToken === "string" ? data.refreshToken : undefined;
if (!accessToken || !refreshToken) return null;
const now = Date.now();
const ttl = typeof data.ttl === "number" ? data.ttl : undefined;
const refreshTtl =
typeof data.refreshTtl === "number" ? data.refreshTtl : undefined;
return {
accessToken,
refreshToken,
accessTokenExpiresAt: ttl !== undefined ? now + ttl * 1000 : undefined,
refreshTokenExpiresAt:
refreshTtl !== undefined ? now + refreshTtl * 1000 : undefined,
};
}
/**
* Reads a `/refresh` response, keeping the current refresh token when the instance
* did not send a new one.
*
* Separate from `tokensFromAuthResponse` because the two answer different questions.
* A login must establish both tokens, and a response missing either is unusable. A
* refresh only has to produce a usable access token: the response schema marks both
* fields optional, so requiring the pair would wipe a session that is still valid
* the first time an instance rotates only the access token.
*/
export function tokensFromRefreshResponse(
data: Record<string, unknown> | null,
current: TokenBundle,
): TokenBundle | null {
if (!data) return null;
const accessToken = typeof data.token === "string" ? data.token : undefined;
if (!accessToken) return null;
const rotated =
typeof data.refreshToken === "string" && data.refreshToken
? data.refreshToken
: undefined;
const now = Date.now();
const ttl = typeof data.ttl === "number" ? data.ttl : undefined;
const refreshTtl =
typeof data.refreshTtl === "number" ? data.refreshTtl : undefined;
return {
accessToken,
refreshToken: rotated ?? current.refreshToken,
accessTokenExpiresAt: ttl !== undefined ? now + ttl * 1000 : undefined,
refreshTokenExpiresAt:
refreshTtl !== undefined
? now + refreshTtl * 1000
: // An unrotated token keeps the expiry it already had.
rotated
? undefined
: current.refreshTokenExpiresAt,
};
}
// A client for an auth instance a developer administers, resolved from the active
// profile. Portal calls use createPortalClient instead: the two sessions are
// different accounts on different hosts and must never be interchanged.
export async function createAuthClient(
opts: { profileFlag?: string } = {},
): Promise<AuthClient> {
const profile = getActiveProfile(opts);
if (!profile) {
throw new ReauthRequiredError(
"No active profile is configured. Add one with: seamless profile add <name> --instance-url <url>, then run seamless profile login <name>.",
);
}
return createClientForProfile(profile, {
label: `profile "${profile.name}"`,
reauthCommand: `seamless profile login ${profile.name}`,
});
}
// A client for the Seamless portal, used for control-plane calls
// (core/portal.ts). Absolute URLs pass through joinUrl untouched, so the same
// client reaches api.seamlessauth.com while refreshing against the portal's own
// auth host.
export async function createPortalClient(): Promise<AuthClient> {
const session = getPortalSession();
if (!session) {
throw new ReauthRequiredError(
"You are not signed in to the Seamless portal. Run: seamless login.",
);
}
return createClientForProfile(session, {
label: "the Seamless portal",
reauthCommand: "seamless login",
});
}
interface ReauthCopy {
label: string;
reauthCommand: string;
}
async function createClientForProfile(
profile: Profile,
copy: ReauthCopy,
): Promise<AuthClient> {
const tokens = await getTokens(profile);
if (!tokens || !tokens.refreshToken) {
throw new ReauthRequiredError(
`No session for ${copy.label}. Run: ${copy.reauthCommand}.`,
);
}
const session = tokens;
// A headless run reads SEAMLESS_REFRESH_TOKEN fresh every time, so the keychain is
// never consulted and writing a rotated token there only makes it look preserved.
const headless = headlessRefreshToken() !== undefined;
const persist = async (next: TokenBundle): Promise<void> => {
session.accessToken = next.accessToken;
session.refreshToken = next.refreshToken;
session.accessTokenExpiresAt = next.accessTokenExpiresAt;
session.refreshTokenExpiresAt = next.refreshTokenExpiresAt;
if (headless) return;
try {
await saveTokens(profile, next);
} catch (err) {
if (!(err instanceof KeychainUnavailableError)) throw err;
}
};
const clearSession = async (): Promise<void> => {
try {
await deleteTokens(profile);
} catch (err) {
if (!(err instanceof KeychainUnavailableError)) throw err;
}
};
const expiredMessage = (): string =>
headless
? `The refresh token in SEAMLESS_REFRESH_TOKEN was rejected by ${copy.label}. The instance rotates the refresh token on every refresh and revokes the session if a spent one is sent again, so a token that has already been refreshed once cannot be reused on a later run. Issue a fresh one with: ${copy.reauthCommand}.`
: `Your session for ${copy.label} has expired or was revoked. Run: ${copy.reauthCommand}.`;
const runRefresh = async (): Promise<void> => {
const res = await apiRequest<Record<string, unknown>>(
joinUrl(profile.instanceUrl, "/refresh"),
{
method: "POST",
headers: { Authorization: `Bearer ${session.refreshToken}` },
},
);
if (!res.ok) {
await clearSession();
throw new ReauthRequiredError(expiredMessage());
}
const next = tokensFromRefreshResponse(res.data, session);
if (!next) {
await clearSession();
throw new ReauthRequiredError(
`Received an unexpected refresh response from ${profile.instanceUrl}. Run: ${copy.reauthCommand}.`,
);
}
await persist(next);
};
// Single-flight. The instance rotates the refresh token on every call and reads a
// second use of the old one as theft, revoking the whole session chain, so two
// concurrent refreshes would not merely race: they would log the developer out.
let inFlight: Promise<void> | null = null;
const refresh = (): Promise<void> => {
if (!inFlight) {
inFlight = runRefresh().finally(() => {
inFlight = null;
});
}
return inFlight;
};
const authedFetch = async <T>(
path: string,
init: RequestInit = {},
): Promise<ApiResponse<T>> => {
const build = (): RequestInit => ({
...init,
headers: {
...(init.headers as Record<string, string> | undefined),
Authorization: `Bearer ${session.accessToken}`,
},
});
const url = joinUrl(profile.instanceUrl, path);
const sentWith = session.accessToken;
let res = await apiRequest<T>(url, build());
if (res.status === 401 && session.refreshToken) {
// Only refresh if nothing else already did while this request was in flight.
// Otherwise a burst of 401s against one stale token would each rotate again.
if (session.accessToken === sentWith) {
await refresh();
}
res = await apiRequest<T>(url, build());
// A 401 on a freshly refreshed token is not something a caller can act on by
// retrying, and returning it verbatim left commands reporting an opaque failure
// instead of saying the session is gone.
if (res.status === 401) {
await clearSession();
throw new ReauthRequiredError(
`Your session for ${copy.label} was rejected after refreshing it. Run: ${copy.reauthCommand}.`,
);
}
}
return res;
};
return {
profile,
request: authedFetch,
get: (path, init = {}) => authedFetch(path, { ...init, method: "GET" }),
post: (path, body, init = {}) => {
const headers: Record<string, string> = {
...(init.headers as Record<string, string> | undefined),
};
let bodyInit = init.body;
if (body !== undefined) {
headers["Content-Type"] = "application/json";
bodyInit = JSON.stringify(body);
}
return authedFetch(path, {
...init,
method: "POST",
headers,
body: bodyInit,
});
},
};
}