@atcute/client 4.2.2 → 5.0.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/dist/client.d.ts +9 -19
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +1 -7
- package/dist/client.js.map +1 -1
- package/dist/index.d.ts +0 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +0 -1
- package/dist/index.js.map +1 -1
- package/lib/client.ts +10 -29
- package/lib/index.ts +0 -1
- package/package.json +7 -7
- package/dist/credential-manager.d.ts +0 -109
- package/dist/credential-manager.d.ts.map +0 -1
- package/dist/credential-manager.js +0 -244
- package/dist/credential-manager.js.map +0 -1
- package/lib/credential-manager.ts +0 -411
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Did } from '@atcute/lexicons';
|
|
2
1
|
import type { XRPCProcedures, XRPCQueries } from '@atcute/lexicons/ambient';
|
|
2
|
+
import type { AtprotoAudience } from '@atcute/lexicons/syntax';
|
|
3
3
|
import * as v from '@atcute/lexicons/validations';
|
|
4
4
|
import type { InferInput, InferOutput, ObjectSchema, XRPCBlobBodyParam, XRPCLexBodyParam, XRPCProcedureMetadata, XRPCQueryMetadata } from '@atcute/lexicons/validations';
|
|
5
5
|
import { type FetchHandler, type FetchHandlerObject } from './fetch-handler.ts';
|
|
@@ -117,35 +117,25 @@ type UnknownClientResponse = {
|
|
|
117
117
|
type Namespaced<T> = {
|
|
118
118
|
mainSchema: T;
|
|
119
119
|
};
|
|
120
|
-
/** options for configuring
|
|
121
|
-
export type
|
|
122
|
-
/**
|
|
123
|
-
|
|
120
|
+
/** options for configuring the client */
|
|
121
|
+
export type ClientOptions = {
|
|
122
|
+
/** the underlying fetch handler it should make requests with */
|
|
123
|
+
handler: FetchHandler | FetchHandlerObject;
|
|
124
124
|
/**
|
|
125
|
-
*
|
|
126
|
-
* that the upstream service should forward requests to.
|
|
127
|
-
*
|
|
128
|
-
* must start with `#`
|
|
125
|
+
* service proxy target, in the form `<did>#<service-id>`.
|
|
129
126
|
*
|
|
130
|
-
* common
|
|
127
|
+
* common service ids include:
|
|
131
128
|
* - `#atproto_pds` (personal data server)
|
|
132
129
|
* - `#atproto_labeler` (labeler service)
|
|
133
130
|
* - `#bsky_chat` (Bluesky chat service)
|
|
134
131
|
*/
|
|
135
|
-
|
|
136
|
-
};
|
|
137
|
-
/** options for configuring the client */
|
|
138
|
-
export type ClientOptions = {
|
|
139
|
-
/** the underlying fetch handler it should make requests with */
|
|
140
|
-
handler: FetchHandler | FetchHandlerObject;
|
|
141
|
-
/** service proxy configuration */
|
|
142
|
-
proxy?: ServiceProxyOptions | null;
|
|
132
|
+
proxy?: AtprotoAudience | null;
|
|
143
133
|
};
|
|
144
134
|
/** XRPC API client */
|
|
145
135
|
export declare class Client<TQueries = XRPCQueries, TProcedures = XRPCProcedures> {
|
|
146
136
|
#private;
|
|
147
137
|
handler: FetchHandler;
|
|
148
|
-
proxy:
|
|
138
|
+
proxy: AtprotoAudience | null;
|
|
149
139
|
constructor({ handler, proxy }: ClientOptions);
|
|
150
140
|
/**
|
|
151
141
|
* clones this XRPC client
|
package/dist/client.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../lib/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../lib/client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5E,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAC;AAC/D,OAAO,KAAK,CAAC,MAAM,8BAA8B,CAAC;AAClD,OAAO,KAAK,EACX,UAAU,EACV,WAAW,EACX,YAAY,EACZ,iBAAiB,EACjB,gBAAgB,EAChB,qBAAqB,EACrB,iBAAiB,EACjB,MA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F;CACD;AAED,uDAAuD;AACvD,qBAAa,qBAAsB,SAAQ,KAAK;IAC/C,mDAAmD;IACnD,QAAQ,CAAC,MAAM,EAAE,QAAQ,GAAG,OAAO,GAAG,QAAQ,CAAC;IAC/C,wBAAwB;IACxB,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC;IAEvB,YAAY,MAAM,EAAE,QAAQ,GAAG,OAAO,GAAG,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,GAAG,EAM/D;CACD"}
|
package/dist/client.js
CHANGED
|
@@ -82,7 +82,7 @@ export class Client {
|
|
|
82
82
|
body: input && !isWebInput ? JSON.stringify(input) : input,
|
|
83
83
|
headers: _mergeHeaders(headers, {
|
|
84
84
|
'content-type': input && !isWebInput ? 'application/json' : null,
|
|
85
|
-
'atproto-proxy':
|
|
85
|
+
'atproto-proxy': this.proxy,
|
|
86
86
|
}),
|
|
87
87
|
duplex: input instanceof ReadableStream ? 'half' : undefined,
|
|
88
88
|
});
|
|
@@ -178,12 +178,6 @@ const _constructSearchParams = (params) => {
|
|
|
178
178
|
}
|
|
179
179
|
return searchParams ? `?` + searchParams.toString() : '';
|
|
180
180
|
};
|
|
181
|
-
const _constructProxyHeader = (proxy) => {
|
|
182
|
-
if (proxy != null) {
|
|
183
|
-
return `${proxy.did}${proxy.serviceId}`;
|
|
184
|
-
}
|
|
185
|
-
return null;
|
|
186
|
-
};
|
|
187
181
|
const _mergeHeaders = (init, defaults) => {
|
|
188
182
|
let headers;
|
|
189
183
|
for (const name in defaults) {
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.js","sourceRoot":"","sources":["../lib/client.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,8BAA8B,CAAC;AAWlD,OAAO,EAAE,iBAAiB,EAA8C,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../lib/client.ts"],"names":[],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AC,OAAO,GAAG,OAAO,CAAC;IACxB,CAAC;CACD;AAED,uDAAuD;AACvD,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAM/C,YAAY,MAAqC,EAAE,MAAa;QAC/D,KAAK,CAAC,yBAAyB,MAAM,KAAK,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC;QAE5D,IAAI,CAAC,IAAI,GAAG,uBAAuB,CAAC;QACpC,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;QACrB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACtB,CAAC;CACD;AAED,aAAa"}
|
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC"}
|
package/lib/client.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Did } from '@atcute/lexicons';
|
|
2
1
|
import type { XRPCProcedures, XRPCQueries } from '@atcute/lexicons/ambient';
|
|
2
|
+
import type { AtprotoAudience } from '@atcute/lexicons/syntax';
|
|
3
3
|
import * as v from '@atcute/lexicons/validations';
|
|
4
4
|
import type {
|
|
5
5
|
InferInput,
|
|
@@ -187,30 +187,19 @@ type Namespaced<T> = { mainSchema: T };
|
|
|
187
187
|
// #endregion
|
|
188
188
|
|
|
189
189
|
// #region Client
|
|
190
|
-
/** options for configuring
|
|
191
|
-
export type
|
|
192
|
-
/**
|
|
193
|
-
|
|
190
|
+
/** options for configuring the client */
|
|
191
|
+
export type ClientOptions = {
|
|
192
|
+
/** the underlying fetch handler it should make requests with */
|
|
193
|
+
handler: FetchHandler | FetchHandlerObject;
|
|
194
194
|
/**
|
|
195
|
-
*
|
|
196
|
-
* that the upstream service should forward requests to.
|
|
195
|
+
* service proxy target, in the form `<did>#<service-id>`.
|
|
197
196
|
*
|
|
198
|
-
*
|
|
199
|
-
*
|
|
200
|
-
* common values include:
|
|
197
|
+
* common service ids include:
|
|
201
198
|
* - `#atproto_pds` (personal data server)
|
|
202
199
|
* - `#atproto_labeler` (labeler service)
|
|
203
200
|
* - `#bsky_chat` (Bluesky chat service)
|
|
204
201
|
*/
|
|
205
|
-
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
/** options for configuring the client */
|
|
209
|
-
export type ClientOptions = {
|
|
210
|
-
/** the underlying fetch handler it should make requests with */
|
|
211
|
-
handler: FetchHandler | FetchHandlerObject;
|
|
212
|
-
/** service proxy configuration */
|
|
213
|
-
proxy?: ServiceProxyOptions | null;
|
|
202
|
+
proxy?: AtprotoAudience | null;
|
|
214
203
|
};
|
|
215
204
|
|
|
216
205
|
const JSON_CONTENT_TYPE_RE = /\bapplication\/json\b/;
|
|
@@ -218,7 +207,7 @@ const JSON_CONTENT_TYPE_RE = /\bapplication\/json\b/;
|
|
|
218
207
|
/** XRPC API client */
|
|
219
208
|
export class Client<TQueries = XRPCQueries, TProcedures = XRPCProcedures> {
|
|
220
209
|
handler: FetchHandler;
|
|
221
|
-
proxy:
|
|
210
|
+
proxy: AtprotoAudience | null;
|
|
222
211
|
|
|
223
212
|
constructor({ handler, proxy = null }: ClientOptions) {
|
|
224
213
|
this.handler = buildFetchHandler(handler);
|
|
@@ -354,7 +343,7 @@ export class Client<TQueries = XRPCQueries, TProcedures = XRPCProcedures> {
|
|
|
354
343
|
body: input && !isWebInput ? JSON.stringify(input) : input,
|
|
355
344
|
headers: _mergeHeaders(headers, {
|
|
356
345
|
'content-type': input && !isWebInput ? 'application/json' : null,
|
|
357
|
-
'atproto-proxy':
|
|
346
|
+
'atproto-proxy': this.proxy,
|
|
358
347
|
}),
|
|
359
348
|
duplex: input instanceof ReadableStream ? 'half' : undefined,
|
|
360
349
|
});
|
|
@@ -466,14 +455,6 @@ const _constructSearchParams = (params: Record<string, unknown> | undefined): st
|
|
|
466
455
|
return searchParams ? `?` + searchParams.toString() : '';
|
|
467
456
|
};
|
|
468
457
|
|
|
469
|
-
const _constructProxyHeader = (proxy: ServiceProxyOptions | null | undefined): string | null => {
|
|
470
|
-
if (proxy != null) {
|
|
471
|
-
return `${proxy.did}${proxy.serviceId}`;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
return null;
|
|
475
|
-
};
|
|
476
|
-
|
|
477
458
|
const _mergeHeaders = (
|
|
478
459
|
init: HeadersInit | undefined,
|
|
479
460
|
defaults: Record<string, string | null>,
|
package/lib/index.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atcute/client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0",
|
|
4
4
|
"description": "lightweight and cute API client for AT Protocol",
|
|
5
5
|
"license": "0BSD",
|
|
6
6
|
"repository": {
|
|
@@ -22,18 +22,18 @@
|
|
|
22
22
|
"access": "public"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@atcute/identity": "^
|
|
26
|
-
"@atcute/lexicons": "^
|
|
25
|
+
"@atcute/identity": "^2.0.0",
|
|
26
|
+
"@atcute/lexicons": "^2.0.0"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@vitest/coverage-v8": "^4.1.5",
|
|
30
30
|
"vitest": "^4.1.5",
|
|
31
|
-
"@atcute/atproto": "^
|
|
32
|
-
"@atcute/
|
|
33
|
-
"@atcute/
|
|
31
|
+
"@atcute/atproto": "^4.0.0",
|
|
32
|
+
"@atcute/bluesky": "^4.0.0",
|
|
33
|
+
"@atcute/internal-dev-env": "^1.0.2"
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@atcute/lexicons": "^
|
|
36
|
+
"@atcute/lexicons": "^2.0.0"
|
|
37
37
|
},
|
|
38
38
|
"scripts": {
|
|
39
39
|
"build": "tsgo",
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import type { Did } from '@atcute/lexicons';
|
|
2
|
-
import { type FetchHandlerObject } from './fetch-handler.ts';
|
|
3
|
-
/**
|
|
4
|
-
* represents the decoded access token, for convenience
|
|
5
|
-
* @deprecated
|
|
6
|
-
*/
|
|
7
|
-
export interface AtpAccessJwt {
|
|
8
|
-
/** access token scope */
|
|
9
|
-
scope: 'com.atproto.access' | 'com.atproto.appPass' | 'com.atproto.appPassPrivileged' | 'com.atproto.signupQueued' | 'com.atproto.takendown';
|
|
10
|
-
/** account DID */
|
|
11
|
-
sub: Did;
|
|
12
|
-
/** expiration time in Unix seconds */
|
|
13
|
-
exp: number;
|
|
14
|
-
/** token issued time in Unix seconds */
|
|
15
|
-
iat: number;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* represents the the decoded refresh token, for convenience
|
|
19
|
-
* @deprecated
|
|
20
|
-
*/
|
|
21
|
-
export interface AtpRefreshJwt {
|
|
22
|
-
/** refresh token scope */
|
|
23
|
-
scope: 'com.atproto.refresh';
|
|
24
|
-
/** unique identifier for this session */
|
|
25
|
-
jti: string;
|
|
26
|
-
/** account DID */
|
|
27
|
-
sub: Did;
|
|
28
|
-
/** intended audience of this refresh token, in DID */
|
|
29
|
-
aud: Did;
|
|
30
|
-
/** token expiration time in seconds */
|
|
31
|
-
exp: number;
|
|
32
|
-
/** token issued time in seconds */
|
|
33
|
-
iat: number;
|
|
34
|
-
}
|
|
35
|
-
/** session data, can be persisted and reused */
|
|
36
|
-
export interface AtpSessionData {
|
|
37
|
-
/** refresh token */
|
|
38
|
-
refreshJwt: string;
|
|
39
|
-
/** access token */
|
|
40
|
-
accessJwt: string;
|
|
41
|
-
/** account handle */
|
|
42
|
-
handle: string;
|
|
43
|
-
/** account DID */
|
|
44
|
-
did: Did;
|
|
45
|
-
/** PDS endpoint found in the DID document, this will be used as the service URI if provided */
|
|
46
|
-
pdsUri?: string;
|
|
47
|
-
/** email address of the account, might not be available if on app password */
|
|
48
|
-
email?: string;
|
|
49
|
-
/** whether the email address has been confirmed or not */
|
|
50
|
-
emailConfirmed?: boolean;
|
|
51
|
-
/** whether the account has email-based two-factor authentication enabled */
|
|
52
|
-
emailAuthFactor?: boolean;
|
|
53
|
-
/** whether the account is active (not deactivated, taken down, or suspended) */
|
|
54
|
-
active: boolean;
|
|
55
|
-
/** possible reason for why the account is inactive */
|
|
56
|
-
inactiveStatus?: string;
|
|
57
|
-
}
|
|
58
|
-
export interface CredentialManagerOptions {
|
|
59
|
-
/** PDS server URL */
|
|
60
|
-
service: string;
|
|
61
|
-
/** custom fetch function */
|
|
62
|
-
fetch?: typeof fetch;
|
|
63
|
-
/** function called when the session expires and can't be refreshed */
|
|
64
|
-
onExpired?: (session: AtpSessionData) => void;
|
|
65
|
-
/** function called after a successful session refresh */
|
|
66
|
-
onRefresh?: (session: AtpSessionData) => void;
|
|
67
|
-
/** function called whenever the session object is updated (login, resume, refresh) */
|
|
68
|
-
onSessionUpdate?: (session: AtpSessionData) => void;
|
|
69
|
-
}
|
|
70
|
-
export declare class CredentialManager implements FetchHandlerObject {
|
|
71
|
-
#private;
|
|
72
|
-
/** service URL to make authentication requests with */
|
|
73
|
-
readonly serviceUrl: string;
|
|
74
|
-
/** fetch implementation */
|
|
75
|
-
fetch: typeof fetch;
|
|
76
|
-
/** current active session, undefined if not authenticated */
|
|
77
|
-
session?: AtpSessionData;
|
|
78
|
-
constructor({ service, onExpired, onRefresh, onSessionUpdate, fetch: _fetch, }: CredentialManagerOptions);
|
|
79
|
-
/** service URL to make actual API requests with */
|
|
80
|
-
get dispatchUrl(): string;
|
|
81
|
-
handle(pathname: string, init: RequestInit): Promise<Response>;
|
|
82
|
-
/**
|
|
83
|
-
* resume from a persisted session
|
|
84
|
-
* @param session session data, taken from `AtpAuth#session` after login
|
|
85
|
-
*/
|
|
86
|
-
resume(session: AtpSessionData): Promise<AtpSessionData>;
|
|
87
|
-
/**
|
|
88
|
-
* sign in to an account
|
|
89
|
-
* @param options credential options
|
|
90
|
-
* @returns session data
|
|
91
|
-
*/
|
|
92
|
-
login(options: AuthLoginOptions): Promise<AtpSessionData>;
|
|
93
|
-
/**
|
|
94
|
-
* sign out of the current session, invalidating it server-side
|
|
95
|
-
*/
|
|
96
|
-
logout(): Promise<void>;
|
|
97
|
-
}
|
|
98
|
-
/** credentials */
|
|
99
|
-
export interface AuthLoginOptions {
|
|
100
|
-
/** what account to login as, this could be domain handle, DID, or email address */
|
|
101
|
-
identifier: string;
|
|
102
|
-
/** account password */
|
|
103
|
-
password: string;
|
|
104
|
-
/** two-factor authentication code, if email TOTP is enabled */
|
|
105
|
-
code?: string;
|
|
106
|
-
/** allow signing in even if the account has been taken down */
|
|
107
|
-
allowTakendown?: boolean;
|
|
108
|
-
}
|
|
109
|
-
//# sourceMappingURL=credential-manager.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"credential-manager.d.ts","sourceRoot":"","sources":["../lib/credential-manager.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,kBAAkB,CAAC;AAG5C,OAAO,EAAsB,KAAK,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAGjF;;;GAGG;AACH,MAAM,WAAW,YAAY;IAC5B,yBAAyB;IACzB,KAAK,EACF,oBAAoB,GACpB,qBAAq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
3
|
-
import { simpleFetchHandler } from './fetch-handler.js';
|
|
4
|
-
import { decodeJwt } from './utils/jwt.js';
|
|
5
|
-
export class CredentialManager {
|
|
6
|
-
/** internal client instance for making authentication requests */
|
|
7
|
-
#server;
|
|
8
|
-
/** holds a promise for the current refresh operation, used for debouncing */
|
|
9
|
-
#refreshSessionPromise;
|
|
10
|
-
/** callback for session expiration */
|
|
11
|
-
#onExpired;
|
|
12
|
-
/** callback for successful session refresh */
|
|
13
|
-
#onRefresh;
|
|
14
|
-
/** callback for session updates */
|
|
15
|
-
#onSessionUpdate;
|
|
16
|
-
constructor({ service, onExpired, onRefresh, onSessionUpdate, fetch: _fetch = fetch, }) {
|
|
17
|
-
this.serviceUrl = service;
|
|
18
|
-
this.fetch = _fetch;
|
|
19
|
-
this.#server = new Client({ handler: simpleFetchHandler({ service, fetch: _fetch }) });
|
|
20
|
-
this.#onRefresh = onRefresh;
|
|
21
|
-
this.#onExpired = onExpired;
|
|
22
|
-
this.#onSessionUpdate = onSessionUpdate;
|
|
23
|
-
}
|
|
24
|
-
/** service URL to make actual API requests with */
|
|
25
|
-
get dispatchUrl() {
|
|
26
|
-
return this.session?.pdsUri ?? this.serviceUrl;
|
|
27
|
-
}
|
|
28
|
-
async handle(pathname, init) {
|
|
29
|
-
await this.#refreshSessionPromise;
|
|
30
|
-
const url = new URL(pathname, this.dispatchUrl);
|
|
31
|
-
const headers = new Headers(init.headers);
|
|
32
|
-
if (!this.session || headers.has('authorization')) {
|
|
33
|
-
return (0, this.fetch)(url, init);
|
|
34
|
-
}
|
|
35
|
-
const initialToken = this.session.accessJwt;
|
|
36
|
-
headers.set('authorization', `Bearer ${initialToken}`);
|
|
37
|
-
const initialResponse = await (0, this.fetch)(url, { ...init, headers });
|
|
38
|
-
if (initialResponse.status !== 401 && !(await isExpiredTokenResponse(initialResponse))) {
|
|
39
|
-
return initialResponse;
|
|
40
|
-
}
|
|
41
|
-
try {
|
|
42
|
-
await this.#refreshSession();
|
|
43
|
-
}
|
|
44
|
-
catch {
|
|
45
|
-
return initialResponse;
|
|
46
|
-
}
|
|
47
|
-
// return initial response if:
|
|
48
|
-
// - request was aborted
|
|
49
|
-
// - refresh failed and cleared the session
|
|
50
|
-
// - token didn't actually change (refresh failed silently)
|
|
51
|
-
// - request body was a stream (can't be resent)
|
|
52
|
-
const updatedToken = this.session?.accessJwt;
|
|
53
|
-
if (init.signal?.aborted ||
|
|
54
|
-
!updatedToken ||
|
|
55
|
-
updatedToken === initialToken ||
|
|
56
|
-
init.body instanceof ReadableStream) {
|
|
57
|
-
return initialResponse;
|
|
58
|
-
}
|
|
59
|
-
// cancel initial response to avoid resource leaks (Node.js)
|
|
60
|
-
await initialResponse.body?.cancel();
|
|
61
|
-
headers.set('authorization', `Bearer ${updatedToken}`);
|
|
62
|
-
return await (0, this.fetch)(url, { ...init, headers });
|
|
63
|
-
}
|
|
64
|
-
#refreshSession() {
|
|
65
|
-
return (this.#refreshSessionPromise ||= this.#refreshSessionInner().finally(() => (this.#refreshSessionPromise = undefined)));
|
|
66
|
-
}
|
|
67
|
-
async #refreshSessionInner() {
|
|
68
|
-
const currentSession = this.session;
|
|
69
|
-
if (!currentSession) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
const response = await this.#server.post('com.atproto.server.refreshSession', {
|
|
73
|
-
headers: {
|
|
74
|
-
authorization: `Bearer ${currentSession.refreshJwt}`,
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
if (!response.ok) {
|
|
78
|
-
const isExpired = response.status === 401 ||
|
|
79
|
-
response.data.error === 'ExpiredToken' ||
|
|
80
|
-
response.data.error === 'InvalidToken';
|
|
81
|
-
if (isExpired) {
|
|
82
|
-
this.session = undefined;
|
|
83
|
-
this.#onExpired?.(currentSession);
|
|
84
|
-
}
|
|
85
|
-
throw new ClientResponseError(response);
|
|
86
|
-
}
|
|
87
|
-
// DID must not change during refresh
|
|
88
|
-
if (response.data.did !== currentSession.did) {
|
|
89
|
-
this.session = undefined;
|
|
90
|
-
this.#onExpired?.(currentSession);
|
|
91
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidDID' } });
|
|
92
|
-
}
|
|
93
|
-
// protect against concurrent session updates
|
|
94
|
-
if (this.session !== currentSession) {
|
|
95
|
-
throw new Error('concurrent session update detected');
|
|
96
|
-
}
|
|
97
|
-
this.#updateSession({ ...currentSession, ...response.data });
|
|
98
|
-
this.#onRefresh?.(this.session);
|
|
99
|
-
}
|
|
100
|
-
#updateSession(raw) {
|
|
101
|
-
const didDoc = raw.didDoc;
|
|
102
|
-
let pdsUri;
|
|
103
|
-
if (didDoc) {
|
|
104
|
-
pdsUri = getPdsEndpoint(didDoc);
|
|
105
|
-
}
|
|
106
|
-
const newSession = {
|
|
107
|
-
accessJwt: raw.accessJwt,
|
|
108
|
-
refreshJwt: raw.refreshJwt,
|
|
109
|
-
handle: raw.handle,
|
|
110
|
-
did: raw.did,
|
|
111
|
-
pdsUri: pdsUri,
|
|
112
|
-
email: raw.email,
|
|
113
|
-
emailConfirmed: raw.emailConfirmed,
|
|
114
|
-
emailAuthFactor: raw.emailAuthFactor,
|
|
115
|
-
active: raw.active ?? true,
|
|
116
|
-
inactiveStatus: raw.status,
|
|
117
|
-
};
|
|
118
|
-
this.session = newSession;
|
|
119
|
-
this.#onSessionUpdate?.(newSession);
|
|
120
|
-
return newSession;
|
|
121
|
-
}
|
|
122
|
-
/**
|
|
123
|
-
* resume from a persisted session
|
|
124
|
-
* @param session session data, taken from `AtpAuth#session` after login
|
|
125
|
-
*/
|
|
126
|
-
async resume(session) {
|
|
127
|
-
// protect against concurrent resume of the same session
|
|
128
|
-
if (session.refreshJwt === this.session?.refreshJwt) {
|
|
129
|
-
await this.#refreshSessionPromise;
|
|
130
|
-
if (!this.session || session.did !== this.session.did) {
|
|
131
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidToken' } });
|
|
132
|
-
}
|
|
133
|
-
return this.session;
|
|
134
|
-
}
|
|
135
|
-
const now = Date.now() / 1_000 + 60 * 5;
|
|
136
|
-
const refreshToken = decodeJwt(session.refreshJwt);
|
|
137
|
-
if (now >= refreshToken.exp || refreshToken.sub !== session.did) {
|
|
138
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidToken' } });
|
|
139
|
-
}
|
|
140
|
-
const accessToken = decodeJwt(session.accessJwt);
|
|
141
|
-
if (accessToken.sub !== session.did) {
|
|
142
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidToken' } });
|
|
143
|
-
}
|
|
144
|
-
// set the session and clear any stale refresh promise
|
|
145
|
-
this.session = session;
|
|
146
|
-
this.#refreshSessionPromise = undefined;
|
|
147
|
-
if (now >= accessToken.exp) {
|
|
148
|
-
// access token expired, need to refresh
|
|
149
|
-
await this.#refreshSession();
|
|
150
|
-
}
|
|
151
|
-
else {
|
|
152
|
-
// access token still valid, fetch session info in background
|
|
153
|
-
const promise = ok(this.#server.get('com.atproto.server.getSession', {
|
|
154
|
-
headers: {
|
|
155
|
-
authorization: `Bearer ${session.accessJwt}`,
|
|
156
|
-
},
|
|
157
|
-
}));
|
|
158
|
-
promise.then((next) => {
|
|
159
|
-
const existing = this.session;
|
|
160
|
-
if (!existing || existing.did !== next.did) {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
this.#updateSession({ ...existing, ...next });
|
|
164
|
-
}, (_err) => {
|
|
165
|
-
// ignore error
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
if (!this.session) {
|
|
169
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidToken' } });
|
|
170
|
-
}
|
|
171
|
-
return this.session;
|
|
172
|
-
}
|
|
173
|
-
/**
|
|
174
|
-
* sign in to an account
|
|
175
|
-
* @param options credential options
|
|
176
|
-
* @returns session data
|
|
177
|
-
*/
|
|
178
|
-
async login(options) {
|
|
179
|
-
// reset the session
|
|
180
|
-
this.session = undefined;
|
|
181
|
-
this.#refreshSessionPromise = undefined;
|
|
182
|
-
const session = await ok(this.#server.post('com.atproto.server.createSession', {
|
|
183
|
-
input: {
|
|
184
|
-
identifier: options.identifier,
|
|
185
|
-
password: options.password,
|
|
186
|
-
authFactorToken: options.code,
|
|
187
|
-
allowTakendown: options.allowTakendown,
|
|
188
|
-
},
|
|
189
|
-
}));
|
|
190
|
-
return this.#updateSession(session);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* sign out of the current session, invalidating it server-side
|
|
194
|
-
*/
|
|
195
|
-
async logout() {
|
|
196
|
-
const currentSession = this.session;
|
|
197
|
-
if (!currentSession) {
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
this.session = undefined;
|
|
201
|
-
this.#refreshSessionPromise = undefined;
|
|
202
|
-
try {
|
|
203
|
-
await this.#server.post('com.atproto.server.deleteSession', {
|
|
204
|
-
as: null,
|
|
205
|
-
headers: {
|
|
206
|
-
authorization: `Bearer ${currentSession.refreshJwt}`,
|
|
207
|
-
},
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
catch {
|
|
211
|
-
// ignore errors - session is already cleared locally
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
const isExpiredTokenResponse = async (response) => {
|
|
216
|
-
if (response.status !== 400) {
|
|
217
|
-
return false;
|
|
218
|
-
}
|
|
219
|
-
if (extractContentType(response.headers) !== 'application/json') {
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
// this is nasty as it relies heavily on what the PDS returns, but avoiding
|
|
223
|
-
// cloning and reading the request as much as possible is better.
|
|
224
|
-
// {"error":"ExpiredToken","message":"Token has expired"}
|
|
225
|
-
// {"error":"ExpiredToken","message":"Token is expired"}
|
|
226
|
-
if (extractContentLength(response.headers) > 54 * 1.5) {
|
|
227
|
-
return false;
|
|
228
|
-
}
|
|
229
|
-
try {
|
|
230
|
-
const data = await response.clone().json();
|
|
231
|
-
if (isXRPCErrorPayload(data)) {
|
|
232
|
-
return data.error === 'ExpiredToken';
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
catch { }
|
|
236
|
-
return false;
|
|
237
|
-
};
|
|
238
|
-
const extractContentType = (headers) => {
|
|
239
|
-
return headers.get('content-type')?.split(';')[0]?.trim();
|
|
240
|
-
};
|
|
241
|
-
const extractContentLength = (headers) => {
|
|
242
|
-
return Number(headers.get('content-length') ?? ';');
|
|
243
|
-
};
|
|
244
|
-
//# sourceMappingURL=credential-manager.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"credential-manager.js","sourceRoot":"","sources":["../lib/credential-manager.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAoB,MAAM,kBAAkB,CAAC;AAGpE,OAAO,EAAE,MAAM,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAClF,OAAO,EAAE,kBAAkB,EAA2B,MAAM,oBAAoB,CAAC;AACjF,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAgF3C,MAAM,OAAO,iBAAiB;IAM7B,kEAAkE;IAClE,OAAO,CAAS;IAChB,6EAA6E;IAC7E,sBAAsB,CAA4B;IAElD,sCAAsC;IACtC,UAAU,CAAwC;IAClD,8CAA8C;IAC9C,UAAU,CAAwC;IAClD,mCAAmC;IACnC,gBAAgB,CAA8C;IAK9D,YAAY,EACX,OAAO,EACP,SAAS,EACT,SAAS,EACT,eAAe,EACf,KAAK,EAAE,MAAM,GAAG,KAAK,GACK;QAC1B,IAAI,CAAC,UAAU,GAAG,OAAO,CAAC;QAC1B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC;QAEpB,IAAI,CAAC,OAAO,GAAG,IAAI,MAAM,CAAC,EAAE,OAAO,EAAE,kBAAkB,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC;QAEvF,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;QAC5B,IAAI,CAAC,UAAU,GAAG,SAAS,C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AC,OAAO,CAAC,KAAK,kBAAkB,EAAE,CAAC;QACjE,OAAO,KAAK,CAAC;IACd,CAAC;IAED,2EAA2E;IAC3E,iEAAiE;IAEjE,yDAAyD;IACzD,wDAAwD;IACxD,IAAI,oBAAoB,CAAC,QAAQ,CAAC,OAAO,CAAC,GAAG,EAAE,GAAG,GAAG,EAAE,CAAC;QACvD,OAAO,KAAK,CAAC;IACd,CAAC;IAED,IAAI,CAAC;QACJ,MAAM,IAAI,GAAG,MAAM,QAAQ,CAAC,KAAK,EAAE,CAAC,IAAI,EAAE,CAAC;QAC3C,IAAI,kBAAkB,CAAC,IAAI,CAAC,EAAE,CAAC;YAC9B,OAAO,IAAI,CAAC,KAAK,KAAK,cAAc,CAAC;QACtC,CAAC;IACF,CAAC;IAAC,MAAM,CAAC,CAAA,CAAC;IAEV,OAAO,KAAK,CAAC;AACd,CAAC,CAAC;AAEF,MAAM,kBAAkB,GAAG,CAAC,OAAgB,EAAE,EAAE;IAC/C,OAAO,OAAO,CAAC,GAAG,CAAC,cAAc,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC;AAC3D,CAAC,CAAC;AACF,MAAM,oBAAoB,GAAG,CAAC,OAAgB,EAAE,EAAE;IACjD,OAAO,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,GAAG,CAAC,CAAC;AACrD,CAAC,CAAC"}
|
|
@@ -1,411 +0,0 @@
|
|
|
1
|
-
import type { ComAtprotoServerCreateSession } from '@atcute/atproto';
|
|
2
|
-
import { getPdsEndpoint, type DidDocument } from '@atcute/identity';
|
|
3
|
-
import type { Did } from '@atcute/lexicons';
|
|
4
|
-
|
|
5
|
-
import { Client, ClientResponseError, isXRPCErrorPayload, ok } from './client.ts';
|
|
6
|
-
import { simpleFetchHandler, type FetchHandlerObject } from './fetch-handler.ts';
|
|
7
|
-
import { decodeJwt } from './utils/jwt.ts';
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* represents the decoded access token, for convenience
|
|
11
|
-
* @deprecated
|
|
12
|
-
*/
|
|
13
|
-
export interface AtpAccessJwt {
|
|
14
|
-
/** access token scope */
|
|
15
|
-
scope:
|
|
16
|
-
| 'com.atproto.access'
|
|
17
|
-
| 'com.atproto.appPass'
|
|
18
|
-
| 'com.atproto.appPassPrivileged'
|
|
19
|
-
| 'com.atproto.signupQueued'
|
|
20
|
-
| 'com.atproto.takendown';
|
|
21
|
-
/** account DID */
|
|
22
|
-
sub: Did;
|
|
23
|
-
/** expiration time in Unix seconds */
|
|
24
|
-
exp: number;
|
|
25
|
-
/** token issued time in Unix seconds */
|
|
26
|
-
iat: number;
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* represents the the decoded refresh token, for convenience
|
|
31
|
-
* @deprecated
|
|
32
|
-
*/
|
|
33
|
-
export interface AtpRefreshJwt {
|
|
34
|
-
/** refresh token scope */
|
|
35
|
-
scope: 'com.atproto.refresh';
|
|
36
|
-
/** unique identifier for this session */
|
|
37
|
-
jti: string;
|
|
38
|
-
/** account DID */
|
|
39
|
-
sub: Did;
|
|
40
|
-
/** intended audience of this refresh token, in DID */
|
|
41
|
-
aud: Did;
|
|
42
|
-
/** token expiration time in seconds */
|
|
43
|
-
exp: number;
|
|
44
|
-
/** token issued time in seconds */
|
|
45
|
-
iat: number;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/** session data, can be persisted and reused */
|
|
49
|
-
export interface AtpSessionData {
|
|
50
|
-
/** refresh token */
|
|
51
|
-
refreshJwt: string;
|
|
52
|
-
/** access token */
|
|
53
|
-
accessJwt: string;
|
|
54
|
-
/** account handle */
|
|
55
|
-
handle: string;
|
|
56
|
-
/** account DID */
|
|
57
|
-
did: Did;
|
|
58
|
-
/** PDS endpoint found in the DID document, this will be used as the service URI if provided */
|
|
59
|
-
pdsUri?: string;
|
|
60
|
-
/** email address of the account, might not be available if on app password */
|
|
61
|
-
email?: string;
|
|
62
|
-
/** whether the email address has been confirmed or not */
|
|
63
|
-
emailConfirmed?: boolean;
|
|
64
|
-
/** whether the account has email-based two-factor authentication enabled */
|
|
65
|
-
emailAuthFactor?: boolean;
|
|
66
|
-
/** whether the account is active (not deactivated, taken down, or suspended) */
|
|
67
|
-
active: boolean;
|
|
68
|
-
/** possible reason for why the account is inactive */
|
|
69
|
-
inactiveStatus?: string;
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export interface CredentialManagerOptions {
|
|
73
|
-
/** PDS server URL */
|
|
74
|
-
service: string;
|
|
75
|
-
|
|
76
|
-
/** custom fetch function */
|
|
77
|
-
fetch?: typeof fetch;
|
|
78
|
-
|
|
79
|
-
/** function called when the session expires and can't be refreshed */
|
|
80
|
-
onExpired?: (session: AtpSessionData) => void;
|
|
81
|
-
/** function called after a successful session refresh */
|
|
82
|
-
onRefresh?: (session: AtpSessionData) => void;
|
|
83
|
-
/** function called whenever the session object is updated (login, resume, refresh) */
|
|
84
|
-
onSessionUpdate?: (session: AtpSessionData) => void;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export class CredentialManager implements FetchHandlerObject {
|
|
88
|
-
/** service URL to make authentication requests with */
|
|
89
|
-
readonly serviceUrl: string;
|
|
90
|
-
/** fetch implementation */
|
|
91
|
-
fetch: typeof fetch;
|
|
92
|
-
|
|
93
|
-
/** internal client instance for making authentication requests */
|
|
94
|
-
#server: Client;
|
|
95
|
-
/** holds a promise for the current refresh operation, used for debouncing */
|
|
96
|
-
#refreshSessionPromise: Promise<void> | undefined;
|
|
97
|
-
|
|
98
|
-
/** callback for session expiration */
|
|
99
|
-
#onExpired: CredentialManagerOptions['onExpired'];
|
|
100
|
-
/** callback for successful session refresh */
|
|
101
|
-
#onRefresh: CredentialManagerOptions['onRefresh'];
|
|
102
|
-
/** callback for session updates */
|
|
103
|
-
#onSessionUpdate: CredentialManagerOptions['onSessionUpdate'];
|
|
104
|
-
|
|
105
|
-
/** current active session, undefined if not authenticated */
|
|
106
|
-
session?: AtpSessionData;
|
|
107
|
-
|
|
108
|
-
constructor({
|
|
109
|
-
service,
|
|
110
|
-
onExpired,
|
|
111
|
-
onRefresh,
|
|
112
|
-
onSessionUpdate,
|
|
113
|
-
fetch: _fetch = fetch,
|
|
114
|
-
}: CredentialManagerOptions) {
|
|
115
|
-
this.serviceUrl = service;
|
|
116
|
-
this.fetch = _fetch;
|
|
117
|
-
|
|
118
|
-
this.#server = new Client({ handler: simpleFetchHandler({ service, fetch: _fetch }) });
|
|
119
|
-
|
|
120
|
-
this.#onRefresh = onRefresh;
|
|
121
|
-
this.#onExpired = onExpired;
|
|
122
|
-
this.#onSessionUpdate = onSessionUpdate;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
/** service URL to make actual API requests with */
|
|
126
|
-
get dispatchUrl() {
|
|
127
|
-
return this.session?.pdsUri ?? this.serviceUrl;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async handle(pathname: string, init: RequestInit): Promise<Response> {
|
|
131
|
-
await this.#refreshSessionPromise;
|
|
132
|
-
|
|
133
|
-
const url = new URL(pathname, this.dispatchUrl);
|
|
134
|
-
const headers = new Headers(init.headers);
|
|
135
|
-
|
|
136
|
-
if (!this.session || headers.has('authorization')) {
|
|
137
|
-
return (0, this.fetch)(url, init);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const initialToken = this.session.accessJwt;
|
|
141
|
-
headers.set('authorization', `Bearer ${initialToken}`);
|
|
142
|
-
|
|
143
|
-
const initialResponse = await (0, this.fetch)(url, { ...init, headers });
|
|
144
|
-
|
|
145
|
-
if (initialResponse.status !== 401 && !(await isExpiredTokenResponse(initialResponse))) {
|
|
146
|
-
return initialResponse;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
try {
|
|
150
|
-
await this.#refreshSession();
|
|
151
|
-
} catch {
|
|
152
|
-
return initialResponse;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// return initial response if:
|
|
156
|
-
// - request was aborted
|
|
157
|
-
// - refresh failed and cleared the session
|
|
158
|
-
// - token didn't actually change (refresh failed silently)
|
|
159
|
-
// - request body was a stream (can't be resent)
|
|
160
|
-
const updatedToken = this.session?.accessJwt;
|
|
161
|
-
if (
|
|
162
|
-
init.signal?.aborted ||
|
|
163
|
-
!updatedToken ||
|
|
164
|
-
updatedToken === initialToken ||
|
|
165
|
-
init.body instanceof ReadableStream
|
|
166
|
-
) {
|
|
167
|
-
return initialResponse;
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
// cancel initial response to avoid resource leaks (Node.js)
|
|
171
|
-
await initialResponse.body?.cancel();
|
|
172
|
-
|
|
173
|
-
headers.set('authorization', `Bearer ${updatedToken}`);
|
|
174
|
-
return await (0, this.fetch)(url, { ...init, headers });
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
#refreshSession() {
|
|
178
|
-
return (this.#refreshSessionPromise ||= this.#refreshSessionInner().finally(
|
|
179
|
-
() => (this.#refreshSessionPromise = undefined),
|
|
180
|
-
));
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async #refreshSessionInner(): Promise<void> {
|
|
184
|
-
const currentSession = this.session;
|
|
185
|
-
if (!currentSession) {
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const response = await this.#server.post('com.atproto.server.refreshSession', {
|
|
190
|
-
headers: {
|
|
191
|
-
authorization: `Bearer ${currentSession.refreshJwt}`,
|
|
192
|
-
},
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
if (!response.ok) {
|
|
196
|
-
const isExpired =
|
|
197
|
-
response.status === 401 ||
|
|
198
|
-
response.data.error === 'ExpiredToken' ||
|
|
199
|
-
response.data.error === 'InvalidToken';
|
|
200
|
-
|
|
201
|
-
if (isExpired) {
|
|
202
|
-
this.session = undefined;
|
|
203
|
-
this.#onExpired?.(currentSession);
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
throw new ClientResponseError(response);
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
// DID must not change during refresh
|
|
210
|
-
if (response.data.did !== currentSession.did) {
|
|
211
|
-
this.session = undefined;
|
|
212
|
-
this.#onExpired?.(currentSession);
|
|
213
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidDID' } });
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
// protect against concurrent session updates
|
|
217
|
-
if (this.session !== currentSession) {
|
|
218
|
-
throw new Error('concurrent session update detected');
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
this.#updateSession({ ...currentSession, ...response.data });
|
|
222
|
-
this.#onRefresh?.(this.session!);
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
#updateSession(raw: ComAtprotoServerCreateSession.$output): AtpSessionData {
|
|
226
|
-
const didDoc = raw.didDoc as DidDocument | undefined;
|
|
227
|
-
|
|
228
|
-
let pdsUri: string | undefined;
|
|
229
|
-
if (didDoc) {
|
|
230
|
-
pdsUri = getPdsEndpoint(didDoc);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
const newSession: AtpSessionData = {
|
|
234
|
-
accessJwt: raw.accessJwt,
|
|
235
|
-
refreshJwt: raw.refreshJwt,
|
|
236
|
-
handle: raw.handle,
|
|
237
|
-
did: raw.did,
|
|
238
|
-
pdsUri: pdsUri,
|
|
239
|
-
email: raw.email,
|
|
240
|
-
emailConfirmed: raw.emailConfirmed,
|
|
241
|
-
emailAuthFactor: raw.emailAuthFactor,
|
|
242
|
-
active: raw.active ?? true,
|
|
243
|
-
inactiveStatus: raw.status,
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
this.session = newSession;
|
|
247
|
-
this.#onSessionUpdate?.(newSession);
|
|
248
|
-
|
|
249
|
-
return newSession;
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/**
|
|
253
|
-
* resume from a persisted session
|
|
254
|
-
* @param session session data, taken from `AtpAuth#session` after login
|
|
255
|
-
*/
|
|
256
|
-
async resume(session: AtpSessionData): Promise<AtpSessionData> {
|
|
257
|
-
// protect against concurrent resume of the same session
|
|
258
|
-
if (session.refreshJwt === this.session?.refreshJwt) {
|
|
259
|
-
await this.#refreshSessionPromise;
|
|
260
|
-
if (!this.session || session.did !== this.session.did) {
|
|
261
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidToken' } });
|
|
262
|
-
}
|
|
263
|
-
return this.session;
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const now = Date.now() / 1_000 + 60 * 5;
|
|
267
|
-
|
|
268
|
-
const refreshToken = decodeJwt(session.refreshJwt) as AtpRefreshJwt;
|
|
269
|
-
if (now >= refreshToken.exp || refreshToken.sub !== session.did) {
|
|
270
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidToken' } });
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
const accessToken = decodeJwt(session.accessJwt) as AtpAccessJwt;
|
|
274
|
-
if (accessToken.sub !== session.did) {
|
|
275
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidToken' } });
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// set the session and clear any stale refresh promise
|
|
279
|
-
this.session = session;
|
|
280
|
-
this.#refreshSessionPromise = undefined;
|
|
281
|
-
|
|
282
|
-
if (now >= accessToken.exp) {
|
|
283
|
-
// access token expired, need to refresh
|
|
284
|
-
await this.#refreshSession();
|
|
285
|
-
} else {
|
|
286
|
-
// access token still valid, fetch session info in background
|
|
287
|
-
const promise = ok(
|
|
288
|
-
this.#server.get('com.atproto.server.getSession', {
|
|
289
|
-
headers: {
|
|
290
|
-
authorization: `Bearer ${session.accessJwt}`,
|
|
291
|
-
},
|
|
292
|
-
}),
|
|
293
|
-
);
|
|
294
|
-
|
|
295
|
-
promise.then(
|
|
296
|
-
(next) => {
|
|
297
|
-
const existing = this.session;
|
|
298
|
-
if (!existing || existing.did !== next.did) {
|
|
299
|
-
return;
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
this.#updateSession({ ...existing, ...next });
|
|
303
|
-
},
|
|
304
|
-
(_err) => {
|
|
305
|
-
// ignore error
|
|
306
|
-
},
|
|
307
|
-
);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
if (!this.session) {
|
|
311
|
-
throw new ClientResponseError({ status: 401, data: { error: 'InvalidToken' } });
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
return this.session;
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* sign in to an account
|
|
319
|
-
* @param options credential options
|
|
320
|
-
* @returns session data
|
|
321
|
-
*/
|
|
322
|
-
async login(options: AuthLoginOptions): Promise<AtpSessionData> {
|
|
323
|
-
// reset the session
|
|
324
|
-
this.session = undefined;
|
|
325
|
-
this.#refreshSessionPromise = undefined;
|
|
326
|
-
|
|
327
|
-
const session = await ok(
|
|
328
|
-
this.#server.post('com.atproto.server.createSession', {
|
|
329
|
-
input: {
|
|
330
|
-
identifier: options.identifier,
|
|
331
|
-
password: options.password,
|
|
332
|
-
authFactorToken: options.code,
|
|
333
|
-
allowTakendown: options.allowTakendown,
|
|
334
|
-
},
|
|
335
|
-
}),
|
|
336
|
-
);
|
|
337
|
-
|
|
338
|
-
return this.#updateSession(session);
|
|
339
|
-
}
|
|
340
|
-
|
|
341
|
-
/**
|
|
342
|
-
* sign out of the current session, invalidating it server-side
|
|
343
|
-
*/
|
|
344
|
-
async logout(): Promise<void> {
|
|
345
|
-
const currentSession = this.session;
|
|
346
|
-
if (!currentSession) {
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
|
|
350
|
-
this.session = undefined;
|
|
351
|
-
this.#refreshSessionPromise = undefined;
|
|
352
|
-
|
|
353
|
-
try {
|
|
354
|
-
await this.#server.post('com.atproto.server.deleteSession', {
|
|
355
|
-
as: null,
|
|
356
|
-
headers: {
|
|
357
|
-
authorization: `Bearer ${currentSession.refreshJwt}`,
|
|
358
|
-
},
|
|
359
|
-
});
|
|
360
|
-
} catch {
|
|
361
|
-
// ignore errors - session is already cleared locally
|
|
362
|
-
}
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
|
|
366
|
-
/** credentials */
|
|
367
|
-
export interface AuthLoginOptions {
|
|
368
|
-
/** what account to login as, this could be domain handle, DID, or email address */
|
|
369
|
-
identifier: string;
|
|
370
|
-
/** account password */
|
|
371
|
-
password: string;
|
|
372
|
-
/** two-factor authentication code, if email TOTP is enabled */
|
|
373
|
-
code?: string;
|
|
374
|
-
/** allow signing in even if the account has been taken down */
|
|
375
|
-
allowTakendown?: boolean;
|
|
376
|
-
}
|
|
377
|
-
|
|
378
|
-
const isExpiredTokenResponse = async (response: Response): Promise<boolean> => {
|
|
379
|
-
if (response.status !== 400) {
|
|
380
|
-
return false;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
if (extractContentType(response.headers) !== 'application/json') {
|
|
384
|
-
return false;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
// this is nasty as it relies heavily on what the PDS returns, but avoiding
|
|
388
|
-
// cloning and reading the request as much as possible is better.
|
|
389
|
-
|
|
390
|
-
// {"error":"ExpiredToken","message":"Token has expired"}
|
|
391
|
-
// {"error":"ExpiredToken","message":"Token is expired"}
|
|
392
|
-
if (extractContentLength(response.headers) > 54 * 1.5) {
|
|
393
|
-
return false;
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
try {
|
|
397
|
-
const data = await response.clone().json();
|
|
398
|
-
if (isXRPCErrorPayload(data)) {
|
|
399
|
-
return data.error === 'ExpiredToken';
|
|
400
|
-
}
|
|
401
|
-
} catch {}
|
|
402
|
-
|
|
403
|
-
return false;
|
|
404
|
-
};
|
|
405
|
-
|
|
406
|
-
const extractContentType = (headers: Headers) => {
|
|
407
|
-
return headers.get('content-type')?.split(';')[0]?.trim();
|
|
408
|
-
};
|
|
409
|
-
const extractContentLength = (headers: Headers) => {
|
|
410
|
-
return Number(headers.get('content-length') ?? ';');
|
|
411
|
-
};
|