generated from fission-codes/project-template
-
Notifications
You must be signed in to change notification settings - Fork 24
/
consumer.ts
359 lines (302 loc) · 10.9 KB
/
consumer.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
import * as Uint8arrays from "uint8arrays"
import * as Auth from "../components/auth/implementation.js"
import * as Crypto from "../components/crypto/implementation.js"
import * as Manners from "../components/manners/implementation.js"
import * as Check from "../common/type-checks.js"
import * as DID from "../did/index.js"
import * as Linking from "./common.js"
import * as Ucan from "../ucan/index.js"
import { Components } from "../components.js"
import { EventEmitter, EventListener } from "../common/event-emitter.js"
import { LinkingError, LinkingStep, LinkingWarning, tryParseMessage } from "./common.js"
import type { Maybe, Result } from "../common/index.js"
export type AccountLinkingConsumer = {
on: <K extends keyof ConsumerEventMap>(eventName: K, listener: EventListener<ConsumerEventMap[ K ]>) => void
cancel: () => void
}
export interface ConsumerEventMap {
"challenge": { pin: number[] }
"link": { approved: boolean; username: string }
"done": undefined
}
export type Dependencies = {
auth: Auth.Implementation<Components>
crypto: Crypto.Implementation
manners: Manners.Implementation
}
type LinkingState = {
username: Maybe<string>
sessionKey: Maybe<Uint8Array>
temporaryRsaPair: Maybe<CryptoKeyPair>
step: Maybe<LinkingStep>
}
/**
* Create an account linking consumer
*
* @param options consumer options
* @param options.username username of the account
* @returns an account linking event emitter and cancel function
*/
export const createConsumer = async (
dependencies: Dependencies,
options: { username: string }
): Promise<AccountLinkingConsumer> => {
const { username } = options
const handleLinkingError = (errorOrWarning: LinkingError | LinkingWarning) => Linking.handleLinkingError(dependencies.manners, errorOrWarning)
let eventEmitter: Maybe<EventEmitter<ConsumerEventMap>> = new EventEmitter()
const ls: LinkingState = {
username,
sessionKey: null,
temporaryRsaPair: null,
step: LinkingStep.Broadcast
}
const handleMessage = async (event: MessageEvent): Promise<void> => {
const { data } = event
const message = data.arrayBuffer ? new TextDecoder().decode(await data.arrayBuffer()) : data
switch (ls.step) {
// Broadcast
// ---------
case LinkingStep.Broadcast:
return handleLinkingError(new LinkingWarning("Consumer is not ready to start linking"))
// Negotiation
// -----------
case LinkingStep.Negotiation:
if (ls.sessionKey) {
handleLinkingError(new LinkingWarning("Consumer already received a session key"))
} else if (!ls.temporaryRsaPair || !ls.temporaryRsaPair.privateKey) {
handleLinkingError(new LinkingError("Consumer missing RSA key pair when handling session key message"))
} else {
const sessionKeyResult = await handleSessionKey(
dependencies.crypto,
ls.temporaryRsaPair.privateKey,
message
)
if (sessionKeyResult.ok) {
ls.sessionKey = sessionKeyResult.value
const { pin, challenge } = await generateUserChallenge(dependencies.crypto, ls.sessionKey)
channel.send(challenge)
eventEmitter?.emit("challenge", { pin: Array.from(pin) })
ls.step = LinkingStep.Delegation
} else {
handleLinkingError(sessionKeyResult.error)
}
}
break
// Delegation
// ----------
case LinkingStep.Delegation:
if (!ls.sessionKey) {
handleLinkingError(new LinkingError("Consumer was missing session key when linking device"))
} else if (!ls.username) {
handleLinkingError(new LinkingError("Consumer was missing username when linking device"))
} else {
const linkingResult = await linkDevice(
dependencies.auth,
dependencies.crypto,
ls.sessionKey,
ls.username,
message
)
if (linkingResult.ok) {
const { approved } = linkingResult.value
eventEmitter?.emit("link", { approved, username: ls.username })
await done()
} else {
handleLinkingError(linkingResult.error)
}
}
break
}
}
const done = async () => {
eventEmitter?.emit("done", undefined)
eventEmitter = null
channel.close()
clearInterval(rsaExchangeInterval)
}
const channel = await dependencies.auth.createChannel({ handleMessage, username })
const rsaExchangeInterval = setInterval(async () => {
if (!ls.sessionKey) {
const { temporaryRsaPair, temporaryDID } = await generateTemporaryExchangeKey(dependencies.crypto)
ls.temporaryRsaPair = temporaryRsaPair
ls.step = LinkingStep.Negotiation
channel.send(temporaryDID)
} else {
clearInterval(rsaExchangeInterval)
}
}, 2000)
return {
on: (...args) => eventEmitter?.on(...args),
cancel: done
}
}
// 🔗 Device Linking Steps
/**
* BROADCAST
*
* Generate a temporary RSA keypair and extract a temporary DID from it.
* The temporary DID will be broadcast on the channel to start the linking process.
*
* @returns temporary RSA key pair and temporary DID
*/
export const generateTemporaryExchangeKey = async (
crypto: Crypto.Implementation
): Promise<{ temporaryRsaPair: CryptoKeyPair; temporaryDID: string }> => {
const temporaryRsaPair = await crypto.rsa.genKey()
const pubKey = await crypto.rsa.exportPublicKey(temporaryRsaPair.publicKey)
const temporaryDID = DID.publicKeyToDid(crypto, pubKey, "rsa")
return { temporaryRsaPair, temporaryDID }
}
/**
* NEGOTIATION
*
* Decrypt the session key and check the closed UCAN for capability.
* The session key is encrypted with the temporary RSA keypair.
* The closed UCAN is encrypted with the session key.
*
* @param temporaryRsaPrivateKey
* @param data
* @returns AES session key
*/
export const handleSessionKey = async (
crypto: Crypto.Implementation,
temporaryRsaPrivateKey: CryptoKey,
data: string
): Promise<Result<Uint8Array, Error>> => {
const typeGuard = (message: unknown): message is { iv: string; msg: string; sessionKey: string } => {
return Check.isObject(message)
&& "iv" in message && typeof message.iv === "string"
&& "msg" in message && typeof message.msg === "string"
&& "sessionKey" in message && typeof message.sessionKey === "string"
}
const parseResult = tryParseMessage(data, typeGuard, { participant: "Consumer", callSite: "handleSessionKey" })
if (parseResult.ok) {
const { iv: encodedIV, msg, sessionKey: encodedSessionKey } = parseResult.value
const iv = Uint8arrays.fromString(encodedIV, "base64pad")
let sessionKey
try {
const encryptedSessionKey = Uint8arrays.fromString(encodedSessionKey, "base64pad")
sessionKey = await crypto.rsa.decrypt(encryptedSessionKey, temporaryRsaPrivateKey)
} catch {
return { ok: false, error: new LinkingWarning(`Consumer received a session key in handleSessionKey that it could not decrypt: ${data}. Ignoring message`) }
}
let encodedUcan = null
try {
encodedUcan = await crypto.aes.decrypt(
Uint8arrays.fromString(msg, "base64pad"),
sessionKey,
Crypto.SymmAlg.AES_GCM,
iv
)
} catch {
return { ok: false, error: new LinkingError("Consumer could not decrypt closed UCAN with provided session key.") }
}
const decodedUcan = Ucan.decode(
Uint8arrays.toString(encodedUcan, "utf8")
)
if (await Ucan.isValid(crypto, decodedUcan) === false) {
return { ok: false, error: new LinkingError("Consumer received an invalid closed UCAN") }
}
if (decodedUcan.payload.ptc) {
return { ok: false, error: new LinkingError("Consumer received a closed UCAN with potency. Closed UCAN must not have potency.") }
}
const sessionKeyFromFact = decodedUcan.payload.fct[ 0 ] && decodedUcan.payload.fct[ 0 ].sessionKey
if (!sessionKeyFromFact) {
return { ok: false, error: new LinkingError("Consumer received a closed UCAN that was missing a session key in facts.") }
}
const sessionKeyWeAlreadyGot = Uint8arrays.toString(sessionKey, "base64pad")
if (sessionKeyFromFact !== sessionKeyWeAlreadyGot) {
return { ok: false, error: new LinkingError("Consumer received a closed UCAN session key does not match the session key") }
}
return { ok: true, value: sessionKey }
} else {
return parseResult
}
}
/**
* NEGOTIATION
*
* Generate pin and challenge message for verification by the producer.
*
* @param sessionKey
* @returns pin and challenge message
*/
export const generateUserChallenge = async (
crypto: Crypto.Implementation,
sessionKey: Uint8Array
): Promise<{ pin: number[]; challenge: string }> => {
const pin = Array.from(crypto.misc.randomNumbers({ amount: 6 })).map(n => n % 9)
const iv = crypto.misc.randomNumbers({ amount: 16 })
const msg = await crypto.aes.encrypt(
Uint8arrays.fromString(
JSON.stringify({
did: await DID.ucan(crypto),
pin
}),
"utf8"
),
sessionKey,
Crypto.SymmAlg.AES_GCM,
iv
)
const challenge = JSON.stringify({
iv: Uint8arrays.toString(iv, "base64pad"),
msg: Uint8arrays.toString(msg, "base64pad")
})
return { pin, challenge }
}
/**
* DELEGATION
*
* Decrypt the delegated credentials and forward to the dependency injected linkDevice function,
* or report that delegation was declined.
*
* @param sessionKey
* @param username
* @param data
* @returns linking result
*/
export const linkDevice = async (
auth: Auth.Implementation<Components>,
crypto: Crypto.Implementation,
sessionKey: Uint8Array,
username: string,
data: string
): Promise<Result<{ approved: boolean }, Error>> => {
const typeGuard = (message: unknown): message is { iv: string; msg: string } => {
return Check.isObject(message)
&& "iv" in message && typeof message.iv === "string"
&& "msg" in message && typeof message.msg === "string"
}
const parseResult = tryParseMessage(data, typeGuard, { participant: "Consumer", callSite: "linkDevice" })
if (parseResult.ok) {
const { iv: encodedIV, msg } = parseResult.value
const iv = Uint8arrays.fromString(encodedIV, "base64")
let message = null
try {
message = await crypto.aes.decrypt(
Uint8arrays.fromString(msg, "base64pad"),
sessionKey,
Crypto.SymmAlg.AES_GCM,
iv
)
} catch {
return { ok: false, error: new LinkingWarning("Consumer ignoring message that could not be decrypted in linkDevice.") }
}
const response = tryParseMessage(
Uint8arrays.toString(message, "utf8"),
Check.isObject,
{ participant: "Consumer", callSite: "linkDevice" }
)
if (!response.ok) {
return response
}
if (response?.value.linkStatus === "DENIED") {
return { ok: true, value: { approved: false } }
}
await auth.linkDevice(username, response.value)
return { ok: true, value: { approved: true } }
} else {
return parseResult
}
}