@bskyprism/atproto-oauth-client-cloudflare-workers 0.2.2

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.
Files changed (131) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +69 -0
  3. package/lib/did-cache-kv.d.ts +18 -0
  4. package/lib/did-cache-kv.js +26 -0
  5. package/lib/did-resolver/did-cache-memory.d.ts +7 -0
  6. package/lib/did-resolver/did-cache-memory.js +10 -0
  7. package/lib/did-resolver/did-cache.d.ts +14 -0
  8. package/lib/did-resolver/did-cache.js +10 -0
  9. package/lib/did-resolver/did-method.d.ts +11 -0
  10. package/lib/did-resolver/did-method.js +1 -0
  11. package/lib/did-resolver/did-resolver-base.d.ts +9 -0
  12. package/lib/did-resolver/did-resolver-base.js +36 -0
  13. package/lib/did-resolver/did-resolver-common.d.ts +8 -0
  14. package/lib/did-resolver/did-resolver-common.js +11 -0
  15. package/lib/did-resolver/did-resolver.d.ts +6 -0
  16. package/lib/did-resolver/did-resolver.js +1 -0
  17. package/lib/did-resolver/index.d.ts +6 -0
  18. package/lib/did-resolver/index.js +7 -0
  19. package/lib/did-resolver/methods/plc.d.ts +43 -0
  20. package/lib/did-resolver/methods/plc.js +22 -0
  21. package/lib/did-resolver/methods/web.d.ts +43 -0
  22. package/lib/did-resolver/methods/web.js +42 -0
  23. package/lib/did-resolver/methods.d.ts +2 -0
  24. package/lib/did-resolver/methods.js +2 -0
  25. package/lib/did-resolver/util.d.ts +3 -0
  26. package/lib/did-resolver/util.js +1 -0
  27. package/lib/dpop-store.d.ts +21 -0
  28. package/lib/dpop-store.js +25 -0
  29. package/lib/handle-cache-kv.d.ts +17 -0
  30. package/lib/handle-cache-kv.js +31 -0
  31. package/lib/handle-resolver/atproto-doh-handle-resolver.d.ts +8 -0
  32. package/lib/handle-resolver/atproto-doh-handle-resolver.js +94 -0
  33. package/lib/handle-resolver/atproto-handle-resolver.d.ts +21 -0
  34. package/lib/handle-resolver/atproto-handle-resolver.js +46 -0
  35. package/lib/handle-resolver/cached-handle-resolver.d.ts +12 -0
  36. package/lib/handle-resolver/cached-handle-resolver.js +17 -0
  37. package/lib/handle-resolver/handle-resolver-error.d.ts +3 -0
  38. package/lib/handle-resolver/handle-resolver-error.js +6 -0
  39. package/lib/handle-resolver/index.d.ts +6 -0
  40. package/lib/handle-resolver/index.js +8 -0
  41. package/lib/handle-resolver/internal-resolvers/dns-handle-resolver.d.ts +11 -0
  42. package/lib/handle-resolver/internal-resolvers/dns-handle-resolver.js +28 -0
  43. package/lib/handle-resolver/internal-resolvers/well-known-handler-resolver.d.ts +17 -0
  44. package/lib/handle-resolver/internal-resolvers/well-known-handler-resolver.js +28 -0
  45. package/lib/handle-resolver/types.d.ts +25 -0
  46. package/lib/handle-resolver/types.js +10 -0
  47. package/lib/handle-resolver/xrpc-handle-resolver.d.ts +31 -0
  48. package/lib/handle-resolver/xrpc-handle-resolver.js +45 -0
  49. package/lib/handle-resolver.d.ts +20 -0
  50. package/lib/handle-resolver.js +19 -0
  51. package/lib/identity-resolver/atproto-identity-resolver.d.ts +20 -0
  52. package/lib/identity-resolver/atproto-identity-resolver.js +72 -0
  53. package/lib/identity-resolver/constants.d.ts +1 -0
  54. package/lib/identity-resolver/constants.js +1 -0
  55. package/lib/identity-resolver/identity-resolver-error.d.ts +3 -0
  56. package/lib/identity-resolver/identity-resolver-error.js +6 -0
  57. package/lib/identity-resolver/identity-resolver.d.ts +19 -0
  58. package/lib/identity-resolver/identity-resolver.js +1 -0
  59. package/lib/identity-resolver/index.d.ts +5 -0
  60. package/lib/identity-resolver/index.js +5 -0
  61. package/lib/identity-resolver/util.d.ts +12 -0
  62. package/lib/identity-resolver/util.js +35 -0
  63. package/lib/index.d.ts +7 -0
  64. package/lib/index.js +6 -0
  65. package/lib/oauth-client/atproto-token-response.d.ts +100 -0
  66. package/lib/oauth-client/atproto-token-response.js +15 -0
  67. package/lib/oauth-client/constants.d.ts +4 -0
  68. package/lib/oauth-client/constants.js +4 -0
  69. package/lib/oauth-client/errors/auth-method-unsatisfiable-error.d.ts +2 -0
  70. package/lib/oauth-client/errors/auth-method-unsatisfiable-error.js +2 -0
  71. package/lib/oauth-client/errors/token-invalid-error.d.ts +6 -0
  72. package/lib/oauth-client/errors/token-invalid-error.js +6 -0
  73. package/lib/oauth-client/errors/token-refresh-error.d.ts +6 -0
  74. package/lib/oauth-client/errors/token-refresh-error.js +6 -0
  75. package/lib/oauth-client/errors/token-revoked-error.d.ts +6 -0
  76. package/lib/oauth-client/errors/token-revoked-error.js +6 -0
  77. package/lib/oauth-client/fetch-dpop.d.ts +19 -0
  78. package/lib/oauth-client/fetch-dpop.js +176 -0
  79. package/lib/oauth-client/identity-resolver.d.ts +15 -0
  80. package/lib/oauth-client/identity-resolver.js +33 -0
  81. package/lib/oauth-client/index.d.ts +17 -0
  82. package/lib/oauth-client/index.js +17 -0
  83. package/lib/oauth-client/lock.d.ts +2 -0
  84. package/lib/oauth-client/lock.js +28 -0
  85. package/lib/oauth-client/oauth-authorization-server-metadata-resolver.d.ts +18 -0
  86. package/lib/oauth-client/oauth-authorization-server-metadata-resolver.js +53 -0
  87. package/lib/oauth-client/oauth-callback-error.d.ts +6 -0
  88. package/lib/oauth-client/oauth-callback-error.js +13 -0
  89. package/lib/oauth-client/oauth-client-auth.d.ts +22 -0
  90. package/lib/oauth-client/oauth-client-auth.js +127 -0
  91. package/lib/oauth-client/oauth-client.d.ts +311 -0
  92. package/lib/oauth-client/oauth-client.js +276 -0
  93. package/lib/oauth-client/oauth-protected-resource-metadata-resolver.d.ts +18 -0
  94. package/lib/oauth-client/oauth-protected-resource-metadata-resolver.js +49 -0
  95. package/lib/oauth-client/oauth-resolver-error.d.ts +6 -0
  96. package/lib/oauth-client/oauth-resolver-error.js +18 -0
  97. package/lib/oauth-client/oauth-resolver.d.ts +71 -0
  98. package/lib/oauth-client/oauth-resolver.js +117 -0
  99. package/lib/oauth-client/oauth-response-error.d.ts +10 -0
  100. package/lib/oauth-client/oauth-response-error.js +22 -0
  101. package/lib/oauth-client/oauth-server-agent.d.ts +54 -0
  102. package/lib/oauth-client/oauth-server-agent.js +250 -0
  103. package/lib/oauth-client/oauth-server-factory.d.ts +32 -0
  104. package/lib/oauth-client/oauth-server-factory.js +37 -0
  105. package/lib/oauth-client/oauth-session.d.ts +33 -0
  106. package/lib/oauth-client/oauth-session.js +122 -0
  107. package/lib/oauth-client/runtime-implementation.d.ts +16 -0
  108. package/lib/oauth-client/runtime-implementation.js +1 -0
  109. package/lib/oauth-client/runtime.d.ts +25 -0
  110. package/lib/oauth-client/runtime.js +99 -0
  111. package/lib/oauth-client/session-getter.d.ts +54 -0
  112. package/lib/oauth-client/session-getter.js +260 -0
  113. package/lib/oauth-client/state-store.d.ts +12 -0
  114. package/lib/oauth-client/state-store.js +1 -0
  115. package/lib/oauth-client/types.d.ts +1365 -0
  116. package/lib/oauth-client/types.js +8 -0
  117. package/lib/oauth-client/util.d.ts +25 -0
  118. package/lib/oauth-client/util.js +139 -0
  119. package/lib/oauth-client/validate-client-metadata.d.ts +4 -0
  120. package/lib/oauth-client/validate-client-metadata.js +68 -0
  121. package/lib/oauth-client.d.ts +27 -0
  122. package/lib/oauth-client.js +30 -0
  123. package/lib/resolve-txt-factory.d.ts +3 -0
  124. package/lib/resolve-txt-factory.js +80 -0
  125. package/lib/session-store-kv.d.ts +9 -0
  126. package/lib/session-store-kv.js +20 -0
  127. package/lib/state-store-kv.d.ts +9 -0
  128. package/lib/state-store-kv.js +20 -0
  129. package/lib/util.d.ts +18 -0
  130. package/lib/util.js +5 -0
  131. package/package.json +58 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 nDimensional Studios
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,69 @@
1
+ # atproto-oauth-client-cloudflare-workers
2
+
3
+ This library contains local patched copies of
4
+
5
+ - [`@atproto/oauth-client-node`](https://github.com/bluesky-social/atproto/tree/main/packages/oauth/oauth-client-node)
6
+ - [`@atproto/oauth-client`](https://github.com/bluesky-social/atproto/tree/main/packages/oauth/oauth-client)
7
+ - [`@atproto-labs/handle-resolver-node`](https://github.com/bluesky-social/atproto/tree/main/packages/internal/handle-resolver-node)
8
+ - [`@atproto-labs/handle-resolver`](https://github.com/bluesky-social/atproto/tree/main/packages/internal/handle-resolver)
9
+ - [`@atproto-labs/identity-resolver`](https://github.com/bluesky-social/atproto/tree/main/packages/internal/identity-resolver)
10
+ - [`@atproto-labs/did-resolver`](https://github.com/bluesky-social/atproto/tree/main/packages/internal/did-resolver)
11
+
12
+ that are compatible with the Cloudflare Workers edge runtime.
13
+
14
+ The only changes applied throughout are:
15
+
16
+ 1. replacing `request.cache: "no-cache"` with `request.headers["cache-control"]: "no-cache"`
17
+ 2. replacing `request.redirect: "error"` with `request.redirect: "follow"`
18
+
19
+ DNS handle resolution requires the [`nodejs_compat` compatibility flag](https://developers.cloudflare.com/workers/runtime-apis/nodejs/).
20
+
21
+ ## Fork
22
+
23
+ This is a fork of [nDimensional/atproto-oauth-client-cloudflare-workers](https://github.com/nDimensional/atproto-oauth-client-cloudflare-workers).
24
+
25
+ ## Usage
26
+
27
+ `WorkersOAuthClient` works mostly as a drop-in replacement for `NodeOAuthClient`.
28
+
29
+ ```ts
30
+ import { WorkersOAuthClient } from "atproto-oauth-client-cloudflare-workers"
31
+
32
+ export const client = new WorkersOAuthClient({
33
+ clientMetadata: {
34
+ // ...
35
+ }
36
+ }
37
+ ```
38
+
39
+ By default, like `NodeOAuthClient`, this will use an in-memory store for the handle cache and DID cache. This doesn't make much sense for the workers environment, since memory is reset after each invocation. To use Cloudflare KV namespaces for your handle and DID caches, create `DidCacheKV` and `HandleCacheKV` instances and pass them to the `WorkersOAuthClient` constructor.
40
+
41
+ Similarly, to use KV namespaces for the oauth state store and oauth session store, (which are required), import and provide `StateStoreKV` and `SessionStoreKV` instances.
42
+
43
+ ```ts
44
+ import { env } from "cloudflare:workers"
45
+
46
+ import {
47
+ WorkersOAuthClient,
48
+ DidCacheKV,
49
+ HandleCacheKV,
50
+ StateStoreKV,
51
+ SessionStoreKV,
52
+ } from "atproto-oauth-client-cloudflare-workers";
53
+
54
+ export const client = new WorkersOAuthClient({
55
+ // did -> didDocument cache
56
+ didCache: new DidCacheKV(env.DID_CACHE),
57
+ // handle -> did cache
58
+ handleCache: new HandleCacheKV(env.HANDLE_CACHE),
59
+
60
+ clientMetadata: {
61
+ // Interface to store authorization state data (during authorization flows)
62
+ stateStore: new StateStoreKV(env.OAUTH_STATE_STORE),
63
+ // Interface to store authenticated session data
64
+ sessionStore: new SessionStoreKV(env.OAUTH_SESSION_STORE),
65
+
66
+ // ...
67
+ }
68
+ }
69
+ ```
@@ -0,0 +1,18 @@
1
+ import type { Did, DidDocument } from "@atproto/did";
2
+ import { DidCache } from "#did-resolver";
3
+ import type { KVNamespace } from "./util.js";
4
+ export interface DidCacheKVOptions {
5
+ /** in milliseconds */
6
+ ttl?: number;
7
+ /** in bytes */
8
+ maxSize?: number;
9
+ }
10
+ export declare class DidCacheKV implements DidCache {
11
+ namespace: KVNamespace;
12
+ ttl: number;
13
+ maxSize: number;
14
+ constructor(namespace: KVNamespace, options?: DidCacheKVOptions);
15
+ get(key: Did): Promise<DidDocument | undefined>;
16
+ set(key: Did, value: DidDocument): Promise<void>;
17
+ del(key: Did): Promise<void>;
18
+ }
@@ -0,0 +1,26 @@
1
+ const DEFAULT_TTL = 60 * 60 * 1000; // 1 hour
2
+ const DEFAULT_MAX_SIZE = 50 * 1024 * 1024; // ~50MB
3
+ export class DidCacheKV {
4
+ constructor(namespace, options = {}) {
5
+ this.namespace = namespace;
6
+ this.ttl = options.ttl ?? DEFAULT_TTL;
7
+ this.maxSize = options.maxSize ?? DEFAULT_MAX_SIZE;
8
+ }
9
+ async get(key) {
10
+ const value = await this.namespace.get(key);
11
+ if (value === null) {
12
+ return undefined;
13
+ }
14
+ else {
15
+ return JSON.parse(value);
16
+ }
17
+ }
18
+ async set(key, value) {
19
+ await this.namespace.put(key, JSON.stringify(value), {
20
+ expirationTtl: Math.round(this.ttl / 1000),
21
+ });
22
+ }
23
+ async del(key) {
24
+ await this.namespace.delete(key);
25
+ }
26
+ }
@@ -0,0 +1,7 @@
1
+ import { Did, DidDocument } from "@atproto/did";
2
+ import { SimpleStoreMemory, SimpleStoreMemoryOptions } from "@atproto-labs/simple-store-memory";
3
+ import { DidCache } from "./did-cache.js";
4
+ export type DidCacheMemoryOptions = SimpleStoreMemoryOptions<Did, DidDocument>;
5
+ export declare class DidCacheMemory extends SimpleStoreMemory<Did, DidDocument> implements DidCache {
6
+ constructor(options?: DidCacheMemoryOptions);
7
+ }
@@ -0,0 +1,10 @@
1
+ import { SimpleStoreMemory, } from "@atproto-labs/simple-store-memory";
2
+ const DEFAULT_TTL = 3600 * 1000; // 1 hour
3
+ const DEFAULT_MAX_SIZE = 50 * 1024 * 1024; // ~50MB
4
+ export class DidCacheMemory extends SimpleStoreMemory {
5
+ constructor(options) {
6
+ super(options?.max == null
7
+ ? { ttl: DEFAULT_TTL, maxSize: DEFAULT_MAX_SIZE, ...options }
8
+ : { ttl: DEFAULT_TTL, ...options });
9
+ }
10
+ }
@@ -0,0 +1,14 @@
1
+ import { Did, DidDocument } from "@atproto/did";
2
+ import { CachedGetter, SimpleStore } from "@atproto-labs/simple-store";
3
+ import { DidMethod, ResolveDidOptions } from "./did-method.js";
4
+ import { DidResolver, ResolvedDocument } from "./did-resolver.js";
5
+ export type { DidMethod, ResolveDidOptions, ResolvedDocument };
6
+ export type DidCache = SimpleStore<Did, DidDocument>;
7
+ export type DidResolverCachedOptions = {
8
+ cache?: DidCache;
9
+ };
10
+ export declare class DidResolverCached<M extends string = string> implements DidResolver<M> {
11
+ protected readonly getter: CachedGetter<Did, DidDocument>;
12
+ constructor(resolver: DidResolver<M>, cache?: DidCache);
13
+ resolve<D extends Did>(did: D, options?: ResolveDidOptions): Promise<ResolvedDocument<D, M>>;
14
+ }
@@ -0,0 +1,10 @@
1
+ import { CachedGetter } from "@atproto-labs/simple-store";
2
+ import { DidCacheMemory } from "./did-cache-memory.js";
3
+ export class DidResolverCached {
4
+ constructor(resolver, cache = new DidCacheMemory()) {
5
+ this.getter = new CachedGetter((did, options) => resolver.resolve(did, options), cache);
6
+ }
7
+ async resolve(did, options) {
8
+ return this.getter.get(did, options);
9
+ }
10
+ }
@@ -0,0 +1,11 @@
1
+ import { Did, DidDocument } from "@atproto/did";
2
+ export type ResolveDidOptions = {
3
+ signal?: AbortSignal;
4
+ noCache?: boolean;
5
+ };
6
+ export interface DidMethod<Method extends string> {
7
+ resolve: (did: Did<Method>, options?: ResolveDidOptions) => DidDocument | PromiseLike<DidDocument>;
8
+ }
9
+ export type DidMethods<M extends string> = {
10
+ [K in M]: DidMethod<K>;
11
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,9 @@
1
+ import { Did } from "@atproto/did";
2
+ import { DidMethod, DidMethods, ResolveDidOptions } from "./did-method.js";
3
+ import { DidResolver, ResolvedDocument } from "./did-resolver.js";
4
+ export type { DidMethod, ResolveDidOptions, ResolvedDocument };
5
+ export declare class DidResolverBase<M extends string = string> implements DidResolver<M> {
6
+ protected readonly methods: Map<string, DidMethod<M>>;
7
+ constructor(methods: DidMethods<M>);
8
+ resolve<D extends Did>(did: D, options?: ResolveDidOptions): Promise<ResolvedDocument<D, M>>;
9
+ }
@@ -0,0 +1,36 @@
1
+ import { ZodError } from "zod";
2
+ import { DidError, extractDidMethod } from "@atproto/did";
3
+ import { FetchError, FetchResponseError } from "@atproto-labs/fetch";
4
+ export class DidResolverBase {
5
+ constructor(methods) {
6
+ this.methods = new Map(Object.entries(methods));
7
+ }
8
+ async resolve(did, options) {
9
+ options?.signal?.throwIfAborted();
10
+ const method = extractDidMethod(did);
11
+ const resolver = this.methods.get(method);
12
+ if (!resolver) {
13
+ throw new DidError(did, `Unsupported DID method`, "did-method-invalid", 400);
14
+ }
15
+ try {
16
+ const document = await resolver.resolve(did, options);
17
+ if (document.id !== did) {
18
+ throw new DidError(did, `DID document id (${document.id}) does not match DID`, "did-document-id-mismatch", 400);
19
+ }
20
+ return document;
21
+ }
22
+ catch (err) {
23
+ if (err instanceof FetchResponseError) {
24
+ const status = err.response.status >= 500 ? 502 : err.response.status;
25
+ throw new DidError(did, err.message, "did-fetch-error", status, err);
26
+ }
27
+ if (err instanceof FetchError) {
28
+ throw new DidError(did, err.message, "did-fetch-error", 400, err);
29
+ }
30
+ if (err instanceof ZodError) {
31
+ throw new DidError(did, err.message, "did-document-format-error", 503, err);
32
+ }
33
+ throw DidError.from(err, did);
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,8 @@
1
+ import { DidResolverBase } from "./did-resolver-base.js";
2
+ import { DidPlcMethodOptions } from "./methods/plc.js";
3
+ import { DidWebMethodOptions } from "./methods/web.js";
4
+ import { Simplify } from "./util.js";
5
+ export type DidResolverCommonOptions = Simplify<DidPlcMethodOptions & DidWebMethodOptions>;
6
+ export declare class DidResolverCommon extends DidResolverBase<"plc" | "web"> implements DidResolverBase<"plc" | "web"> {
7
+ constructor(options?: DidResolverCommonOptions);
8
+ }
@@ -0,0 +1,11 @@
1
+ import { DidResolverBase } from "./did-resolver-base.js";
2
+ import { DidPlcMethod } from "./methods/plc.js";
3
+ import { DidWebMethod } from "./methods/web.js";
4
+ export class DidResolverCommon extends DidResolverBase {
5
+ constructor(options) {
6
+ super({
7
+ plc: new DidPlcMethod(options),
8
+ web: new DidWebMethod(options),
9
+ });
10
+ }
11
+ }
@@ -0,0 +1,6 @@
1
+ import { Did, DidDocument } from "@atproto/did";
2
+ import { ResolveDidOptions } from "./did-method.js";
3
+ export type ResolvedDocument<D extends Did, M extends string = string> = D extends Did<infer N> ? DidDocument<N extends string ? M : N extends M ? N : never> : never;
4
+ export interface DidResolver<M extends string = string> {
5
+ resolve<D extends Did>(did: D, options?: ResolveDidOptions): Promise<ResolvedDocument<D, M>>;
6
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,6 @@
1
+ export * from "./did-cache-memory.js";
2
+ export * from "./did-cache.js";
3
+ export * from "./did-method.js";
4
+ export * from "./did-resolver-common.js";
5
+ export * from "./did-resolver.js";
6
+ export * from "./methods.js";
@@ -0,0 +1,7 @@
1
+ // export * from "@atproto/did";
2
+ export * from "./did-cache-memory.js";
3
+ export * from "./did-cache.js";
4
+ export * from "./did-method.js";
5
+ export * from "./did-resolver-common.js";
6
+ export * from "./did-resolver.js";
7
+ export * from "./methods.js";
@@ -0,0 +1,43 @@
1
+ import { Did } from "@atproto/did";
2
+ import { Fetch } from "@atproto-labs/fetch";
3
+ import { DidMethod, ResolveDidOptions } from "../did-method.js";
4
+ export type DidPlcMethodOptions = {
5
+ /**
6
+ * @default globalThis.fetch
7
+ */
8
+ fetch?: Fetch;
9
+ /**
10
+ * @default 'https://plc.directory/'
11
+ */
12
+ plcDirectoryUrl?: string | URL;
13
+ };
14
+ export declare class DidPlcMethod implements DidMethod<"plc"> {
15
+ protected readonly fetch: Fetch<unknown>;
16
+ readonly plcDirectoryUrl: URL;
17
+ constructor(options?: DidPlcMethodOptions);
18
+ resolve(did: Did<"plc">, options?: ResolveDidOptions): Promise<{
19
+ id: `did:${string}:${string}`;
20
+ '@context': "https://www.w3.org/ns/did/v1" | [string, ...string[]];
21
+ controller?: `did:${string}:${string}` | `did:${string}:${string}`[] | undefined;
22
+ alsoKnownAs?: string[] | undefined;
23
+ service?: {
24
+ type: string | string[];
25
+ id: string;
26
+ serviceEndpoint: string | Record<string, string> | [string | Record<string, string>, ...(string | Record<string, string>)[]];
27
+ }[] | undefined;
28
+ authentication?: (string | {
29
+ type: string;
30
+ id: string;
31
+ controller: `did:${string}:${string}` | `did:${string}:${string}`[];
32
+ publicKeyJwk?: Record<string, unknown> | undefined;
33
+ publicKeyMultibase?: string | undefined;
34
+ })[] | undefined;
35
+ verificationMethod?: (string | {
36
+ type: string;
37
+ id: string;
38
+ controller: `did:${string}:${string}` | `did:${string}:${string}`[];
39
+ publicKeyJwk?: Record<string, unknown> | undefined;
40
+ publicKeyMultibase?: string | undefined;
41
+ })[] | undefined;
42
+ }>;
43
+ }
@@ -0,0 +1,22 @@
1
+ import { assertDidPlc, didDocumentValidator } from "@atproto/did";
2
+ import { bindFetch, fetchJsonProcessor, fetchJsonZodProcessor, fetchOkProcessor, } from "@atproto-labs/fetch";
3
+ import { pipe } from "@atproto-labs/pipe";
4
+ const fetchSuccessHandler = pipe(fetchOkProcessor(), fetchJsonProcessor(/^application\/(did\+ld\+)?json$/), fetchJsonZodProcessor(didDocumentValidator));
5
+ export class DidPlcMethod {
6
+ constructor(options) {
7
+ this.plcDirectoryUrl = new URL(options?.plcDirectoryUrl || "https://plc.directory/");
8
+ this.fetch = bindFetch(options?.fetch);
9
+ }
10
+ async resolve(did, options) {
11
+ // Although the did should start with `did:plc:` (thanks to typings), we
12
+ // should still check if the msid is valid.
13
+ assertDidPlc(did);
14
+ // Should never throw
15
+ const url = new URL(`/${encodeURIComponent(did)}`, this.plcDirectoryUrl);
16
+ return this.fetch(url, {
17
+ redirect: "follow",
18
+ headers: { accept: "application/did+ld+json,application/json" },
19
+ signal: options?.signal,
20
+ }).then(fetchSuccessHandler);
21
+ }
22
+ }
@@ -0,0 +1,43 @@
1
+ import { Did } from "@atproto/did";
2
+ import { Fetch } from "@atproto-labs/fetch";
3
+ import { DidMethod, ResolveDidOptions } from "../did-method.js";
4
+ export type DidWebMethodOptions = {
5
+ fetch?: Fetch;
6
+ /** @default true */
7
+ allowHttp?: boolean;
8
+ };
9
+ export declare class DidWebMethod implements DidMethod<"web"> {
10
+ protected readonly fetch: Fetch<unknown>;
11
+ protected readonly allowHttp: boolean;
12
+ constructor({ fetch, allowHttp, }?: DidWebMethodOptions);
13
+ resolve(did: Did<"web">, options?: ResolveDidOptions): Promise<{
14
+ id: `did:${string}:${string}`;
15
+ '@context': "https://www.w3.org/ns/did/v1" | [string, ...string[]];
16
+ controller?: `did:${string}:${string}` | `did:${string}:${string}`[] | undefined;
17
+ alsoKnownAs?: string[] | undefined;
18
+ service?: {
19
+ type: string | string[];
20
+ id: string;
21
+ serviceEndpoint: string | Record<string, string> | [string | Record<string, string>, ...(string | Record<string, string>)[]];
22
+ }[] | undefined;
23
+ authentication?: (string | {
24
+ type: string;
25
+ id: string;
26
+ controller: `did:${string}:${string}` | `did:${string}:${string}`[];
27
+ publicKeyJwk?: Record<string, unknown> | undefined;
28
+ publicKeyMultibase?: string | undefined;
29
+ })[] | undefined;
30
+ verificationMethod?: (string | {
31
+ type: string;
32
+ id: string;
33
+ controller: `did:${string}:${string}` | `did:${string}:${string}`[];
34
+ publicKeyJwk?: Record<string, unknown> | undefined;
35
+ publicKeyMultibase?: string | undefined;
36
+ })[] | undefined;
37
+ }>;
38
+ }
39
+ /**
40
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc8615}
41
+ * @see {@link https://w3c-ccg.github.io/did-method-web/#create-register}
42
+ */
43
+ export declare function buildDidWebDocumentUrl(did: Did<"web">): URL;
@@ -0,0 +1,42 @@
1
+ import { DidError, didDocumentValidator, didWebToUrl } from "@atproto/did";
2
+ import { bindFetch, fetchJsonProcessor, fetchJsonZodProcessor, fetchOkProcessor, } from "@atproto-labs/fetch";
3
+ import { pipe } from "@atproto-labs/pipe";
4
+ const fetchSuccessHandler = pipe(fetchOkProcessor(), fetchJsonProcessor(/^application\/(did\+ld\+)?json$/), fetchJsonZodProcessor(didDocumentValidator));
5
+ export class DidWebMethod {
6
+ constructor({ fetch = globalThis.fetch, allowHttp = true, } = {}) {
7
+ this.fetch = bindFetch(fetch);
8
+ this.allowHttp = allowHttp;
9
+ }
10
+ async resolve(did, options) {
11
+ const didDocumentUrl = buildDidWebDocumentUrl(did);
12
+ if (!this.allowHttp && didDocumentUrl.protocol === "http:") {
13
+ throw new DidError(did, 'Resolution of "http" did:web is not allowed', "did-web-http-not-allowed");
14
+ }
15
+ // Note we do not explicitly check for "localhost" here. Instead, we rely on
16
+ // the injected 'fetch' function to handle the URL. If the URL is
17
+ // "localhost", or resolves to a private IP address, the fetch function is
18
+ // responsible for handling it.
19
+ return this.fetch(didDocumentUrl, {
20
+ redirect: "follow",
21
+ headers: { accept: "application/did+ld+json,application/json" },
22
+ signal: options?.signal,
23
+ }).then(fetchSuccessHandler);
24
+ }
25
+ }
26
+ /**
27
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc8615}
28
+ * @see {@link https://w3c-ccg.github.io/did-method-web/#create-register}
29
+ */
30
+ export function buildDidWebDocumentUrl(did) {
31
+ const url = didWebToUrl(did); // Will throw if the DID is invalid
32
+ // Note: DID cannot end with an `:`, so they cannot end with a `/`. This is
33
+ // true unless when there is no path at all, in which case the URL constructor
34
+ // will set the pathname to `/`.
35
+ // https://w3c-ccg.github.io/did-method-web/#read-resolve
36
+ if (url.pathname === "/") {
37
+ return new URL(`/.well-known/did.json`, url);
38
+ }
39
+ else {
40
+ return new URL(`${url.pathname}/did.json`, url);
41
+ }
42
+ }
@@ -0,0 +1,2 @@
1
+ export * from "./methods/plc.js";
2
+ export * from "./methods/web.js";
@@ -0,0 +1,2 @@
1
+ export * from "./methods/plc.js";
2
+ export * from "./methods/web.js";
@@ -0,0 +1,3 @@
1
+ export type Simplify<T> = {
2
+ [K in keyof T]: T[K];
3
+ } & NonNullable<unknown>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,21 @@
1
+ import { Jwk, Key } from "@atproto/jwk";
2
+ import { InternalStateData, Session } from "#oauth-client";
3
+ import { SimpleStore } from "@atproto-labs/simple-store";
4
+ type ToDpopJwkValue<V extends {
5
+ dpopKey: Key;
6
+ }> = Omit<V, "dpopKey"> & {
7
+ dpopJwk: Jwk;
8
+ };
9
+ /**
10
+ * Utility function that allows to simplify the store interface by exposing a
11
+ * JWK (JSON) instead of a Key instance.
12
+ */
13
+ export declare function toDpopKeyStore<K extends string, V extends {
14
+ dpopKey: Key;
15
+ dpopJwk?: never;
16
+ }>(store: SimpleStore<K, ToDpopJwkValue<V>>): SimpleStore<K, V>;
17
+ export type WorkersSavedState = ToDpopJwkValue<InternalStateData>;
18
+ export type WorkersSavedStateStore = SimpleStore<string, WorkersSavedState>;
19
+ export type WorkersSavedSession = ToDpopJwkValue<Session>;
20
+ export type WorkersSavedSessionStore = SimpleStore<string, WorkersSavedSession>;
21
+ export {};
@@ -0,0 +1,25 @@
1
+ import { JoseKey } from "@atproto/jwk-jose";
2
+ /**
3
+ * Utility function that allows to simplify the store interface by exposing a
4
+ * JWK (JSON) instead of a Key instance.
5
+ */
6
+ export function toDpopKeyStore(store) {
7
+ return {
8
+ async set(sub, { dpopKey, ...data }) {
9
+ const dpopJwk = dpopKey.privateJwk;
10
+ if (!dpopJwk)
11
+ throw new Error("Private DPoP JWK is missing.");
12
+ await store.set(sub, { ...data, dpopJwk });
13
+ },
14
+ async get(sub) {
15
+ const result = await store.get(sub);
16
+ if (!result)
17
+ return undefined;
18
+ const { dpopJwk, ...data } = result;
19
+ const dpopKey = await JoseKey.fromJWK(dpopJwk);
20
+ return { ...data, dpopKey };
21
+ },
22
+ del: store.del.bind(store),
23
+ clear: store.clear?.bind(store),
24
+ };
25
+ }
@@ -0,0 +1,17 @@
1
+ import type { HandleCache, ResolvedHandle } from "#handle-resolver";
2
+ import type { KVNamespace } from "./util.js";
3
+ export interface HandleCacheKVOptions {
4
+ /** in milliseconds */
5
+ ttl?: number;
6
+ /** in bytes */
7
+ maxSize?: number;
8
+ }
9
+ export declare class HandleCacheKV implements HandleCache {
10
+ namespace: KVNamespace;
11
+ ttl: number;
12
+ maxSize: number;
13
+ constructor(namespace: KVNamespace, options: HandleCacheKVOptions);
14
+ get(key: string): Promise<ResolvedHandle | undefined>;
15
+ set(key: string, value: ResolvedHandle): Promise<void>;
16
+ del(key: string): Promise<void>;
17
+ }
@@ -0,0 +1,31 @@
1
+ const DEFAULT_TTL = 60 * 60 * 1000; // 1 hour
2
+ const DEFAULT_MAX_SIZE = 1024;
3
+ export class HandleCacheKV {
4
+ constructor(namespace, options) {
5
+ this.namespace = namespace;
6
+ this.ttl = options.ttl ?? DEFAULT_TTL;
7
+ this.maxSize = options.maxSize ?? DEFAULT_MAX_SIZE;
8
+ }
9
+ async get(key) {
10
+ const value = await this.namespace.get(key);
11
+ if (value === null) {
12
+ return undefined;
13
+ }
14
+ else {
15
+ return value;
16
+ }
17
+ }
18
+ async set(key, value) {
19
+ if (value === null) {
20
+ await this.namespace.delete(key);
21
+ }
22
+ else {
23
+ await this.namespace.put(key, value, {
24
+ expirationTtl: Math.round(this.ttl / 1000),
25
+ });
26
+ }
27
+ }
28
+ async del(key) {
29
+ await this.namespace.delete(key);
30
+ }
31
+ }
@@ -0,0 +1,8 @@
1
+ import { AtprotoHandleResolver, AtprotoHandleResolverOptions } from "./atproto-handle-resolver.js";
2
+ import { HandleResolver } from "./types.js";
3
+ export type AtprotoDohHandleResolverOptions = Omit<AtprotoHandleResolverOptions, "resolveTxt" | "resolveTxtFallback"> & {
4
+ dohEndpoint: string | URL;
5
+ };
6
+ export declare class AtprotoDohHandleResolver extends AtprotoHandleResolver implements HandleResolver {
7
+ constructor(options: AtprotoDohHandleResolverOptions);
8
+ }