@dotrino/identity 0.98.0 → 0.99.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dotrino/identity",
3
- "version": "0.98.0",
3
+ "version": "0.99.0",
4
4
  "description": "Identidad y rating de usuarios compartidos entre apps de Dotrino (vault iframe + postMessage)",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/vault/vault.js CHANGED
@@ -415,7 +415,7 @@ import { pubkeyId } from './capabilities.js'
415
415
  }
416
416
  const nombre = String(label || 'equipo prestado')
417
417
  const reg = opaque.registrationStart({ password })
418
- const { response } = d.loginRegisterBegin({ user, request: reg.request })
418
+ const { response } = await d.loginRegisterBegin({ user, request: reg.request })
419
419
  const fin = opaque.registrationFinish({ state: reg.state, response, password })
420
420
  const device = await makeDeviceKey({ label: nombre })
421
421
  const enc = await makeDeviceEncKey()
@@ -440,15 +440,15 @@ import { pubkeyId } from './capabilities.js'
440
440
  throw Object.assign(new Error('the password must be at least 12 characters'), { code: 'weak-password' })
441
441
  }
442
442
  const start = opaque.loginStart({ password: oldPassword })
443
- const begun = d.loginBegin({ user, request: start.request })
443
+ const begun = await d.loginBegin({ user, request: start.request })
444
444
  let fin
445
445
  try { fin = opaque.loginFinish({ state: start.state, response: begun.response, password: oldPassword }) }
446
446
  catch (_) { throw Object.assign(new Error('wrong password'), { code: 'login-failed' }) }
447
- const entered = d.loginEnd({ lid: begun.lid, finalization: fin.finalization, label: 'consola' })
447
+ const entered = await d.loginEnd({ lid: begun.lid, finalization: fin.finalization, label: 'consola' })
448
448
  const keys = await openDeviceKeys(fin.exportKey, entered.blob)
449
449
 
450
450
  const reg = opaque.registrationStart({ password: newPassword })
451
- const { response } = d.loginRegisterBegin({ user, request: reg.request, replace: true })
451
+ const { response } = await d.loginRegisterBegin({ user, request: reg.request, replace: true })
452
452
  const nueva = opaque.registrationFinish({ state: reg.state, response, password: newPassword })
