@dotrino/identity 0.136.2 → 0.136.3

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.136.2",
3
+ "version": "0.136.3",
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",
@@ -1,4 +1,4 @@
1
- Copia vendorizada de @dotrino/opaque@0.1.0 (dotrino-opaque/{build/opaque,build/wasm-bytes,src/index}.js).
1
+ Copia vendorizada de @dotrino/opaque@0.1.1 (dotrino-opaque/{build/opaque,build/wasm-bytes,src/index}.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
  src/index.js importa ../build/{opaque,wasm-bytes}.js, así que la copia CONSERVA
4
4
  esas dos carpetas: aplanarla rompería el import relativo.
@@ -20,7 +20,23 @@
20
20
  import * as wasm from '../build/opaque.js'
21
21
  import wasmBytes from '../build/wasm-bytes.js'
22
22
 
23
+ /**
24
+ * @typedef {'bad-input'|'login-failed'|'protocol'|'internal'} OpaqueErrorCode
25
+ *
26
+ * @typedef {object} Identifiers bound to the exchange; both ends must pass the same ones
27
+ * @property {string} [client]
28
+ * @property {string} [server]
29
+ *
30
+ * Every field below is base64url. Fields marked optional are still required at run time:
31
+ * a missing one throws `bad-input`, which is why they are typed as optional here.
32
+ */
33
+
34
+ /** An error with a stable `code` (see the list at the top of this file). */
23
35
  export class OpaqueError extends Error {
36
+ /**
37
+ * @param {OpaqueErrorCode} code
38
+ * @param {string} message
39
+ */
24
40
  constructor (code, message) {
25
41
  super(message)
26
42
  this.name = 'OpaqueError'
@@ -38,18 +54,30 @@ function init () {
38
54
 
39
55
  const CODE = /^(bad-input|login-failed|protocol): ([\s\S]*)$/
40
56
 
57
+ /**
58
+ * @template {unknown[]} A
59
+ * @template R
60
+ * @param {(...args: A) => R} fn
61
+ * @param {A} args
62
+ * @returns {R}
63
+ */
41
64
  function call (fn, ...args) {
42
65
  init()
43
66
  try {
44
67
  return fn(...args)
45
68
  } catch (e) {
46
- const msg = String(e?.message ?? e)
69
+ const msg = String(/** @type {{ message?: unknown } | null | undefined} */ (e)?.message ?? e)
47
70
  const m = CODE.exec(msg)
48
- if (m) throw new OpaqueError(m[1], m[2])
71
+ if (m) throw new OpaqueError(/** @type {OpaqueErrorCode} */ (m[1]), m[2])
49
72
  throw new OpaqueError('internal', msg)
50
73
  }
51
74
  }
52
75
 
76
+ /**
77
+ * @param {string} name
78
+ * @param {unknown} v
79
+ * @returns {string}
80
+ */
53
81
  function need (name, v) {
54
82
  if (typeof v !== 'string' || !v) throw new OpaqueError('bad-input', `${name} is required`)
55
83
  return v
@@ -58,6 +86,9 @@ function need (name, v) {
58
86
  /**
59
87
  * Los identificadores que se atan al intercambio. Si se usan, las DOS puntas tienen que
60
88
  * pasar los mismos; si no casan, el inicio falla como una contraseña equivocada.
89
+ *
90
+ * @param {Identifiers} identifiers
91
+ * @returns {[string|undefined, string|undefined]}
61
92
  */
62
93
  function idents (identifiers) {
63
94
  if (identifiers == null || typeof identifiers !== 'object') throw new OpaqueError('bad-input', 'identifiers must be an object')
@@ -68,55 +99,101 @@ function idents (identifiers) {
68
99
  return [client, server]
69
100
  }
70
101
 
71
- /** La suite y sus parámetros. Se guarda junto a cada registro. */
102
+ /**
103
+ * La suite y sus parámetros. Se guarda junto a cada registro.
104
+ *
105
+ * @returns {string}
106
+ */
72
107
  export function suiteId () {
73
108
  return call(wasm.suiteId)
74
109
  }
75
110
 
76
111
  export const server = {
77
- /** La preparación del servidor: SECRETA, una por bóveda. Sin ella no se puede comprobar nada. */
112
+ /**
113
+ * La preparación del servidor: SECRETA, una por bóveda. Sin ella no se puede comprobar nada.
114
+ *
115
+ * @returns {string}
116
+ */
78
117
  createSetup () {
79
118
  return call(wasm.serverCreateSetup)
80
119
  },
120
+ /**
121
+ * @param {{ setup?: string }} [args]
122
+ * @returns {string}
123
+ */
81
124
  publicKey ({ setup } = {}) {
82
125
  return call(wasm.serverPublicKey, need('setup', setup))
83
126
  },
127
+ /**
128
+ * @param {{ setup?: string, request?: string, credentialId?: string }} [args]
129
+ * @returns {string} the response for the client
130
+ */
84
131
  registrationResponse ({ setup, request, credentialId } = {}) {
85
132
  return call(wasm.serverRegistrationResponse, need('setup', setup), need('request', request), need('credentialId', credentialId))
86
133
  },
87
- /** Lo que se guarda del usuario (el «registro»). */
134
+ /**
135
+ * Lo que se guarda del usuario (el «registro»).
136
+ *
137
+ * @param {{ upload?: string }} [args]
138
+ * @returns {string} the record
139
+ */
88
140
  registrationFinish ({ upload } = {}) {
89
141
  return call(wasm.serverRegistrationFinish, need('upload', upload))
90
142
  },
91
143
  /**
92
144
  * `record` tiene que venir SIEMPRE: el registro, o `null` si el usuario no existe. Con
93
145
  * `null` responde igual, así que desde fuera no se sabe qué usuarios hay.
146
+ *
147
+ * @param {{ setup?: string, record?: string|null, request?: string, credentialId?: string, identifiers?: Identifiers }} [args]
148
+ * @returns {{ state: string, response: string }}
94
149
  */
95
150
  loginStart ({ setup, record, request, credentialId, identifiers = {} } = {}) {
96
151
  if (record !== null && (typeof record !== 'string' || !record)) {
97
152
  throw new OpaqueError('bad-input', 'record must be the stored record, or null for an unknown user')
98
153
  }
99
- return call(wasm.serverLoginStart, need('setup', setup), record ?? undefined, need('request', request), need('credentialId', credentialId), ...idents(identifiers))
154
+ return /** @type {{ state: string, response: string }} */ (call(wasm.serverLoginStart, need('setup', setup), record ?? undefined, need('request', request), need('credentialId', credentialId), ...idents(identifiers)))
100
155
  },
156
+ /**
157
+ * @param {{ state?: string, finalization?: string, identifiers?: Identifiers }} [args]
158
+ * @returns {{ sessionKey: string }}
159
+ */
101
160
  loginFinish ({ state, finalization, identifiers = {} } = {}) {
102
- return call(wasm.serverLoginFinish, need('state', state), need('finalization', finalization), ...idents(identifiers))
161
+ return /** @type {{ sessionKey: string }} */ (call(wasm.serverLoginFinish, need('state', state), need('finalization', finalization), ...idents(identifiers)))
103
162
  },
104
163
  }
105
164
 
106
165
  export const client = {
166
+ /**
167
+ * @param {{ password?: string }} [args]
168
+ * @returns {{ state: string, request: string }}
169
+ */
107
170
  registrationStart ({ password } = {}) {
108
- return call(wasm.clientRegistrationStart, need('password', password))
171
+ return /** @type {{ state: string, request: string }} */ (call(wasm.clientRegistrationStart, need('password', password)))
109
172
  },
110
- /** Devuelve `upload` (para el servidor) y `exportKey`, que solo sale de la contraseña. */
173
+ /**
174
+ * Devuelve `upload` (para el servidor) y `exportKey`, que solo sale de la contraseña.
175
+ *
176
+ * @param {{ state?: string, response?: string, password?: string, identifiers?: Identifiers }} [args]
177
+ * @returns {{ upload: string, exportKey: string, serverPublicKey: string }}
178
+ */
111
179
  registrationFinish ({ state, response, password, identifiers = {} } = {}) {
112
- return call(wasm.clientRegistrationFinish, need('state', state), need('response', response), need('password', password), ...idents(identifiers))
180
+ return /** @type {{ upload: string, exportKey: string, serverPublicKey: string }} */ (call(wasm.clientRegistrationFinish, need('state', state), need('response', response), need('password', password), ...idents(identifiers)))
113
181
  },
182
+ /**
183
+ * @param {{ password?: string }} [args]
184
+ * @returns {{ state: string, request: string }}
185
+ */
114
186
  loginStart ({ password } = {}) {
115
- return call(wasm.clientLoginStart, need('password', password))
187
+ return /** @type {{ state: string, request: string }} */ (call(wasm.clientLoginStart, need('password', password)))
116
188
  },
117
- /** Devuelve `finalization` (para el servidor), `sessionKey` y el mismo `exportKey` del registro. */
189
+ /**
190
+ * Devuelve `finalization` (para el servidor), `sessionKey` y el mismo `exportKey` del registro.
191
+ *
192
+ * @param {{ state?: string, response?: string, password?: string, identifiers?: Identifiers }} [args]
193
+ * @returns {{ finalization: string, sessionKey: string, exportKey: string, serverPublicKey: string }}
194
+ */
118
195
  loginFinish ({ state, response, password, identifiers = {} } = {}) {
119
- return call(wasm.clientLoginFinish, need('state', state), need('response', response), need('password', password), ...idents(identifiers))
196
+ return /** @type {{ finalization: string, sessionKey: string, exportKey: string, serverPublicKey: string }} */ (call(wasm.clientLoginFinish, need('state', state), need('response', response), need('password', password), ...idents(identifiers)))
120
197
  },
121
198
  }
122
199
 
@@ -1,4 +1,4 @@
1
- Copia vendorizada de @dotrino/proxy-client@0.47.0 (dotrino-proxy-client/src/{binpack,ble/manager,ble/wire,canonical,client,encpub,errors,index,parts,sealing,signature,stats-log,stats,webrtc}.js).
1
+ Copia vendorizada de @dotrino/proxy-client@0.47.2 (dotrino-proxy-client/src/{binpack,ble/manager,ble/wire,canonical,client,encpub,errors,index,parts,sealing,signature,stats-log,stats,webrtc}.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
  sealing.js resuelve @dotrino/identity/content de forma PEREZOSA (= ../../content.js
4
4
  por el import map): solo se carga si de verdad se sella algo.
@@ -32,7 +32,11 @@ const FRAME_VERSION = 0x01
32
32
  const enc = new TextEncoder()
33
33
  const dec = new TextDecoder()
34
34
 
35
- /** Bytes, sin importar de qué vista vengan (`Buffer`, `ArrayBuffer`, `Uint8Array`). */
35
+ /**
36
+ * Bytes, sin importar de qué vista vengan (`Buffer`, `ArrayBuffer`, `Uint8Array`).
37
+ * @param {unknown} v
38
+ * @returns {Uint8Array | null}
39
+ */
36
40
  export function asBytes (v) {
37
41
  if (v instanceof Uint8Array) return v
38
42
  if (v instanceof ArrayBuffer) return new Uint8Array(v)
@@ -40,7 +44,11 @@ export function asBytes (v) {
40
44
  return null
41
45
  }
42
46
 
43
- /** ¿Lleva bytes este valor, a cualquier profundidad? Es lo que decide texto o binario. */
47
+ /**
48
+ * ¿Lleva bytes este valor, a cualquier profundidad? Es lo que decide texto o binario.
49
+ * @param {any} v
50
+ * @returns {boolean}
51
+ */
44
52
  export function hasBinary (v) {
45
53
  if (v instanceof Uint8Array || v instanceof ArrayBuffer) return true
46
54
  if (Array.isArray(v)) return v.some(hasBinary)
@@ -50,11 +58,16 @@ export function hasBinary (v) {
50
58
  return false
51
59
  }
52
60
 
53
- /** Un objeto con bytes dentro → un solo `Uint8Array`. */
61
+ /**
62
+ * Un objeto con bytes dentro → un solo `Uint8Array`.
63
+ * @param {unknown} obj
64
+ * @returns {Uint8Array}
65
+ */
54
66
  export function pack (obj) {
67
+ /** @type {Uint8Array[]} */
55
68
  const blobs = []
56
69
  let off = 0
57
- const json = JSON.stringify(obj, function (k, v) {
70
+ const json = JSON.stringify(obj, /** @this {any} @param {string} k @param {unknown} v */ function (k, v) {
58
71
  // `this[k]` es el valor antes de `toJSON`: un Buffer de Node se convertiría en
59
72
  // `{ type: 'Buffer', data: [...] }` y aquí lo queremos como bytes.
60
73
  const b = asBytes(this[k])
@@ -74,11 +87,14 @@ export function pack (obj) {
74
87
  return out
75
88
  }
76
89
 
90
+ /** @param {unknown} b @returns {b is Uint8Array} */
77
91
  export const isPacked = (b) => b instanceof Uint8Array && b.length >= 5 && b[0] === PACK_MAGIC
78
92
 
79
93
  /**
80
94
  * Un paquete → el objeto, con sus bytes como `Uint8Array` (vistas sobre `bytes`, sin copiar).
81
95
  * Lanza con `code: 'bad-pack'` si no es un paquete o una referencia se sale de él.
96
+ * @param {Uint8Array | ArrayBuffer | ArrayBufferView} bytes
97
+ * @returns {any}
82
98
  */
83
99
  export function unpack (bytes) {
84
100
  const b = asBytes(bytes)
@@ -88,6 +104,7 @@ export function unpack (bytes) {
88
104
  const area = b.subarray(5 + len)
89
105
  let obj
90
106
  try { obj = JSON.parse(dec.decode(b.subarray(5, 5 + len))) } catch (_) { throw errorWithCode('packed message header is not JSON', 'bad-pack') }
107
+ /** @param {any} v @returns {any} */
91
108
  const revive = (v) => {
92
109
  if (Array.isArray(v)) return v.map(revive)
93
110
  if (!v || typeof v !== 'object') return v
@@ -99,6 +116,7 @@ export function unpack (bytes) {
99
116
  }
100
117
  return area.subarray(ref[0], ref[0] + ref[1])
101
118
  }
119
+ /** @type {Record<string, unknown>} */
102
120
  const out = {}
103
121
  for (const k of Object.keys(v)) out[k] = revive(v[k])
104
122
  return out
@@ -106,7 +124,11 @@ export function unpack (bytes) {
106
124
  return revive(obj)
107
125
  }
108
126
 
109
- /** Une varios trozos de bytes en uno. */
127
+ /**
128
+ * Une varios trozos de bytes en uno.
129
+ * @param {Uint8Array[]} list
130
+ * @returns {Uint8Array}
131
+ */
110
132
  export function concatBytes (list) {
111
133
  const out = new Uint8Array(list.reduce((a, b) => a + b.byteLength, 0))
112
134
  let p = 0
@@ -114,7 +136,12 @@ export function concatBytes (list) {
114
136
  return out
115
137
  }
116
138
 
117
- /** La trama binaria del proxio: la cabecera de texto de siempre, y el `message` en crudo. */
139
+ /**
140
+ * La trama binaria del proxio: la cabecera de texto de siempre, y el `message` en crudo.
141
+ * @param {Record<string, unknown>} header
142
+ * @param {Uint8Array} body
143
+ * @returns {Uint8Array}
144
+ */
118
145
  export function encodeFrame (header, body) {
119
146
  const head = enc.encode(JSON.stringify(header))
120
147
  const out = new Uint8Array(5 + head.length + body.byteLength)
@@ -125,7 +152,11 @@ export function encodeFrame (header, body) {
125
152
  return out
126
153
  }
127
154
 
128
- /** El inverso: `{ header, body }`. Lanza con `code: 'bad-frame'` si no es una trama válida. */
155
+ /**
156
+ * El inverso: `{ header, body }`. Lanza con `code: 'bad-frame'` si no es una trama válida.
157
+ * @param {unknown} data
158
+ * @returns {{ header: Record<string, any>, body: Uint8Array }}
159
+ */
129
160
  export function decodeFrame (data) {
130
161
  const b = asBytes(data)
131
162
  if (!b || b.length < 5 || b[0] !== FRAME_VERSION) throw errorWithCode('not a binary frame of a known version', 'bad-frame')