@atproto/oauth-client 0.0.0-spaces-alpha-20260818021221
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/CHANGELOG.md +810 -0
- package/LICENSE.txt +7 -0
- package/README.md +387 -0
- package/dist/constants.d.ts +5 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/constants.js +5 -0
- package/dist/constants.js.map +1 -0
- package/dist/errors/auth-method-unsatisfiable-error.d.ts +3 -0
- package/dist/errors/auth-method-unsatisfiable-error.d.ts.map +1 -0
- package/dist/errors/auth-method-unsatisfiable-error.js +3 -0
- package/dist/errors/auth-method-unsatisfiable-error.js.map +1 -0
- package/dist/errors/token-invalid-error.d.ts +7 -0
- package/dist/errors/token-invalid-error.d.ts.map +1 -0
- package/dist/errors/token-invalid-error.js +7 -0
- package/dist/errors/token-invalid-error.js.map +1 -0
- package/dist/errors/token-refresh-error.d.ts +7 -0
- package/dist/errors/token-refresh-error.d.ts.map +1 -0
- package/dist/errors/token-refresh-error.js +7 -0
- package/dist/errors/token-refresh-error.js.map +1 -0
- package/dist/errors/token-revoked-error.d.ts +7 -0
- package/dist/errors/token-revoked-error.d.ts.map +1 -0
- package/dist/errors/token-revoked-error.js +7 -0
- package/dist/errors/token-revoked-error.js.map +1 -0
- package/dist/fetch-dpop.d.ts +20 -0
- package/dist/fetch-dpop.d.ts.map +1 -0
- package/dist/fetch-dpop.js +176 -0
- package/dist/fetch-dpop.js.map +1 -0
- package/dist/identity-resolver.d.ts +2 -0
- package/dist/identity-resolver.d.ts.map +1 -0
- package/dist/identity-resolver.js +21 -0
- package/dist/identity-resolver.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/lock.d.ts +3 -0
- package/dist/lock.d.ts.map +1 -0
- package/dist/lock.js +29 -0
- package/dist/lock.js.map +1 -0
- package/dist/oauth-authorization-server-metadata-resolver.d.ts +25 -0
- package/dist/oauth-authorization-server-metadata-resolver.d.ts.map +1 -0
- package/dist/oauth-authorization-server-metadata-resolver.js +54 -0
- package/dist/oauth-authorization-server-metadata-resolver.js.map +1 -0
- package/dist/oauth-callback-error.d.ts +7 -0
- package/dist/oauth-callback-error.d.ts.map +1 -0
- package/dist/oauth-callback-error.js +14 -0
- package/dist/oauth-callback-error.js.map +1 -0
- package/dist/oauth-client-auth.d.ts +23 -0
- package/dist/oauth-client-auth.d.ts.map +1 -0
- package/dist/oauth-client-auth.js +126 -0
- package/dist/oauth-client-auth.js.map +1 -0
- package/dist/oauth-client.d.ts +338 -0
- package/dist/oauth-client.d.ts.map +1 -0
- package/dist/oauth-client.js +274 -0
- package/dist/oauth-client.js.map +1 -0
- package/dist/oauth-protected-resource-metadata-resolver.d.ts +25 -0
- package/dist/oauth-protected-resource-metadata-resolver.d.ts.map +1 -0
- package/dist/oauth-protected-resource-metadata-resolver.js +54 -0
- package/dist/oauth-protected-resource-metadata-resolver.js.map +1 -0
- package/dist/oauth-resolver-error.d.ts +7 -0
- package/dist/oauth-resolver-error.d.ts.map +1 -0
- package/dist/oauth-resolver-error.js +19 -0
- package/dist/oauth-resolver-error.js.map +1 -0
- package/dist/oauth-resolver.d.ts +73 -0
- package/dist/oauth-resolver.d.ts.map +1 -0
- package/dist/oauth-resolver.js +99 -0
- package/dist/oauth-resolver.js.map +1 -0
- package/dist/oauth-response-error.d.ts +11 -0
- package/dist/oauth-response-error.d.ts.map +1 -0
- package/dist/oauth-response-error.js +22 -0
- package/dist/oauth-response-error.js.map +1 -0
- package/dist/oauth-server-agent.d.ts +55 -0
- package/dist/oauth-server-agent.d.ts.map +1 -0
- package/dist/oauth-server-agent.js +189 -0
- package/dist/oauth-server-agent.js.map +1 -0
- package/dist/oauth-server-factory.d.ts +33 -0
- package/dist/oauth-server-factory.d.ts.map +1 -0
- package/dist/oauth-server-factory.js +31 -0
- package/dist/oauth-server-factory.js.map +1 -0
- package/dist/oauth-session.d.ts +34 -0
- package/dist/oauth-session.d.ts.map +1 -0
- package/dist/oauth-session.js +120 -0
- package/dist/oauth-session.js.map +1 -0
- package/dist/runtime-implementation.d.ts +17 -0
- package/dist/runtime-implementation.d.ts.map +1 -0
- package/dist/runtime-implementation.js +2 -0
- package/dist/runtime-implementation.js.map +1 -0
- package/dist/runtime.d.ts +26 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +100 -0
- package/dist/runtime.js.map +1 -0
- package/dist/session-getter.d.ts +44 -0
- package/dist/session-getter.d.ts.map +1 -0
- package/dist/session-getter.js +265 -0
- package/dist/session-getter.js.map +1 -0
- package/dist/state-store.d.ts +23 -0
- package/dist/state-store.d.ts.map +1 -0
- package/dist/state-store.js +2 -0
- package/dist/state-store.js.map +1 -0
- package/dist/types.d.ts +430 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +9 -0
- package/dist/types.js.map +1 -0
- package/dist/util.d.ts +20 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +68 -0
- package/dist/util.js.map +1 -0
- package/dist/validate-client-metadata.d.ts +5 -0
- package/dist/validate-client-metadata.d.ts.map +1 -0
- package/dist/validate-client-metadata.js +76 -0
- package/dist/validate-client-metadata.js.map +1 -0
- package/package.json +55 -0
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Dual MIT/Apache-2.0 License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-2026 Bluesky Social PBC, and Contributors
|
|
4
|
+
|
|
5
|
+
Except as otherwise noted in individual files, this software is licensed under the MIT license (<http://opensource.org/licenses/MIT>), or the Apache License, Version 2.0 (<http://www.apache.org/licenses/LICENSE-2.0>).
|
|
6
|
+
|
|
7
|
+
Downstream projects and end users may chose either license individually, or both together, at their discretion. The motivation for this dual-licensing is the additional software patent assurance provided by Apache 2.0.
|
package/README.md
ADDED
|
@@ -0,0 +1,387 @@
|
|
|
1
|
+
# @atproto/oauth-client: atproto flavoured OAuth client
|
|
2
|
+
|
|
3
|
+
Core library for implementing [atproto][ATPROTO] OAuth clients.
|
|
4
|
+
|
|
5
|
+
For a browser specific implementation, see [@atproto/oauth-client-browser](https://www.npmjs.com/package/@atproto/oauth-client-browser).
|
|
6
|
+
For a node specific implementation, see
|
|
7
|
+
[@atproto/oauth-client-node](https://www.npmjs.com/package/@atproto/oauth-client-node).
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
### Configuration
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
import { OAuthClient, Key, Session } from '@atproto/oauth-client'
|
|
15
|
+
import { JoseKey } from '@atproto/jwk-jose' // NodeJS/Browser only
|
|
16
|
+
|
|
17
|
+
const client = new OAuthClient({
|
|
18
|
+
handleResolver: 'https://my-backend.example', // backend instances should use a DNS based resolver
|
|
19
|
+
responseMode: 'query', // or "fragment" (frontend only) or "form_post" (backend only)
|
|
20
|
+
|
|
21
|
+
// These must be the same metadata as the one exposed on the
|
|
22
|
+
// "client_id" endpoint (except when using a loopback client)
|
|
23
|
+
clientMetadata: {
|
|
24
|
+
client_id: 'https://my-app.example/atproto-oauth-client.json',
|
|
25
|
+
jwks_uri: 'https://my-app.example/jwks.json',
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
runtimeImplementation: {
|
|
29
|
+
// A runtime specific implementation of the crypto operations needed by the
|
|
30
|
+
// OAuth client. See "@atproto/oauth-client-browser" for a browser specific
|
|
31
|
+
// implementation. The following example is suitable for use in NodeJS.
|
|
32
|
+
|
|
33
|
+
createKey(algs: string[]): Promise<Key> {
|
|
34
|
+
// algs is an ordered array of preferred algorithms (e.g. ['RS256', 'ES256'])
|
|
35
|
+
|
|
36
|
+
// Note, in browser environments, it is better to use non extractable keys
|
|
37
|
+
// to prevent the private key from being stolen. This can be done using
|
|
38
|
+
// the WebcryptoKey class from the "@atproto/jwk-webcrypto" package. The
|
|
39
|
+
// inconvenient of these keys (which is also what makes them stronger) is
|
|
40
|
+
// that the only way to persist them across browser reloads is to save
|
|
41
|
+
// them in the indexed DB.
|
|
42
|
+
return JoseKey.generate(algs)
|
|
43
|
+
},
|
|
44
|
+
|
|
45
|
+
getRandomValues(length: number): Uint8Array | PromiseLike<Uint8Array> {
|
|
46
|
+
return crypto.getRandomValues(new Uint8Array(length))
|
|
47
|
+
},
|
|
48
|
+
|
|
49
|
+
digest(
|
|
50
|
+
bytes: Uint8Array,
|
|
51
|
+
algorithm: { name: string },
|
|
52
|
+
): Uint8Array | PromiseLike<Uint8Array> {
|
|
53
|
+
// sha256 is required. Unsupported algorithms should throw an error.
|
|
54
|
+
|
|
55
|
+
if (algorithm.name.startsWith('sha')) {
|
|
56
|
+
const subtleAlgo = `SHA-${algorithm.name.slice(3)}`
|
|
57
|
+
const buffer = await crypto.subtle.digest(subtleAlgo, bytes)
|
|
58
|
+
return new Uint8Array(buffer)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
throw new TypeError(`Unsupported algorithm: ${algorithm.name}`)
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
requestLock: <T>(
|
|
65
|
+
name: string,
|
|
66
|
+
fn: () => T | PromiseLike<T>,
|
|
67
|
+
): Promise<T> => {
|
|
68
|
+
// This function is used to prevent concurrent refreshes of the same
|
|
69
|
+
// credentials. It is important to ensure that only one refresh is done at
|
|
70
|
+
// a time to prevent the sessions from being revoked.
|
|
71
|
+
|
|
72
|
+
// The following example shows a simple in-memory lock. In a real
|
|
73
|
+
// application, you should use a more robust solution (e.g. a system wide
|
|
74
|
+
// lock manager). Note that not providing a lock will result in an
|
|
75
|
+
// in-memory lock to be used (DO NOT copy-paste the following code).
|
|
76
|
+
|
|
77
|
+
declare const locks: Map<string, Promise<void>>
|
|
78
|
+
|
|
79
|
+
const current = locks.get(name) || Promise.resolve()
|
|
80
|
+
const next = current
|
|
81
|
+
.then(fn)
|
|
82
|
+
.catch(() => {})
|
|
83
|
+
.finally(() => {
|
|
84
|
+
if (locks.get(name) === next) locks.delete(name)
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
locks.set(name, next)
|
|
88
|
+
return next
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
|
|
92
|
+
stateStore: {
|
|
93
|
+
// A store for saving state data while the user is being redirected to the
|
|
94
|
+
// authorization server.
|
|
95
|
+
|
|
96
|
+
set(key: string, internalState: InternalStateData): Promise<void> {
|
|
97
|
+
throw new Error('Not implemented')
|
|
98
|
+
},
|
|
99
|
+
get(key: string): Promise<InternalStateData | undefined> {
|
|
100
|
+
throw new Error('Not implemented')
|
|
101
|
+
},
|
|
102
|
+
del(key: string): Promise<void> {
|
|
103
|
+
throw new Error('Not implemented')
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
|
|
107
|
+
sessionStore: {
|
|
108
|
+
// A store for saving session data.
|
|
109
|
+
|
|
110
|
+
set(sub: string, session: Session): Promise<void> {
|
|
111
|
+
throw new Error('Not implemented')
|
|
112
|
+
},
|
|
113
|
+
get(sub: string): Promise<Session | undefined> {
|
|
114
|
+
throw new Error('Not implemented')
|
|
115
|
+
},
|
|
116
|
+
del(sub: string): Promise<void> {
|
|
117
|
+
throw new Error('Not implemented')
|
|
118
|
+
},
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
keyset: [
|
|
122
|
+
// For backend clients only, a list of private keys to use for signing
|
|
123
|
+
// credentials. These keys MUST correspond to the public keys exposed on the
|
|
124
|
+
// "jwks_uri" of the client metadata. Note that the jwks JSON corresponding
|
|
125
|
+
// to the following keys can be obtained using the `client.jwks` getter.
|
|
126
|
+
await JoseKey.fromImportable(process.env.PRIVATE_KEY_1),
|
|
127
|
+
await JoseKey.fromImportable(process.env.PRIVATE_KEY_2),
|
|
128
|
+
await JoseKey.fromImportable(process.env.PRIVATE_KEY_3),
|
|
129
|
+
],
|
|
130
|
+
})
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Authentication
|
|
134
|
+
|
|
135
|
+
```ts
|
|
136
|
+
const url = await client.authorize('foo.bsky.team', {
|
|
137
|
+
state: '434321',
|
|
138
|
+
prompt: 'consent',
|
|
139
|
+
scope: 'email',
|
|
140
|
+
ui_locales: 'fr',
|
|
141
|
+
})
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Make user visit `url`. Then, once it was redirected to the callback URI, perform the following:
|
|
145
|
+
|
|
146
|
+
```ts
|
|
147
|
+
// Parse the query params from the callback URI
|
|
148
|
+
const params = new URLSearchParams('code=...&state=...')
|
|
149
|
+
|
|
150
|
+
// Process the callback using the OAuth client
|
|
151
|
+
const result = await client.callback(params)
|
|
152
|
+
|
|
153
|
+
// Verify the state (e.g. to link to an internal user)
|
|
154
|
+
result.state === '434321' // true
|
|
155
|
+
|
|
156
|
+
const oauthSession = result.session
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
The sign-in process results in an `OAuthSession` instance that can be used to make
|
|
160
|
+
authenticated requests to the resource server. This instance will automatically
|
|
161
|
+
refresh the credentials when needed.
|
|
162
|
+
|
|
163
|
+
### Making authenticated requests
|
|
164
|
+
|
|
165
|
+
The `OAuthSession` instance obtained after signing in can be used to make
|
|
166
|
+
authenticated requests to the user's PDS. There are two main use-cases:
|
|
167
|
+
|
|
168
|
+
1. Making authenticated request to Bluesky's AppView in order to fetch and
|
|
169
|
+
manipulate data from the `app.bsky` lexicon.
|
|
170
|
+
|
|
171
|
+
2. Making authenticated request to your own AppView, in order to fetch and
|
|
172
|
+
manipulate data from your own lexicon.
|
|
173
|
+
|
|
174
|
+
#### Making authenticated requests to Bluesky's AppView
|
|
175
|
+
|
|
176
|
+
The `@atproto/oauth-client` package provides a `OAuthSession` class that can be
|
|
177
|
+
used to make authenticated requests to Bluesky's AppView. This can be achieved
|
|
178
|
+
by constructing an `Agent` (from `@atproto/api`) instance using the
|
|
179
|
+
`OAuthSession` instance.
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import { Agent } from '@atproto/api'
|
|
183
|
+
|
|
184
|
+
const agent = new Agent(oauthSession)
|
|
185
|
+
|
|
186
|
+
// Make an authenticated request to the server. New credentials will be
|
|
187
|
+
// automatically fetched if needed (causing sessionStore.set() to be called).
|
|
188
|
+
await agent.post({
|
|
189
|
+
text: 'Hello, world!',
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
// revoke credentials on the server (causing sessionStore.del() to be called)
|
|
193
|
+
await agent.signOut()
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### Making authenticated requests to your own AppView
|
|
197
|
+
|
|
198
|
+
The `OAuthSession` instance obtained after signing in can be used to instantiate
|
|
199
|
+
the `XrpcClient` class from the `@atproto/xrpc` package.
|
|
200
|
+
|
|
201
|
+
```ts
|
|
202
|
+
import { Lexicons } from '@atproto/lexicon'
|
|
203
|
+
import { OAuthClient } from '@atproto/oauth-client' // or "@atproto/oauth-client-browser" or "@atproto/oauth-client-node"
|
|
204
|
+
import { XrpcClient } from '@atproto/xrpc'
|
|
205
|
+
|
|
206
|
+
// Define your lexicons
|
|
207
|
+
const myLexicon = new Lexicons([
|
|
208
|
+
{
|
|
209
|
+
lexicon: 1,
|
|
210
|
+
id: 'com.example.query',
|
|
211
|
+
defs: {
|
|
212
|
+
main: {
|
|
213
|
+
// ...
|
|
214
|
+
},
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
])
|
|
218
|
+
|
|
219
|
+
// Describe your app's oauth client
|
|
220
|
+
const oauthClient = new OAuthClient({
|
|
221
|
+
// ...
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
// Authenticate the user
|
|
225
|
+
const oauthSession = await oauthClient.restore('did:plc:123')
|
|
226
|
+
|
|
227
|
+
// Instantiate a client using the `oauthSession` as fetch handler object
|
|
228
|
+
const client = new XrpcClient(oauthSession, myLexicon)
|
|
229
|
+
|
|
230
|
+
// Make authenticated calls
|
|
231
|
+
const response = await client.call('com.example.query')
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Note that the user's PDS might not know about your lexicon, or what to do with
|
|
235
|
+
those calls (PDS' are only mandated to implement the `com.atproto` lexicon). In
|
|
236
|
+
order to process your calls, you need to have a backend that will process those
|
|
237
|
+
calls. You can then instruct your PDS to forward those calls to your backend.
|
|
238
|
+
|
|
239
|
+
```ts
|
|
240
|
+
const response = await client.call(
|
|
241
|
+
'com.example.query',
|
|
242
|
+
{
|
|
243
|
+
// Params
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
headers: {
|
|
247
|
+
// The PDS will proxy calls to the specified service in did:plc:xyz's did document.
|
|
248
|
+
// These calls will be authenticated using "service auth", a single use JWT Bearer token, signed with the logged-in user's private key.
|
|
249
|
+
'atproto-proxy': 'did:plc:xyz#serviceId',
|
|
250
|
+
},
|
|
251
|
+
},
|
|
252
|
+
)
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
You can also instantiate the `XrpcClient` class with a custom `fetch` function
|
|
256
|
+
that will provide the `atproto-proxy` header on all calls:
|
|
257
|
+
|
|
258
|
+
```ts
|
|
259
|
+
const boundClient = new XrpcClient((url, init) => {
|
|
260
|
+
const headers = new Headers(init?.headers)
|
|
261
|
+
|
|
262
|
+
// Add the atproto-proxy header if it is not already present
|
|
263
|
+
if (!headers.has('atproto-proxy')) {
|
|
264
|
+
headers.set('atproto-proxy', 'did:plc:xyz#serviceId')
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return oauthSession.fetchHandler(url, { ...init, headers })
|
|
268
|
+
}, myLexicon)
|
|
269
|
+
|
|
270
|
+
// No need to specify the atproto-proxy header anymore
|
|
271
|
+
const response = await boundClient.call('com.example.query')
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
> [!NOTE]
|
|
275
|
+
>
|
|
276
|
+
> Proxying every call through the PDS is not recommended for performance
|
|
277
|
+
> reasons, as it will increase the latency of readonly calls to your lexicon.
|
|
278
|
+
> Doing so will also prevent your backend from being able to anticipate writes
|
|
279
|
+
> on the network. Indeed, write calls will be sent to the PDS, which will then
|
|
280
|
+
> propagate them on the network through a relay (a.k.a. "firehose"). This will
|
|
281
|
+
> introduce a delay between the time the write is made and the time it is
|
|
282
|
+
> processed by your backend.
|
|
283
|
+
>
|
|
284
|
+
> In order to avoid those issues, it is recommended that you implement your
|
|
285
|
+
> backend using a backend-for-frontend pattern. This backend will be responsible
|
|
286
|
+
> for processing the calls made by the client, and will be able to anticipate
|
|
287
|
+
> writes on the network.
|
|
288
|
+
>
|
|
289
|
+
> Read more about the backend-for-frontend pattern in the [atproto][ATPROTO]
|
|
290
|
+
> documentation website.
|
|
291
|
+
|
|
292
|
+
## Advances use-cases
|
|
293
|
+
|
|
294
|
+
### Listening for session updates and deletion
|
|
295
|
+
|
|
296
|
+
The `OAuthClient` will emit events whenever a session is updated or deleted.
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
import {
|
|
300
|
+
Session,
|
|
301
|
+
TokenRefreshError,
|
|
302
|
+
TokenRevokedError,
|
|
303
|
+
} from '@atproto/oauth-client'
|
|
304
|
+
|
|
305
|
+
client.addEventListener('updated', (event: CustomEvent<Session>) => {
|
|
306
|
+
console.log('Refreshed tokens were saved in the store:', event.detail)
|
|
307
|
+
})
|
|
308
|
+
|
|
309
|
+
client.addEventListener(
|
|
310
|
+
'deleted',
|
|
311
|
+
(
|
|
312
|
+
event: CustomEvent<{
|
|
313
|
+
sub: string
|
|
314
|
+
cause: TokenRefreshError | TokenRevokedError | unknown
|
|
315
|
+
}>,
|
|
316
|
+
) => {
|
|
317
|
+
console.log('Session was deleted from the session store:', event.detail)
|
|
318
|
+
|
|
319
|
+
const { cause } = event.detail
|
|
320
|
+
|
|
321
|
+
if (cause instanceof TokenRefreshError) {
|
|
322
|
+
// - refresh_token unavailable or expired
|
|
323
|
+
// - oauth response error (`cause.cause instanceof OAuthResponseError`)
|
|
324
|
+
// - session data does not match expected values returned by the OAuth server
|
|
325
|
+
} else if (cause instanceof TokenRevokedError) {
|
|
326
|
+
// Session was revoked through:
|
|
327
|
+
// - agent.signOut()
|
|
328
|
+
// - client.revoke(sub)
|
|
329
|
+
} else {
|
|
330
|
+
// An unexpected error occurred, causing the session to be deleted
|
|
331
|
+
}
|
|
332
|
+
},
|
|
333
|
+
)
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
### Force user to re-authenticate
|
|
337
|
+
|
|
338
|
+
```ts
|
|
339
|
+
const url = await client.authorize(handle, {
|
|
340
|
+
prompt: 'login',
|
|
341
|
+
state,
|
|
342
|
+
})
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
or
|
|
346
|
+
|
|
347
|
+
```ts
|
|
348
|
+
const url = await client.authorize(handle, {
|
|
349
|
+
state,
|
|
350
|
+
})
|
|
351
|
+
```
|
|
352
|
+
|
|
353
|
+
### Silent Sign-In
|
|
354
|
+
|
|
355
|
+
Using silent sign-in requires to handle retries on the callback endpoint.
|
|
356
|
+
|
|
357
|
+
```ts
|
|
358
|
+
async function createLoginUrl(handle: string, state?: string): string {
|
|
359
|
+
return client.authorize(handle, {
|
|
360
|
+
state,
|
|
361
|
+
// Use "prompt=none" to attempt silent sign-in
|
|
362
|
+
prompt: 'none',
|
|
363
|
+
})
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async function handleCallback(params: URLSearchParams) {
|
|
367
|
+
try {
|
|
368
|
+
return await client.callback(params)
|
|
369
|
+
} catch (err) {
|
|
370
|
+
// Silent sign-in failed, retry without prompt=none
|
|
371
|
+
if (
|
|
372
|
+
err instanceof OAuthCallbackError &&
|
|
373
|
+
['login_required', 'consent_required'].includes(err.params.get('error'))
|
|
374
|
+
) {
|
|
375
|
+
// Do *not* use prompt=none when retrying (to avoid infinite redirects)
|
|
376
|
+
const url = await client.authorize(handle, { state: err.state })
|
|
377
|
+
|
|
378
|
+
// Allow calling code to catch the error and redirect the user to the new URL
|
|
379
|
+
return new MyLoginRequiredError(url)
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
throw err
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
```
|
|
386
|
+
|
|
387
|
+
[ATPROTO]: https://atproto.com/ 'AT Protocol'
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,YAAY,UAAU,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,OAAO,CAAA","sourcesContent":["/**\n * Per ATProto spec (OpenID uses RS256)\n */\nexport const FALLBACK_ALG = 'ES256'\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-method-unsatisfiable-error.d.ts","sourceRoot":"","sources":["../../src/errors/auth-method-unsatisfiable-error.ts"],"names":[],"mappings":"AAAA,qBAAa,4BAA6B,SAAQ,KAAK;CAAG"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth-method-unsatisfiable-error.js","sourceRoot":"","sources":["../../src/errors/auth-method-unsatisfiable-error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,4BAA6B,SAAQ,KAAK;CAAG","sourcesContent":["export class AuthMethodUnsatisfiableError extends Error {}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-invalid-error.d.ts","sourceRoot":"","sources":["../../src/errors/token-invalid-error.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAkB,SAAQ,KAAK;aAExB,GAAG,EAAE,MAAM;IAD7B,YACkB,GAAG,EAAE,MAAM,EAC3B,OAAO,SAAwC,EAC/C,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAG9B;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-invalid-error.js","sourceRoot":"","sources":["../../src/errors/token-invalid-error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YACkB,GAAW,EAC3B,OAAO,GAAG,oBAAoB,GAAG,cAAc,EAC/C,OAA6B;QAE7B,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;mBAJP,GAAG;IAKrB,CAAC;CACF","sourcesContent":["export class TokenInvalidError extends Error {\n constructor(\n public readonly sub: string,\n message = `The session for \"${sub}\" is invalid`,\n options?: { cause?: unknown },\n ) {\n super(message, options)\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-refresh-error.d.ts","sourceRoot":"","sources":["../../src/errors/token-refresh-error.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAkB,SAAQ,KAAK;aAExB,GAAG,EAAE,MAAM;IAD7B,YACkB,GAAG,EAAE,MAAM,EAC3B,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAG9B;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-refresh-error.js","sourceRoot":"","sources":["../../src/errors/token-refresh-error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YACkB,GAAW,EAC3B,OAAe,EACf,OAA6B;QAE7B,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;mBAJP,GAAG;IAKrB,CAAC;CACF","sourcesContent":["export class TokenRefreshError extends Error {\n constructor(\n public readonly sub: string,\n message: string,\n options?: { cause?: unknown },\n ) {\n super(message, options)\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-revoked-error.d.ts","sourceRoot":"","sources":["../../src/errors/token-revoked-error.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAkB,SAAQ,KAAK;aAExB,GAAG,EAAE,MAAM;IAD7B,YACkB,GAAG,EAAE,MAAM,EAC3B,OAAO,SAAsD,EAC7D,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAG9B;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-revoked-error.js","sourceRoot":"","sources":["../../src/errors/token-revoked-error.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,iBAAkB,SAAQ,KAAK;IAC1C,YACkB,GAAW,EAC3B,OAAO,GAAG,oBAAoB,GAAG,4BAA4B,EAC7D,OAA6B;QAE7B,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;mBAJP,GAAG;IAKrB,CAAC;CACF","sourcesContent":["export class TokenRevokedError extends Error {\n constructor(\n public readonly sub: string,\n message = `The session for \"${sub}\" was successfully revoked`,\n options?: { cause?: unknown },\n ) {\n super(message, options)\n }\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Key } from '@atproto/jwk';
|
|
2
|
+
import { type Fetch, type FetchContext } from '@atproto-labs/fetch';
|
|
3
|
+
import type { SimpleStore } from '@atproto-labs/simple-store';
|
|
4
|
+
export type DpopFetchWrapperOptions<C = FetchContext> = {
|
|
5
|
+
key: Key;
|
|
6
|
+
nonces: SimpleStore<string, string>;
|
|
7
|
+
supportedAlgs?: string[];
|
|
8
|
+
sha256?: (input: string) => Promise<string>;
|
|
9
|
+
/**
|
|
10
|
+
* Is the intended server an authorization server (true) or a resource server
|
|
11
|
+
* (false)? Setting this may allow to avoid parsing the response body to
|
|
12
|
+
* determine the dpop-nonce.
|
|
13
|
+
*
|
|
14
|
+
* @default undefined
|
|
15
|
+
*/
|
|
16
|
+
isAuthServer?: boolean;
|
|
17
|
+
fetch?: Fetch<C>;
|
|
18
|
+
};
|
|
19
|
+
export declare function dpopFetchWrapper<C = FetchContext>({ key, supportedAlgs, nonces, sha256, isAuthServer, fetch, }: DpopFetchWrapperOptions<C>): Fetch<C>;
|
|
20
|
+
//# sourceMappingURL=fetch-dpop.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"fetch-dpop.d.ts","sourceRoot":"","sources":["../src/fetch-dpop.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,cAAc,CAAA;AACvC,OAAO,EACL,KAAK,KAAK,EACV,KAAK,YAAY,EAGlB,MAAM,qBAAqB,CAAA;AAC5B,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AAQ7D,MAAM,MAAM,uBAAuB,CAAC,CAAC,GAAG,YAAY,IAAI;IACtD,GAAG,EAAE,GAAG,CAAA;IACR,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACnC,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;IACxB,MAAM,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,OAAO,CAAC,MAAM,CAAC,CAAA;IAE3C;;;;;;OAMG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC,CAAC,CAAA;CACjB,CAAA;AAED,wBAAgB,gBAAgB,CAAC,CAAC,GAAG,YAAY,EAAE,EACjD,GAAG,EAEH,aAAa,EACb,MAAM,EACN,MAAiE,EACjE,YAAY,EACZ,KAAwB,GACzB,EAAE,uBAAuB,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,CAsGvC"}
|