453
453
  await d.loginRegisterFinish({
454
454
  user, upload: nueva.upload, blob: await sealDeviceKeys(nueva.exportKey, keys), replace: true
@@ -1,4 +1,4 @@
1
- Copia vendorizada de @dotrino/vault@0.67.0 (dotrino-vault/lib/src/{index,enroll,protocol,passwordLogins,loginClient,b64}.js).
1
+ Copia vendorizada de @dotrino/vault@0.68.0 (dotrino-vault/lib/src/{index,enroll,protocol,passwordLogins,loginClient,b64}.js).
2
2
  NO se edita a mano: la escribe `node vendor.mjs` y la vigila test/vendor-up-to-date.test.mjs.
3
3
  index.js importa ./enroll.js y ./protocol.js (relativos, van en esta misma copia),
4
4
  @dotrino/identity/{capabilities,acta} (= ../../{capabilities,acta}.js) y
@@ -394,7 +394,7 @@ export async function startDeviceVault (identity, { proxyUrl, client: injectedCl
394
394
  async function handleLoginStart (from, p) {
395
395
  if (!logins) return noLogins(from)
396
396
  try {
397
- const { lid, response } = logins.loginBegin({ user: p?.user, request: p?.request })
397
+ const { lid, response } = await logins.loginBegin({ user: p?.user, request: p?.request })
398
398
  send(from, { type: MSG.LOGIN_RESPONSE, lid, response })
399
399
  } catch (e) { loginError(from, e) }
400
400
  }
@@ -402,7 +402,7 @@ export async function startDeviceVault (identity, { proxyUrl, client: injectedCl
402
402
  async function handleLoginFinish (from, p) {
403
403
  if (!logins) return noLogins(from)
404
404
  try {
405
- const r = logins.loginEnd({ lid: p?.lid, finalization: p?.finalization, label: p?.label })
405
+ const r = await logins.loginEnd({ lid: p?.lid, finalization: p?.finalization, label: p?.label })
406
406
  const acta = (await identity.profileActa?.().catch(() => null))?.acta || null
407
407
  send(from, { type: MSG.LOGIN_OK, sid: r.sid, blob: r.blob, cert: r.cert, iss: r.iss || iss, acta })
408
408
  } catch (e) { loginError(from, e) }
@@ -24,7 +24,7 @@
24
24
  * binario, `lib/src/index.js` en la pestaña—. Lo único que no es puro es `registerLogin`, al
25
25
  * final, porque hace falta la identidad que firma.
26
26
  */
27
- import { server as opaque, suiteId } from '@dotrino/opaque'
27
+ import { server as opaquePorDefecto, suiteId as suitePorDefecto } from '@dotrino/opaque'
28
28
  import { pubkeyId } from '@dotrino/identity/capabilities'
29
29
  import { deviceIdOf, scopeToCaps, scopeToCn } from './enroll.js'
30
30
  import { SCOPE } from './protocol.js'
@@ -181,10 +181,25 @@ export async function openDeviceKeys (exportKey, blob) {
181
181
  * `load()` devuelve el estado guardado (o `null` la primera vez)
182
182
  * `save(s)` lo guarda
183
183
  * `now()` el reloj, para poder probar el freno sin esperar una hora
184
+ * `opaque` la mitad de SERVIDOR de OPAQUE. Por defecto la del paquete, que es WASM y
185
+ * corre en el mismo hilo. Se puede inyectar OTRA porque en una extensión MV3 el WASM
186
+ * vive al otro lado de un `postMessage` —en una página sandbox, para no tener que
187
+ * abrirle la CSP a toda la extensión—, y desde allí solo puede contestar una promesa.
188
+ *
189
+ * **Por eso los métodos que tocan OPAQUE son ASÍNCRONOS**, también con la implementación de
190
+ * casa: una firma que cambia según quién la use son dos mostradores, y de eso va la regla
191
+ * de las tres versiones.
184
192
  */
185
- export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
193
+ export function createLoginDesk ({ load, save, now = () => Date.now(), opaque = opaquePorDefecto } = {}) {
186
194
  if (typeof load !== 'function' || typeof save !== 'function') throw new Error('createLoginDesk: load and save are required')
187
195
 
196
+ /**
197
+ * QUÉ SUITE se está usando, dicha por quien hace el cálculo. Con el OPAQUE de casa es la
198
+ * del paquete; con uno inyectado, la suya — preguntárselo a él es lo único que no miente,
199
+ * porque el registro que queda guardado lo hizo él.
200
+ */
201
+ const suite = () => (typeof opaque.suiteId === 'function' ? opaque.suiteId() : suitePorDefecto())
202
+
188
203
  /** Intercambios a medias: viven en memoria, y por eso un reinicio los tira. */
189
204
  const exchanges = new Map()
190
205
 
@@ -195,9 +210,9 @@ export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
195
210
  }
196
211
 
197
212
  /** La preparación del servidor nace con el primer usuario y no se toca nunca más. */
198
- function setupOf (state) {
213
+ async function setupOf (state) {
199
214
  if (state.setup) return state.setup
200
- state.setup = opaque.createSetup()
215
+ state.setup = await opaque.createSetup()
201
216
  return state.setup
202
217
  }
203
218
 
@@ -234,30 +249,30 @@ export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
234
249
  * ALTA, primer paso. La contraseña no llega hasta aquí: lo que llega es el mensaje de
235
250
  * registro de OPAQUE, que no la lleva ni permite adivinarla.
236
251
  */
237
- registerBegin ({ user, request, replace = false } = {}) {
252
+ async registerBegin ({ user, request, replace = false } = {}) {
238
253
  const state = read()
239
254
  const existing = userOf(state, user)
240
255
  if (existing && !replace) throw err('user-exists', `there is already a login called "${user}"`)
241
256
  if (!existing && replace) throw err('no-user', `there is no login called "${user}"`)
242
257
  if (typeof request !== 'string' || !request) throw err('bad-input', 'request required')
243
- const setup = setupOf(state)
244
- const response = opaque.registrationResponse({ setup, request, credentialId: user })
258
+ const setup = await setupOf(state)
259
+ const response = await opaque.registrationResponse({ setup, request, credentialId: user })
245
260
  save(state)
246
- return { response, suite: suiteId() }
261
+ return { response, suite: await suite() }
247
262
  },
248
263
 
249
264
  /**
250
265
  * ALTA, segundo paso. `blob` son las llaves privadas del aparato, ya cerradas por quien
251
266
  * creó el aparato con la llave que sale de la contraseña: aquí no se abre nunca.
252
267
  */
253
- registerFinish ({ user, upload, pub, encPub = null, deviceId = null, label = '', blob, replace = false } = {}) {
268
+ async registerFinish ({ user, upload, pub, encPub = null, deviceId = null, label = '', blob, replace = false } = {}) {
254
269
  const state = read()
255
270
  const existing = userOf(state, user)
256
271
  if (existing && !replace) throw err('user-exists', `there is already a login called "${user}"`)
257
272
  if (!existing && replace) throw err('no-user', `there is no login called "${user}"`)
258
273
  if (typeof blob !== 'string' || !blob) throw err('bad-input', 'blob required (the device keys, sealed with the password)')
259
274
  if (!replace && (typeof pub !== 'string' || !pub)) throw err('bad-input', 'pub required')
260
- const record = opaque.registrationFinish({ upload })
275
+ const record = await opaque.registrationFinish({ upload })
261
276
  const t = now()
262
277
  state.users[user] = {
263
278
  ...(existing || {}),
@@ -267,7 +282,7 @@ export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
267
282
  label: label || existing?.label || '',
268
283
  record,
269
284
  blob,
270
- suite: suiteId(),
285
+ suite: await suite(),
271
286
  createdAt: existing?.createdAt || t,
272
287
  passwordChangedAt: t,
273
288
  // Cambiar la contraseña cierra lo abierto: si alguien entró con la vieja, deja de
@@ -277,7 +292,7 @@ export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
277
292
  nextTryAt: 0
278
293
  }
279
294
  save(state)
280
- return { user, suite: suiteId() }
295
+ return { user, suite: await suite() }
281
296
  },
282
297
 
283
298
  /** El certificado del aparato, que firma la bóveda al admitirlo en el acta. Es público. */
@@ -298,7 +313,7 @@ export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
298
313
  * registro inventado: desde fuera no se puede averiguar qué usuarios hay. Y cuenta
299
314
  * contra el mismo freno, para que tampoco se pueda medir por el tiempo.
300
315
  */
301
- loginBegin ({ user, request } = {}) {
316
+ async loginBegin ({ user, request } = {}) {
302
317
  sweepExchanges()
303
318
  const state = read()
304
319
  if (typeof request !== 'string' || !request) throw err('bad-input', 'request required')
@@ -314,8 +329,8 @@ export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
314
329
  u.fails = (u.fails || 0) + 1
315
330
  u.nextTryAt = t + backoffFor(u.fails)
316
331
  }
317
- const setup = setupOf(state)
318
- const { state: serverState, response } = opaque.loginStart({
332
+ const setup = await setupOf(state)
333
+ const { state: serverState, response } = await opaque.loginStart({
319
334
  setup,
320
335
  record: u?.record || null,
321
336
  request,
@@ -332,7 +347,7 @@ export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
332
347
  * INICIO DE SESIÓN, segundo paso. Si la contraseña es la buena, devuelve el paquete de
333
348
  * llaves —que solo abre esa contraseña—, el certificado y el acta.
334
349
  */
335
- loginEnd ({ lid, finalization, label = '' } = {}) {
350
+ async loginEnd ({ lid, finalization, label = '' } = {}) {
336
351
  sweepExchanges()
337
352
  const x = exchanges.get(lid)
338
353
  if (!x) throw err('no-exchange', 'that login is not in flight any more: start again')
@@ -341,7 +356,7 @@ export function createLoginDesk ({ load, save, now = () => Date.now() } = {}) {
341
356
  const u = x.known ? state.users[x.user] : null
342
357
 
343
358
  try {
344
- opaque.loginFinish({ state: x.state, finalization })
359
+ await opaque.loginFinish({ state: x.state, finalization })
345
360
  } catch (e) {
346
361
  // Contraseña equivocada, usuario inexistente o mensaje alterado: el MISMO error.
347
362
  // Distinguirlos diría qué usuarios existen. El intento ya está contado desde
@@ -438,7 +453,7 @@ export async function registerLogin ({
438
453
  scope, unattended = false, replace = false
439
454
  } = {}) {
440
455
  if (replace) {
441
- logins.registerFinish({ user, upload, blob, replace: true })
456
+ await logins.registerFinish({ user, upload, blob, replace: true })
442
457
  return { ok: true, user, replaced: true }
443
458
  }
444
459
  // PERMISOS, no tipos: los del scope, más `unattended` si quien lo crea lo eligió
@@ -452,7 +467,7 @@ export async function registerLogin ({
452
467
  throw Object.assign(new Error('this vault cannot add devices to the account record: no login was created'), { code: 'admit-unavailable' })
453
468
  }
454
469
  const deviceId = await deviceIdOf(pub)
455
- logins.registerFinish({ user, upload, pub, encPub, deviceId, label, blob })
470
+ await logins.registerFinish({ user, upload, pub, encPub, deviceId, label, blob })
456
471
  try {
457
472
  const { cert } = await identity.signDelegation(pub, scopes, { label: label || `login:${user}` })
458
473
  const cn = scopeToCn(scopes)