@tdxvolt/volt-client-grpc 0.16.0-beta.1 → 0.16.0-beta.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/lib/index.cjs CHANGED
@@ -589,8 +589,12 @@ class VoltCredential {
589
589
  return this._cryptoCache;
590
590
  }
591
591
 
592
- get clientId() {
593
- return this._cryptoCache.client_id;
592
+ get sessionId() {
593
+ return this._cryptoCache.session_id;
594
+ }
595
+
596
+ get identityId() {
597
+ return this._cryptoCache.identity_id;
594
598
  }
595
599
 
596
600
  get voltPublicKey() {
@@ -678,7 +682,7 @@ class VoltCredential {
678
682
  aud: audience,
679
683
  iat: Math.floor(Date.now() / 1000) - ttl,
680
684
  exp: Math.floor(Date.now() / 1000) + ttl,
681
- sub: this._cryptoCache.client_id
685
+ sub: this._cryptoCache.session_id
682
686
  };
683
687
 
684
688
  let sharedKey;
@@ -1735,7 +1739,8 @@ function issueAuthenticate(authenticateRequest, ttl) {
1735
1739
  this._credential.cache.ca = authenticateResponse.chain;
1736
1740
 
1737
1741
  // Identity resource id is assigned by the volt.
1738
- this._credential.cache.client_id = authenticateResponse.session_id;
1742
+ this._credential.cache.session_id = authenticateResponse.session_id;
1743
+
1739
1744
  this._credential.saveCache();
1740
1745
  break;
1741
1746
  }
@@ -1770,8 +1775,7 @@ function findDIDDocumentService(document, serviceType) {
1770
1775
  return matches;
1771
1776
  }
1772
1777
 
1773
-
1774
- async function authenticateInternal() {
1778
+ async function authenticateInternal(createDID = true) {
1775
1779
  try {
1776
1780
  // Check if we need to resolve a Relay volt.
1777
1781
  if (
@@ -1808,7 +1812,9 @@ async function authenticateInternal() {
1808
1812
  }
1809
1813
 
1810
1814
  const keyPair = this._credential.getKey();
1811
- const publicKeyPem = pki.publicKeyToPem(keyPair.publicKey);
1815
+ const publicKeyPem = removeCarriageReturn(
1816
+ pki.publicKeyToPem(keyPair.publicKey)
1817
+ );
1812
1818
 
1813
1819
  if (!this._credential.cache.bindIp) {
1814
1820
  // Default the IP address if none is specified. This is used to set the SAN in the
@@ -1821,8 +1827,7 @@ async function authenticateInternal() {
1821
1827
  // Form the request message.
1822
1828
  const authenticateRequest = {
1823
1829
  binding_name: this._config.client_name,
1824
- public_key: publicKeyPem,
1825
- host: this._credential.cache.bindIp,
1830
+ host: this._credential.cache.bindIp
1826
1831
  };
1827
1832
 
1828
1833
  if (this._voltConfig.challenge_code) {
@@ -1836,6 +1841,70 @@ async function authenticateInternal() {
1836
1841
  );
1837
1842
  }
1838
1843
 
1844
+ if (!createDID) {
1845
+ authenticateRequest.public_key = publicKeyPem;
1846
+ } else {
1847
+ if (!this._credential.cache.identity_id) {
1848
+ // If we don't have an identity id, then we need to create a DID.
1849
+ this._credential.cache.identity_id = `did:volt:${uuid.v4()}`;
1850
+ }
1851
+
1852
+ const did = this._credential.cache.identity_id;
1853
+
1854
+ // Order of properties in the DID document is important,
1855
+ // they must be in alphabetical order throughout the document.
1856
+ // This is because the DID document is signed and the signature
1857
+ // is over the entire document.
1858
+ const didDocument = {
1859
+ "@context": "https://www.w3.org/ns/did/v1",
1860
+ authentication: [`${did}#key-1`],
1861
+ description: this._config.client_name,
1862
+ id: did,
1863
+ verificationMethod: [
1864
+ {
1865
+ controller: did,
1866
+ id: `${did}#key-1`,
1867
+ publicKeyPem: publicKeyPem,
1868
+ type: "RsaSignature2018"
1869
+ }
1870
+ ]
1871
+ };
1872
+
1873
+ const signDocument = JSON.stringify(didDocument, null, 2);
1874
+ log$1("signing did document");
1875
+ log$1(signDocument);
1876
+
1877
+ // Sign the DID document.
1878
+ const documentDigest = signBase64(
1879
+ keyPair.privateKey,
1880
+ signDocument
1881
+ );
1882
+
1883
+ const proofSignature = jwsSignature(documentDigest);
1884
+
1885
+ // Create the DID document proof.
1886
+ didDocument.proof = {
1887
+ created: new Date().toISOString(),
1888
+ jws: proofSignature,
1889
+ proofPurpose: "assertionMethod",
1890
+ type: "RsaSignature2018",
1891
+ verificationMethod: `${did}#key-1`
1892
+ };
1893
+
1894
+ // Set the DID document with proof in the authenticate request.
1895
+ authenticateRequest.did_document = JSON.stringify(didDocument, null, 2);
1896
+
1897
+ // Now sign the entire DID document, including the proof and set it in the
1898
+ // bind request. This is required by the Volt to verify that we have the private key for the DID.
1899
+ authenticateRequest.did_document_signature = signBase64(
1900
+ keyPair.privateKey,
1901
+ authenticateRequest.did_document
1902
+ );
1903
+
1904
+ log$1("did document is %s", authenticateRequest.did_document);
1905
+ log$1("did signature is %s", authenticateRequest.did_document_signature);
1906
+ }
1907
+
1839
1908
  log$1("authenticate TTL is ", this._config.bindRequestTTL);
1840
1909
 
1841
1910
  // This will block while the request is pending.
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.16.0-beta.1",
6
+ "version": "0.16.0-beta.3",
7
7
  "description": "tdx Volt library for nodejs clients",
8
8
  "type": "module",
9
9
  "exports": {
@@ -55,7 +55,8 @@ function issueAuthenticate(authenticateRequest, ttl) {
55
55
  this._credential.cache.ca = authenticateResponse.chain;
56
56
 
57
57
  // Identity resource id is assigned by the volt.
58
- this._credential.cache.client_id = authenticateResponse.session_id;
58
+ this._credential.cache.session_id = authenticateResponse.session_id;
59
+
59
60
  this._credential.saveCache();
60
61
  break;
61
62
  }
@@ -81,52 +82,6 @@ function issueAuthenticate(authenticateRequest, ttl) {
81
82
  });
82
83
  }
83
84
 
84
- function issueBind(bindRequest, ttl) {
85
- // eslint-disable-next-line no-use-before-define
86
- return unaryCallInternal
87
- .call(this, "Bind", bindRequest)
88
- .then((bindResponse) => {
89
- log("got binding request response %j", bindResponse);
90
- if (bindResponse.status?.code) {
91
- log("error in bind response: %s", bindResponse.status.message);
92
- return Promise.reject(new Error(bindResponse.status.message));
93
- } else {
94
- switch (bindResponse.decision) {
95
- case "POLICY_DECISION_PERMIT": {
96
- //
97
- // The request status is permit => cache the bind response info.
98
- //
99
-
100
- // This is the certificate assigned to us by the volt.
101
- this._credential.cache.cert = bindResponse.cert;
102
-
103
- // This is the signing CA used by the volt.
104
- this._credential.cache.ca = bindResponse.chain;
105
-
106
- // Identity resource id is assigned by the volt.
107
- this._credential.cache.client_id = bindResponse.identity_id;
108
- this._credential.saveCache();
109
- break;
110
- }
111
- case "POLICY_DECISION_DENY": {
112
- log(">>>>>>>>>>>>>>> access request is DENIED <<<<<<<<<<<<<<<<<<<");
113
- break;
114
- }
115
- case "POLICY_DECISION_PROMPT":
116
- case "POLICY_DECISION_PENDING": {
117
- log("access pending approval - waiting...");
118
- break;
119
- }
120
- default:
121
- log("ignoring unknown bind descision %s", bindResponse.status);
122
- break;
123
- }
124
- }
125
-
126
- return bindResponse.decision;
127
- });
128
- }
129
-
130
85
  function findDIDDocumentService(document, serviceType) {
131
86
  if (!document.service) {
132
87
  return [];
@@ -136,8 +91,7 @@ function findDIDDocumentService(document, serviceType) {
136
91
  return matches;
137
92
  }
138
93
 
139
-
140
- export async function authenticateInternal() {
94
+ export async function authenticateInternal(createDID = true) {
141
95
  try {
142
96
  // Check if we need to resolve a Relay volt.
143
97
  if (
@@ -174,7 +128,9 @@ export async function authenticateInternal() {
174
128
  }
175
129
 
176
130
  const keyPair = this._credential.getKey();
177
- const publicKeyPem = pki.publicKeyToPem(keyPair.publicKey);
131
+ const publicKeyPem = voltUtils.removeCarriageReturn(
132
+ pki.publicKeyToPem(keyPair.publicKey)
133
+ );
178
134
 
179
135
  if (!this._credential.cache.bindIp) {
180
136
  // Default the IP address if none is specified. This is used to set the SAN in the
@@ -187,8 +143,7 @@ export async function authenticateInternal() {
187
143
  // Form the request message.
188
144
  const authenticateRequest = {
189
145
  binding_name: this._config.client_name,
190
- public_key: publicKeyPem,
191
- host: this._credential.cache.bindIp,
146
+ host: this._credential.cache.bindIp
192
147
  };
193
148
 
194
149
  if (this._voltConfig.challenge_code) {
@@ -202,161 +157,71 @@ export async function authenticateInternal() {
202
157
  );
203
158
  }
204
159
 
205
- log("authenticate TTL is ", this._config.bindRequestTTL);
206
-
207
- // This will block while the request is pending.
208
- const pollInterval = 10000;
209
- let decision = "";
210
- do {
211
- if (decision) {
212
- // Wait a while before retrying.
213
- // eslint-disable-next-line no-await-in-loop
214
- await new Promise((resolve) => setTimeout(resolve, pollInterval));
160
+ if (!createDID) {
161
+ authenticateRequest.public_key = publicKeyPem;
162
+ } else {
163
+ if (!this._credential.cache.identity_id) {
164
+ // If we don't have an identity id, then we need to create a DID.
165
+ this._credential.cache.identity_id = `did:volt:${generateId()}`;
215
166
  }
216
- // eslint-disable-next-line no-await-in-loop
217
- decision = await issueAuthenticate.call(
218
- this,
219
- authenticateRequest,
220
- this._voltConfig.bindRequestTTL
221
- );
222
- log("authenticate decision: %s", decision);
223
- } while (
224
- decision === "POLICY_DECISION_PROMPT" ||
225
- decision === "POLICY_DECISION_PENDING"
226
- );
227
167
 
228
- return decision === "POLICY_DECISION_PERMIT";
229
- } catch (err) {
230
- log("failure binding to Volt [%s]", err.message);
231
- throw err;
232
- }
233
- }
234
-
235
- export async function bindInternal() {
236
- try {
237
- // Check if we need to resolve a Relay volt.
238
- if (
239
- typeof this._voltConfig?.relay === "string" ||
240
- this._voltConfig?.relay?.http_address
241
- ) {
242
- const relayURL =
243
- this._voltConfig.relay?.http_address || this._voltConfig.relay;
244
- log("attempting to retrieve Relay information from %s", relayURL);
245
- this._voltConfig.relay = await fetchVoltConfig.call(
246
- this,
247
- `${relayURL}/discovery`
248
- );
249
- if (this._voltConfig.relay.ca_pem) {
250
- log(
251
- "Auto-fetched Relay CA %s, remote address %s",
252
- this._voltConfig.relay.ca_pem,
253
- this._voltConfig.relay.address
254
- );
255
- } else {
256
- throw new Error("Unable to fetch Relay CA - cannot securely connect.");
257
- }
258
- }
168
+ const did = this._credential.cache.identity_id;
169
+
170
+ // Order of properties in the DID document is important,
171
+ // they must be in alphabetical order throughout the document.
172
+ // This is because the DID document is signed and the signature
173
+ // is over the entire document.
174
+ const didDocument = {
175
+ "@context": "https://www.w3.org/ns/did/v1",
176
+ authentication: [`${did}#key-1`],
177
+ description: this._config.client_name,
178
+ id: did,
179
+ verificationMethod: [
180
+ {
181
+ controller: did,
182
+ id: `${did}#key-1`,
183
+ publicKeyPem: publicKeyPem,
184
+ type: "RsaSignature2018"
185
+ }
186
+ ]
187
+ };
259
188
 
260
- if (!this._credential.cache.ca) {
261
- this._credential.cache.ca = this._voltConfig.ca_pem;
262
- }
189
+ const signDocument = JSON.stringify(didDocument, null, 2);
190
+ log("signing did document");
191
+ log(signDocument);
263
192
 
264
- if (!this._credential.cache.ca) {
265
- throw new Error(
266
- "No certificate authority found in configuration for target Volt - check configuration"
193
+ // Sign the DID document.
194
+ const documentDigest = voltUtils.signBase64(
195
+ keyPair.privateKey,
196
+ signDocument
267
197
  );
268
- }
269
198
 
270
- const keyPair = this._credential.getKey();
271
- const publicKeyPem = voltUtils.removeCarriageReturn(
272
- pki.publicKeyToPem(keyPair.publicKey)
273
- );
199
+ const proofSignature = voltUtils.jwsSignature(documentDigest);
274
200
 
275
- if (!this._credential.cache.bindIp) {
276
- // Default the IP address if none is specified. This is used to set the SAN in the
277
- // certificate issued by the Volt.
278
- this._credential.cache.bindIp = ip.address();
279
- }
201
+ // Create the DID document proof.
202
+ didDocument.proof = {
203
+ created: new Date().toISOString(),
204
+ jws: proofSignature,
205
+ proofPurpose: "assertionMethod",
206
+ type: "RsaSignature2018",
207
+ verificationMethod: `${did}#key-1`
208
+ };
280
209
 
281
- this._credential.saveCache();
210
+ // Set the DID document with proof in the authenticate request.
211
+ authenticateRequest.did_document = JSON.stringify(didDocument, null, 2);
282
212
 
283
- // Form the request message.
284
- const bindRequest = {
285
- binding_name: this._config.client_name,
286
- host: this._credential.cache.bindIp
287
- };
288
-
289
- if (this._voltConfig.challenge_code) {
290
- bindRequest.challenge = voltUtils.signBase64(
213
+ // Now sign the entire DID document, including the proof and set it in the
214
+ // bind request. This is required by the Volt to verify that we have the private key for the DID.
215
+ authenticateRequest.did_document_signature = voltUtils.signBase64(
291
216
  keyPair.privateKey,
292
- this._voltConfig.challenge_code
293
- );
294
- } else {
295
- log(
296
- "****no Volt challenge code available**** => not sending challenge signature"
217
+ authenticateRequest.did_document
297
218
  );
298
- }
299
219
 
300
- if (!this._credential.cache.client_id) {
301
- // If we don't have a client id, then we need to create a DID.
302
- this._credential.cache.client_id = `did:volt:${generateId()}`;
220
+ log("did document is %s", authenticateRequest.did_document);
221
+ log("did signature is %s", authenticateRequest.did_document_signature);
303
222
  }
304
223
 
305
- const did = this._credential.cache.client_id;
306
-
307
- // Order of properties in the DID document is important,
308
- // they must be in alphabetical order throughout the document.
309
- // This is because the DID document is signed and the signature
310
- // is over the entire document.
311
- const didDocument = {
312
- "@context": "https://www.w3.org/ns/did/v1",
313
- authentication: [`${did}#key-1`],
314
- description: this._config.client_name,
315
- id: did,
316
- verificationMethod: [
317
- {
318
- controller: did,
319
- id: `${did}#key-1`,
320
- publicKeyPem: publicKeyPem,
321
- type: "RsaSignature2018"
322
- }
323
- ]
324
- };
325
-
326
- const signDocument = JSON.stringify(didDocument, null, 2);
327
- log("signing did document");
328
- log(signDocument);
329
-
330
- // Sign the DID document.
331
- const documentDigest = voltUtils.signBase64(
332
- keyPair.privateKey,
333
- signDocument
334
- );
335
-
336
- const proofSignature = voltUtils.jwsSignature(documentDigest);
337
-
338
- // Create the DID document proof.
339
- didDocument.proof = {
340
- created: new Date().toISOString(),
341
- jws: proofSignature,
342
- proofPurpose: "assertionMethod",
343
- type: "RsaSignature2018",
344
- verificationMethod: `${did}#key-1`
345
- };
346
-
347
- // Set the DID document with proof in the bind request.
348
- bindRequest.did_document = JSON.stringify(didDocument, null, 2);
349
-
350
- // Now sign the entire DID document, including the proof and set it in the
351
- // bind request. This is required by the Volt to verify that we have the private key for the DID.
352
- bindRequest.did_document_signature = voltUtils.signBase64(
353
- keyPair.privateKey,
354
- bindRequest.did_document
355
- );
356
-
357
- log("did document is %s", bindRequest.did_document);
358
- log("did signature is %s", bindRequest.did_document_signature);
359
- log("bind TTL is ", this._config.bindRequestTTL);
224
+ log("authenticate TTL is ", this._config.bindRequestTTL);
360
225
 
361
226
  // This will block while the request is pending.
362
227
  const pollInterval = 10000;
@@ -368,12 +233,12 @@ export async function bindInternal() {
368
233
  await new Promise((resolve) => setTimeout(resolve, pollInterval));
369
234
  }
370
235
  // eslint-disable-next-line no-await-in-loop
371
- decision = await issueBind.call(
236
+ decision = await issueAuthenticate.call(
372
237
  this,
373
- bindRequest,
238
+ authenticateRequest,
374
239
  this._voltConfig.bindRequestTTL
375
240
  );
376
- log("binding decision: %s", decision);
241
+ log("authenticate decision: %s", decision);
377
242
  } while (
378
243
  decision === "POLICY_DECISION_PROMPT" ||
379
244
  decision === "POLICY_DECISION_PENDING"
@@ -6,7 +6,6 @@ import { getServiceDescriptors } from "./proto-utils.js";
6
6
  import { createClient as createGrpcClient } from "./grpc-utils.js";
7
7
  import {
8
8
  authenticateInternal,
9
- bindInternal,
10
9
  connectInternal,
11
10
  fetchVoltConfig,
12
11
  fetchVoltConfigFromDID,
@@ -69,8 +69,12 @@ export class VoltCredential {
69
69
  return this._cryptoCache;
70
70
  }
71
71
 
72
- get clientId() {
73
- return this._cryptoCache.client_id;
72
+ get sessionId() {
73
+ return this._cryptoCache.session_id;
74
+ }
75
+
76
+ get identityId() {
77
+ return this._cryptoCache.identity_id;
74
78
  }
75
79
 
76
80
  get voltPublicKey() {
@@ -159,7 +163,7 @@ export class VoltCredential {
159
163
  aud: audience,
160
164
  iat: Math.floor(Date.now() / 1000) - ttl,
161
165
  exp: Math.floor(Date.now() / 1000) + ttl,
162
- sub: this._cryptoCache.client_id
166
+ sub: this._cryptoCache.session_id
163
167
  };
164
168
 
165
169
  let sharedKey;