sliftutils 1.7.142 → 1.7.143

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": "sliftutils",
3
- "version": "1.7.142",
3
+ "version": "1.7.143",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "files": [
@@ -24,7 +24,7 @@ export type SignedRequest<T> = {
24
24
  };
25
25
  };
26
26
 
27
- export type SignedReply<T> = {
27
+ export type SignedReply = {
28
28
  signature: string;
29
29
  payload: {
30
30
  time: number;
@@ -33,7 +33,6 @@ export type SignedReply<T> = {
33
33
  request: Omit<SignedRequest<unknown>["payload"], "data"> & { dataHash: string };
34
34
  cert: string;
35
35
  certIssuer: string;
36
- data: T;
37
36
  };
38
37
  };
39
38
 
@@ -54,16 +53,20 @@ export function signRequest<T>(domain: string, config: { targetId: string; data:
54
53
  return { signature: sign(threadKeyCert, payload), payload };
55
54
  }
56
55
 
57
- /** Returns the machineId that signed, the data, and reply - which signs a reply to exactly this
58
- request, so a reply cannot be produced for a request that was never verified. Throws if the
59
- signature, certificate chain, or time do not check out, or if the request was addressed to
60
- someone we are not: ownIdentities is every name this server answers to - its IPs (there are
61
- always several, internal and external), its machineId, its threadId - and the signed targetId
62
- must be one of them. */
56
+ /** Returns the machineId that signed, the data, and the signed reply to send back - always
57
+ produced, so the caller's only job is to return it. Throws if the signature, certificate
58
+ chain, or time do not check out, or if the request was addressed to someone we are not:
59
+ ownIdentities is every name this server answers to - its IPs (there are always several,
60
+ internal and external), its machineId, its threadId - and the signed targetId must be one of
61
+ them.
62
+
63
+ IMPORTANT! You still need to check the machine with isMachineAccepted if you want to know if
64
+ the machine is trusted. We just verify that they are who they say they are, not that who they
65
+ say they are is allowed. */
63
66
  export function verifyRequest<T>(domain: string, signed: SignedRequest<T>, ownIdentities: string[]): {
64
67
  machineId: string;
65
68
  data: T;
66
- reply<R>(data: R): SignedReply<R>;
69
+ reply: SignedReply;
67
70
  } {
68
71
  let { signature, payload } = signed;
69
72
  let signedThreshold = Date.now() - MAX_SIGNED_AGE;
@@ -76,30 +79,27 @@ export function verifyRequest<T>(domain: string, signed: SignedRequest<T>, ownId
76
79
  throw new Error(`Request is for someone else. It is addressed to ${payload.targetId}, we are ${ownIdentities.join(", ")}`);
77
80
  }
78
81
  let machineId = getMachineId(getCommonName(payload.certIssuer), domain);
82
+ let threadKeyCert = getThreadKeyCert(domain);
83
+ let issuer = getIdentityCA(domain);
84
+ let { data: requestData, ...requestRest } = payload;
85
+ let replyPayload: SignedReply["payload"] = {
86
+ time: Date.now(),
87
+ request: { ...requestRest, dataHash: dataHash(requestData) },
88
+ cert: threadKeyCert.cert.toString(),
89
+ certIssuer: issuer.cert.toString(),
90
+ };
79
91
  return {
80
92
  machineId,
81
93
  data: payload.data,
82
- reply<R>(data: R): SignedReply<R> {
83
- let threadKeyCert = getThreadKeyCert(domain);
84
- let issuer = getIdentityCA(domain);
85
- let { data: requestData, ...requestRest } = payload;
86
- let replyPayload: SignedReply<R>["payload"] = {
87
- time: Date.now(),
88
- request: { ...requestRest, dataHash: dataHash(requestData) },
89
- cert: threadKeyCert.cert.toString(),
90
- certIssuer: issuer.cert.toString(),
91
- data,
92
- };
93
- return { signature: sign(threadKeyCert, replyPayload), payload: replyPayload };
94
- },
94
+ reply: { signature: sign(threadKeyCert, replyPayload), payload: replyPayload },
95
95
  };
96
96
  }
97
97
 
98
- /** Returns the machineId that replied, and the data. Throws if the signature, certificate chain,
99
- or time do not check out, or if the reply does not answer the request we actually sent. On
100
- node the replying machine must also be one of the trusted machines - the browser has no
101
- machine list, so there this check is skipped. */
102
- export async function verifyReply<T>(domain: string, request: SignedRequest<unknown>, reply: SignedReply<T>): Promise<{ machineId: string; data: T }> {
98
+ /** Returns the machineId that replied. Throws if the signature, certificate chain, or time do
99
+ not check out, or if the reply does not answer the request we actually sent. On node the
100
+ replying machine must also be one of the trusted machines - the browser has no machine list,
101
+ so there this check is skipped. */
102
+ export async function verifyReply(domain: string, request: SignedRequest<unknown>, reply: SignedReply): Promise<{ machineId: string }> {
103
103
  let { signature, payload } = reply;
104
104
  let signedThreshold = Date.now() - MAX_SIGNED_AGE;
105
105
  if (payload.time < signedThreshold) {
@@ -118,5 +118,5 @@ export async function verifyReply<T>(domain: string, request: SignedRequest<unkn
118
118
  throw new Error(`Reply is signed by ${machineId}, which is not a trusted machine`);
119
119
  }
120
120
  }
121
- return { machineId, data: payload.data };
121
+ return { machineId };
122
122
  }