@keetanetwork/anchor 0.0.112 → 0.0.114
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/http-server/common.d.ts +3 -2
- package/lib/http-server/common.d.ts.map +1 -1
- package/lib/http-server/common.js +9 -13
- package/lib/http-server/common.js.map +1 -1
- package/lib/http-server/index.d.ts +31 -1
- package/lib/http-server/index.d.ts.map +1 -1
- package/lib/http-server/index.js +93 -36
- package/lib/http-server/index.js.map +1 -1
- package/lib/http-server/replay-guard-postgres.d.ts +41 -0
- package/lib/http-server/replay-guard-postgres.d.ts.map +1 -0
- package/lib/http-server/replay-guard-postgres.js +113 -0
- package/lib/http-server/replay-guard-postgres.js.map +1 -0
- package/lib/http-server/replay-guard-redis.d.ts +29 -0
- package/lib/http-server/replay-guard-redis.d.ts.map +1 -0
- package/lib/http-server/replay-guard-redis.js +23 -0
- package/lib/http-server/replay-guard-redis.js.map +1 -0
- package/lib/http-server/replay-guard.d.ts +29 -0
- package/lib/http-server/replay-guard.d.ts.map +1 -0
- package/lib/http-server/replay-guard.js +58 -0
- package/lib/http-server/replay-guard.js.map +1 -0
- package/lib/http-server/request-signing.d.ts +70 -0
- package/lib/http-server/request-signing.d.ts.map +1 -0
- package/lib/http-server/request-signing.js +179 -0
- package/lib/http-server/request-signing.js.map +1 -0
- package/lib/utils/signing.d.ts +6 -1
- package/lib/utils/signing.d.ts.map +1 -1
- package/lib/utils/signing.js +11 -6
- package/lib/utils/signing.js.map +1 -1
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/services/asset-movement/client.d.ts +5 -4
- package/services/asset-movement/client.d.ts.map +1 -1
- package/services/asset-movement/client.js +27 -13
- package/services/asset-movement/client.js.map +1 -1
- package/services/asset-movement/common.generated.js +1 -1
- package/services/asset-movement/common.server.generated.js +50 -50
- package/services/asset-movement/server.d.ts.map +1 -1
- package/services/asset-movement/server.js +22 -4
- package/services/asset-movement/server.js.map +1 -1
- package/services/fx/server.d.ts.map +1 -1
- package/services/fx/server.js +45 -38
- package/services/fx/server.js.map +1 -1
- package/services/kyc/client.d.ts +8 -3
- package/services/kyc/client.d.ts.map +1 -1
- package/services/kyc/client.js +24 -23
- package/services/kyc/client.js.map +1 -1
- package/services/kyc/common.d.ts +0 -7
- package/services/kyc/common.d.ts.map +1 -1
- package/services/kyc/common.js +0 -8
- package/services/kyc/common.js.map +1 -1
- package/services/kyc/server.d.ts +3 -1
- package/services/kyc/server.d.ts.map +1 -1
- package/services/kyc/server.js +15 -33
- package/services/kyc/server.js.map +1 -1
- package/services/notification/client.d.ts.map +1 -1
- package/services/notification/client.js +39 -33
- package/services/notification/client.js.map +1 -1
- package/services/notification/server.js +10 -10
- package/services/notification/server.js.map +1 -1
- package/services/storage/client.d.ts +3 -0
- package/services/storage/client.d.ts.map +1 -1
- package/services/storage/client.js +41 -11
- package/services/storage/client.js.map +1 -1
- package/services/storage/server.d.ts.map +1 -1
- package/services/storage/server.js +12 -11
- package/services/storage/server.js.map +1 -1
- package/services/username/client.d.ts +12 -1
- package/services/username/client.d.ts.map +1 -1
- package/services/username/client.js +36 -30
- package/services/username/client.js.map +1 -1
- package/services/username/server.d.ts.map +1 -1
- package/services/username/server.js +7 -6
- package/services/username/server.js.map +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Account } from "@keetanetwork/keetanet-client/lib/account.js";
|
|
2
2
|
import type { ResolvedCertificateChainRequirement } from "../utils/certificate-network.js";
|
|
3
3
|
import type { Signable } from "../utils/signing.js";
|
|
4
|
+
import type { RequestAuthContext } from "./request-signing.js";
|
|
4
5
|
import type { DistributiveOmit } from "@keetanetwork/keetanet-client/lib/utils/helper.js";
|
|
5
6
|
export type ExtractOk<T> = DistributiveOmit<Extract<T, {
|
|
6
7
|
ok: true;
|
|
@@ -26,11 +27,11 @@ export declare function parseSignatureFromURL(input: URL | string): Partial<HTTP
|
|
|
26
27
|
export declare function verifyBodyAuth<T extends {
|
|
27
28
|
account?: string;
|
|
28
29
|
signed?: unknown;
|
|
29
|
-
}>(request: T, getSigningData: (req: T) => Signable, certificateChain?: ResolvedCertificateChainRequirement): Promise<Account>;
|
|
30
|
+
}>(context: RequestAuthContext, request: T, getSigningData: (req: T) => Signable, certificateChain?: ResolvedCertificateChainRequirement): Promise<Account>;
|
|
30
31
|
/**
|
|
31
32
|
* Verify a URL-signed request, then enforce the cert-chain gate. Returns
|
|
32
33
|
* the authenticated account. The signable is built by the caller from a
|
|
33
34
|
* request derived from the URL-bound account public key.
|
|
34
35
|
*/
|
|
35
|
-
export declare function verifyURLAuth(
|
|
36
|
+
export declare function verifyURLAuth(context: RequestAuthContext, getSigningData: (account: Account) => Signable, certificateChain?: ResolvedCertificateChainRequirement): Promise<Account>;
|
|
36
37
|
//# sourceMappingURL=common.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../src/lib/http-server/common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,8CAA8C,CAAC;AAC5E,OAAO,KAAK,EAAE,mCAAmC,EAAE,MAAM,iCAAiC,CAAC;AAC3F,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;
|
|
1
|
+
{"version":3,"file":"common.d.ts","sourceRoot":"","sources":["../../../src/lib/http-server/common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,8CAA8C,CAAC;AAC5E,OAAO,KAAK,EAAE,mCAAmC,EAAE,MAAM,iCAAiC,CAAC;AAC3F,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAKpD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,mDAAmD,CAAC;AAE1F,MAAM,MAAM,SAAS,CAAC,CAAC,IAAI,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAAE;IAAE,EAAE,EAAE,IAAI,CAAA;CAAE,CAAC,EAAE,IAAI,CAAC,CAAC;AAE5E,MAAM,MAAM,eAAe,GAAG;IAC7B,KAAK,EAAE,MAAM,CAAC;IAEd,SAAS,EAAE,MAAM,CAAC;IAElB,SAAS,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,eAAO,MAAM,qBAAqB,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,eAAuD,CAAC;AAEhH,MAAM,WAAW,4BAA4B;IAC5C,WAAW,EAAE,eAAe,CAAC;IAC7B,OAAO,EAAE,OAAO,CAAC;CACjB;AAED,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM,EAAE,IAAI,EAAE,4BAA4B,GAAG,GAAG,CAsB9F;AAED,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,GAAG,GAAG,MAAM,GAAG,OAAO,CAAC,4BAA4B,CAAC,CA6ChG;AAED;;;;;GAKG;AACH,wBAAsB,cAAc,CAAC,CAAC,SAAS;IAAE,OAAO,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,EACpF,OAAO,EAAE,kBAAkB,EAC3B,OAAO,EAAE,CAAC,EACV,cAAc,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,QAAQ,EACpC,gBAAgB,CAAC,EAAE,mCAAmC,GACpD,OAAO,CAAC,OAAO,CAAC,CAmBlB;AAED;;;;GAIG;AACH,wBAAsB,aAAa,CAClC,OAAO,EAAE,kBAAkB,EAC3B,cAAc,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,QAAQ,EAC9C,gBAAgB,CAAC,EAAE,mCAAmC,GACpD,OAAO,CAAC,OAAO,CAAC,CAgBlB"}
|
|
@@ -4,7 +4,7 @@ import { KeetaAnchorUserError } from "../error.js";
|
|
|
4
4
|
import * as KeetaNet from "@keetanetwork/keetanet-client";
|
|
5
5
|
import { assertAccountCertificateChain } from "../utils/certificate-network.js";
|
|
6
6
|
import { createAssertEquals } from "typia";
|
|
7
|
-
import {
|
|
7
|
+
import { claimRequestNonce, verifyRequestSignature } from "./request-signing.js";
|
|
8
8
|
export const assertHTTPSignedField = (() => { const _io0 = (input, _exceptionable = true) => "string" === typeof input.nonce && "string" === typeof input.timestamp && "string" === typeof input.signature && (3 === Object.keys(input).length || Object.keys(input).every(key => {
|
|
9
9
|
if (["nonce", "timestamp", "signature"].some(prop => key === prop))
|
|
10
10
|
return true;
|
|
@@ -116,18 +116,20 @@ export function parseSignatureFromURL(input) {
|
|
|
116
116
|
* invalid auth, and `KeetaAnchorCertificateRequiredError` when the
|
|
117
117
|
* caller's on-chain cert chain doesn't satisfy `certificateChain`.
|
|
118
118
|
*/
|
|
119
|
-
export async function verifyBodyAuth(request, getSigningData, certificateChain) {
|
|
119
|
+
export async function verifyBodyAuth(context, request, getSigningData, certificateChain) {
|
|
120
120
|
if (!request.account || !request.signed) {
|
|
121
121
|
throw (new KeetaAnchorUserError('Authentication required'));
|
|
122
122
|
}
|
|
123
123
|
const account = KeetaNet.lib.Account.fromPublicKeyString(request.account).assertAccount();
|
|
124
124
|
const signable = getSigningData(request);
|
|
125
125
|
const signed = assertHTTPSignedField(request.signed);
|
|
126
|
-
const valid = await
|
|
126
|
+
const valid = await verifyRequestSignature(account, context, signable, signed);
|
|
127
127
|
if (!valid) {
|
|
128
128
|
throw (new KeetaAnchorUserError('Invalid signature'));
|
|
129
129
|
}
|
|
130
|
+
/* Only a caller that passes the cert-chain gate spends a nonce */
|
|
130
131
|
await assertAccountCertificateChain(account, certificateChain);
|
|
132
|
+
await claimRequestNonce(account, context, signed);
|
|
131
133
|
return (account);
|
|
132
134
|
}
|
|
133
135
|
/**
|
|
@@ -135,24 +137,18 @@ export async function verifyBodyAuth(request, getSigningData, certificateChain)
|
|
|
135
137
|
* the authenticated account. The signable is built by the caller from a
|
|
136
138
|
* request derived from the URL-bound account public key.
|
|
137
139
|
*/
|
|
138
|
-
export async function verifyURLAuth(
|
|
139
|
-
|
|
140
|
-
if (typeof url === 'string') {
|
|
141
|
-
urlString = url;
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
urlString = url.href;
|
|
145
|
-
}
|
|
146
|
-
const parsed = parseSignatureFromURL(urlString);
|
|
140
|
+
export async function verifyURLAuth(context, getSigningData, certificateChain) {
|
|
141
|
+
const parsed = parseSignatureFromURL(context.url);
|
|
147
142
|
if (!parsed.account || !parsed.signedField) {
|
|
148
143
|
throw (new KeetaAnchorUserError('Authentication required'));
|
|
149
144
|
}
|
|
150
145
|
const signable = getSigningData(parsed.account);
|
|
151
|
-
const valid = await
|
|
146
|
+
const valid = await verifyRequestSignature(parsed.account, context, signable, parsed.signedField);
|
|
152
147
|
if (!valid) {
|
|
153
148
|
throw (new KeetaAnchorUserError('Invalid signature'));
|
|
154
149
|
}
|
|
155
150
|
await assertAccountCertificateChain(parsed.account, certificateChain);
|
|
151
|
+
await claimRequestNonce(parsed.account, context, parsed.signedField);
|
|
156
152
|
return (parsed.account);
|
|
157
153
|
}
|
|
158
154
|
//# sourceMappingURL=common.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/lib/http-server/common.ts"],"names":[],"mappings":";;AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,QAAQ,MAAM,+BAA+B,CAAC;AAC1D,OAAO,EAAE,6BAA6B,EAAE,MAAM,iCAAiC,CAAC;AAChF,OAAO,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/lib/http-server/common.ts"],"names":[],"mappings":";;AAGA,OAAO,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,KAAK,QAAQ,MAAM,+BAA+B,CAAC;AAC1D,OAAO,EAAE,6BAA6B,EAAE,MAAM,iCAAiC,CAA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C,CAAC;IAClD,IAAI,CAAC,MAAM,CAAC,OAAO,IAAI,CAAC,MAAM,CAAC,WAAW,EAAE,CAAC;QAC5C,MAAK,CAAC,IAAI,oBAAoB,CAAC,yBAAyB,CAAC,CAAC,CAAC;IAC5D,CAAC;IAED,MAAM,QAAQ,GAAG,cAAc,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;IAChD,MAAM,KAAK,GAAG,MAAM,sBAAsB,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAClG,IAAI,CAAC,KAAK,EAAE,CAAC;QACZ,MAAK,CAAC,IAAI,oBAAoB,CAAC,mBAAmB,CAAC,CAAC,CAAC;IACtD,CAAC;IAED,MAAM,6BAA6B,CAAC,MAAM,CAAC,OAAO,EAAE,gBAAgB,CAAC,CAAC;IACtE,MAAM,iBAAiB,CAAC,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,WAAW,CAAC,CAAC;IAErE,OAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACxB,CAAC","sourcesContent":["import type { Account } from \"@keetanetwork/keetanet-client/lib/account.js\";\nimport type { ResolvedCertificateChainRequirement } from \"../utils/certificate-network.js\";\nimport type { Signable } from \"../utils/signing.js\";\nimport { KeetaAnchorUserError } from \"../error.js\";\nimport * as KeetaNet from \"@keetanetwork/keetanet-client\";\nimport { assertAccountCertificateChain } from \"../utils/certificate-network.js\";\nimport { createAssertEquals } from \"typia\";\nimport type { RequestAuthContext } from \"./request-signing.js\";\nimport { claimRequestNonce, verifyRequestSignature } from \"./request-signing.js\";\nimport type { DistributiveOmit } from \"@keetanetwork/keetanet-client/lib/utils/helper.js\";\n\nexport type ExtractOk<T> = DistributiveOmit<Extract<T, { ok: true }>, 'ok'>;\n\nexport type HTTPSignedField = {\n\tnonce: string;\n\t/* Date and time of the request in ISO 8601 format */\n\ttimestamp: string;\n\t/* Signature of the account public key and the nonce as an ASN.1 Sequence, Base64 DER */\n\tsignature: string;\n};\n\nexport const assertHTTPSignedField: (input: unknown) => HTTPSignedField = createAssertEquals<HTTPSignedField>();\n\nexport interface HTTPSignedFieldURLParameters {\n\tsignedField: HTTPSignedField;\n\taccount: Account;\n}\n\nexport function addSignatureToURL(input: URL | string, data: HTTPSignedFieldURLParameters): URL {\n\tlet url: URL;\n\n\tif (typeof input === 'string') {\n\t\turl = new URL(input);\n\t} else {\n\t\turl = new URL(input.toString());\n\t}\n\n\tfor (const key of [ 'nonce', 'timestamp', 'signature' ] as const) {\n\t\tconst searchKey = `signed.${key}`;\n\n\t\tif (url.searchParams.has(searchKey)) {\n\t\t\tthrow(new KeetaAnchorUserError(`URL already has signed field parameter: ${searchKey}`));\n\t\t}\n\n\t\turl.searchParams.set(`signed.${key}`, data.signedField[key]);\n\t}\n\n\turl.searchParams.set('account', data.account.publicKeyString.get());\n\n\treturn(url);\n}\n\nexport function parseSignatureFromURL(input: URL | string): Partial<HTTPSignedFieldURLParameters> {\n\tlet url: URL;\n\n\tif (typeof input === 'string') {\n\t\turl = new URL(input);\n\t} else {\n\t\turl = new URL(input.toString());\n\t}\n\n\tconst retVal: Partial<HTTPSignedFieldURLParameters> = {};\n\n\tconst signedField = ((): HTTPSignedField | undefined => {\n\t\tconst nonce = url.searchParams.get('signed.nonce');\n\t\tconst timestamp = url.searchParams.get('signed.timestamp');\n\t\tconst signature = url.searchParams.get('signed.signature');\n\n\t\tif (!nonce && !timestamp && !signature) {\n\t\t\treturn(undefined);\n\t\t}\n\n\t\tif (!nonce || !timestamp || !signature) {\n\t\t\tthrow(new KeetaAnchorUserError('Incomplete signature fields in URL'));\n\t\t}\n\n\t\treturn({ nonce, timestamp, signature });\n\t})();\n\n\tif (signedField) {\n\t\tretVal.signedField = signedField;\n\t}\n\n\tconst account = ((): Account | undefined => {\n\t\tconst accountParam = url.searchParams.get('account');\n\t\tif (!accountParam) {\n\t\t\treturn(undefined);\n\t\t}\n\n\t\treturn(KeetaNet.lib.Account.fromPublicKeyString(accountParam).assertAccount());\n\t})();\n\n\tif (account) {\n\t\tretVal.account = account;\n\t}\n\n\treturn(retVal);\n}\n\n/**\n * Verify a signed body, then enforce the cert-chain gate. Returns the\n * authenticated account. Throws `KeetaAnchorUserError` on missing or\n * invalid auth, and `KeetaAnchorCertificateRequiredError` when the\n * caller's on-chain cert chain doesn't satisfy `certificateChain`.\n */\nexport async function verifyBodyAuth<T extends { account?: string; signed?: unknown }>(\n\tcontext: RequestAuthContext,\n\trequest: T,\n\tgetSigningData: (req: T) => Signable,\n\tcertificateChain?: ResolvedCertificateChainRequirement\n): Promise<Account> {\n\tif (!request.account || !request.signed) {\n\t\tthrow(new KeetaAnchorUserError('Authentication required'));\n\t}\n\n\tconst account = KeetaNet.lib.Account.fromPublicKeyString(request.account).assertAccount();\n\n\tconst signable = getSigningData(request);\n\tconst signed = assertHTTPSignedField(request.signed);\n\tconst valid = await verifyRequestSignature(account, context, signable, signed);\n\tif (!valid) {\n\t\tthrow(new KeetaAnchorUserError('Invalid signature'));\n\t}\n\n\t/* Only a caller that passes the cert-chain gate spends a nonce */\n\tawait assertAccountCertificateChain(account, certificateChain);\n\tawait claimRequestNonce(account, context, signed);\n\n\treturn(account);\n}\n\n/**\n * Verify a URL-signed request, then enforce the cert-chain gate. Returns\n * the authenticated account. The signable is built by the caller from a\n * request derived from the URL-bound account public key.\n */\nexport async function verifyURLAuth(\n\tcontext: RequestAuthContext,\n\tgetSigningData: (account: Account) => Signable,\n\tcertificateChain?: ResolvedCertificateChainRequirement\n): Promise<Account> {\n\tconst parsed = parseSignatureFromURL(context.url);\n\tif (!parsed.account || !parsed.signedField) {\n\t\tthrow(new KeetaAnchorUserError('Authentication required'));\n\t}\n\n\tconst signable = getSigningData(parsed.account);\n\tconst valid = await verifyRequestSignature(parsed.account, context, signable, parsed.signedField);\n\tif (!valid) {\n\t\tthrow(new KeetaAnchorUserError('Invalid signature'));\n\t}\n\n\tawait assertAccountCertificateChain(parsed.account, certificateChain);\n\tawait claimRequestNonce(parsed.account, context, parsed.signedField);\n\n\treturn(parsed.account);\n}\n"]}
|
|
@@ -3,12 +3,19 @@ import type { JSONSerializable } from '../utils/json.js';
|
|
|
3
3
|
import type { Logger } from '../log/index.js';
|
|
4
4
|
import type { CertificateChainConfig, ResolvedCertificateChainRequirement } from '../utils/certificate-network.js';
|
|
5
5
|
import { Buffer } from '../utils/buffer.js';
|
|
6
|
+
import { lib as KeetaNetLib } from '@keetanetwork/keetanet-client';
|
|
7
|
+
import type { KeetaAnchorReplayGuard } from './replay-guard.js';
|
|
8
|
+
import type { RequestAuthContext } from './request-signing.js';
|
|
9
|
+
type RequestTiming = InstanceType<typeof KeetaNetLib.RequestTiming>;
|
|
6
10
|
export declare const AssertHTTPErrorData: (input: unknown) => {
|
|
7
11
|
error: string;
|
|
8
12
|
statusCode?: number;
|
|
9
13
|
contentType?: string;
|
|
10
14
|
};
|
|
11
|
-
type
|
|
15
|
+
type RouteHandlerContext = {
|
|
16
|
+
timing: RequestTiming;
|
|
17
|
+
};
|
|
18
|
+
type RouteHandlerMethod<BodyDataType = JSONSerializable | undefined> = (urlParams: Map<string, string>, postData: BodyDataType, requestHeaders: http.IncomingHttpHeaders, requestUrl: URL, context: RouteHandlerContext) => Promise<{
|
|
12
19
|
output: string | Buffer;
|
|
13
20
|
statusCode?: number;
|
|
14
21
|
contentType?: string;
|
|
@@ -65,6 +72,21 @@ export interface KeetaAnchorHTTPServerConfig {
|
|
|
65
72
|
* MUST have a published cert chaining to one of `trustedIssuers`.
|
|
66
73
|
*/
|
|
67
74
|
requireCertificateChain?: CertificateChainConfig | undefined;
|
|
75
|
+
/**
|
|
76
|
+
* Where signed request nonces are recorded so each signature is
|
|
77
|
+
* accepted once. Defaults to a {@link MemoryReplayGuard}, which is
|
|
78
|
+
* per process: servers running several instances behind one URL
|
|
79
|
+
* must share one guard, such as a `RedisReplayGuard` or
|
|
80
|
+
* `PostgresReplayGuard`.
|
|
81
|
+
*/
|
|
82
|
+
replayGuard?: KeetaAnchorReplayGuard | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* Accept request signatures from clients that predate request
|
|
85
|
+
* binding. Such a signature covers only the operation's data, so it
|
|
86
|
+
* can be replayed at another anchor. Defaults to `true`; set it to
|
|
87
|
+
* `false` once every client signs requests.
|
|
88
|
+
*/
|
|
89
|
+
acceptLegacyRequestSignatures?: boolean | undefined;
|
|
68
90
|
}
|
|
69
91
|
export declare abstract class KeetaNetAnchorHTTPServer<ConfigType extends KeetaAnchorHTTPServerConfig = KeetaAnchorHTTPServerConfig> implements Required<KeetaAnchorHTTPServerConfig> {
|
|
70
92
|
#private;
|
|
@@ -76,6 +98,8 @@ export declare abstract class KeetaNetAnchorHTTPServer<ConfigType extends KeetaA
|
|
|
76
98
|
* @see {@link requireCertificateChain}.
|
|
77
99
|
*/
|
|
78
100
|
protected readonly resolvedCertificateChainRequirement: ResolvedCertificateChainRequirement | undefined;
|
|
101
|
+
readonly replayGuard: KeetaAnchorReplayGuard;
|
|
102
|
+
readonly acceptLegacyRequestSignatures: boolean;
|
|
79
103
|
constructor(config: ConfigType);
|
|
80
104
|
protected abstract initRoutes(config: ConfigType): Promise<Routes>;
|
|
81
105
|
/**
|
|
@@ -114,6 +138,12 @@ export declare abstract class KeetaNetAnchorHTTPServer<ConfigType extends KeetaA
|
|
|
114
138
|
*/
|
|
115
139
|
get url(): string;
|
|
116
140
|
set url(value: undefined | string | URL | ((object: this) => string));
|
|
141
|
+
/**
|
|
142
|
+
* The context to verify a signed request against: the request as
|
|
143
|
+
* seen at this server's public URL, this server's replay guard, and
|
|
144
|
+
* whether it accepts legacy signatures.
|
|
145
|
+
*/
|
|
146
|
+
protected requestAuthContext(method: string, requestURL: URL, body?: RequestAuthContext['body']): RequestAuthContext;
|
|
117
147
|
[Symbol.asyncDispose](): Promise<void>;
|
|
118
148
|
}
|
|
119
149
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/http-server/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAM7B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EAAE,MAAM,EAAY,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EACX,sBAAsB,EACtB,mCAAmC,EACnC,MAAM,iCAAiC,CAAC;AAKzC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/lib/http-server/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,MAAM,CAAC;AAM7B,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AACzD,OAAO,KAAK,EAAE,MAAM,EAAY,MAAM,iBAAiB,CAAC;AACxD,OAAO,KAAK,EACX,sBAAsB,EACtB,mCAAmC,EACnC,MAAM,iCAAiC,CAAC;AAKzC,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAC5C,OAAO,EAAE,GAAG,IAAI,WAAW,EAAE,MAAM,+BAA+B,CAAC;AACnE,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAEhE,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAG/D,KAAK,aAAa,GAAG,YAAY,CAAC,OAAO,WAAW,CAAC,aAAa,CAAC,CAAC;AAEpE,eAAO,MAAM,mBAAmB,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAC;CAAkF,CAAC;AA8BpM,KAAK,mBAAmB,GAAG;IAC1B,MAAM,EAAE,aAAa,CAAC;CACtB,CAAC;AASF,KAAK,kBAAkB,C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,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE,EAAE,EAAE,GAAG,SAAS;IAS9E,OAAO,CAAC,MAAM,CAAC,UAAU;IAyEzB,OAAO,CAAC,MAAM,CAAC,SAAS;IA2CxB,OAAO,CAAC,MAAM,CAAC,OAAO;YAyHR,IAAI;IA8RlB;;OAEG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAc5B;;;OAGG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAI3B;;OAEG;IACG,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAO3B;;;;;OAKG;IACH,IAAI,GAAG,IAAI,MAAM,CAoChB;IAED,IAAI,GAAG,CAAC,KAAK,EAAE,SAAS,GAAG,MAAM,GAAG,GAAG,GAAG,CAAC,CAAC,MAAM,EAAE,IAAI,KAAK,MAAM,CAAC,EAGnE;IAED;;;;OAIG;IACH,SAAS,CAAC,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,GAAG,EAAE,IAAI,CAAC,EAAE,kBAAkB,CAAC,MAAM,CAAC,GAAG,kBAAkB;IAUpH,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,OAAO,CAAC,IAAI,CAAC;CAGtC"}
|
package/lib/http-server/index.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { __addDisposableResource, __disposeResources } from "tslib";
|
|
1
2
|
import * as __typia_transform__assertGuard from "typia/lib/internal/_assertGuard";
|
|
2
3
|
import * as http from 'http';
|
|
3
4
|
import { KeetaAnchorError, KeetaAnchorUserError } from '../error.js';
|
|
@@ -6,6 +7,9 @@ import { createAssert } from 'typia';
|
|
|
6
7
|
import { assertNever } from '../utils/never.js';
|
|
7
8
|
import { resolveCertificateChainConfig } from '../utils/certificate-network.js';
|
|
8
9
|
import { Buffer } from '../utils/buffer.js';
|
|
10
|
+
import { lib as KeetaNetLib } from '@keetanetwork/keetanet-client';
|
|
11
|
+
import { MemoryReplayGuard } from './replay-guard.js';
|
|
12
|
+
import { normalizeRequestPath, publicRequestURL } from './request-signing.js';
|
|
9
13
|
export const AssertHTTPErrorData = (() => { const _io0 = input => "string" === typeof input.error && (undefined === input.statusCode || "number" === typeof input.statusCode) && (undefined === input.contentType || "string" === typeof input.contentType); const _ao0 = (input, _path, _exceptionable = true) => ("string" === typeof input.error || __typia_transform__assertGuard._assertGuard(_exceptionable, {
|
|
10
14
|
method: "createAssert",
|
|
11
15
|
path: _path + ".error",
|
|
@@ -39,9 +43,29 @@ export const AssertHTTPErrorData = (() => { const _io0 = input => "string" === t
|
|
|
39
43
|
return input;
|
|
40
44
|
}; })();
|
|
41
45
|
/**
|
|
42
|
-
* The maximum size of a request
|
|
46
|
+
* The maximum size of a request body when the route sets no
|
|
47
|
+
* `maxBodySize` (1 MiB)
|
|
43
48
|
*/
|
|
44
|
-
const MAX_REQUEST_SIZE =
|
|
49
|
+
const MAX_REQUEST_SIZE = 1024 ** 2;
|
|
50
|
+
/**
|
|
51
|
+
* Read a request body, refusing it once it passes `limit` bytes.
|
|
52
|
+
* Returns null when the body is too large.
|
|
53
|
+
*/
|
|
54
|
+
async function readBody(request, limit) {
|
|
55
|
+
const chunks = [];
|
|
56
|
+
let size = 0;
|
|
57
|
+
for await (const chunk of request) {
|
|
58
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
59
|
+
const buffer = Buffer.from(chunk);
|
|
60
|
+
size += buffer.length;
|
|
61
|
+
if (size > limit) {
|
|
62
|
+
return (null);
|
|
63
|
+
}
|
|
64
|
+
chunks.push(buffer);
|
|
65
|
+
}
|
|
66
|
+
const body = Buffer.concat(chunks, size);
|
|
67
|
+
return (body);
|
|
68
|
+
}
|
|
45
69
|
;
|
|
46
70
|
export class KeetaNetAnchorHTTPServer {
|
|
47
71
|
port;
|
|
@@ -52,6 +76,8 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
52
76
|
* @see {@link requireCertificateChain}.
|
|
53
77
|
*/
|
|
54
78
|
resolvedCertificateChainRequirement;
|
|
79
|
+
replayGuard;
|
|
80
|
+
acceptLegacyRequestSignatures;
|
|
55
81
|
#serverPromise;
|
|
56
82
|
#server;
|
|
57
83
|
#urlParts;
|
|
@@ -63,6 +89,13 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
63
89
|
this.id = config.id ?? crypto.randomUUID();
|
|
64
90
|
this.logger = config.logger ?? Log.Legacy('ANCHOR');
|
|
65
91
|
this.resolvedCertificateChainRequirement = resolveCertificateChainConfig(config.requireCertificateChain);
|
|
92
|
+
this.replayGuard = config.replayGuard ?? new MemoryReplayGuard();
|
|
93
|
+
/*
|
|
94
|
+
* XXX:TODO: Default to `false` in the first release after every
|
|
95
|
+
* supported client sends dual signatures, and drop the
|
|
96
|
+
* legacy path once no anchor runs a release before them
|
|
97
|
+
*/
|
|
98
|
+
this.acceptLegacyRequestSignatures = config.acceptLegacyRequestSignatures ?? true;
|
|
66
99
|
if (config.url !== undefined) {
|
|
67
100
|
if (config.url instanceof URL || typeof config.url === 'string') {
|
|
68
101
|
this.#url = config.url;
|
|
@@ -318,6 +351,8 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
318
351
|
...(await this.initRoutes(this.#config))
|
|
319
352
|
});
|
|
320
353
|
const server = new http.Server(async (request, response) => {
|
|
354
|
+
const timing = new KeetaNetLib.RequestTiming();
|
|
355
|
+
timing.log = this.logger;
|
|
321
356
|
const forwardedProtoHeader = request.headers['x-forwarded-proto'];
|
|
322
357
|
let forwardedProto;
|
|
323
358
|
if (Array.isArray(forwardedProtoHeader)) {
|
|
@@ -348,7 +383,7 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
348
383
|
* Normalize the input URL by stripping leading slashes and
|
|
349
384
|
* combining it with the base URL to get the full URL for routing.
|
|
350
385
|
*/
|
|
351
|
-
const inputURLRaw = (request.url ?? '/')
|
|
386
|
+
const inputURLRaw = normalizeRequestPath(request.url ?? '/');
|
|
352
387
|
const inputURLObject = new URL(inputURLRaw, 'http://localhost');
|
|
353
388
|
const inputURL = inputURLObject.pathname + (function () {
|
|
354
389
|
if (inputURLObject.search === '') {
|
|
@@ -359,10 +394,16 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
359
394
|
const url = new URL(inputURL, inputURLBase);
|
|
360
395
|
const method = request.method ?? 'GET';
|
|
361
396
|
/*
|
|
362
|
-
* Finalize the response by
|
|
363
|
-
* the
|
|
397
|
+
* Finalize the response by adding the Server-Timing
|
|
398
|
+
* header, **writing the output**, syncing the logger and
|
|
399
|
+
* ending the response.
|
|
364
400
|
*/
|
|
365
|
-
const responseFinalize = async () => {
|
|
401
|
+
const responseFinalize = async (output) => {
|
|
402
|
+
const serverTiming = timing.toServerTimingHeader();
|
|
403
|
+
if (serverTiming !== '') {
|
|
404
|
+
response.setHeader('Server-Timing', serverTiming);
|
|
405
|
+
}
|
|
406
|
+
response.write(output);
|
|
366
407
|
if ('sync' in this.logger && typeof this.logger.sync === 'function') {
|
|
367
408
|
try {
|
|
368
409
|
await this.logger.sync();
|
|
@@ -379,8 +420,7 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
379
420
|
if (inputURLRaw.at(0) !== '/') {
|
|
380
421
|
response.statusCode = 400;
|
|
381
422
|
response.setHeader('Content-Type', 'text/plain');
|
|
382
|
-
|
|
383
|
-
await responseFinalize();
|
|
423
|
+
await responseFinalize('Bad Request');
|
|
384
424
|
return;
|
|
385
425
|
}
|
|
386
426
|
/*
|
|
@@ -390,8 +430,7 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
390
430
|
if (requestedRouteAndParams === null) {
|
|
391
431
|
response.statusCode = 404;
|
|
392
432
|
response.setHeader('Content-Type', 'text/plain');
|
|
393
|
-
|
|
394
|
-
await responseFinalize();
|
|
433
|
+
await responseFinalize('Not Found');
|
|
395
434
|
return;
|
|
396
435
|
}
|
|
397
436
|
/*
|
|
@@ -420,29 +459,33 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
420
459
|
const bodySizeLimit = route.maxBodySize ?? MAX_REQUEST_SIZE;
|
|
421
460
|
// Early rejection based on Content-Length header
|
|
422
461
|
const contentLength = request.headers['content-length'];
|
|
462
|
+
let declaredSize = Number.NaN;
|
|
423
463
|
if (contentLength !== undefined) {
|
|
424
|
-
|
|
425
|
-
if (!isNaN(declaredSize) && declaredSize > bodySizeLimit) {
|
|
426
|
-
request.resume();
|
|
427
|
-
response.statusCode = 413;
|
|
428
|
-
response.setHeader('Content-Type', 'text/plain');
|
|
429
|
-
response.write('Payload Too Large');
|
|
430
|
-
await responseFinalize();
|
|
431
|
-
return;
|
|
432
|
-
}
|
|
464
|
+
declaredSize = Number.parseInt(contentLength, 10);
|
|
433
465
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
466
|
+
let data = null;
|
|
467
|
+
if (Number.isNaN(declaredSize) || declaredSize <= bodySizeLimit) {
|
|
468
|
+
const env_1 = { stack: [], error: void 0, hasError: false };
|
|
469
|
+
try {
|
|
470
|
+
const bodyTimer = __addDisposableResource(env_1, timing.startTime('http-request-body'), false);
|
|
471
|
+
data = await readBody(request, bodySizeLimit);
|
|
472
|
+
bodyTimer.end();
|
|
440
473
|
}
|
|
441
|
-
|
|
442
|
-
|
|
474
|
+
catch (e_1) {
|
|
475
|
+
env_1.error = e_1;
|
|
476
|
+
env_1.hasError = true;
|
|
443
477
|
}
|
|
444
|
-
|
|
445
|
-
|
|
478
|
+
finally {
|
|
479
|
+
__disposeResources(env_1);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
if (data === null) {
|
|
483
|
+
request.resume();
|
|
484
|
+
response.statusCode = 413;
|
|
485
|
+
response.setHeader('Content-Type', 'text/plain');
|
|
486
|
+
await responseFinalize('Payload Too Large');
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
446
489
|
if (route.bodyType === 'raw') {
|
|
447
490
|
bodyData = data;
|
|
448
491
|
}
|
|
@@ -462,8 +505,10 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
462
505
|
/**
|
|
463
506
|
* Call the route handler
|
|
464
507
|
*/
|
|
465
|
-
|
|
466
|
-
|
|
508
|
+
result = await timing.runTimer('http-handler', async function () {
|
|
509
|
+
// @ts-ignore
|
|
510
|
+
return (await route.handler(params, bodyData, request.headers, url, { timing }));
|
|
511
|
+
});
|
|
467
512
|
generatedResult = true;
|
|
468
513
|
}
|
|
469
514
|
catch (err) {
|
|
@@ -497,7 +542,7 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
497
542
|
};
|
|
498
543
|
}
|
|
499
544
|
// @ts-ignore
|
|
500
|
-
result = await errorHandlerRoute.handler(new Map(), errBody, request.headers, url);
|
|
545
|
+
result = await errorHandlerRoute.handler(new Map(), errBody, request.headers, url, { timing });
|
|
501
546
|
generatedResult = true;
|
|
502
547
|
}
|
|
503
548
|
if (!generatedResult) {
|
|
@@ -506,8 +551,7 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
506
551
|
*/
|
|
507
552
|
response.statusCode = 500;
|
|
508
553
|
response.setHeader('Content-Type', 'text/plain');
|
|
509
|
-
|
|
510
|
-
await responseFinalize();
|
|
554
|
+
await responseFinalize('Internal Server Error');
|
|
511
555
|
return;
|
|
512
556
|
}
|
|
513
557
|
}
|
|
@@ -525,8 +569,7 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
525
569
|
}
|
|
526
570
|
}
|
|
527
571
|
response.setHeader('Content-Type', result.contentType ?? 'application/json');
|
|
528
|
-
|
|
529
|
-
await responseFinalize();
|
|
572
|
+
await responseFinalize(result.output);
|
|
530
573
|
});
|
|
531
574
|
this.#server = server;
|
|
532
575
|
/**
|
|
@@ -630,6 +673,20 @@ export class KeetaNetAnchorHTTPServer {
|
|
|
630
673
|
this.#urlParts = undefined;
|
|
631
674
|
this.#url = value;
|
|
632
675
|
}
|
|
676
|
+
/**
|
|
677
|
+
* The context to verify a signed request against: the request as
|
|
678
|
+
* seen at this server's public URL, this server's replay guard, and
|
|
679
|
+
* whether it accepts legacy signatures.
|
|
680
|
+
*/
|
|
681
|
+
requestAuthContext(method, requestURL, body) {
|
|
682
|
+
return ({
|
|
683
|
+
method,
|
|
684
|
+
url: publicRequestURL(this.url, requestURL),
|
|
685
|
+
body,
|
|
686
|
+
replayGuard: this.replayGuard,
|
|
687
|
+
acceptLegacySignatures: this.acceptLegacyRequestSignatures
|
|
688
|
+
});
|
|
689
|
+
}
|
|
633
690
|
[Symbol.asyncDispose]() {
|
|
634
691
|
return (this.stop());
|
|
635
692
|
}
|