-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.ts
More file actions
104 lines (88 loc) · 2.82 KB
/
client.ts
File metadata and controls
104 lines (88 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import clientMetadata from "./client-metadata"
import { JoseKey, type Importable } from "@atproto/jwk-jose"
import { NodeOAuthClient } from "@atproto/oauth-client-node"
import type { SimpleStore } from "@atproto-labs/simple-store"
class Store implements SimpleStore {
map: Map<string, any>
set: (key: string, value: any) => void
get: (key: string) => any
del: (key: string) => void
constructor() {
this.map = new Map()
this.set = this.map.set.bind(this.map)
this.get = this.map.get.bind(this.map)
this.del = this.map.delete.bind(this.map)
}
}
// let keyset = await Promise.all(
// new Array(5)
// .fill(null)
// .map(() =>
// JoseKey.generate(["ES256"], crypto.randomUUID())
// )
// )
// let keyset = await Promise.all(
// new Array(5)
// .fill(null)
// .map(() =>
// JoseKey.generate(["ES256"], crypto.randomUUID())
// )
// )
// // use local keys in dev because auth server caches keys
// // & considers new keys invalid until cache is refreshed
// if (import.meta.env.DEV) {
// const { readFileSync, writeFileSync } = await import(
// "node:fs"
// )
// // use local keys in dev because auth server caches keys
// // & considers new keys invalid until cache is refreshed
// if (import.meta.env.DEV) {
// const { readFileSync, writeFileSync } = await import(
// "node:fs"
// )
// const jwkCache = ".jwks.json"
// const jwkCache = ".jwks.json"
// try {
// keyset = await Promise.all(
// JSON.parse(
// readFileSync(jwkCache).toString()
// ).map(({ jwk }: { jwk: Importable }) =>
// JoseKey.fromImportable(jwk)
// )
// )
// } catch (error: unknown) {
// if (
// error instanceof Error &&
// (error as any)?.code == "ENOENT"
// )
// writeFileSync(jwkCache, JSON.stringify(keyset))
// else throw error
// }
// }
const client = new NodeOAuthClient({
// This object will be used to build the payload of the /client-metadata.json
// endpoint metadata, exposing the client metadata to the OAuth server.
clientMetadata,
// Used to authenticate the client to the token endpoint. Will be used to
// build the jwks object to be exposed on the "jwks_uri" endpoint.
keyset: await Promise.all(
JSON.parse(import.meta.env.JWKS).map(
({ jwk }: { jwk: Importable }) =>
JoseKey.fromImportable(jwk)
)
),
// Interface to store authorization state data (during authorization flows)
stateStore: new Store(),
// Interface to store authenticated session data
sessionStore: new Store()
// A lock to prevent concurrent access to the session store. Optional if only one instance is running.
// requestLock
})
console.log(client.jwks)
// client.addEventListener("updated", event =>
// console.log("Refreshed tokens were saved in the store:", event.detail)
// )
// client.addEventListener("deleted", event =>
// console.log("Session was deleted from the session store:", event.detail)
// )
export default client