/
index.ts
397 lines (363 loc) · 12.6 KB
/
index.ts
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/**
* Manages user account DIDs in web based environments.
*
* ## Purpose
*
* Manages, creates and authorizes a DID session key for a user. Returns an authenticated DIDs instance
* to be used in other Ceramic libraries. Supports did:pkh for blockchain accounts with Sign-In with
* Ethereum and CACAO for authorization.
*
* ## Installation
*
* ```sh
* npm install did-session
* ```
*
* ## Usage
*
* Authorize and use DIDs where needed. Import the AuthMethod you need, Ethereum accounts used here for example.
*
* ```js
* import { DIDSession } from 'did-session'
* import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'
*
* const ethProvider = // import/get your web3 eth provider
* const addresses = await ethProvider.request({ method: 'eth_requestAccounts' })
* const accountId = await getAccountId(ethProvider, addresses[0])
* const authMethod = await EthereumWebAuth.getAuthMethod(ethprovider, accountId)
*
* const session = await DIDSession.get(accountId, authMethod, { resources: [...]})
*
* // Uses DIDs in ceramic & glaze libraries, ie
* const ceramic = new CeramicClient()
* ceramic.did = session.did
*
* // pass ceramic instance where needed
*
* ```
*
* Additional helper functions are available to help you manage a session lifecycle and the user experience.
*
* ```ss
* // Check if authorized or created from existing session string
* didsession.hasSession
*
* // Check if session expired
* didsession.isExpired
*
* // Get resources session is authorized for
* didsession.authorizations
*
* // Check number of seconds till expiration, may want to re auth user at a time before expiration
* didsession.expiresInSecs
* ```
*
* ## Configuration
*
* The resources your app needs to write access to must be passed during authorization. Resources are an array
* of Model Stream Ids or Streams Ids. Typically you will just pass resources from `@composedb` libraries as
* you will already manage your Composites and Models there. For example:
*
* ```js
* import { ComposeClient } from '@composedb/client'
*
* //... Reference above and `@composedb` docs for additional configuration here
*
* const client = new ComposeClient({ceramic, definition})
* const resources = client.resources
* const session = await DIDSession.get(accountId, authMethod, { resources })
* client.setDID(session.did)
* ```
*
* By default a session will expire in 1 week. You can change this time by passing the `expiresInSecs` option to
* indicate how many seconds from the current time you want this session to expire.
*
* ```js
* const oneDay = 60 * 60 * 24
* const session = await DIDSession.get(accountId, authMethod, { resources: [...], expiresInSecs: oneDay })
* ```
*
* A domain/app name is used when making requests, by default in a browser based environment the library will use
* the domain name of your app. If you are using the library in a non web based environment you will need to pass
* the `domain` option otherwise an error will thrown.
*
* ```js
* const session = await DIDSession.get(accountId, authMethod, { resources: [...], domain: 'YourAppName' })
* ```
*
* ## Upgrading from `@glazed/did-session` to `did-session`
*
* `authorize` changes to a static method which returns a did-session instance and `getDID()` becomes a `did` getter. For example:
*
* ```js
* // Before @glazed/did-session
* const session = new DIDSession({ authProvider })
* const did = await session.authorize()
*
* // Now did-session
* const session = await DIDSession.get(accountId, authMethod, { resources: [...]})
* const did = session.did
* ```
*
* ## Upgrading from `did-session@0.x.x` to `did-session@1.x.x`
*
* AuthProviders change to AuthMethod interfaces. Similarly you can import the auth libraries you need. How you configure and manage
* these AuthMethods may differ, but each will return an AuthMethod function to be used with did-session.
*
* ```js
* // Before with v0.x.x
* ...
* import { EthereumAuthProvider } from '@ceramicnetwork/blockchain-utils-linking'
*
* const ethProvider = // import/get your web3 eth provider
* const addresses = await ethProvider.request({ method: 'eth_requestAccounts' })
* const authProvider = new EthereumAuthProvider(ethProvider, addresses[0])
* const session = new DIDSession({ authProvider })
* const did = await session.authorize()
*
* // Now did-session@1.0.0
* ...
* import { EthereumWebAuth, getAccountId } from '@didtools/pkh-ethereum'
*
* const ethProvider = // import/get your web3 eth provider
* const addresses = await ethProvider.request({ method: 'eth_requestAccounts' })
* const accountId = await getAccountId(ethProvider, addresses[0])
* const authMethod = await EthereumWebAuth.getAuthMethod(ethProvider, accountId)
* const session = await DIDSession.get(accountId, authMethod, { resources: [...]})
* const did = session.did
* ```
*
* @module did-session
*/
import { Ed25519Provider } from 'key-did-provider-ed25519'
import { WebcryptoProvider, generateP256KeyPair } from '@didtools/key-webcrypto'
import KeyDidResolver from 'key-did-resolver'
import { randomBytes } from '@stablelib/random'
import { DID } from 'dids'
import { AccountId } from 'caip'
import type { Cacao, AuthMethod, AuthMethodOpts } from '@didtools/cacao'
import * as u8a from 'uint8arrays'
import { SessionStore } from './sessionStore.js'
export type SessionParams = {
keySeed?: Uint8Array
cacao: Cacao
did: DID
}
type SessionObj = {
sessionKeySeed: string
cacao: Cacao
}
interface AuthOpts {
domain?: string
statement?: string
version?: string
nonce?: string
requestId?: string
expirationTime?: string
resources?: Array<string>
expiresInSecs?: number
}
export async function createDIDKey(seed?: Uint8Array): Promise<DID> {
const didProvider = new Ed25519Provider(seed || randomBytes(32))
const didKey = new DID({
provider: didProvider,
resolver: KeyDidResolver.getResolver(),
})
await didKey.authenticate()
return didKey
}
export async function createDIDCacao(didKey: DID, cacao: Cacao): Promise<DID> {
const didWithCap = didKey.withCapability(cacao)
await didWithCap.authenticate()
return didWithCap
}
export function getAccountIdByDID(did: string): AccountId {
return new AccountId(did.slice(8))
}
function JSONToBase64url(object: Record<string, any>): string {
return u8a.toString(u8a.fromString(JSON.stringify(object)), 'base64url')
}
function base64urlToJSON(s: string): Record<string, any> {
return JSON.parse(u8a.toString(u8a.fromString(s, 'base64url'))) as Record<string, any>
}
function bytesToBase64(b: Uint8Array): string {
return u8a.toString(b, 'base64pad')
}
function base64ToBytes(s: string): Uint8Array {
return u8a.fromString(s, 'base64pad')
}
export function cacaoContainsResources(cacao: Cacao, resources: Array<string>): boolean {
return resources.every((res) => cacao.p.resources?.includes(res))
}
function isExpired(expTime?: string): boolean {
if (!expTime) return false
return Date.parse(expTime) < Date.now()
}
/**
* DID Session
*
* ```sh
* import { DIDSession } from 'did-session'
* ```
*/
export class DIDSession {
#did: DID
#keySeed: Uint8Array | undefined
#cacao: Cacao
constructor(params: SessionParams) {
this.#keySeed = params.keySeed
this.#cacao = params.cacao
this.#did = params.did // Remove did init param if/when async didKey authorize is removed
}
/**
* Request authorization for session
*/
static async authorize(authMethod: AuthMethod, authOpts: AuthOpts = {}): Promise<DIDSession> {
if (!authOpts.resources || authOpts.resources.length === 0)
throw new Error('Required: resource argument option when authorizing')
const authMethodOpts: AuthMethodOpts = authOpts
const keySeed = randomBytes(32)
const didKey = await createDIDKey(keySeed)
authMethodOpts.uri = didKey.id
if (authOpts.expiresInSecs) {
const exp = new Date(Date.now() + authOpts.expiresInSecs * 1000)
authMethodOpts.expirationTime = exp.toISOString()
}
const cacao = await authMethod(authOpts)
const did = await createDIDCacao(didKey, cacao)
return new DIDSession({ cacao, keySeed, did })
}
static async initDID(didKey: DID, cacao: Cacao): Promise<DID> {
const didWithCap = didKey.withCapability(cacao)
await didWithCap.authenticate()
return didWithCap
}
/**
* Get a session for the given accountId, if one exists, otherwise creates a new one.
*/
static async get(
account: AccountId,
authMethod: AuthMethod,
authOpts: AuthOpts = {},
): Promise<DIDSession> {
if (!authOpts.resources || authOpts.resources.length === 0)
throw new Error('Required: resource argument option when authorizing')
const store = await SessionStore.create()
const result = (await store.get(account)) || {}
let { cacao, keypair } = result as { cacao: Cacao; keypair: CryptoKeyPair }
if (
cacao &&
keypair &&
cacaoContainsResources(cacao, authOpts.resources) &&
!isExpired(cacao.p.exp)
) {
const provider = new WebcryptoProvider(keypair)
const did = new DID({ provider, resolver: KeyDidResolver.getResolver(), capability: cacao })
await did.authenticate()
const session = new DIDSession({ cacao, did })
return session
}
// create a new DID instance using the WebcryptoProvider
keypair = await generateP256KeyPair()
const provider = new WebcryptoProvider(keypair)
const didKey = new DID({ provider, resolver: KeyDidResolver.getResolver() })
await didKey.authenticate()
const authMethodOpts: AuthMethodOpts = authOpts
authMethodOpts.uri = didKey.id
if (authOpts.expiresInSecs) {
const exp = new Date(Date.now() + authOpts.expiresInSecs * 1000)
authMethodOpts.expirationTime = exp.toISOString()
}
cacao = await authMethod(authMethodOpts)
const did = await createDIDCacao(didKey, cacao)
await store.set(account, { cacao, keypair })
store.close()
return new DIDSession({ cacao, did })
}
/**
* Removes a session from storage for a given account (if created using `DIDSession.get`)
*/
static async remove(account: AccountId): Promise<void> {
const store = await SessionStore.create()
await store.remove(account)
store.close()
}
/**
* Check if there is an active session for a given account.
*/
static async hasSessionFor(account: AccountId, resources: Array<string>): Promise<boolean> {
const store = await SessionStore.create()
const { cacao } = (await store.get(account)) || ({} as { cacao: Cacao })
store.close()
return cacao && cacaoContainsResources(cacao, resources) && !isExpired(cacao.p.exp)
}
/**
* Get DID instance, if authorized
*/
get did(): DID {
return this.#did
}
/**
* Serialize session into string, can store and initalize the same session again while valid
*/
serialize(): string {
if (!this.#keySeed) throw new Error('Secure sessions cannot be serialized')
const session = {
sessionKeySeed: bytesToBase64(this.#keySeed),
cacao: this.#cacao,
}
return JSONToBase64url(session)
}
/**
* Initialize a session from a serialized session string
*/
static async fromSession(session: string): Promise<DIDSession> {
const { sessionKeySeed, cacao } = base64urlToJSON(session) as SessionObj
const keySeed = base64ToBytes(sessionKeySeed)
const didKey = await createDIDKey(keySeed)
const did = await DIDSession.initDID(didKey, cacao)
return new DIDSession({ cacao, keySeed, did })
}
get hasSession(): boolean {
return !!this.#cacao && !!this.#did
}
/**
* Determine if a session is expired or not
*/
get isExpired(): boolean {
return isExpired(this.#cacao.p.exp)
}
/**
* Number of seconds until a session expires
*/
get expireInSecs(): number {
const expTime = this.#cacao.p.exp
if (!expTime) throw new Error('Session does not expire') // Removed in future
const timeDiff = Date.parse(expTime) - Date.now()
return timeDiff < 0 ? 0 : timeDiff / 1000
}
/**
* Get the list of resources a session is authorized for
*/
get authorizations(): Array<string> {
return this.#cacao?.p.resources ?? []
}
/**
* Get the session CACAO
*/
get cacao(): Cacao {
return this.#cacao
}
/**
* Determine if session is available and optionally if authorized for given resources
*/
isAuthorized(resources?: Array<string>): boolean {
if (!this.hasSession || this.isExpired) return false
if (!resources) return true
return resources.every((val) => this.authorizations.includes(val))
}
/** DID string associated to the session instance. session.id == session.getDID().parent */
get id(): string {
return this.#did.parent
}
}