-
Notifications
You must be signed in to change notification settings - Fork 643
/
Copy pathoauth-client.ts
489 lines (429 loc) · 14.1 KB
/
oauth-client.ts
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
import {
DidCache,
DidResolverCached,
DidResolverCommon,
} from '@atproto-labs/did-resolver'
import { Fetch } from '@atproto-labs/fetch'
import {
AppViewHandleResolver,
CachedHandleResolver,
HandleCache,
HandleResolver,
} from '@atproto-labs/handle-resolver'
import { IdentityResolver } from '@atproto-labs/identity-resolver'
import { SimpleStoreMemory } from '@atproto-labs/simple-store-memory'
import { Key, Keyset } from '@atproto/jwk'
import {
OAuthClientIdDiscoverable,
OAuthClientMetadata,
OAuthClientMetadataInput,
oauthClientMetadataSchema,
OAuthResponseMode,
} from '@atproto/oauth-types'
import { FALLBACK_ALG } from './constants.js'
import { TokenRevokedError } from './errors/token-revoked-error.js'
import {
AuthorizationServerMetadataCache,
OAuthAuthorizationServerMetadataResolver,
} from './oauth-authorization-server-metadata-resolver.js'
import { OAuthCallbackError } from './oauth-callback-error.js'
import {
OAuthProtectedResourceMetadataResolver,
ProtectedResourceMetadataCache,
} from './oauth-protected-resource-metadata-resolver.js'
import { OAuthResolver } from './oauth-resolver.js'
import { DpopNonceCache, OAuthServerAgent } from './oauth-server-agent.js'
import { OAuthServerFactory } from './oauth-server-factory.js'
import { OAuthSession } from './oauth-session.js'
import { RuntimeImplementation } from './runtime-implementation.js'
import { Runtime } from './runtime.js'
import {
SessionEventMap,
SessionGetter,
SessionStore,
} from './session-getter.js'
import { InternalStateData, StateStore } from './state-store.js'
import { AuthorizeOptions, ClientMetadata } from './types.js'
import { CustomEventTarget } from './util.js'
import { validateClientMetadata } from './validate-client-metadata.js'
// Export all types needed to construct OAuthClientOptions
export type {
AuthorizationServerMetadataCache,
DidCache,
DpopNonceCache,
Fetch,
HandleCache,
HandleResolver,
InternalStateData,
Key,
Keyset,
OAuthClientMetadata,
OAuthClientMetadataInput,
OAuthResponseMode,
ProtectedResourceMetadataCache,
RuntimeImplementation,
SessionStore,
StateStore,
}
export type OAuthClientOptions = {
// Config
responseMode: OAuthResponseMode
clientMetadata: Readonly<OAuthClientMetadataInput>
keyset?: Keyset | Iterable<Key | undefined | null | false>
// Stores
stateStore: StateStore
sessionStore: SessionStore
didCache?: DidCache
handleCache?: HandleCache
authorizationServerMetadataCache?: AuthorizationServerMetadataCache
protectedResourceMetadataCache?: ProtectedResourceMetadataCache
dpopNonceCache?: DpopNonceCache
// Services
handleResolver: HandleResolver | URL | string
plcDirectoryUrl?: URL | string
runtimeImplementation: RuntimeImplementation
fetch?: Fetch
}
export type OAuthClientEventMap = SessionEventMap
export type OAuthClientFetchMetadataOptions = {
clientId: OAuthClientIdDiscoverable
fetch?: Fetch
signal?: AbortSignal
}
export class OAuthClient extends CustomEventTarget<OAuthClientEventMap> {
static async fetchMetadata({
clientId,
fetch = globalThis.fetch,
signal,
}: OAuthClientFetchMetadataOptions) {
signal?.throwIfAborted()
const request = new Request(clientId, {
redirect: 'error',
signal: signal,
})
const response = await fetch(request)
if (response.status !== 200) {
response.body?.cancel?.()
throw new TypeError(`Failed to fetch client metadata: ${response.status}`)
}
// https://drafts.aaronpk.com/draft-parecki-oauth-client-id-metadata-document/draft-parecki-oauth-client-id-metadata-document.html#section-4.1
const mime = response.headers.get('content-type')?.split(';')[0].trim()
if (mime !== 'application/json') {
response.body?.cancel?.()
throw new TypeError(`Invalid client metadata content type: ${mime}`)
}
const json: unknown = await response.json()
signal?.throwIfAborted()
return oauthClientMetadataSchema.parse(json)
}
// Config
readonly clientMetadata: ClientMetadata
readonly responseMode: OAuthResponseMode
readonly keyset?: Keyset
// Services
readonly runtime: Runtime
readonly fetch: Fetch
readonly oauthResolver: OAuthResolver
readonly serverFactory: OAuthServerFactory
// Stores
readonly sessionGetter: SessionGetter
readonly stateStore: StateStore
constructor({
fetch = globalThis.fetch,
stateStore,
sessionStore,
didCache = undefined,
dpopNonceCache = new SimpleStoreMemory({ ttl: 60e3, max: 100 }),
handleCache = undefined,
authorizationServerMetadataCache = new SimpleStoreMemory({
ttl: 60e3,
max: 100,
}),
protectedResourceMetadataCache = new SimpleStoreMemory({
ttl: 60e3,
max: 100,
}),
responseMode,
clientMetadata,
handleResolver,
plcDirectoryUrl,
runtimeImplementation,
keyset,
}: OAuthClientOptions) {
super()
this.keyset = keyset
? keyset instanceof Keyset
? keyset
: new Keyset(keyset)
: undefined
this.clientMetadata = validateClientMetadata(clientMetadata, this.keyset)
this.responseMode = responseMode
this.runtime = new Runtime(runtimeImplementation)
this.fetch = fetch
this.oauthResolver = new OAuthResolver(
new IdentityResolver(
new DidResolverCached(
new DidResolverCommon({ fetch, plcDirectoryUrl }),
didCache,
),
new CachedHandleResolver(
AppViewHandleResolver.from(handleResolver, { fetch }),
handleCache,
),
),
new OAuthProtectedResourceMetadataResolver(
protectedResourceMetadataCache,
fetch,
),
new OAuthAuthorizationServerMetadataResolver(
authorizationServerMetadataCache,
fetch,
),
)
this.serverFactory = new OAuthServerFactory(
this.clientMetadata,
this.runtime,
this.oauthResolver,
this.fetch,
this.keyset,
dpopNonceCache,
)
this.sessionGetter = new SessionGetter(
sessionStore,
this.serverFactory,
this.runtime,
)
this.stateStore = stateStore
// Proxy sessionGetter events
for (const type of ['deleted', 'updated'] as const) {
this.sessionGetter.addEventListener(type, (event) => {
if (!this.dispatchCustomEvent(type, event.detail)) {
event.preventDefault()
}
})
}
}
// Exposed as public API for convenience
get identityResolver() {
return this.oauthResolver.identityResolver
}
// Exposed as public API for convenience
get didResolver() {
return this.identityResolver.didResolver
}
// Exposed as public API for convenience
get handleResolver() {
return this.identityResolver.handleResolver
}
get jwks() {
return this.keyset?.publicJwks ?? ({ keys: [] as const } as const)
}
async authorize(input: string, options?: AuthorizeOptions): Promise<URL> {
const redirectUri =
options?.redirect_uri ?? this.clientMetadata.redirect_uris[0]
if (!this.clientMetadata.redirect_uris.includes(redirectUri)) {
// The server will enforce this, but let's catch it early
throw new TypeError('Invalid redirect_uri')
}
const { identity, metadata } = await this.oauthResolver.resolve(
input,
options,
)
const pkce = await this.runtime.generatePKCE()
const dpopKey = await this.runtime.generateKey(
metadata.dpop_signing_alg_values_supported || [FALLBACK_ALG],
)
const state = await this.runtime.generateNonce()
await this.stateStore.set(state, {
iss: metadata.issuer,
dpopKey,
verifier: pkce.verifier,
appState: options?.state,
})
const parameters = {
client_id: this.clientMetadata.client_id,
redirect_uri: redirectUri,
code_challenge: pkce.challenge,
code_challenge_method: pkce.method,
state,
login_hint: identity
? input // If input is a handle or a DID, use it as a login_hint
: undefined,
response_mode: this.responseMode,
response_type:
// Negotiate by using the order in the client metadata
this.clientMetadata.response_types?.find((t) =>
metadata['response_types_supported']?.includes(t),
) ?? 'code',
display: options?.display,
prompt: options?.prompt,
scope: options?.scope || undefined,
ui_locales: options?.ui_locales,
}
if (metadata.pushed_authorization_request_endpoint) {
const server = await this.serverFactory.fromMetadata(metadata, dpopKey)
const parResponse = await server.request(
'pushed_authorization_request',
parameters,
)
const authorizationUrl = new URL(metadata.authorization_endpoint)
authorizationUrl.searchParams.set(
'client_id',
this.clientMetadata.client_id,
)
authorizationUrl.searchParams.set('request_uri', parResponse.request_uri)
return authorizationUrl
} else if (metadata.require_pushed_authorization_requests) {
throw new Error(
'Server requires pushed authorization requests (PAR) but no PAR endpoint is available',
)
} else {
const authorizationUrl = new URL(metadata.authorization_endpoint)
for (const [key, value] of Object.entries(parameters)) {
if (value) authorizationUrl.searchParams.set(key, String(value))
}
// Length of the URL that will be sent to the server
const urlLength =
authorizationUrl.pathname.length + authorizationUrl.search.length
if (urlLength < 2048) {
return authorizationUrl
} else if (!metadata.pushed_authorization_request_endpoint) {
throw new Error('Login URL too long')
}
}
throw new Error(
'Server does not support pushed authorization requests (PAR)',
)
}
/**
* This method allows the client to proactively revoke the request_uri it
* created through PAR.
*/
async abortRequest(authorizeUrl: URL) {
const requestUri = authorizeUrl.searchParams.get('request_uri')
if (!requestUri) return
// @NOTE This is not implemented here because, 1) the request server should
// invalidate the request_uri after some delay anyways, and 2) I am not sure
// that the revocation endpoint is even supposed to support this (and I
// don't want to spend the time checking now).
// @TODO investigate actual necessity & feasibility of this feature
}
async callback(params: URLSearchParams): Promise<{
session: OAuthSession
state: string | null
}> {
const responseJwt = params.get('response')
if (responseJwt != null) {
// https://openid.net/specs/oauth-v2-jarm.html
throw new OAuthCallbackError(params, 'JARM not supported')
}
const issuerParam = params.get('iss')
const stateParam = params.get('state')
const errorParam = params.get('error')
const codeParam = params.get('code')
if (!stateParam) {
throw new OAuthCallbackError(params, 'Missing "state" parameter')
}
const stateData = await this.stateStore.get(stateParam)
if (stateData) {
// Prevent any kind of replay
await this.stateStore.del(stateParam)
} else {
throw new OAuthCallbackError(
params,
`Unknown authorization session "${stateParam}"`,
)
}
try {
if (errorParam != null) {
throw new OAuthCallbackError(params, undefined, stateData.appState)
}
if (!codeParam) {
throw new OAuthCallbackError(
params,
'Missing "code" query param',
stateData.appState,
)
}
const server = await this.serverFactory.fromIssuer(
stateData.iss,
stateData.dpopKey,
)
if (issuerParam != null) {
if (!server.serverMetadata.issuer) {
throw new OAuthCallbackError(
params,
'Issuer not found in metadata',
stateData.appState,
)
}
if (server.serverMetadata.issuer !== issuerParam) {
throw new OAuthCallbackError(
params,
'Issuer mismatch',
stateData.appState,
)
}
} else if (
server.serverMetadata.authorization_response_iss_parameter_supported
) {
throw new OAuthCallbackError(
params,
'iss missing from the response',
stateData.appState,
)
}
const tokenSet = await server.exchangeCode(codeParam, stateData.verifier)
try {
await this.sessionGetter.setStored(tokenSet.sub, {
dpopKey: stateData.dpopKey,
tokenSet,
})
const session = this.createSession(server, tokenSet.sub)
return { session, state: stateData.appState ?? null }
} catch (err) {
await server.revoke(tokenSet.access_token)
throw err
}
} catch (err) {
// Make sure, whatever the underlying error, that the appState is
// available in the calling code
throw OAuthCallbackError.from(err, params, stateData.appState)
}
}
/**
* Load a stored session. This will refresh the token only if needed (about to
* expire) by default.
*
* @param refresh See {@link SessionGetter.getSession}
*/
async restore(sub: string, refresh?: boolean): Promise<OAuthSession> {
const { dpopKey, tokenSet } = await this.sessionGetter.getSession(
sub,
refresh,
)
const server = await this.serverFactory.fromIssuer(tokenSet.iss, dpopKey, {
noCache: refresh === true,
allowStale: refresh === false,
})
return this.createSession(server, sub)
}
async revoke(sub: string) {
const { dpopKey, tokenSet } = await this.sessionGetter.getSession(
sub,
false,
)
// NOT using `;(await this.restore(sub, false)).signOut()` because we want
// the tokens to be deleted even if it was not possible to fetch the issuer
// data.
try {
const server = await this.serverFactory.fromIssuer(tokenSet.iss, dpopKey)
await server.revoke(tokenSet.access_token)
} finally {
await this.sessionGetter.delStored(sub, new TokenRevokedError(sub))
}
}
protected createSession(server: OAuthServerAgent, sub: string): OAuthSession {
return new OAuthSession(server, sub, this.sessionGetter, this.fetch)
}
}