@happyview/oauth-client 1.1.0-dev.2 → 1.1.1
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/README.md +15 -0
- package/dist/index.cjs +28 -89
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +28 -61
- package/dist/index.d.ts +28 -61
- package/dist/index.js +27 -86
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -52,6 +52,21 @@ const session = await client.registerSession({
|
|
|
52
52
|
});
|
|
53
53
|
```
|
|
54
54
|
|
|
55
|
+
The returned session includes the approved scopes:
|
|
56
|
+
|
|
57
|
+
```typescript
|
|
58
|
+
console.log(session.scopes); // ["atproto", "transition:generic"]
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Retrieving Session Info
|
|
62
|
+
|
|
63
|
+
Fetch the current session's approved scopes from the server:
|
|
64
|
+
|
|
65
|
+
```typescript
|
|
66
|
+
const info = await client.getSession("did:plc:abc123");
|
|
67
|
+
console.log(info.scopes); // ["atproto", "transition:generic"]
|
|
68
|
+
```
|
|
69
|
+
|
|
55
70
|
### Making Authenticated Requests
|
|
56
71
|
|
|
57
72
|
The returned `HappyViewSession` provides a `fetchHandler` that automatically attaches DPoP proof headers:
|
package/dist/index.cjs
CHANGED
|
@@ -15,7 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
15
15
|
}
|
|
16
16
|
return to;
|
|
17
17
|
};
|
|
18
|
-
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
19
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
20
19
|
|
|
21
20
|
// src/index.ts
|
|
@@ -29,14 +28,11 @@ __export(index_exports, {
|
|
|
29
28
|
InvalidStateError: () => InvalidStateError,
|
|
30
29
|
LAST_ACTIVE_KEY: () => LAST_ACTIVE_KEY,
|
|
31
30
|
MemoryStorage: () => MemoryStorage,
|
|
32
|
-
OAuthCallbackError: () => OAuthCallbackError,
|
|
33
31
|
ResolutionError: () => ResolutionError,
|
|
34
32
|
TokenExchangeError: () => TokenExchangeError,
|
|
35
33
|
importJwk: () => importJwk
|
|
36
34
|
});
|
|
37
35
|
module.exports = __toCommonJS(index_exports);
|
|
38
|
-
__reExport(index_exports, require("@atproto/jwk"), module.exports);
|
|
39
|
-
__reExport(index_exports, require("@atproto/jwk-webcrypto"), module.exports);
|
|
40
36
|
|
|
41
37
|
// src/errors.ts
|
|
42
38
|
var HappyViewError = class extends Error {
|
|
@@ -85,26 +81,6 @@ var ResolutionError = class extends HappyViewError {
|
|
|
85
81
|
this.name = "ResolutionError";
|
|
86
82
|
}
|
|
87
83
|
};
|
|
88
|
-
var OAuthCallbackError = class _OAuthCallbackError extends HappyViewError {
|
|
89
|
-
params;
|
|
90
|
-
state;
|
|
91
|
-
static from(err, params, state) {
|
|
92
|
-
if (err instanceof _OAuthCallbackError) return err;
|
|
93
|
-
const message = err instanceof Error ? err.message : void 0;
|
|
94
|
-
return new _OAuthCallbackError(params, message, state, err);
|
|
95
|
-
}
|
|
96
|
-
constructor(params, message, state, cause) {
|
|
97
|
-
super(
|
|
98
|
-
message ?? params.get("error_description") ?? "OAuth callback error"
|
|
99
|
-
);
|
|
100
|
-
this.name = "OAuthCallbackError";
|
|
101
|
-
this.params = params;
|
|
102
|
-
this.state = state;
|
|
103
|
-
if (cause !== void 0) {
|
|
104
|
-
this.cause = cause;
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
};
|
|
108
84
|
|
|
109
85
|
// src/import-jwk.ts
|
|
110
86
|
var import_jwk_webcrypto = require("@atproto/jwk-webcrypto");
|
|
@@ -142,43 +118,21 @@ function base64url(buffer) {
|
|
|
142
118
|
}
|
|
143
119
|
var HappyViewSession = class {
|
|
144
120
|
did;
|
|
121
|
+
scopes;
|
|
145
122
|
dpopKey;
|
|
146
123
|
accessToken;
|
|
147
124
|
clientKey;
|
|
148
125
|
instanceUrl;
|
|
149
|
-
_scopes;
|
|
150
|
-
_pdsUrl;
|
|
151
|
-
_issuer;
|
|
152
126
|
_fetch;
|
|
153
|
-
_onSignOut;
|
|
154
127
|
dpopNonce;
|
|
155
128
|
constructor(options) {
|
|
156
129
|
this.did = options.did;
|
|
130
|
+
this.scopes = options.scopes;
|
|
157
131
|
this.dpopKey = options.dpopKey;
|
|
158
132
|
this.accessToken = options.accessToken;
|
|
159
133
|
this.clientKey = options.clientKey;
|
|
160
134
|
this.instanceUrl = options.instanceUrl.replace(/\/+$/, "");
|
|
161
|
-
this._scopes = options.scopes;
|
|
162
|
-
this._pdsUrl = options.pdsUrl;
|
|
163
|
-
this._issuer = options.issuer;
|
|
164
135
|
this._fetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
165
|
-
this._onSignOut = options.onSignOut;
|
|
166
|
-
}
|
|
167
|
-
get sub() {
|
|
168
|
-
return this.did;
|
|
169
|
-
}
|
|
170
|
-
getTokenInfo() {
|
|
171
|
-
return {
|
|
172
|
-
scope: this._scopes,
|
|
173
|
-
iss: this._issuer,
|
|
174
|
-
aud: this._pdsUrl,
|
|
175
|
-
sub: this.did
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
async signOut() {
|
|
179
|
-
if (this._onSignOut) {
|
|
180
|
-
await this._onSignOut();
|
|
181
|
-
}
|
|
182
136
|
}
|
|
183
137
|
async fetchHandler(url, init) {
|
|
184
138
|
const fullUrl = /^https?:\/\//i.test(url) ? url : `${this.instanceUrl}${url}`;
|
|
@@ -257,37 +211,17 @@ var MemoryStorage = class {
|
|
|
257
211
|
var STORAGE_PREFIX = "happyview:session:";
|
|
258
212
|
var LAST_ACTIVE_KEY = "happyview:last-active-did";
|
|
259
213
|
var HappyViewOAuthClient = class {
|
|
260
|
-
static async fetchMetadata({
|
|
261
|
-
clientId,
|
|
262
|
-
fetch: fetchFn = globalThis.fetch,
|
|
263
|
-
signal
|
|
264
|
-
}) {
|
|
265
|
-
signal?.throwIfAborted();
|
|
266
|
-
const resp = await fetchFn(clientId, { redirect: "error", signal });
|
|
267
|
-
if (resp.status !== 200) {
|
|
268
|
-
resp.body?.cancel?.();
|
|
269
|
-
throw new Error(`Failed to fetch client metadata: ${resp.status}`);
|
|
270
|
-
}
|
|
271
|
-
const mime = resp.headers.get("content-type")?.split(";")[0].trim();
|
|
272
|
-
if (mime !== "application/json") {
|
|
273
|
-
resp.body?.cancel?.();
|
|
274
|
-
throw new Error(`Invalid client metadata content type: ${mime}`);
|
|
275
|
-
}
|
|
276
|
-
return resp.json();
|
|
277
|
-
}
|
|
278
214
|
instanceUrl;
|
|
279
215
|
clientKey;
|
|
280
216
|
storage;
|
|
281
217
|
clientSecret;
|
|
282
218
|
_fetch;
|
|
283
|
-
sessionHooks;
|
|
284
219
|
constructor(options) {
|
|
285
220
|
this.instanceUrl = options.instanceUrl.replace(/\/+$/, "");
|
|
286
221
|
this.clientKey = options.clientKey;
|
|
287
222
|
this.clientSecret = options.clientSecret;
|
|
288
223
|
this.storage = options.storage ?? new MemoryStorage();
|
|
289
224
|
this._fetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
290
|
-
this.sessionHooks = options.sessionHooks ?? {};
|
|
291
225
|
}
|
|
292
226
|
get isConfidential() {
|
|
293
227
|
return this.clientSecret !== void 0;
|
|
@@ -370,29 +304,22 @@ var HappyViewOAuthClient = class {
|
|
|
370
304
|
accessToken: params.accessToken,
|
|
371
305
|
clientKey: this.clientKey,
|
|
372
306
|
instanceUrl: this.instanceUrl,
|
|
373
|
-
scopes:
|
|
374
|
-
pdsUrl: params.pdsUrl,
|
|
375
|
-
issuer: params.issuer
|
|
307
|
+
scopes: data.scopes
|
|
376
308
|
};
|
|
377
309
|
await this.storage.set(
|
|
378
310
|
`${STORAGE_PREFIX}${data.did}`,
|
|
379
311
|
JSON.stringify(storedSession)
|
|
380
312
|
);
|
|
381
313
|
await this.storage.set(LAST_ACTIVE_KEY, data.did);
|
|
382
|
-
|
|
314
|
+
return new HappyViewSession({
|
|
383
315
|
did: data.did,
|
|
384
316
|
dpopKey,
|
|
385
317
|
accessToken: params.accessToken,
|
|
386
318
|
clientKey: this.clientKey,
|
|
387
319
|
instanceUrl: this.instanceUrl,
|
|
388
|
-
scopes:
|
|
389
|
-
|
|
390
|
-
issuer: params.issuer,
|
|
391
|
-
fetch: this._fetch,
|
|
392
|
-
onSignOut: () => this.deleteSession(data.did)
|
|
320
|
+
scopes: data.scopes,
|
|
321
|
+
fetch: this._fetch
|
|
393
322
|
});
|
|
394
|
-
this.sessionHooks.onSessionUpdate?.(data.did);
|
|
395
|
-
return session;
|
|
396
323
|
}
|
|
397
324
|
async deleteSession(did) {
|
|
398
325
|
const session = await this.restoreSession(did);
|
|
@@ -415,7 +342,6 @@ var HappyViewOAuthClient = class {
|
|
|
415
342
|
if (lastActive === did) {
|
|
416
343
|
await this.storage.delete(LAST_ACTIVE_KEY);
|
|
417
344
|
}
|
|
418
|
-
this.sessionHooks.onSessionDelete?.(did);
|
|
419
345
|
}
|
|
420
346
|
async restoreSession(did) {
|
|
421
347
|
const stored = await this.storage.get(`${STORAGE_PREFIX}${did}`);
|
|
@@ -428,13 +354,29 @@ var HappyViewOAuthClient = class {
|
|
|
428
354
|
accessToken: data.accessToken,
|
|
429
355
|
clientKey: data.clientKey,
|
|
430
356
|
instanceUrl: data.instanceUrl,
|
|
431
|
-
scopes: data.scopes,
|
|
432
|
-
|
|
433
|
-
issuer: data.issuer,
|
|
434
|
-
fetch: this._fetch,
|
|
435
|
-
onSignOut: () => this.deleteSession(data.did)
|
|
357
|
+
scopes: data.scopes ?? [],
|
|
358
|
+
fetch: this._fetch
|
|
436
359
|
});
|
|
437
360
|
}
|
|
361
|
+
async getSession(did) {
|
|
362
|
+
const session = await this.restoreSession(did);
|
|
363
|
+
if (!session) {
|
|
364
|
+
throw new ApiError("No stored session for this DID", 404, {});
|
|
365
|
+
}
|
|
366
|
+
const resp = await session.fetchHandler(
|
|
367
|
+
`${this.instanceUrl}/oauth/sessions/${did}`,
|
|
368
|
+
{ method: "GET" }
|
|
369
|
+
);
|
|
370
|
+
if (!resp.ok) {
|
|
371
|
+
const body = await resp.json().catch(() => ({}));
|
|
372
|
+
throw new ApiError(
|
|
373
|
+
`Failed to get session: ${resp.status} ${body.message ?? resp.statusText}`,
|
|
374
|
+
resp.status,
|
|
375
|
+
body
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
return resp.json();
|
|
379
|
+
}
|
|
438
380
|
async restore() {
|
|
439
381
|
const did = await this.storage.get(LAST_ACTIVE_KEY);
|
|
440
382
|
if (!did) return null;
|
|
@@ -471,11 +413,8 @@ async function computePkceChallenge(verifier) {
|
|
|
471
413
|
InvalidStateError,
|
|
472
414
|
LAST_ACTIVE_KEY,
|
|
473
415
|
MemoryStorage,
|
|
474
|
-
OAuthCallbackError,
|
|
475
416
|
ResolutionError,
|
|
476
417
|
TokenExchangeError,
|
|
477
|
-
importJwk
|
|
478
|
-
...require("@atproto/jwk"),
|
|
479
|
-
...require("@atproto/jwk-webcrypto")
|
|
418
|
+
importJwk
|
|
480
419
|
});
|
|
481
420
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/import-jwk.ts","../src/session.ts","../src/storage.ts","../src/client.ts"],"sourcesContent":["export * from \"@atproto/jwk\";\nexport * from \"@atproto/jwk-webcrypto\";\n\nexport { HappyViewOAuthClient, LAST_ACTIVE_KEY } from \"./client\";\nexport type { FetchMetadataOptions } from \"./client\";\nexport { importJwk } from \"./import-jwk\";\nexport {\n ApiError,\n AuthenticationError,\n HappyViewError,\n InvalidStateError,\n OAuthCallbackError,\n ResolutionError,\n TokenExchangeError,\n} from \"./errors\";\nexport { HappyViewSession } from \"./session\";\nexport type { HappyViewSessionOptions } from \"./session\";\nexport { MemoryStorage } from \"./storage\";\nexport type {\n DpopProvision,\n HappyViewOAuthClientOptions,\n ProvisionKeyResponse,\n RegisterSessionParams,\n RegisterSessionResponse,\n SessionEventHooks,\n StorageAdapter,\n StoredSession,\n TokenInfo,\n} from \"./types\";\n","export class HappyViewError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"HappyViewError\";\n }\n}\n\nexport class ApiError extends HappyViewError {\n readonly status: number;\n readonly body: unknown;\n\n constructor(message: string, status: number, body?: unknown) {\n super(message);\n this.name = \"ApiError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport class AuthenticationError extends HappyViewError {\n readonly status: number;\n\n constructor(message: string, status: number = 401) {\n super(message);\n this.name = \"AuthenticationError\";\n this.status = status;\n }\n}\n\nexport class InvalidStateError extends HappyViewError {\n constructor(message: string) {\n super(message);\n this.name = \"InvalidStateError\";\n }\n}\n\nexport class TokenExchangeError extends HappyViewError {\n readonly status: number;\n readonly body: string;\n\n constructor(message: string, status: number, body: string) {\n super(message);\n this.name = \"TokenExchangeError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport class ResolutionError extends HappyViewError {\n constructor(message: string) {\n super(message);\n this.name = \"ResolutionError\";\n }\n}\n\nexport class OAuthCallbackError extends HappyViewError {\n readonly params: URLSearchParams;\n readonly state: string | undefined;\n\n static from(\n err: unknown,\n params: URLSearchParams,\n state?: string,\n ): OAuthCallbackError {\n if (err instanceof OAuthCallbackError) return err;\n const message = err instanceof Error ? err.message : undefined;\n return new OAuthCallbackError(params, message, state, err);\n }\n\n constructor(\n params: URLSearchParams,\n message?: string,\n state?: string,\n cause?: unknown,\n ) {\n super(\n message ??\n params.get(\"error_description\") ??\n \"OAuth callback error\",\n );\n this.name = \"OAuthCallbackError\";\n this.params = params;\n this.state = state;\n if (cause !== undefined) {\n this.cause = cause;\n }\n }\n}\n","import { WebcryptoKey } from \"@atproto/jwk-webcrypto\";\n\n/**\n * Import a raw ES256 JWK (as returned by HappyView's /oauth/dpop-keys)\n * into a WebcryptoKey that can sign JWTs.\n */\nexport async function importJwk(jwk: JsonWebKey): Promise<WebcryptoKey> {\n const privateKey = await crypto.subtle.importKey(\n \"jwk\",\n jwk,\n { name: \"ECDSA\", namedCurve: \"P-256\" },\n true,\n [\"sign\"],\n );\n\n // Derive public key by stripping the private component\n const { d: _, ...publicJwkFields } = jwk;\n const publicKey = await crypto.subtle.importKey(\n \"jwk\",\n publicJwkFields,\n { name: \"ECDSA\", namedCurve: \"P-256\" },\n true,\n [\"verify\"],\n );\n\n return WebcryptoKey.fromKeypair({ privateKey, publicKey });\n}\n","import type { Key } from \"@atproto/jwk\";\nimport type { TokenInfo } from \"./types\";\n\nexport interface HappyViewSessionOptions {\n did: string;\n dpopKey: Key;\n accessToken: string;\n clientKey: string;\n instanceUrl: string;\n scopes?: string;\n pdsUrl?: string;\n issuer?: string;\n fetch?: typeof globalThis.fetch;\n onSignOut?: () => Promise<void>;\n}\n\nfunction randomHex(byteLength: number): string {\n const bytes = crypto.getRandomValues(new Uint8Array(byteLength));\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nfunction base64url(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n\nexport class HappyViewSession {\n readonly did: string;\n\n private readonly dpopKey: Key;\n private readonly accessToken: string;\n private readonly clientKey: string;\n private readonly instanceUrl: string;\n private readonly _scopes: string | undefined;\n private readonly _pdsUrl: string | undefined;\n private readonly _issuer: string | undefined;\n private readonly _fetch: typeof globalThis.fetch;\n private readonly _onSignOut: (() => Promise<void>) | undefined;\n private dpopNonce: string | undefined;\n\n constructor(options: HappyViewSessionOptions) {\n this.did = options.did;\n this.dpopKey = options.dpopKey;\n this.accessToken = options.accessToken;\n this.clientKey = options.clientKey;\n this.instanceUrl = options.instanceUrl.replace(/\\/+$/, \"\");\n this._scopes = options.scopes;\n this._pdsUrl = options.pdsUrl;\n this._issuer = options.issuer;\n this._fetch = options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init)) as typeof globalThis.fetch;\n this._onSignOut = options.onSignOut;\n }\n\n get sub(): string {\n return this.did;\n }\n\n getTokenInfo(): TokenInfo {\n return {\n scope: this._scopes,\n iss: this._issuer,\n aud: this._pdsUrl,\n sub: this.did,\n };\n }\n\n async signOut(): Promise<void> {\n if (this._onSignOut) {\n await this._onSignOut();\n }\n }\n\n async fetchHandler(url: string, init: RequestInit): Promise<Response> {\n const fullUrl = /^https?:\\/\\//i.test(url)\n ? url\n : `${this.instanceUrl}${url}`;\n const method = (init.method ?? \"GET\").toUpperCase();\n const htu = fullUrl.split(\"?\")[0];\n\n const sendWithProof = async (): Promise<Response> => {\n const tokenHash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(this.accessToken),\n );\n const ath = base64url(tokenHash);\n\n const proof = await this.dpopKey.createJwt(\n {\n alg: \"ES256\",\n typ: \"dpop+jwt\",\n jwk: this.dpopKey.publicJwk!,\n },\n {\n htm: method,\n htu,\n iat: Math.floor(Date.now() / 1000),\n jti: randomHex(16),\n ath,\n nonce: this.dpopNonce,\n },\n );\n\n const headers = new Headers(init.headers);\n headers.set(\"authorization\", `DPoP ${this.accessToken}`);\n headers.set(\"dpop\", proof);\n headers.set(\"x-client-key\", this.clientKey);\n\n return this._fetch(fullUrl, { ...init, headers });\n };\n\n let response: Response;\n try {\n response = await sendWithProof();\n } catch (e) {\n const info = `_fetch type: ${typeof this._fetch}, toString: ${String(this._fetch).slice(0, 120)}, url: ${fullUrl}`;\n throw new Error(`fetchHandler failed: ${(e as Error).message}\\n\\nDEBUG: ${info}`);\n }\n\n // Retry once if the server requires a DPoP nonce we didn't have\n const nonce = response.headers.get(\"dpop-nonce\");\n if (nonce && nonce !== this.dpopNonce && (response.status === 401 || response.status === 400)) {\n this.dpopNonce = nonce;\n try {\n response = await sendWithProof();\n } catch (e) {\n throw new Error(`fetchHandler retry failed: ${(e as Error).message}`);\n }\n const retryNonce = response.headers.get(\"dpop-nonce\");\n if (retryNonce) {\n this.dpopNonce = retryNonce;\n }\n } else if (nonce) {\n this.dpopNonce = nonce;\n }\n\n return response;\n }\n}\n","import type { StorageAdapter } from \"./types\";\n\nexport class MemoryStorage implements StorageAdapter {\n private data = new Map<string, string>();\n\n async get(key: string): Promise<string | null> {\n return this.data.get(key) ?? null;\n }\n\n async set(key: string, value: string): Promise<void> {\n this.data.set(key, value);\n }\n\n async delete(key: string): Promise<void> {\n this.data.delete(key);\n }\n}\n","import type { Key } from \"@atproto/jwk\";\nimport { ApiError } from \"./errors\";\nimport { importJwk } from \"./import-jwk\";\nimport { HappyViewSession } from \"./session\";\nimport { MemoryStorage } from \"./storage\";\nimport type {\n HappyViewOAuthClientOptions,\n ProvisionKeyResponse,\n RegisterSessionParams,\n RegisterSessionResponse,\n SessionEventHooks,\n StorageAdapter,\n StoredSession,\n} from \"./types\";\n\nconst STORAGE_PREFIX = \"happyview:session:\";\nexport const LAST_ACTIVE_KEY = \"happyview:last-active-did\";\n\nexport interface FetchMetadataOptions {\n clientId: string;\n fetch?: typeof globalThis.fetch;\n signal?: AbortSignal;\n}\n\nexport class HappyViewOAuthClient {\n static async fetchMetadata({\n clientId,\n fetch: fetchFn = globalThis.fetch,\n signal,\n }: FetchMetadataOptions): Promise<Record<string, unknown>> {\n signal?.throwIfAborted();\n const resp = await fetchFn(clientId, { redirect: \"error\", signal });\n if (resp.status !== 200) {\n resp.body?.cancel?.();\n throw new Error(`Failed to fetch client metadata: ${resp.status}`);\n }\n const mime = resp.headers.get(\"content-type\")?.split(\";\")[0].trim();\n if (mime !== \"application/json\") {\n resp.body?.cancel?.();\n throw new Error(`Invalid client metadata content type: ${mime}`);\n }\n return resp.json() as Promise<Record<string, unknown>>;\n }\n\n protected readonly instanceUrl: string;\n protected readonly clientKey: string;\n protected readonly storage: StorageAdapter;\n private readonly clientSecret: string | undefined;\n protected readonly _fetch: typeof globalThis.fetch;\n private readonly sessionHooks: SessionEventHooks;\n\n constructor(\n options: HappyViewOAuthClientOptions & {\n fetch?: typeof globalThis.fetch;\n },\n ) {\n this.instanceUrl = options.instanceUrl.replace(/\\/+$/, \"\");\n this.clientKey = options.clientKey;\n this.clientSecret = options.clientSecret;\n this.storage = options.storage ?? new MemoryStorage();\n this._fetch = options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init)) as typeof globalThis.fetch;\n this.sessionHooks = options.sessionHooks ?? {};\n }\n\n get isConfidential(): boolean {\n return this.clientSecret !== undefined;\n }\n\n async provisionDpopKey(): Promise<{\n provisionId: string;\n dpopKey: Key;\n rawJwk: JsonWebKey;\n pkceVerifier?: string;\n }> {\n const headers: Record<string, string> = {\n \"content-type\": \"application/json\",\n \"x-client-key\": this.clientKey,\n };\n if (this.clientSecret) {\n headers[\"x-client-secret\"] = this.clientSecret;\n }\n\n const body: Record<string, unknown> = {};\n let pkceVerifier: string | undefined;\n\n if (!this.isConfidential) {\n pkceVerifier = generatePkceVerifier();\n const challenge = await computePkceChallenge(pkceVerifier);\n body.pkce_challenge = challenge;\n }\n\n const resp = await this._fetch(`${this.instanceUrl}/oauth/dpop-keys`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n });\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to provision DPoP key: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n const data: ProvisionKeyResponse = await resp.json();\n const dpopKey = await importJwk(data.dpop_key);\n return {\n provisionId: data.provision_id,\n dpopKey,\n rawJwk: data.dpop_key,\n pkceVerifier,\n };\n }\n\n async registerSession(\n params: RegisterSessionParams,\n ): Promise<HappyViewSession> {\n const headers: Record<string, string> = {\n \"content-type\": \"application/json\",\n \"x-client-key\": this.clientKey,\n };\n if (this.clientSecret) {\n headers[\"x-client-secret\"] = this.clientSecret;\n }\n\n const body: Record<string, unknown> = {\n provision_id: params.provisionId,\n did: params.did,\n access_token: params.accessToken,\n refresh_token: params.refreshToken,\n scopes: params.scopes,\n pds_url: params.pdsUrl,\n issuer: params.issuer,\n };\n\n if (!this.isConfidential && params.pkceVerifier) {\n body.pkce_verifier = params.pkceVerifier;\n }\n\n const resp = await this._fetch(`${this.instanceUrl}/oauth/sessions`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n });\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to register session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n const data: RegisterSessionResponse = await resp.json();\n const dpopKey = await importJwk(params.dpopKey);\n\n const storedSession: StoredSession = {\n did: data.did,\n dpopKey: params.dpopKey,\n accessToken: params.accessToken,\n clientKey: this.clientKey,\n instanceUrl: this.instanceUrl,\n scopes: params.scopes,\n pdsUrl: params.pdsUrl,\n issuer: params.issuer,\n };\n await this.storage.set(\n `${STORAGE_PREFIX}${data.did}`,\n JSON.stringify(storedSession),\n );\n await this.storage.set(LAST_ACTIVE_KEY, data.did);\n\n const session = new HappyViewSession({\n did: data.did,\n dpopKey,\n accessToken: params.accessToken,\n clientKey: this.clientKey,\n instanceUrl: this.instanceUrl,\n scopes: params.scopes,\n pdsUrl: params.pdsUrl,\n issuer: params.issuer,\n fetch: this._fetch,\n onSignOut: () => this.deleteSession(data.did),\n });\n\n this.sessionHooks.onSessionUpdate?.(data.did);\n\n return session;\n }\n\n async deleteSession(did: string): Promise<void> {\n const session = await this.restoreSession(did);\n if (session) {\n const resp = await session.fetchHandler(\n `${this.instanceUrl}/oauth/sessions/${did}`,\n { method: \"DELETE\" },\n );\n\n if (!resp.ok && resp.status !== 404) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to delete session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n }\n\n await this.storage.delete(`${STORAGE_PREFIX}${did}`);\n\n const lastActive = await this.storage.get(LAST_ACTIVE_KEY);\n if (lastActive === did) {\n await this.storage.delete(LAST_ACTIVE_KEY);\n }\n\n this.sessionHooks.onSessionDelete?.(did);\n }\n\n async restoreSession(did: string): Promise<HappyViewSession | null> {\n const stored = await this.storage.get(`${STORAGE_PREFIX}${did}`);\n if (!stored) return null;\n\n const data: StoredSession = JSON.parse(stored);\n const dpopKey = await importJwk(data.dpopKey);\n return new HappyViewSession({\n did: data.did,\n dpopKey,\n accessToken: data.accessToken,\n clientKey: data.clientKey,\n instanceUrl: data.instanceUrl,\n scopes: data.scopes,\n pdsUrl: data.pdsUrl,\n issuer: data.issuer,\n fetch: this._fetch,\n onSignOut: () => this.deleteSession(data.did),\n });\n }\n\n async restore(): Promise<HappyViewSession | null> {\n const did = await this.storage.get(LAST_ACTIVE_KEY);\n if (!did) return null;\n return this.restoreSession(did);\n }\n}\n\n// PKCE helpers — inline since they're just a few lines of Web Crypto\nfunction generatePkceVerifier(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(32));\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n\nasync function computePkceChallenge(verifier: string): Promise<string> {\n const hash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(verifier),\n );\n const bytes = new Uint8Array(hash);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAAc,yBAAd;AAC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AChD,UAAM,MAAM,MAAM,KAAK,QAAQ,IAAI,eAAe;AAClD,QAAI,CAAC,IAAK,QAAO;AACjB,WAAO,KAAK,eAAe,GAAG;AAAA,EAChC;AACF;AAGA,SAAS,uBAA+B;AACtC,QAAM,QAAQ,OAAO,gBAAgB,IAAI,WAAW,EAAE,CAAC;AACvD,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAU,OAAO,aAAa,MAAM,CAAC,CAAC;AAAA,EACxC;AACA,SAAO,KAAK,MAAM,EACf,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,EAAE;AACtB;AAEA,eAAe,qBAAqB,UAAmC;AACrE,QAAM,OAAO,MAAM,OAAO,OAAO;AAAA,IAC/B;AAAA,IACA,IAAI,YAAY,EAAE,OAAO,QAAQ;AAAA,EACnC;AACA,QAAM,QAAQ,IAAI,WAAW,IAAI;AACjC,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAU,OAAO,aAAa,MAAM,CAAC,CAAC;AAAA,EACxC;AACA,SAAO,KAAK,MAAM,EACf,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,EAAE;AACtB;","names":["body"]}
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/import-jwk.ts","../src/session.ts","../src/storage.ts","../src/client.ts"],"sourcesContent":["export { HappyViewOAuthClient, LAST_ACTIVE_KEY } from \"./client\";\nexport { importJwk } from \"./import-jwk\";\nexport {\n ApiError,\n AuthenticationError,\n HappyViewError,\n InvalidStateError,\n ResolutionError,\n TokenExchangeError,\n} from \"./errors\";\nexport { HappyViewSession } from \"./session\";\nexport type { HappyViewSessionOptions } from \"./session\";\nexport { MemoryStorage } from \"./storage\";\nexport type {\n DpopProvision,\n GetSessionResponse,\n HappyViewOAuthClientOptions,\n ProvisionKeyResponse,\n RegisterSessionParams,\n RegisterSessionResponse,\n StorageAdapter,\n StoredSession,\n} from \"./types\";\n","export class HappyViewError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"HappyViewError\";\n }\n}\n\nexport class ApiError extends HappyViewError {\n readonly status: number;\n readonly body: unknown;\n\n constructor(message: string, status: number, body?: unknown) {\n super(message);\n this.name = \"ApiError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport class AuthenticationError extends HappyViewError {\n readonly status: number;\n\n constructor(message: string, status: number = 401) {\n super(message);\n this.name = \"AuthenticationError\";\n this.status = status;\n }\n}\n\nexport class InvalidStateError extends HappyViewError {\n constructor(message: string) {\n super(message);\n this.name = \"InvalidStateError\";\n }\n}\n\nexport class TokenExchangeError extends HappyViewError {\n readonly status: number;\n readonly body: string;\n\n constructor(message: string, status: number, body: string) {\n super(message);\n this.name = \"TokenExchangeError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport class ResolutionError extends HappyViewError {\n constructor(message: string) {\n super(message);\n this.name = \"ResolutionError\";\n }\n}\n","import { WebcryptoKey } from \"@atproto/jwk-webcrypto\";\n\n/**\n * Import a raw ES256 JWK (as returned by HappyView's /oauth/dpop-keys)\n * into a WebcryptoKey that can sign JWTs.\n */\nexport async function importJwk(jwk: JsonWebKey): Promise<WebcryptoKey> {\n const privateKey = await crypto.subtle.importKey(\n \"jwk\",\n jwk,\n { name: \"ECDSA\", namedCurve: \"P-256\" },\n true,\n [\"sign\"],\n );\n\n // Derive public key by stripping the private component\n const { d: _, ...publicJwkFields } = jwk;\n const publicKey = await crypto.subtle.importKey(\n \"jwk\",\n publicJwkFields,\n { name: \"ECDSA\", namedCurve: \"P-256\" },\n true,\n [\"verify\"],\n );\n\n return WebcryptoKey.fromKeypair({ privateKey, publicKey });\n}\n","import type { Key } from \"@atproto/jwk\";\n\nexport interface HappyViewSessionOptions {\n did: string;\n dpopKey: Key;\n accessToken: string;\n clientKey: string;\n instanceUrl: string;\n scopes: string[];\n fetch?: typeof globalThis.fetch;\n}\n\nfunction randomHex(byteLength: number): string {\n const bytes = crypto.getRandomValues(new Uint8Array(byteLength));\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nfunction base64url(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n\nexport class HappyViewSession {\n readonly did: string;\n readonly scopes: string[];\n\n private readonly dpopKey: Key;\n private readonly accessToken: string;\n private readonly clientKey: string;\n private readonly instanceUrl: string;\n private readonly _fetch: typeof globalThis.fetch;\n private dpopNonce: string | undefined;\n\n constructor(options: HappyViewSessionOptions) {\n this.did = options.did;\n this.scopes = options.scopes;\n this.dpopKey = options.dpopKey;\n this.accessToken = options.accessToken;\n this.clientKey = options.clientKey;\n this.instanceUrl = options.instanceUrl.replace(/\\/+$/, \"\");\n this._fetch = options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init)) as typeof globalThis.fetch;\n }\n\n async fetchHandler(url: string, init: RequestInit): Promise<Response> {\n const fullUrl = /^https?:\\/\\//i.test(url)\n ? url\n : `${this.instanceUrl}${url}`;\n const method = (init.method ?? \"GET\").toUpperCase();\n const htu = fullUrl.split(\"?\")[0];\n\n const sendWithProof = async (): Promise<Response> => {\n const tokenHash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(this.accessToken),\n );\n const ath = base64url(tokenHash);\n\n const proof = await this.dpopKey.createJwt(\n {\n alg: \"ES256\",\n typ: \"dpop+jwt\",\n jwk: this.dpopKey.publicJwk!,\n },\n {\n htm: method,\n htu,\n iat: Math.floor(Date.now() / 1000),\n jti: randomHex(16),\n ath,\n nonce: this.dpopNonce,\n },\n );\n\n const headers = new Headers(init.headers);\n headers.set(\"authorization\", `DPoP ${this.accessToken}`);\n headers.set(\"dpop\", proof);\n headers.set(\"x-client-key\", this.clientKey);\n\n return this._fetch(fullUrl, { ...init, headers });\n };\n\n let response: Response;\n try {\n response = await sendWithProof();\n } catch (e) {\n const info = `_fetch type: ${typeof this._fetch}, toString: ${String(this._fetch).slice(0, 120)}, url: ${fullUrl}`;\n throw new Error(`fetchHandler failed: ${(e as Error).message}\\n\\nDEBUG: ${info}`);\n }\n\n // Retry once if the server requires a DPoP nonce we didn't have\n const nonce = response.headers.get(\"dpop-nonce\");\n if (nonce && nonce !== this.dpopNonce && (response.status === 401 || response.status === 400)) {\n this.dpopNonce = nonce;\n try {\n response = await sendWithProof();\n } catch (e) {\n throw new Error(`fetchHandler retry failed: ${(e as Error).message}`);\n }\n const retryNonce = response.headers.get(\"dpop-nonce\");\n if (retryNonce) {\n this.dpopNonce = retryNonce;\n }\n } else if (nonce) {\n this.dpopNonce = nonce;\n }\n\n return response;\n }\n}\n","import type { StorageAdapter } from \"./types\";\n\nexport class MemoryStorage implements StorageAdapter {\n private data = new Map<string, string>();\n\n async get(key: string): Promise<string | null> {\n return this.data.get(key) ?? null;\n }\n\n async set(key: string, value: string): Promise<void> {\n this.data.set(key, value);\n }\n\n async delete(key: string): Promise<void> {\n this.data.delete(key);\n }\n}\n","import type { Key } from \"@atproto/jwk\";\nimport { ApiError } from \"./errors\";\nimport { importJwk } from \"./import-jwk\";\nimport { HappyViewSession } from \"./session\";\nimport { MemoryStorage } from \"./storage\";\nimport type {\n GetSessionResponse,\n HappyViewOAuthClientOptions,\n ProvisionKeyResponse,\n RegisterSessionParams,\n RegisterSessionResponse,\n StorageAdapter,\n StoredSession,\n} from \"./types\";\n\nconst STORAGE_PREFIX = \"happyview:session:\";\nexport const LAST_ACTIVE_KEY = \"happyview:last-active-did\";\n\nexport class HappyViewOAuthClient {\n protected readonly instanceUrl: string;\n protected readonly clientKey: string;\n protected readonly storage: StorageAdapter;\n private readonly clientSecret: string | undefined;\n protected readonly _fetch: typeof globalThis.fetch;\n\n constructor(\n options: HappyViewOAuthClientOptions & {\n fetch?: typeof globalThis.fetch;\n },\n ) {\n this.instanceUrl = options.instanceUrl.replace(/\\/+$/, \"\");\n this.clientKey = options.clientKey;\n this.clientSecret = options.clientSecret;\n this.storage = options.storage ?? new MemoryStorage();\n this._fetch = options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init)) as typeof globalThis.fetch;\n }\n\n get isConfidential(): boolean {\n return this.clientSecret !== undefined;\n }\n\n async provisionDpopKey(): Promise<{\n provisionId: string;\n dpopKey: Key;\n rawJwk: JsonWebKey;\n pkceVerifier?: string;\n }> {\n const headers: Record<string, string> = {\n \"content-type\": \"application/json\",\n \"x-client-key\": this.clientKey,\n };\n if (this.clientSecret) {\n headers[\"x-client-secret\"] = this.clientSecret;\n }\n\n const body: Record<string, unknown> = {};\n let pkceVerifier: string | undefined;\n\n if (!this.isConfidential) {\n pkceVerifier = generatePkceVerifier();\n const challenge = await computePkceChallenge(pkceVerifier);\n body.pkce_challenge = challenge;\n }\n\n const resp = await this._fetch(`${this.instanceUrl}/oauth/dpop-keys`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n });\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to provision DPoP key: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n const data: ProvisionKeyResponse = await resp.json();\n const dpopKey = await importJwk(data.dpop_key);\n return {\n provisionId: data.provision_id,\n dpopKey,\n rawJwk: data.dpop_key,\n pkceVerifier,\n };\n }\n\n async registerSession(\n params: RegisterSessionParams,\n ): Promise<HappyViewSession> {\n const headers: Record<string, string> = {\n \"content-type\": \"application/json\",\n \"x-client-key\": this.clientKey,\n };\n if (this.clientSecret) {\n headers[\"x-client-secret\"] = this.clientSecret;\n }\n\n const body: Record<string, unknown> = {\n provision_id: params.provisionId,\n did: params.did,\n access_token: params.accessToken,\n refresh_token: params.refreshToken,\n scopes: params.scopes,\n pds_url: params.pdsUrl,\n issuer: params.issuer,\n };\n\n if (!this.isConfidential && params.pkceVerifier) {\n body.pkce_verifier = params.pkceVerifier;\n }\n\n const resp = await this._fetch(`${this.instanceUrl}/oauth/sessions`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n });\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to register session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n const data: RegisterSessionResponse = await resp.json();\n const dpopKey = await importJwk(params.dpopKey);\n\n const storedSession: StoredSession = {\n did: data.did,\n dpopKey: params.dpopKey,\n accessToken: params.accessToken,\n clientKey: this.clientKey,\n instanceUrl: this.instanceUrl,\n scopes: data.scopes,\n };\n await this.storage.set(\n `${STORAGE_PREFIX}${data.did}`,\n JSON.stringify(storedSession),\n );\n await this.storage.set(LAST_ACTIVE_KEY, data.did);\n\n return new HappyViewSession({\n did: data.did,\n dpopKey,\n accessToken: params.accessToken,\n clientKey: this.clientKey,\n instanceUrl: this.instanceUrl,\n scopes: data.scopes,\n fetch: this._fetch,\n });\n }\n\n async deleteSession(did: string): Promise<void> {\n const session = await this.restoreSession(did);\n if (session) {\n const resp = await session.fetchHandler(\n `${this.instanceUrl}/oauth/sessions/${did}`,\n { method: \"DELETE\" },\n );\n\n if (!resp.ok && resp.status !== 404) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to delete session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n }\n\n await this.storage.delete(`${STORAGE_PREFIX}${did}`);\n\n const lastActive = await this.storage.get(LAST_ACTIVE_KEY);\n if (lastActive === did) {\n await this.storage.delete(LAST_ACTIVE_KEY);\n }\n }\n\n async restoreSession(did: string): Promise<HappyViewSession | null> {\n const stored = await this.storage.get(`${STORAGE_PREFIX}${did}`);\n if (!stored) return null;\n\n const data: StoredSession = JSON.parse(stored);\n const dpopKey = await importJwk(data.dpopKey);\n return new HappyViewSession({\n did: data.did,\n dpopKey,\n accessToken: data.accessToken,\n clientKey: data.clientKey,\n instanceUrl: data.instanceUrl,\n scopes: data.scopes ?? [],\n fetch: this._fetch,\n });\n }\n\n async getSession(did: string): Promise<GetSessionResponse> {\n const session = await this.restoreSession(did);\n if (!session) {\n throw new ApiError(\"No stored session for this DID\", 404, {});\n }\n\n const resp = await session.fetchHandler(\n `${this.instanceUrl}/oauth/sessions/${did}`,\n { method: \"GET\" },\n );\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to get session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n return resp.json();\n }\n\n async restore(): Promise<HappyViewSession | null> {\n const did = await this.storage.get(LAST_ACTIVE_KEY);\n if (!did) return null;\n return this.restoreSession(did);\n }\n}\n\n// PKCE helpers — inline since they're just a few lines of Web Crypto\nfunction generatePkceVerifier(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(32));\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n\nasync function computePkceChallenge(verifier: string): Promise<string> {\n const hash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(verifier),\n );\n const bytes = new Uint8Array(hash);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EACxC,YAAY,SAAiB;AAC3B,UAAM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,WAAO,KAAK,eAAe,GAAG;AAAA,EAChC;AACF;AAGA,SAAS,uBAA+B;AACtC,QAAM,QAAQ,OAAO,gBAAgB,IAAI,WAAW,EAAE,CAAC;AACvD,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAU,OAAO,aAAa,MAAM,CAAC,CAAC;AAAA,EACxC;AACA,SAAO,KAAK,MAAM,EACf,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,EAAE;AACtB;AAEA,eAAe,qBAAqB,UAAmC;AACrE,QAAM,OAAO,MAAM,OAAO,OAAO;AAAA,IAC/B;AAAA,IACA,IAAI,YAAY,EAAE,OAAO,QAAQ;AAAA,EACnC;AACA,QAAM,QAAQ,IAAI,WAAW,IAAI;AACjC,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAU,OAAO,aAAa,MAAM,CAAC,CAAC;AAAA,EACxC;AACA,SAAO,KAAK,MAAM,EACf,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,EAAE;AACtB;","names":["body"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
1
|
import { Key } from '@atproto/jwk';
|
|
2
|
-
export * from '@atproto/jwk';
|
|
3
2
|
import { WebcryptoKey } from '@atproto/jwk-webcrypto';
|
|
4
|
-
|
|
3
|
+
|
|
4
|
+
interface HappyViewSessionOptions {
|
|
5
|
+
did: string;
|
|
6
|
+
dpopKey: Key;
|
|
7
|
+
accessToken: string;
|
|
8
|
+
clientKey: string;
|
|
9
|
+
instanceUrl: string;
|
|
10
|
+
scopes: string[];
|
|
11
|
+
fetch?: typeof globalThis.fetch;
|
|
12
|
+
}
|
|
13
|
+
declare class HappyViewSession {
|
|
14
|
+
readonly did: string;
|
|
15
|
+
readonly scopes: string[];
|
|
16
|
+
private readonly dpopKey;
|
|
17
|
+
private readonly accessToken;
|
|
18
|
+
private readonly clientKey;
|
|
19
|
+
private readonly instanceUrl;
|
|
20
|
+
private readonly _fetch;
|
|
21
|
+
private dpopNonce;
|
|
22
|
+
constructor(options: HappyViewSessionOptions);
|
|
23
|
+
fetchHandler(url: string, init: RequestInit): Promise<Response>;
|
|
24
|
+
}
|
|
5
25
|
|
|
6
26
|
interface StorageAdapter {
|
|
7
27
|
get(key: string): Promise<string | null>;
|
|
8
28
|
set(key: string, value: string): Promise<void>;
|
|
9
29
|
delete(key: string): Promise<void>;
|
|
10
30
|
}
|
|
11
|
-
interface SessionEventHooks {
|
|
12
|
-
onSessionUpdate?: (did: string) => void;
|
|
13
|
-
onSessionDelete?: (did: string, cause?: unknown) => void;
|
|
14
|
-
}
|
|
15
31
|
interface HappyViewOAuthClientOptions {
|
|
16
32
|
instanceUrl: string;
|
|
17
33
|
clientKey: string;
|
|
18
34
|
clientSecret?: string;
|
|
19
35
|
storage?: StorageAdapter;
|
|
20
|
-
sessionHooks?: SessionEventHooks;
|
|
21
36
|
}
|
|
22
37
|
interface DpopProvision {
|
|
23
38
|
provisionId: string;
|
|
@@ -46,17 +61,7 @@ interface StoredSession {
|
|
|
46
61
|
accessToken: string;
|
|
47
62
|
clientKey: string;
|
|
48
63
|
instanceUrl: string;
|
|
49
|
-
scopes
|
|
50
|
-
pdsUrl?: string;
|
|
51
|
-
issuer?: string;
|
|
52
|
-
}
|
|
53
|
-
interface TokenInfo {
|
|
54
|
-
expiresAt?: Date;
|
|
55
|
-
expired?: boolean;
|
|
56
|
-
scope?: string;
|
|
57
|
-
iss?: string;
|
|
58
|
-
aud?: string;
|
|
59
|
-
sub: string;
|
|
64
|
+
scopes: string[];
|
|
60
65
|
}
|
|
61
66
|
interface ProvisionKeyResponse {
|
|
62
67
|
provision_id: string;
|
|
@@ -65,53 +70,20 @@ interface ProvisionKeyResponse {
|
|
|
65
70
|
interface RegisterSessionResponse {
|
|
66
71
|
session_id: string;
|
|
67
72
|
did: string;
|
|
73
|
+
scopes: string[];
|
|
68
74
|
}
|
|
69
|
-
|
|
70
|
-
interface HappyViewSessionOptions {
|
|
75
|
+
interface GetSessionResponse {
|
|
71
76
|
did: string;
|
|
72
|
-
|
|
73
|
-
accessToken: string;
|
|
74
|
-
clientKey: string;
|
|
75
|
-
instanceUrl: string;
|
|
76
|
-
scopes?: string;
|
|
77
|
-
pdsUrl?: string;
|
|
78
|
-
issuer?: string;
|
|
79
|
-
fetch?: typeof globalThis.fetch;
|
|
80
|
-
onSignOut?: () => Promise<void>;
|
|
81
|
-
}
|
|
82
|
-
declare class HappyViewSession {
|
|
83
|
-
readonly did: string;
|
|
84
|
-
private readonly dpopKey;
|
|
85
|
-
private readonly accessToken;
|
|
86
|
-
private readonly clientKey;
|
|
87
|
-
private readonly instanceUrl;
|
|
88
|
-
private readonly _scopes;
|
|
89
|
-
private readonly _pdsUrl;
|
|
90
|
-
private readonly _issuer;
|
|
91
|
-
private readonly _fetch;
|
|
92
|
-
private readonly _onSignOut;
|
|
93
|
-
private dpopNonce;
|
|
94
|
-
constructor(options: HappyViewSessionOptions);
|
|
95
|
-
get sub(): string;
|
|
96
|
-
getTokenInfo(): TokenInfo;
|
|
97
|
-
signOut(): Promise<void>;
|
|
98
|
-
fetchHandler(url: string, init: RequestInit): Promise<Response>;
|
|
77
|
+
scopes: string[];
|
|
99
78
|
}
|
|
100
79
|
|
|
101
80
|
declare const LAST_ACTIVE_KEY = "happyview:last-active-did";
|
|
102
|
-
interface FetchMetadataOptions {
|
|
103
|
-
clientId: string;
|
|
104
|
-
fetch?: typeof globalThis.fetch;
|
|
105
|
-
signal?: AbortSignal;
|
|
106
|
-
}
|
|
107
81
|
declare class HappyViewOAuthClient {
|
|
108
|
-
static fetchMetadata({ clientId, fetch: fetchFn, signal, }: FetchMetadataOptions): Promise<Record<string, unknown>>;
|
|
109
82
|
protected readonly instanceUrl: string;
|
|
110
83
|
protected readonly clientKey: string;
|
|
111
84
|
protected readonly storage: StorageAdapter;
|
|
112
85
|
private readonly clientSecret;
|
|
113
86
|
protected readonly _fetch: typeof globalThis.fetch;
|
|
114
|
-
private readonly sessionHooks;
|
|
115
87
|
constructor(options: HappyViewOAuthClientOptions & {
|
|
116
88
|
fetch?: typeof globalThis.fetch;
|
|
117
89
|
});
|
|
@@ -125,6 +97,7 @@ declare class HappyViewOAuthClient {
|
|
|
125
97
|
registerSession(params: RegisterSessionParams): Promise<HappyViewSession>;
|
|
126
98
|
deleteSession(did: string): Promise<void>;
|
|
127
99
|
restoreSession(did: string): Promise<HappyViewSession | null>;
|
|
100
|
+
getSession(did: string): Promise<GetSessionResponse>;
|
|
128
101
|
restore(): Promise<HappyViewSession | null>;
|
|
129
102
|
}
|
|
130
103
|
|
|
@@ -157,12 +130,6 @@ declare class TokenExchangeError extends HappyViewError {
|
|
|
157
130
|
declare class ResolutionError extends HappyViewError {
|
|
158
131
|
constructor(message: string);
|
|
159
132
|
}
|
|
160
|
-
declare class OAuthCallbackError extends HappyViewError {
|
|
161
|
-
readonly params: URLSearchParams;
|
|
162
|
-
readonly state: string | undefined;
|
|
163
|
-
static from(err: unknown, params: URLSearchParams, state?: string): OAuthCallbackError;
|
|
164
|
-
constructor(params: URLSearchParams, message?: string, state?: string, cause?: unknown);
|
|
165
|
-
}
|
|
166
133
|
|
|
167
134
|
declare class MemoryStorage implements StorageAdapter {
|
|
168
135
|
private data;
|
|
@@ -171,4 +138,4 @@ declare class MemoryStorage implements StorageAdapter {
|
|
|
171
138
|
delete(key: string): Promise<void>;
|
|
172
139
|
}
|
|
173
140
|
|
|
174
|
-
export { ApiError, AuthenticationError, type DpopProvision, type
|
|
141
|
+
export { ApiError, AuthenticationError, type DpopProvision, type GetSessionResponse, HappyViewError, HappyViewOAuthClient, type HappyViewOAuthClientOptions, HappyViewSession, type HappyViewSessionOptions, InvalidStateError, LAST_ACTIVE_KEY, MemoryStorage, type ProvisionKeyResponse, type RegisterSessionParams, type RegisterSessionResponse, ResolutionError, type StorageAdapter, type StoredSession, TokenExchangeError, importJwk };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
1
|
import { Key } from '@atproto/jwk';
|
|
2
|
-
export * from '@atproto/jwk';
|
|
3
2
|
import { WebcryptoKey } from '@atproto/jwk-webcrypto';
|
|
4
|
-
|
|
3
|
+
|
|
4
|
+
interface HappyViewSessionOptions {
|
|
5
|
+
did: string;
|
|
6
|
+
dpopKey: Key;
|
|
7
|
+
accessToken: string;
|
|
8
|
+
clientKey: string;
|
|
9
|
+
instanceUrl: string;
|
|
10
|
+
scopes: string[];
|
|
11
|
+
fetch?: typeof globalThis.fetch;
|
|
12
|
+
}
|
|
13
|
+
declare class HappyViewSession {
|
|
14
|
+
readonly did: string;
|
|
15
|
+
readonly scopes: string[];
|
|
16
|
+
private readonly dpopKey;
|
|
17
|
+
private readonly accessToken;
|
|
18
|
+
private readonly clientKey;
|
|
19
|
+
private readonly instanceUrl;
|
|
20
|
+
private readonly _fetch;
|
|
21
|
+
private dpopNonce;
|
|
22
|
+
constructor(options: HappyViewSessionOptions);
|
|
23
|
+
fetchHandler(url: string, init: RequestInit): Promise<Response>;
|
|
24
|
+
}
|
|
5
25
|
|
|
6
26
|
interface StorageAdapter {
|
|
7
27
|
get(key: string): Promise<string | null>;
|
|
8
28
|
set(key: string, value: string): Promise<void>;
|
|
9
29
|
delete(key: string): Promise<void>;
|
|
10
30
|
}
|
|
11
|
-
interface SessionEventHooks {
|
|
12
|
-
onSessionUpdate?: (did: string) => void;
|
|
13
|
-
onSessionDelete?: (did: string, cause?: unknown) => void;
|
|
14
|
-
}
|
|
15
31
|
interface HappyViewOAuthClientOptions {
|
|
16
32
|
instanceUrl: string;
|
|
17
33
|
clientKey: string;
|
|
18
34
|
clientSecret?: string;
|
|
19
35
|
storage?: StorageAdapter;
|
|
20
|
-
sessionHooks?: SessionEventHooks;
|
|
21
36
|
}
|
|
22
37
|
interface DpopProvision {
|
|
23
38
|
provisionId: string;
|
|
@@ -46,17 +61,7 @@ interface StoredSession {
|
|
|
46
61
|
accessToken: string;
|
|
47
62
|
clientKey: string;
|
|
48
63
|
instanceUrl: string;
|
|
49
|
-
scopes
|
|
50
|
-
pdsUrl?: string;
|
|
51
|
-
issuer?: string;
|
|
52
|
-
}
|
|
53
|
-
interface TokenInfo {
|
|
54
|
-
expiresAt?: Date;
|
|
55
|
-
expired?: boolean;
|
|
56
|
-
scope?: string;
|
|
57
|
-
iss?: string;
|
|
58
|
-
aud?: string;
|
|
59
|
-
sub: string;
|
|
64
|
+
scopes: string[];
|
|
60
65
|
}
|
|
61
66
|
interface ProvisionKeyResponse {
|
|
62
67
|
provision_id: string;
|
|
@@ -65,53 +70,20 @@ interface ProvisionKeyResponse {
|
|
|
65
70
|
interface RegisterSessionResponse {
|
|
66
71
|
session_id: string;
|
|
67
72
|
did: string;
|
|
73
|
+
scopes: string[];
|
|
68
74
|
}
|
|
69
|
-
|
|
70
|
-
interface HappyViewSessionOptions {
|
|
75
|
+
interface GetSessionResponse {
|
|
71
76
|
did: string;
|
|
72
|
-
|
|
73
|
-
accessToken: string;
|
|
74
|
-
clientKey: string;
|
|
75
|
-
instanceUrl: string;
|
|
76
|
-
scopes?: string;
|
|
77
|
-
pdsUrl?: string;
|
|
78
|
-
issuer?: string;
|
|
79
|
-
fetch?: typeof globalThis.fetch;
|
|
80
|
-
onSignOut?: () => Promise<void>;
|
|
81
|
-
}
|
|
82
|
-
declare class HappyViewSession {
|
|
83
|
-
readonly did: string;
|
|
84
|
-
private readonly dpopKey;
|
|
85
|
-
private readonly accessToken;
|
|
86
|
-
private readonly clientKey;
|
|
87
|
-
private readonly instanceUrl;
|
|
88
|
-
private readonly _scopes;
|
|
89
|
-
private readonly _pdsUrl;
|
|
90
|
-
private readonly _issuer;
|
|
91
|
-
private readonly _fetch;
|
|
92
|
-
private readonly _onSignOut;
|
|
93
|
-
private dpopNonce;
|
|
94
|
-
constructor(options: HappyViewSessionOptions);
|
|
95
|
-
get sub(): string;
|
|
96
|
-
getTokenInfo(): TokenInfo;
|
|
97
|
-
signOut(): Promise<void>;
|
|
98
|
-
fetchHandler(url: string, init: RequestInit): Promise<Response>;
|
|
77
|
+
scopes: string[];
|
|
99
78
|
}
|
|
100
79
|
|
|
101
80
|
declare const LAST_ACTIVE_KEY = "happyview:last-active-did";
|
|
102
|
-
interface FetchMetadataOptions {
|
|
103
|
-
clientId: string;
|
|
104
|
-
fetch?: typeof globalThis.fetch;
|
|
105
|
-
signal?: AbortSignal;
|
|
106
|
-
}
|
|
107
81
|
declare class HappyViewOAuthClient {
|
|
108
|
-
static fetchMetadata({ clientId, fetch: fetchFn, signal, }: FetchMetadataOptions): Promise<Record<string, unknown>>;
|
|
109
82
|
protected readonly instanceUrl: string;
|
|
110
83
|
protected readonly clientKey: string;
|
|
111
84
|
protected readonly storage: StorageAdapter;
|
|
112
85
|
private readonly clientSecret;
|
|
113
86
|
protected readonly _fetch: typeof globalThis.fetch;
|
|
114
|
-
private readonly sessionHooks;
|
|
115
87
|
constructor(options: HappyViewOAuthClientOptions & {
|
|
116
88
|
fetch?: typeof globalThis.fetch;
|
|
117
89
|
});
|
|
@@ -125,6 +97,7 @@ declare class HappyViewOAuthClient {
|
|
|
125
97
|
registerSession(params: RegisterSessionParams): Promise<HappyViewSession>;
|
|
126
98
|
deleteSession(did: string): Promise<void>;
|
|
127
99
|
restoreSession(did: string): Promise<HappyViewSession | null>;
|
|
100
|
+
getSession(did: string): Promise<GetSessionResponse>;
|
|
128
101
|
restore(): Promise<HappyViewSession | null>;
|
|
129
102
|
}
|
|
130
103
|
|
|
@@ -157,12 +130,6 @@ declare class TokenExchangeError extends HappyViewError {
|
|
|
157
130
|
declare class ResolutionError extends HappyViewError {
|
|
158
131
|
constructor(message: string);
|
|
159
132
|
}
|
|
160
|
-
declare class OAuthCallbackError extends HappyViewError {
|
|
161
|
-
readonly params: URLSearchParams;
|
|
162
|
-
readonly state: string | undefined;
|
|
163
|
-
static from(err: unknown, params: URLSearchParams, state?: string): OAuthCallbackError;
|
|
164
|
-
constructor(params: URLSearchParams, message?: string, state?: string, cause?: unknown);
|
|
165
|
-
}
|
|
166
133
|
|
|
167
134
|
declare class MemoryStorage implements StorageAdapter {
|
|
168
135
|
private data;
|
|
@@ -171,4 +138,4 @@ declare class MemoryStorage implements StorageAdapter {
|
|
|
171
138
|
delete(key: string): Promise<void>;
|
|
172
139
|
}
|
|
173
140
|
|
|
174
|
-
export { ApiError, AuthenticationError, type DpopProvision, type
|
|
141
|
+
export { ApiError, AuthenticationError, type DpopProvision, type GetSessionResponse, HappyViewError, HappyViewOAuthClient, type HappyViewOAuthClientOptions, HappyViewSession, type HappyViewSessionOptions, InvalidStateError, LAST_ACTIVE_KEY, MemoryStorage, type ProvisionKeyResponse, type RegisterSessionParams, type RegisterSessionResponse, ResolutionError, type StorageAdapter, type StoredSession, TokenExchangeError, importJwk };
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// src/index.ts
|
|
2
|
-
export * from "@atproto/jwk";
|
|
3
|
-
export * from "@atproto/jwk-webcrypto";
|
|
4
|
-
|
|
5
1
|
// src/errors.ts
|
|
6
2
|
var HappyViewError = class extends Error {
|
|
7
3
|
constructor(message) {
|
|
@@ -49,26 +45,6 @@ var ResolutionError = class extends HappyViewError {
|
|
|
49
45
|
this.name = "ResolutionError";
|
|
50
46
|
}
|
|
51
47
|
};
|
|
52
|
-
var OAuthCallbackError = class _OAuthCallbackError extends HappyViewError {
|
|
53
|
-
params;
|
|
54
|
-
state;
|
|
55
|
-
static from(err, params, state) {
|
|
56
|
-
if (err instanceof _OAuthCallbackError) return err;
|
|
57
|
-
const message = err instanceof Error ? err.message : void 0;
|
|
58
|
-
return new _OAuthCallbackError(params, message, state, err);
|
|
59
|
-
}
|
|
60
|
-
constructor(params, message, state, cause) {
|
|
61
|
-
super(
|
|
62
|
-
message ?? params.get("error_description") ?? "OAuth callback error"
|
|
63
|
-
);
|
|
64
|
-
this.name = "OAuthCallbackError";
|
|
65
|
-
this.params = params;
|
|
66
|
-
this.state = state;
|
|
67
|
-
if (cause !== void 0) {
|
|
68
|
-
this.cause = cause;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
48
|
|
|
73
49
|
// src/import-jwk.ts
|
|
74
50
|
import { WebcryptoKey } from "@atproto/jwk-webcrypto";
|
|
@@ -106,43 +82,21 @@ function base64url(buffer) {
|
|
|
106
82
|
}
|
|
107
83
|
var HappyViewSession = class {
|
|
108
84
|
did;
|
|
85
|
+
scopes;
|
|
109
86
|
dpopKey;
|
|
110
87
|
accessToken;
|
|
111
88
|
clientKey;
|
|
112
89
|
instanceUrl;
|
|
113
|
-
_scopes;
|
|
114
|
-
_pdsUrl;
|
|
115
|
-
_issuer;
|
|
116
90
|
_fetch;
|
|
117
|
-
_onSignOut;
|
|
118
91
|
dpopNonce;
|
|
119
92
|
constructor(options) {
|
|
120
93
|
this.did = options.did;
|
|
94
|
+
this.scopes = options.scopes;
|
|
121
95
|
this.dpopKey = options.dpopKey;
|
|
122
96
|
this.accessToken = options.accessToken;
|
|
123
97
|
this.clientKey = options.clientKey;
|
|
124
98
|
this.instanceUrl = options.instanceUrl.replace(/\/+$/, "");
|
|
125
|
-
this._scopes = options.scopes;
|
|
126
|
-
this._pdsUrl = options.pdsUrl;
|
|
127
|
-
this._issuer = options.issuer;
|
|
128
99
|
this._fetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
129
|
-
this._onSignOut = options.onSignOut;
|
|
130
|
-
}
|
|
131
|
-
get sub() {
|
|
132
|
-
return this.did;
|
|
133
|
-
}
|
|
134
|
-
getTokenInfo() {
|
|
135
|
-
return {
|
|
136
|
-
scope: this._scopes,
|
|
137
|
-
iss: this._issuer,
|
|
138
|
-
aud: this._pdsUrl,
|
|
139
|
-
sub: this.did
|
|
140
|
-
};
|
|
141
|
-
}
|
|
142
|
-
async signOut() {
|
|
143
|
-
if (this._onSignOut) {
|
|
144
|
-
await this._onSignOut();
|
|
145
|
-
}
|
|
146
100
|
}
|
|
147
101
|
async fetchHandler(url, init) {
|
|
148
102
|
const fullUrl = /^https?:\/\//i.test(url) ? url : `${this.instanceUrl}${url}`;
|
|
@@ -221,37 +175,17 @@ var MemoryStorage = class {
|
|
|
221
175
|
var STORAGE_PREFIX = "happyview:session:";
|
|
222
176
|
var LAST_ACTIVE_KEY = "happyview:last-active-did";
|
|
223
177
|
var HappyViewOAuthClient = class {
|
|
224
|
-
static async fetchMetadata({
|
|
225
|
-
clientId,
|
|
226
|
-
fetch: fetchFn = globalThis.fetch,
|
|
227
|
-
signal
|
|
228
|
-
}) {
|
|
229
|
-
signal?.throwIfAborted();
|
|
230
|
-
const resp = await fetchFn(clientId, { redirect: "error", signal });
|
|
231
|
-
if (resp.status !== 200) {
|
|
232
|
-
resp.body?.cancel?.();
|
|
233
|
-
throw new Error(`Failed to fetch client metadata: ${resp.status}`);
|
|
234
|
-
}
|
|
235
|
-
const mime = resp.headers.get("content-type")?.split(";")[0].trim();
|
|
236
|
-
if (mime !== "application/json") {
|
|
237
|
-
resp.body?.cancel?.();
|
|
238
|
-
throw new Error(`Invalid client metadata content type: ${mime}`);
|
|
239
|
-
}
|
|
240
|
-
return resp.json();
|
|
241
|
-
}
|
|
242
178
|
instanceUrl;
|
|
243
179
|
clientKey;
|
|
244
180
|
storage;
|
|
245
181
|
clientSecret;
|
|
246
182
|
_fetch;
|
|
247
|
-
sessionHooks;
|
|
248
183
|
constructor(options) {
|
|
249
184
|
this.instanceUrl = options.instanceUrl.replace(/\/+$/, "");
|
|
250
185
|
this.clientKey = options.clientKey;
|
|
251
186
|
this.clientSecret = options.clientSecret;
|
|
252
187
|
this.storage = options.storage ?? new MemoryStorage();
|
|
253
188
|
this._fetch = options.fetch ?? ((input, init) => fetch(input, init));
|
|
254
|
-
this.sessionHooks = options.sessionHooks ?? {};
|
|
255
189
|
}
|
|
256
190
|
get isConfidential() {
|
|
257
191
|
return this.clientSecret !== void 0;
|
|
@@ -334,29 +268,22 @@ var HappyViewOAuthClient = class {
|
|
|
334
268
|
accessToken: params.accessToken,
|
|
335
269
|
clientKey: this.clientKey,
|
|
336
270
|
instanceUrl: this.instanceUrl,
|
|
337
|
-
scopes:
|
|
338
|
-
pdsUrl: params.pdsUrl,
|
|
339
|
-
issuer: params.issuer
|
|
271
|
+
scopes: data.scopes
|
|
340
272
|
};
|
|
341
273
|
await this.storage.set(
|
|
342
274
|
`${STORAGE_PREFIX}${data.did}`,
|
|
343
275
|
JSON.stringify(storedSession)
|
|
344
276
|
);
|
|
345
277
|
await this.storage.set(LAST_ACTIVE_KEY, data.did);
|
|
346
|
-
|
|
278
|
+
return new HappyViewSession({
|
|
347
279
|
did: data.did,
|
|
348
280
|
dpopKey,
|
|
349
281
|
accessToken: params.accessToken,
|
|
350
282
|
clientKey: this.clientKey,
|
|
351
283
|
instanceUrl: this.instanceUrl,
|
|
352
|
-
scopes:
|
|
353
|
-
|
|
354
|
-
issuer: params.issuer,
|
|
355
|
-
fetch: this._fetch,
|
|
356
|
-
onSignOut: () => this.deleteSession(data.did)
|
|
284
|
+
scopes: data.scopes,
|
|
285
|
+
fetch: this._fetch
|
|
357
286
|
});
|
|
358
|
-
this.sessionHooks.onSessionUpdate?.(data.did);
|
|
359
|
-
return session;
|
|
360
287
|
}
|
|
361
288
|
async deleteSession(did) {
|
|
362
289
|
const session = await this.restoreSession(did);
|
|
@@ -379,7 +306,6 @@ var HappyViewOAuthClient = class {
|
|
|
379
306
|
if (lastActive === did) {
|
|
380
307
|
await this.storage.delete(LAST_ACTIVE_KEY);
|
|
381
308
|
}
|
|
382
|
-
this.sessionHooks.onSessionDelete?.(did);
|
|
383
309
|
}
|
|
384
310
|
async restoreSession(did) {
|
|
385
311
|
const stored = await this.storage.get(`${STORAGE_PREFIX}${did}`);
|
|
@@ -392,13 +318,29 @@ var HappyViewOAuthClient = class {
|
|
|
392
318
|
accessToken: data.accessToken,
|
|
393
319
|
clientKey: data.clientKey,
|
|
394
320
|
instanceUrl: data.instanceUrl,
|
|
395
|
-
scopes: data.scopes,
|
|
396
|
-
|
|
397
|
-
issuer: data.issuer,
|
|
398
|
-
fetch: this._fetch,
|
|
399
|
-
onSignOut: () => this.deleteSession(data.did)
|
|
321
|
+
scopes: data.scopes ?? [],
|
|
322
|
+
fetch: this._fetch
|
|
400
323
|
});
|
|
401
324
|
}
|
|
325
|
+
async getSession(did) {
|
|
326
|
+
const session = await this.restoreSession(did);
|
|
327
|
+
if (!session) {
|
|
328
|
+
throw new ApiError("No stored session for this DID", 404, {});
|
|
329
|
+
}
|
|
330
|
+
const resp = await session.fetchHandler(
|
|
331
|
+
`${this.instanceUrl}/oauth/sessions/${did}`,
|
|
332
|
+
{ method: "GET" }
|
|
333
|
+
);
|
|
334
|
+
if (!resp.ok) {
|
|
335
|
+
const body = await resp.json().catch(() => ({}));
|
|
336
|
+
throw new ApiError(
|
|
337
|
+
`Failed to get session: ${resp.status} ${body.message ?? resp.statusText}`,
|
|
338
|
+
resp.status,
|
|
339
|
+
body
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
return resp.json();
|
|
343
|
+
}
|
|
402
344
|
async restore() {
|
|
403
345
|
const did = await this.storage.get(LAST_ACTIVE_KEY);
|
|
404
346
|
if (!did) return null;
|
|
@@ -434,7 +376,6 @@ export {
|
|
|
434
376
|
InvalidStateError,
|
|
435
377
|
LAST_ACTIVE_KEY,
|
|
436
378
|
MemoryStorage,
|
|
437
|
-
OAuthCallbackError,
|
|
438
379
|
ResolutionError,
|
|
439
380
|
TokenExchangeError,
|
|
440
381
|
importJwk
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/import-jwk.ts","../src/session.ts","../src/storage.ts","../src/client.ts"],"sourcesContent":["export * from \"@atproto/jwk\";\nexport * from \"@atproto/jwk-webcrypto\";\n\nexport { HappyViewOAuthClient, LAST_ACTIVE_KEY } from \"./client\";\nexport type { FetchMetadataOptions } from \"./client\";\nexport { importJwk } from \"./import-jwk\";\nexport {\n ApiError,\n AuthenticationError,\n HappyViewError,\n InvalidStateError,\n OAuthCallbackError,\n ResolutionError,\n TokenExchangeError,\n} from \"./errors\";\nexport { HappyViewSession } from \"./session\";\nexport type { HappyViewSessionOptions } from \"./session\";\nexport { MemoryStorage } from \"./storage\";\nexport type {\n DpopProvision,\n HappyViewOAuthClientOptions,\n ProvisionKeyResponse,\n RegisterSessionParams,\n RegisterSessionResponse,\n SessionEventHooks,\n StorageAdapter,\n StoredSession,\n TokenInfo,\n} from \"./types\";\n","export class HappyViewError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"HappyViewError\";\n }\n}\n\nexport class ApiError extends HappyViewError {\n readonly status: number;\n readonly body: unknown;\n\n constructor(message: string, status: number, body?: unknown) {\n super(message);\n this.name = \"ApiError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport class AuthenticationError extends HappyViewError {\n readonly status: number;\n\n constructor(message: string, status: number = 401) {\n super(message);\n this.name = \"AuthenticationError\";\n this.status = status;\n }\n}\n\nexport class InvalidStateError extends HappyViewError {\n constructor(message: string) {\n super(message);\n this.name = \"InvalidStateError\";\n }\n}\n\nexport class TokenExchangeError extends HappyViewError {\n readonly status: number;\n readonly body: string;\n\n constructor(message: string, status: number, body: string) {\n super(message);\n this.name = \"TokenExchangeError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport class ResolutionError extends HappyViewError {\n constructor(message: string) {\n super(message);\n this.name = \"ResolutionError\";\n }\n}\n\nexport class OAuthCallbackError extends HappyViewError {\n readonly params: URLSearchParams;\n readonly state: string | undefined;\n\n static from(\n err: unknown,\n params: URLSearchParams,\n state?: string,\n ): OAuthCallbackError {\n if (err instanceof OAuthCallbackError) return err;\n const message = err instanceof Error ? err.message : undefined;\n return new OAuthCallbackError(params, message, state, err);\n }\n\n constructor(\n params: URLSearchParams,\n message?: string,\n state?: string,\n cause?: unknown,\n ) {\n super(\n message ??\n params.get(\"error_description\") ??\n \"OAuth callback error\",\n );\n this.name = \"OAuthCallbackError\";\n this.params = params;\n this.state = state;\n if (cause !== undefined) {\n this.cause = cause;\n }\n }\n}\n","import { WebcryptoKey } from \"@atproto/jwk-webcrypto\";\n\n/**\n * Import a raw ES256 JWK (as returned by HappyView's /oauth/dpop-keys)\n * into a WebcryptoKey that can sign JWTs.\n */\nexport async function importJwk(jwk: JsonWebKey): Promise<WebcryptoKey> {\n const privateKey = await crypto.subtle.importKey(\n \"jwk\",\n jwk,\n { name: \"ECDSA\", namedCurve: \"P-256\" },\n true,\n [\"sign\"],\n );\n\n // Derive public key by stripping the private component\n const { d: _, ...publicJwkFields } = jwk;\n const publicKey = await crypto.subtle.importKey(\n \"jwk\",\n publicJwkFields,\n { name: \"ECDSA\", namedCurve: \"P-256\" },\n true,\n [\"verify\"],\n );\n\n return WebcryptoKey.fromKeypair({ privateKey, publicKey });\n}\n","import type { Key } from \"@atproto/jwk\";\nimport type { TokenInfo } from \"./types\";\n\nexport interface HappyViewSessionOptions {\n did: string;\n dpopKey: Key;\n accessToken: string;\n clientKey: string;\n instanceUrl: string;\n scopes?: string;\n pdsUrl?: string;\n issuer?: string;\n fetch?: typeof globalThis.fetch;\n onSignOut?: () => Promise<void>;\n}\n\nfunction randomHex(byteLength: number): string {\n const bytes = crypto.getRandomValues(new Uint8Array(byteLength));\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nfunction base64url(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n\nexport class HappyViewSession {\n readonly did: string;\n\n private readonly dpopKey: Key;\n private readonly accessToken: string;\n private readonly clientKey: string;\n private readonly instanceUrl: string;\n private readonly _scopes: string | undefined;\n private readonly _pdsUrl: string | undefined;\n private readonly _issuer: string | undefined;\n private readonly _fetch: typeof globalThis.fetch;\n private readonly _onSignOut: (() => Promise<void>) | undefined;\n private dpopNonce: string | undefined;\n\n constructor(options: HappyViewSessionOptions) {\n this.did = options.did;\n this.dpopKey = options.dpopKey;\n this.accessToken = options.accessToken;\n this.clientKey = options.clientKey;\n this.instanceUrl = options.instanceUrl.replace(/\\/+$/, \"\");\n this._scopes = options.scopes;\n this._pdsUrl = options.pdsUrl;\n this._issuer = options.issuer;\n this._fetch = options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init)) as typeof globalThis.fetch;\n this._onSignOut = options.onSignOut;\n }\n\n get sub(): string {\n return this.did;\n }\n\n getTokenInfo(): TokenInfo {\n return {\n scope: this._scopes,\n iss: this._issuer,\n aud: this._pdsUrl,\n sub: this.did,\n };\n }\n\n async signOut(): Promise<void> {\n if (this._onSignOut) {\n await this._onSignOut();\n }\n }\n\n async fetchHandler(url: string, init: RequestInit): Promise<Response> {\n const fullUrl = /^https?:\\/\\//i.test(url)\n ? url\n : `${this.instanceUrl}${url}`;\n const method = (init.method ?? \"GET\").toUpperCase();\n const htu = fullUrl.split(\"?\")[0];\n\n const sendWithProof = async (): Promise<Response> => {\n const tokenHash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(this.accessToken),\n );\n const ath = base64url(tokenHash);\n\n const proof = await this.dpopKey.createJwt(\n {\n alg: \"ES256\",\n typ: \"dpop+jwt\",\n jwk: this.dpopKey.publicJwk!,\n },\n {\n htm: method,\n htu,\n iat: Math.floor(Date.now() / 1000),\n jti: randomHex(16),\n ath,\n nonce: this.dpopNonce,\n },\n );\n\n const headers = new Headers(init.headers);\n headers.set(\"authorization\", `DPoP ${this.accessToken}`);\n headers.set(\"dpop\", proof);\n headers.set(\"x-client-key\", this.clientKey);\n\n return this._fetch(fullUrl, { ...init, headers });\n };\n\n let response: Response;\n try {\n response = await sendWithProof();\n } catch (e) {\n const info = `_fetch type: ${typeof this._fetch}, toString: ${String(this._fetch).slice(0, 120)}, url: ${fullUrl}`;\n throw new Error(`fetchHandler failed: ${(e as Error).message}\\n\\nDEBUG: ${info}`);\n }\n\n // Retry once if the server requires a DPoP nonce we didn't have\n const nonce = response.headers.get(\"dpop-nonce\");\n if (nonce && nonce !== this.dpopNonce && (response.status === 401 || response.status === 400)) {\n this.dpopNonce = nonce;\n try {\n response = await sendWithProof();\n } catch (e) {\n throw new Error(`fetchHandler retry failed: ${(e as Error).message}`);\n }\n const retryNonce = response.headers.get(\"dpop-nonce\");\n if (retryNonce) {\n this.dpopNonce = retryNonce;\n }\n } else if (nonce) {\n this.dpopNonce = nonce;\n }\n\n return response;\n }\n}\n","import type { StorageAdapter } from \"./types\";\n\nexport class MemoryStorage implements StorageAdapter {\n private data = new Map<string, string>();\n\n async get(key: string): Promise<string | null> {\n return this.data.get(key) ?? null;\n }\n\n async set(key: string, value: string): Promise<void> {\n this.data.set(key, value);\n }\n\n async delete(key: string): Promise<void> {\n this.data.delete(key);\n }\n}\n","import type { Key } from \"@atproto/jwk\";\nimport { ApiError } from \"./errors\";\nimport { importJwk } from \"./import-jwk\";\nimport { HappyViewSession } from \"./session\";\nimport { MemoryStorage } from \"./storage\";\nimport type {\n HappyViewOAuthClientOptions,\n ProvisionKeyResponse,\n RegisterSessionParams,\n RegisterSessionResponse,\n SessionEventHooks,\n StorageAdapter,\n StoredSession,\n} from \"./types\";\n\nconst STORAGE_PREFIX = \"happyview:session:\";\nexport const LAST_ACTIVE_KEY = \"happyview:last-active-did\";\n\nexport interface FetchMetadataOptions {\n clientId: string;\n fetch?: typeof globalThis.fetch;\n signal?: AbortSignal;\n}\n\nexport class HappyViewOAuthClient {\n static async fetchMetadata({\n clientId,\n fetch: fetchFn = globalThis.fetch,\n signal,\n }: FetchMetadataOptions): Promise<Record<string, unknown>> {\n signal?.throwIfAborted();\n const resp = await fetchFn(clientId, { redirect: \"error\", signal });\n if (resp.status !== 200) {\n resp.body?.cancel?.();\n throw new Error(`Failed to fetch client metadata: ${resp.status}`);\n }\n const mime = resp.headers.get(\"content-type\")?.split(\";\")[0].trim();\n if (mime !== \"application/json\") {\n resp.body?.cancel?.();\n throw new Error(`Invalid client metadata content type: ${mime}`);\n }\n return resp.json() as Promise<Record<string, unknown>>;\n }\n\n protected readonly instanceUrl: string;\n protected readonly clientKey: string;\n protected readonly storage: StorageAdapter;\n private readonly clientSecret: string | undefined;\n protected readonly _fetch: typeof globalThis.fetch;\n private readonly sessionHooks: SessionEventHooks;\n\n constructor(\n options: HappyViewOAuthClientOptions & {\n fetch?: typeof globalThis.fetch;\n },\n ) {\n this.instanceUrl = options.instanceUrl.replace(/\\/+$/, \"\");\n this.clientKey = options.clientKey;\n this.clientSecret = options.clientSecret;\n this.storage = options.storage ?? new MemoryStorage();\n this._fetch = options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init)) as typeof globalThis.fetch;\n this.sessionHooks = options.sessionHooks ?? {};\n }\n\n get isConfidential(): boolean {\n return this.clientSecret !== undefined;\n }\n\n async provisionDpopKey(): Promise<{\n provisionId: string;\n dpopKey: Key;\n rawJwk: JsonWebKey;\n pkceVerifier?: string;\n }> {\n const headers: Record<string, string> = {\n \"content-type\": \"application/json\",\n \"x-client-key\": this.clientKey,\n };\n if (this.clientSecret) {\n headers[\"x-client-secret\"] = this.clientSecret;\n }\n\n const body: Record<string, unknown> = {};\n let pkceVerifier: string | undefined;\n\n if (!this.isConfidential) {\n pkceVerifier = generatePkceVerifier();\n const challenge = await computePkceChallenge(pkceVerifier);\n body.pkce_challenge = challenge;\n }\n\n const resp = await this._fetch(`${this.instanceUrl}/oauth/dpop-keys`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n });\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to provision DPoP key: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n const data: ProvisionKeyResponse = await resp.json();\n const dpopKey = await importJwk(data.dpop_key);\n return {\n provisionId: data.provision_id,\n dpopKey,\n rawJwk: data.dpop_key,\n pkceVerifier,\n };\n }\n\n async registerSession(\n params: RegisterSessionParams,\n ): Promise<HappyViewSession> {\n const headers: Record<string, string> = {\n \"content-type\": \"application/json\",\n \"x-client-key\": this.clientKey,\n };\n if (this.clientSecret) {\n headers[\"x-client-secret\"] = this.clientSecret;\n }\n\n const body: Record<string, unknown> = {\n provision_id: params.provisionId,\n did: params.did,\n access_token: params.accessToken,\n refresh_token: params.refreshToken,\n scopes: params.scopes,\n pds_url: params.pdsUrl,\n issuer: params.issuer,\n };\n\n if (!this.isConfidential && params.pkceVerifier) {\n body.pkce_verifier = params.pkceVerifier;\n }\n\n const resp = await this._fetch(`${this.instanceUrl}/oauth/sessions`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n });\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to register session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n const data: RegisterSessionResponse = await resp.json();\n const dpopKey = await importJwk(params.dpopKey);\n\n const storedSession: StoredSession = {\n did: data.did,\n dpopKey: params.dpopKey,\n accessToken: params.accessToken,\n clientKey: this.clientKey,\n instanceUrl: this.instanceUrl,\n scopes: params.scopes,\n pdsUrl: params.pdsUrl,\n issuer: params.issuer,\n };\n await this.storage.set(\n `${STORAGE_PREFIX}${data.did}`,\n JSON.stringify(storedSession),\n );\n await this.storage.set(LAST_ACTIVE_KEY, data.did);\n\n const session = new HappyViewSession({\n did: data.did,\n dpopKey,\n accessToken: params.accessToken,\n clientKey: this.clientKey,\n instanceUrl: this.instanceUrl,\n scopes: params.scopes,\n pdsUrl: params.pdsUrl,\n issuer: params.issuer,\n fetch: this._fetch,\n onSignOut: () => this.deleteSession(data.did),\n });\n\n this.sessionHooks.onSessionUpdate?.(data.did);\n\n return session;\n }\n\n async deleteSession(did: string): Promise<void> {\n const session = await this.restoreSession(did);\n if (session) {\n const resp = await session.fetchHandler(\n `${this.instanceUrl}/oauth/sessions/${did}`,\n { method: \"DELETE\" },\n );\n\n if (!resp.ok && resp.status !== 404) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to delete session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n }\n\n await this.storage.delete(`${STORAGE_PREFIX}${did}`);\n\n const lastActive = await this.storage.get(LAST_ACTIVE_KEY);\n if (lastActive === did) {\n await this.storage.delete(LAST_ACTIVE_KEY);\n }\n\n this.sessionHooks.onSessionDelete?.(did);\n }\n\n async restoreSession(did: string): Promise<HappyViewSession | null> {\n const stored = await this.storage.get(`${STORAGE_PREFIX}${did}`);\n if (!stored) return null;\n\n const data: StoredSession = JSON.parse(stored);\n const dpopKey = await importJwk(data.dpopKey);\n return new HappyViewSession({\n did: data.did,\n dpopKey,\n accessToken: data.accessToken,\n clientKey: data.clientKey,\n instanceUrl: data.instanceUrl,\n scopes: data.scopes,\n pdsUrl: data.pdsUrl,\n issuer: data.issuer,\n fetch: this._fetch,\n onSignOut: () => this.deleteSession(data.did),\n });\n }\n\n async restore(): Promise<HappyViewSession | null> {\n const did = await this.storage.get(LAST_ACTIVE_KEY);\n if (!did) return null;\n return this.restoreSession(did);\n }\n}\n\n// PKCE helpers — inline since they're just a few lines of Web Crypto\nfunction generatePkceVerifier(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(32));\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n\nasync function computePkceChallenge(verifier: string): Promise<string> {\n const hash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(verifier),\n );\n const bytes = new Uint8Array(hash);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(b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,KAAK;AAAA,MACZ,WAAW,MAAM,KAAK,cAAc,KAAK,GAAG;AAAA,IAC9C,CAAC;AAAA,EACH;AAAA,EAEA,MAAM,UAA4C;AAChD,UAAM,MAAM,MAAM,KAAK,QAAQ,IAAI,eAAe;AAClD,QAAI,CAAC,IAAK,QAAO;AACjB,WAAO,KAAK,eAAe,GAAG;AAAA,EAChC;AACF;AAGA,SAAS,uBAA+B;AACtC,QAAM,QAAQ,OAAO,gBAAgB,IAAI,WAAW,EAAE,CAAC;AACvD,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAU,OAAO,aAAa,MAAM,CAAC,CAAC;AAAA,EACxC;AACA,SAAO,KAAK,MAAM,EACf,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,EAAE;AACtB;AAEA,eAAe,qBAAqB,UAAmC;AACrE,QAAM,OAAO,MAAM,OAAO,OAAO;AAAA,IAC/B;AAAA,IACA,IAAI,YAAY,EAAE,OAAO,QAAQ;AAAA,EACnC;AACA,QAAM,QAAQ,IAAI,WAAW,IAAI;AACjC,MAAI,SAAS;AACb,WAAS,IAAI,GAAG,IAAI,MAAM,QAAQ,KAAK;AACrC,cAAU,OAAO,aAAa,MAAM,CAAC,CAAC;AAAA,EACxC;AACA,SAAO,KAAK,MAAM,EACf,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,GAAG,EAClB,QAAQ,OAAO,EAAE;AACtB;","names":["body"]}
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts","../src/import-jwk.ts","../src/session.ts","../src/storage.ts","../src/client.ts"],"sourcesContent":["export class HappyViewError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"HappyViewError\";\n }\n}\n\nexport class ApiError extends HappyViewError {\n readonly status: number;\n readonly body: unknown;\n\n constructor(message: string, status: number, body?: unknown) {\n super(message);\n this.name = \"ApiError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport class AuthenticationError extends HappyViewError {\n readonly status: number;\n\n constructor(message: string, status: number = 401) {\n super(message);\n this.name = \"AuthenticationError\";\n this.status = status;\n }\n}\n\nexport class InvalidStateError extends HappyViewError {\n constructor(message: string) {\n super(message);\n this.name = \"InvalidStateError\";\n }\n}\n\nexport class TokenExchangeError extends HappyViewError {\n readonly status: number;\n readonly body: string;\n\n constructor(message: string, status: number, body: string) {\n super(message);\n this.name = \"TokenExchangeError\";\n this.status = status;\n this.body = body;\n }\n}\n\nexport class ResolutionError extends HappyViewError {\n constructor(message: string) {\n super(message);\n this.name = \"ResolutionError\";\n }\n}\n","import { WebcryptoKey } from \"@atproto/jwk-webcrypto\";\n\n/**\n * Import a raw ES256 JWK (as returned by HappyView's /oauth/dpop-keys)\n * into a WebcryptoKey that can sign JWTs.\n */\nexport async function importJwk(jwk: JsonWebKey): Promise<WebcryptoKey> {\n const privateKey = await crypto.subtle.importKey(\n \"jwk\",\n jwk,\n { name: \"ECDSA\", namedCurve: \"P-256\" },\n true,\n [\"sign\"],\n );\n\n // Derive public key by stripping the private component\n const { d: _, ...publicJwkFields } = jwk;\n const publicKey = await crypto.subtle.importKey(\n \"jwk\",\n publicJwkFields,\n { name: \"ECDSA\", namedCurve: \"P-256\" },\n true,\n [\"verify\"],\n );\n\n return WebcryptoKey.fromKeypair({ privateKey, publicKey });\n}\n","import type { Key } from \"@atproto/jwk\";\n\nexport interface HappyViewSessionOptions {\n did: string;\n dpopKey: Key;\n accessToken: string;\n clientKey: string;\n instanceUrl: string;\n scopes: string[];\n fetch?: typeof globalThis.fetch;\n}\n\nfunction randomHex(byteLength: number): string {\n const bytes = crypto.getRandomValues(new Uint8Array(byteLength));\n return Array.from(bytes, (b) => b.toString(16).padStart(2, \"0\")).join(\"\");\n}\n\nfunction base64url(buffer: ArrayBuffer): string {\n const bytes = new Uint8Array(buffer);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n\nexport class HappyViewSession {\n readonly did: string;\n readonly scopes: string[];\n\n private readonly dpopKey: Key;\n private readonly accessToken: string;\n private readonly clientKey: string;\n private readonly instanceUrl: string;\n private readonly _fetch: typeof globalThis.fetch;\n private dpopNonce: string | undefined;\n\n constructor(options: HappyViewSessionOptions) {\n this.did = options.did;\n this.scopes = options.scopes;\n this.dpopKey = options.dpopKey;\n this.accessToken = options.accessToken;\n this.clientKey = options.clientKey;\n this.instanceUrl = options.instanceUrl.replace(/\\/+$/, \"\");\n this._fetch = options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init)) as typeof globalThis.fetch;\n }\n\n async fetchHandler(url: string, init: RequestInit): Promise<Response> {\n const fullUrl = /^https?:\\/\\//i.test(url)\n ? url\n : `${this.instanceUrl}${url}`;\n const method = (init.method ?? \"GET\").toUpperCase();\n const htu = fullUrl.split(\"?\")[0];\n\n const sendWithProof = async (): Promise<Response> => {\n const tokenHash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(this.accessToken),\n );\n const ath = base64url(tokenHash);\n\n const proof = await this.dpopKey.createJwt(\n {\n alg: \"ES256\",\n typ: \"dpop+jwt\",\n jwk: this.dpopKey.publicJwk!,\n },\n {\n htm: method,\n htu,\n iat: Math.floor(Date.now() / 1000),\n jti: randomHex(16),\n ath,\n nonce: this.dpopNonce,\n },\n );\n\n const headers = new Headers(init.headers);\n headers.set(\"authorization\", `DPoP ${this.accessToken}`);\n headers.set(\"dpop\", proof);\n headers.set(\"x-client-key\", this.clientKey);\n\n return this._fetch(fullUrl, { ...init, headers });\n };\n\n let response: Response;\n try {\n response = await sendWithProof();\n } catch (e) {\n const info = `_fetch type: ${typeof this._fetch}, toString: ${String(this._fetch).slice(0, 120)}, url: ${fullUrl}`;\n throw new Error(`fetchHandler failed: ${(e as Error).message}\\n\\nDEBUG: ${info}`);\n }\n\n // Retry once if the server requires a DPoP nonce we didn't have\n const nonce = response.headers.get(\"dpop-nonce\");\n if (nonce && nonce !== this.dpopNonce && (response.status === 401 || response.status === 400)) {\n this.dpopNonce = nonce;\n try {\n response = await sendWithProof();\n } catch (e) {\n throw new Error(`fetchHandler retry failed: ${(e as Error).message}`);\n }\n const retryNonce = response.headers.get(\"dpop-nonce\");\n if (retryNonce) {\n this.dpopNonce = retryNonce;\n }\n } else if (nonce) {\n this.dpopNonce = nonce;\n }\n\n return response;\n }\n}\n","import type { StorageAdapter } from \"./types\";\n\nexport class MemoryStorage implements StorageAdapter {\n private data = new Map<string, string>();\n\n async get(key: string): Promise<string | null> {\n return this.data.get(key) ?? null;\n }\n\n async set(key: string, value: string): Promise<void> {\n this.data.set(key, value);\n }\n\n async delete(key: string): Promise<void> {\n this.data.delete(key);\n }\n}\n","import type { Key } from \"@atproto/jwk\";\nimport { ApiError } from \"./errors\";\nimport { importJwk } from \"./import-jwk\";\nimport { HappyViewSession } from \"./session\";\nimport { MemoryStorage } from \"./storage\";\nimport type {\n GetSessionResponse,\n HappyViewOAuthClientOptions,\n ProvisionKeyResponse,\n RegisterSessionParams,\n RegisterSessionResponse,\n StorageAdapter,\n StoredSession,\n} from \"./types\";\n\nconst STORAGE_PREFIX = \"happyview:session:\";\nexport const LAST_ACTIVE_KEY = \"happyview:last-active-did\";\n\nexport class HappyViewOAuthClient {\n protected readonly instanceUrl: string;\n protected readonly clientKey: string;\n protected readonly storage: StorageAdapter;\n private readonly clientSecret: string | undefined;\n protected readonly _fetch: typeof globalThis.fetch;\n\n constructor(\n options: HappyViewOAuthClientOptions & {\n fetch?: typeof globalThis.fetch;\n },\n ) {\n this.instanceUrl = options.instanceUrl.replace(/\\/+$/, \"\");\n this.clientKey = options.clientKey;\n this.clientSecret = options.clientSecret;\n this.storage = options.storage ?? new MemoryStorage();\n this._fetch = options.fetch ?? ((input: RequestInfo | URL, init?: RequestInit) => fetch(input, init)) as typeof globalThis.fetch;\n }\n\n get isConfidential(): boolean {\n return this.clientSecret !== undefined;\n }\n\n async provisionDpopKey(): Promise<{\n provisionId: string;\n dpopKey: Key;\n rawJwk: JsonWebKey;\n pkceVerifier?: string;\n }> {\n const headers: Record<string, string> = {\n \"content-type\": \"application/json\",\n \"x-client-key\": this.clientKey,\n };\n if (this.clientSecret) {\n headers[\"x-client-secret\"] = this.clientSecret;\n }\n\n const body: Record<string, unknown> = {};\n let pkceVerifier: string | undefined;\n\n if (!this.isConfidential) {\n pkceVerifier = generatePkceVerifier();\n const challenge = await computePkceChallenge(pkceVerifier);\n body.pkce_challenge = challenge;\n }\n\n const resp = await this._fetch(`${this.instanceUrl}/oauth/dpop-keys`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n });\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to provision DPoP key: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n const data: ProvisionKeyResponse = await resp.json();\n const dpopKey = await importJwk(data.dpop_key);\n return {\n provisionId: data.provision_id,\n dpopKey,\n rawJwk: data.dpop_key,\n pkceVerifier,\n };\n }\n\n async registerSession(\n params: RegisterSessionParams,\n ): Promise<HappyViewSession> {\n const headers: Record<string, string> = {\n \"content-type\": \"application/json\",\n \"x-client-key\": this.clientKey,\n };\n if (this.clientSecret) {\n headers[\"x-client-secret\"] = this.clientSecret;\n }\n\n const body: Record<string, unknown> = {\n provision_id: params.provisionId,\n did: params.did,\n access_token: params.accessToken,\n refresh_token: params.refreshToken,\n scopes: params.scopes,\n pds_url: params.pdsUrl,\n issuer: params.issuer,\n };\n\n if (!this.isConfidential && params.pkceVerifier) {\n body.pkce_verifier = params.pkceVerifier;\n }\n\n const resp = await this._fetch(`${this.instanceUrl}/oauth/sessions`, {\n method: \"POST\",\n headers,\n body: JSON.stringify(body),\n });\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to register session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n const data: RegisterSessionResponse = await resp.json();\n const dpopKey = await importJwk(params.dpopKey);\n\n const storedSession: StoredSession = {\n did: data.did,\n dpopKey: params.dpopKey,\n accessToken: params.accessToken,\n clientKey: this.clientKey,\n instanceUrl: this.instanceUrl,\n scopes: data.scopes,\n };\n await this.storage.set(\n `${STORAGE_PREFIX}${data.did}`,\n JSON.stringify(storedSession),\n );\n await this.storage.set(LAST_ACTIVE_KEY, data.did);\n\n return new HappyViewSession({\n did: data.did,\n dpopKey,\n accessToken: params.accessToken,\n clientKey: this.clientKey,\n instanceUrl: this.instanceUrl,\n scopes: data.scopes,\n fetch: this._fetch,\n });\n }\n\n async deleteSession(did: string): Promise<void> {\n const session = await this.restoreSession(did);\n if (session) {\n const resp = await session.fetchHandler(\n `${this.instanceUrl}/oauth/sessions/${did}`,\n { method: \"DELETE\" },\n );\n\n if (!resp.ok && resp.status !== 404) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to delete session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n }\n\n await this.storage.delete(`${STORAGE_PREFIX}${did}`);\n\n const lastActive = await this.storage.get(LAST_ACTIVE_KEY);\n if (lastActive === did) {\n await this.storage.delete(LAST_ACTIVE_KEY);\n }\n }\n\n async restoreSession(did: string): Promise<HappyViewSession | null> {\n const stored = await this.storage.get(`${STORAGE_PREFIX}${did}`);\n if (!stored) return null;\n\n const data: StoredSession = JSON.parse(stored);\n const dpopKey = await importJwk(data.dpopKey);\n return new HappyViewSession({\n did: data.did,\n dpopKey,\n accessToken: data.accessToken,\n clientKey: data.clientKey,\n instanceUrl: data.instanceUrl,\n scopes: data.scopes ?? [],\n fetch: this._fetch,\n });\n }\n\n async getSession(did: string): Promise<GetSessionResponse> {\n const session = await this.restoreSession(did);\n if (!session) {\n throw new ApiError(\"No stored session for this DID\", 404, {});\n }\n\n const resp = await session.fetchHandler(\n `${this.instanceUrl}/oauth/sessions/${did}`,\n { method: \"GET\" },\n );\n\n if (!resp.ok) {\n const body = await resp.json().catch(() => ({}));\n throw new ApiError(\n `Failed to get session: ${resp.status} ${(body as any).message ?? resp.statusText}`,\n resp.status,\n body,\n );\n }\n\n return resp.json();\n }\n\n async restore(): Promise<HappyViewSession | null> {\n const did = await this.storage.get(LAST_ACTIVE_KEY);\n if (!did) return null;\n return this.restoreSession(did);\n }\n}\n\n// PKCE helpers — inline since they're just a few lines of Web Crypto\nfunction generatePkceVerifier(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(32));\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n\nasync function computePkceChallenge(verifier: string): Promise<string> {\n const hash = await crypto.subtle.digest(\n \"SHA-256\",\n new TextEncoder().encode(verifier),\n );\n const bytes = new Uint8Array(hash);\n let binary = \"\";\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i]);\n }\n return btoa(binary)\n .replace(/\\+/g, \"-\")\n .replace(/\\//g, \"_\")\n .replace(/=+$/, \"\");\n}\n"],"mappings":";AAAO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EACxC,YAAY,SAAiB;AAC3B,UAAM,OAAO;AACb,SAAK,OAAO;AAAA,EACd;AACF;AAEO,IAAM,WAAN,cAAuB,eAAe;AAAA,EAClC;AAAA,EACA;AAAA,EAET,YAAY,SAAiB,QAAgB,MAAgB;AAC3D,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AACd,SAAK,OAAO;AAAA,EACd;AACF;AAEO,IAAM,sBAAN,cAAkC,eAAe;AAAA,EAC7C;AAAA,EAET,YAAY,SAAiB,SAAiB,KAAK;AACjD,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAChB;AACF;AAEO,IAAM,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