@prosopo/api-express-router 2.5.5 → 3.0.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/CHANGELOG.md +70 -0
- package/dist/cjs/apiExpressRouterFactory.cjs +29 -0
- package/dist/cjs/endpointAdapter/apiExpressDefaultEndpointAdapter.cjs +34 -0
- package/dist/cjs/errorHandler.cjs +12 -0
- package/dist/cjs/index.cjs +17 -0
- package/dist/cjs/middlewares/authMiddleware.cjs +84 -0
- package/dist/cjs/middlewares/requestLoggerMiddleware.cjs +19 -0
- package/dist/endpointAdapter/apiExpressDefaultEndpointAdapter.d.ts.map +1 -1
- package/dist/endpointAdapter/apiExpressDefaultEndpointAdapter.js +3 -1
- package/dist/endpointAdapter/apiExpressDefaultEndpointAdapter.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/middlewares/authMiddleware.d.ts +5 -0
- package/dist/middlewares/authMiddleware.d.ts.map +1 -0
- package/dist/middlewares/authMiddleware.js +85 -0
- package/dist/middlewares/authMiddleware.js.map +1 -0
- package/dist/middlewares/requestLoggerMiddleware.d.ts +4 -0
- package/dist/middlewares/requestLoggerMiddleware.d.ts.map +1 -0
- package/dist/middlewares/requestLoggerMiddleware.js +14 -0
- package/dist/middlewares/requestLoggerMiddleware.js.map +1 -0
- package/dist/tests/unit/middlewares/authMiddleware.unit.test.d.ts +2 -0
- package/dist/tests/unit/middlewares/authMiddleware.unit.test.d.ts.map +1 -0
- package/dist/tests/unit/middlewares/authMiddleware.unit.test.js +135 -0
- package/dist/tests/unit/middlewares/authMiddleware.unit.test.js.map +1 -0
- package/package.json +5 -4
- package/vite.test.config.ts +1 -1
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# @prosopo/api-express-router
|
|
2
|
+
|
|
3
|
+
## 3.0.3
|
|
4
|
+
### Patch Changes
|
|
5
|
+
|
|
6
|
+
- 9671152: uuid
|
|
7
|
+
|
|
8
|
+
## 3.0.2
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- @prosopo/common@3.0.2
|
|
12
|
+
- @prosopo/api-route@2.6.7
|
|
13
|
+
|
|
14
|
+
## 3.0.1
|
|
15
|
+
### Patch Changes
|
|
16
|
+
|
|
17
|
+
- @prosopo/common@3.0.1
|
|
18
|
+
- @prosopo/api-route@2.6.6
|
|
19
|
+
|
|
20
|
+
## 3.0.0
|
|
21
|
+
### Major Changes
|
|
22
|
+
|
|
23
|
+
- 64b5bcd: Access Controls
|
|
24
|
+
|
|
25
|
+
### Patch Changes
|
|
26
|
+
|
|
27
|
+
- Updated dependencies [64b5bcd]
|
|
28
|
+
- @prosopo/common@3.0.0
|
|
29
|
+
- @prosopo/api-route@2.6.5
|
|
30
|
+
|
|
31
|
+
## 2.6.4
|
|
32
|
+
### Patch Changes
|
|
33
|
+
|
|
34
|
+
- 86c22b8: structured logging
|
|
35
|
+
- Updated dependencies [86c22b8]
|
|
36
|
+
- @prosopo/api-route@2.6.4
|
|
37
|
+
- @prosopo/common@2.7.2
|
|
38
|
+
|
|
39
|
+
## 2.6.3
|
|
40
|
+
### Patch Changes
|
|
41
|
+
|
|
42
|
+
- @prosopo/common@2.7.1
|
|
43
|
+
- @prosopo/api-route@2.6.3
|
|
44
|
+
|
|
45
|
+
## 2.6.2
|
|
46
|
+
### Patch Changes
|
|
47
|
+
|
|
48
|
+
- Updated dependencies [8f0644a]
|
|
49
|
+
- @prosopo/common@2.7.0
|
|
50
|
+
- @prosopo/api-route@2.6.2
|
|
51
|
+
|
|
52
|
+
## 2.6.1
|
|
53
|
+
|
|
54
|
+
### Patch Changes
|
|
55
|
+
|
|
56
|
+
- Updated dependencies [04cc7ee]
|
|
57
|
+
- @prosopo/common@2.6.1
|
|
58
|
+
- @prosopo/api-route@2.6.1
|
|
59
|
+
|
|
60
|
+
## 2.6.0
|
|
61
|
+
|
|
62
|
+
### Minor Changes
|
|
63
|
+
|
|
64
|
+
- a0bfc8a: bump all pkg versions since independent versioning applied
|
|
65
|
+
|
|
66
|
+
### Patch Changes
|
|
67
|
+
|
|
68
|
+
- Updated dependencies [a0bfc8a]
|
|
69
|
+
- @prosopo/api-route@2.6.0
|
|
70
|
+
- @prosopo/common@2.6.0
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const express = require("express");
|
|
4
|
+
const errorHandler = require("./errorHandler.cjs");
|
|
5
|
+
class ApiExpressRouterFactory {
|
|
6
|
+
createRouter(routersProvider, apiEndpointAdapter) {
|
|
7
|
+
const router = express.Router();
|
|
8
|
+
const apiRoutes = routersProvider.getRoutes();
|
|
9
|
+
this.registerRoutes(router, apiRoutes, apiEndpointAdapter);
|
|
10
|
+
router.use(errorHandler.handleErrors);
|
|
11
|
+
return router;
|
|
12
|
+
}
|
|
13
|
+
registerRoutes(router, routes, apiEndpointAdapter) {
|
|
14
|
+
for (const route of routes) {
|
|
15
|
+
router.post(
|
|
16
|
+
route.path,
|
|
17
|
+
async (request, response, next) => {
|
|
18
|
+
return await apiEndpointAdapter.handleRequest(
|
|
19
|
+
route.endpoint,
|
|
20
|
+
request,
|
|
21
|
+
response,
|
|
22
|
+
next
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.ApiExpressRouterFactory = ApiExpressRouterFactory;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const common = require("@prosopo/common");
|
|
4
|
+
class ApiExpressDefaultEndpointAdapter {
|
|
5
|
+
constructor(logLevel, errorStatusCode) {
|
|
6
|
+
this.logLevel = logLevel;
|
|
7
|
+
this.errorStatusCode = errorStatusCode;
|
|
8
|
+
}
|
|
9
|
+
async handleRequest(endpoint, request, response, next) {
|
|
10
|
+
let args;
|
|
11
|
+
try {
|
|
12
|
+
args = endpoint.getRequestArgsSchema()?.parse(request.body);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
return next(
|
|
15
|
+
new common.ProsopoApiError("API.PARSE_ERROR", {
|
|
16
|
+
context: { code: 400, error }
|
|
17
|
+
})
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
try {
|
|
21
|
+
const apiEndpointResponse = await endpoint.processRequest(
|
|
22
|
+
args,
|
|
23
|
+
request.logger
|
|
24
|
+
);
|
|
25
|
+
response.json(apiEndpointResponse);
|
|
26
|
+
} catch (error) {
|
|
27
|
+
request.logger.error(() => ({
|
|
28
|
+
err: error
|
|
29
|
+
}));
|
|
30
|
+
response.status(500).send("An internal server error occurred.");
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.ApiExpressDefaultEndpointAdapter = ApiExpressDefaultEndpointAdapter;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const common = require("@prosopo/common");
|
|
4
|
+
const handleErrors = (err, request, response, next) => {
|
|
5
|
+
const { code, statusMessage, jsonError } = common.unwrapError(err, request.i18n);
|
|
6
|
+
response.statusMessage = statusMessage;
|
|
7
|
+
response.set("content-type", "application/json");
|
|
8
|
+
response.status(code);
|
|
9
|
+
response.send({ error: jsonError });
|
|
10
|
+
response.end();
|
|
11
|
+
};
|
|
12
|
+
exports.handleErrors = handleErrors;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const apiExpressRouterFactory$1 = require("./apiExpressRouterFactory.cjs");
|
|
4
|
+
const apiExpressDefaultEndpointAdapter = require("./endpointAdapter/apiExpressDefaultEndpointAdapter.cjs");
|
|
5
|
+
const errorHandler = require("./errorHandler.cjs");
|
|
6
|
+
const authMiddleware = require("./middlewares/authMiddleware.cjs");
|
|
7
|
+
const requestLoggerMiddleware = require("./middlewares/requestLoggerMiddleware.cjs");
|
|
8
|
+
const apiExpressRouterFactory = new apiExpressRouterFactory$1.ApiExpressRouterFactory();
|
|
9
|
+
const createApiExpressDefaultEndpointAdapter = (logLevel, errorStatusCode = 500) => {
|
|
10
|
+
return new apiExpressDefaultEndpointAdapter.ApiExpressDefaultEndpointAdapter(logLevel, errorStatusCode);
|
|
11
|
+
};
|
|
12
|
+
exports.handleErrors = errorHandler.handleErrors;
|
|
13
|
+
exports.authMiddleware = authMiddleware.authMiddleware;
|
|
14
|
+
exports.verifySignature = authMiddleware.verifySignature;
|
|
15
|
+
exports.requestLoggerMiddleware = requestLoggerMiddleware.requestLoggerMiddleware;
|
|
16
|
+
exports.apiExpressRouterFactory = apiExpressRouterFactory;
|
|
17
|
+
exports.createApiExpressDefaultEndpointAdapter = createApiExpressDefaultEndpointAdapter;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const util = require("@polkadot/util");
|
|
4
|
+
const common = require("@prosopo/common");
|
|
5
|
+
const authMiddleware = (pair, authAccount) => {
|
|
6
|
+
return async (req, res, next) => {
|
|
7
|
+
try {
|
|
8
|
+
const { signature, timestamp } = extractHeaders(req);
|
|
9
|
+
let error;
|
|
10
|
+
if (authAccount) {
|
|
11
|
+
try {
|
|
12
|
+
verifySignature(signature, timestamp, authAccount);
|
|
13
|
+
next();
|
|
14
|
+
return;
|
|
15
|
+
} catch (e) {
|
|
16
|
+
req.logger.warn(() => ({
|
|
17
|
+
err: e,
|
|
18
|
+
data: {
|
|
19
|
+
account: authAccount?.address
|
|
20
|
+
}
|
|
21
|
+
}));
|
|
22
|
+
error = e;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
if (pair) {
|
|
26
|
+
verifySignature(signature, timestamp, pair);
|
|
27
|
+
next();
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
res.status(401).json({
|
|
31
|
+
error: "Unauthorized",
|
|
32
|
+
message: new common.ProsopoEnvError(error || "CONTRACT.CANNOT_FIND_KEYPAIR")
|
|
33
|
+
});
|
|
34
|
+
return;
|
|
35
|
+
} catch (err) {
|
|
36
|
+
req.logger.error(() => ({ err, msg: "Auth Middleware Error" }));
|
|
37
|
+
res.status(401).json({ error: "Unauthorized", message: err });
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const extractHeaders = (req) => {
|
|
43
|
+
const signature = req.headers.signature;
|
|
44
|
+
const timestamp = req.headers.timestamp;
|
|
45
|
+
if (!timestamp) {
|
|
46
|
+
throw new common.ProsopoApiError("GENERAL.INVALID_TIMESTAMP", {
|
|
47
|
+
context: { error: "Missing timestamp", code: 400 }
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (!signature) {
|
|
51
|
+
throw new common.ProsopoApiError("GENERAL.INVALID_SIGNATURE", {
|
|
52
|
+
context: { error: "Missing signature", code: 400 }
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(signature) || Array.isArray(timestamp) || !util.isHex(signature)) {
|
|
56
|
+
throw new common.ProsopoApiError("CONTRACT.INVALID_DATA_FORMAT", {
|
|
57
|
+
context: { error: "Invalid header format", code: 400 }
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const now = (/* @__PURE__ */ new Date()).getTime();
|
|
61
|
+
const ts = Number.parseInt(timestamp);
|
|
62
|
+
if (now - ts > 3e5) {
|
|
63
|
+
throw new common.ProsopoApiError("GENERAL.INVALID_TIMESTAMP", {
|
|
64
|
+
context: { error: "Timestamp is too old", code: 400 }
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return { signature, timestamp };
|
|
68
|
+
};
|
|
69
|
+
const verifySignature = (signature, message, pair) => {
|
|
70
|
+
const u8Sig = util.hexToU8a(signature);
|
|
71
|
+
if (!pair.verify(message, u8Sig, pair.publicKey)) {
|
|
72
|
+
throw new common.ProsopoApiError("GENERAL.INVALID_SIGNATURE", {
|
|
73
|
+
context: {
|
|
74
|
+
error: "Signature verification failed",
|
|
75
|
+
code: 401,
|
|
76
|
+
account: pair.address,
|
|
77
|
+
message,
|
|
78
|
+
signature
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
exports.authMiddleware = authMiddleware;
|
|
84
|
+
exports.verifySignature = verifySignature;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const common = require("@prosopo/common");
|
|
4
|
+
const uuid = require("uuid");
|
|
5
|
+
function requestLoggerMiddleware(env) {
|
|
6
|
+
return (req, res, next) => {
|
|
7
|
+
const requestId = req.headers["x-request-id"] || `e-${uuid.v4()}`;
|
|
8
|
+
const logger = common.getLogger(
|
|
9
|
+
common.parseLogLevel(env.config.logLevel),
|
|
10
|
+
"request-logger"
|
|
11
|
+
).with({
|
|
12
|
+
requestId
|
|
13
|
+
});
|
|
14
|
+
req.logger = logger;
|
|
15
|
+
req.requestId = requestId;
|
|
16
|
+
next();
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
exports.requestLoggerMiddleware = requestLoggerMiddleware;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiExpressDefaultEndpointAdapter.d.ts","sourceRoot":"","sources":["../../src/endpointAdapter/apiExpressDefaultEndpointAdapter.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACtD,OAAO,EAAE,KAAK,QAAQ,EAAmB,MAAM,iBAAiB,CAAC;AACjE,OAAO,KA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,EAClB,IAAI,EAAE,YAAY,GAChB,OAAO,CAAC,IAAI,CAAC;CA2BhB;AAED,OAAO,EAAE,gCAAgC,EAAE,CAAC"}
|
|
@@ -19,7 +19,9 @@ class ApiExpressDefaultEndpointAdapter {
|
|
|
19
19
|
response.json(apiEndpointResponse);
|
|
20
20
|
}
|
|
21
21
|
catch (error) {
|
|
22
|
-
request.logger.error(
|
|
22
|
+
request.logger.error(() => ({
|
|
23
|
+
err: error,
|
|
24
|
+
}));
|
|
23
25
|
response.status(500).send("An internal server error occurred.");
|
|
24
26
|
}
|
|
25
27
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiExpressDefaultEndpointAdapter.js","sourceRoot":"","sources":["../../src/endpointAdapter/apiExpressDefaultEndpointAdapter.ts"],"names":[],"mappings":"AAeA,OAAO,EAAiB,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAKjE,MAAM,gCAAgC;IACrC,YACkB,QAAkB,EAClB,eAAuB;QADvB,aAAQ,GAAR,QAAQ,CAAU;QAClB,oBAAe,GAAf,eAAe,CAAQ;IACtC,CAAC;IAEG,KAAK,CAAC,aAAa,CACzB,QAA0C,EAC1C,OAAgB,EAChB,QAAkB,EAClB,IAAkB;QAElB,IAAI,IAAa,CAAC;QAClB,IAAI,CAAC;YACJ,IAAI,GAAG,QAAQ,CAAC,oBAAoB,EAAE,EAAE,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC7D,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,OAAO,IAAI,CACV,IAAI,eAAe,CAAC,iBAAiB,EAAE;gBACtC,OAAO,EAAE,EAAE,IAAI,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE;aACpC,CAAC,CACF,CAAC;QACH,CAAC;QAED,IAAI,CAAC;YACJ,MAAM,mBAAmB,GAAG,MAAM,QAAQ,CAAC,cAAc,CACxD,IAAI,EACJ,OAAO,CAAC,MAAM,CACd,CAAC;YAEF,QA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CAAoC,CAAC,CAAC;QACjE,CAAC;IACF,CAAC;CACD;AAED,OAAO,EAAE,gCAAgC,EAAE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -5,4 +5,6 @@ declare const apiExpressRouterFactory: ApiExpressRouterFactory;
|
|
|
5
5
|
declare const createApiExpressDefaultEndpointAdapter: (logLevel: LogLevel, errorStatusCode?: number) => ApiExpressEndpointAdapter;
|
|
6
6
|
export { apiExpressRouterFactory, createApiExpressDefaultEndpointAdapter, type ApiExpressEndpointAdapter, };
|
|
7
7
|
export * from "./errorHandler.js";
|
|
8
|
+
export * from "./middlewares/authMiddleware.js";
|
|
9
|
+
export * from "./middlewares/requestLoggerMiddleware.js";
|
|
8
10
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAC;AAEhG,QAAA,MAAM,uBAAuB,yBAAgC,CAAC;AAE9D,QAAA,MAAM,sCAAsC,aACjC,QAAQ,+BAEhB,yBAEF,CAAC;AAEF,OAAO,EACN,uBAAuB,EACvB,sCAAsC,EACtC,KAAK,yBAAyB,GAC9B,CAAC;AAEF,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AAEvE,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,gDAAgD,CAAC;AAEhG,QAAA,MAAM,uBAAuB,yBAAgC,CAAC;AAE9D,QAAA,MAAM,sCAAsC,aACjC,QAAQ,+BAEhB,yBAEF,CAAC;AAEF,OAAO,EACN,uBAAuB,EACvB,sCAAsC,EACtC,KAAK,yBAAyB,GAC9B,CAAC;AAEF,cAAc,mBAAmB,CAAC;AAClC,cAAc,iCAAiC,CAAC;AAChD,cAAc,0CAA0C,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -6,4 +6,6 @@ const createApiExpressDefaultEndpointAdapter = (logLevel, errorStatusCode = 500)
|
|
|
6
6
|
};
|
|
7
7
|
export { apiExpressRouterFactory, createApiExpressDefaultEndpointAdapter, };
|
|
8
8
|
export * from "./errorHandler.js";
|
|
9
|
+
export * from "./middlewares/authMiddleware.js";
|
|
10
|
+
export * from "./middlewares/requestLoggerMiddleware.js";
|
|
9
11
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,gCAAgC,EAAE,MAAM,uDAAuD,CAAC;AAGzG,MAAM,uBAAuB,GAAG,IAAI,uBAAuB,EAAE,CAAC;AAE9D,MAAM,sCAAsC,GAAG,CAC9C,QAAkB,EAClB,eAAe,GAAG,GAAG,EACO,EAAE;IAC9B,OAAO,IAAI,gCAAgC,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC;AACxE,CAAC,CAAC;AAEF,OAAO,EACN,uBAAuB,EACvB,sCAAsC,GAEtC,CAAC;AAEF,cAAc,mBAAmB,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,uBAAuB,EAAE,MAAM,8BAA8B,CAAC;AACvE,OAAO,EAAE,gCAAgC,EAAE,MAAM,uDAAuD,CAAC;AAGzG,MAAM,uBAAuB,GAAG,IAAI,uBAAuB,EAAE,CAAC;AAE9D,MAAM,sCAAsC,GAAG,CAC9C,QAAkB,EAClB,eAAe,GAAG,GAAG,EACO,EAAE;IAC9B,OAAO,IAAI,gCAAgC,CAAC,QAAQ,EAAE,eAAe,CAAC,CAAC;AACxE,CAAC,CAAC;AAEF,OAAO,EACN,uBAAuB,EACvB,sCAAsC,GAEtC,CAAC;AAEF,cAAc,mBAAmB,CAAC;AAClC,cAAc,iCAAiC,CAAC;AAChD,cAAc,0CAA0C,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { KeyringPair } from "@prosopo/types";
|
|
2
|
+
import type { NextFunction, Request, Response } from "express";
|
|
3
|
+
export declare const authMiddleware: (pair: KeyringPair | undefined, authAccount?: KeyringPair | undefined) => (req: Request, res: Response, next: NextFunction) => Promise<void>;
|
|
4
|
+
export declare const verifySignature: (signature: string, message: string, pair: KeyringPair) => void;
|
|
5
|
+
//# sourceMappingURL=authMiddleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authMiddleware.d.ts","sourceRoot":"","sources":["../../src/middlewares/authMiddleware.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAE/D,eAAO,MAAM,cAAc,SACpB,WAAW,GAAG,SAAS,gBACf,WAAW,GAAG,SAAS,WAElB,OAAO,OAAO,QAAQ,QAAQ,YAAY,kBAwC7D,CAAC;AAyCF,eAAO,MAAM,eAAe,cAChB,MAAM,WACR,MAAM,QACT,WAAW,SAejB,CAAC"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { hexToU8a, isHex } from "@polkadot/util";
|
|
2
|
+
import { ProsopoApiError, ProsopoEnvError } from "@prosopo/common";
|
|
3
|
+
export const authMiddleware = (pair, authAccount) => {
|
|
4
|
+
return async (req, res, next) => {
|
|
5
|
+
try {
|
|
6
|
+
const { signature, timestamp } = extractHeaders(req);
|
|
7
|
+
let error;
|
|
8
|
+
if (authAccount) {
|
|
9
|
+
try {
|
|
10
|
+
verifySignature(signature, timestamp, authAccount);
|
|
11
|
+
next();
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
catch (e) {
|
|
15
|
+
req.logger.warn(() => ({
|
|
16
|
+
err: e,
|
|
17
|
+
data: {
|
|
18
|
+
account: authAccount?.address,
|
|
19
|
+
},
|
|
20
|
+
}));
|
|
21
|
+
error = e;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (pair) {
|
|
25
|
+
verifySignature(signature, timestamp, pair);
|
|
26
|
+
next();
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
res.status(401).json({
|
|
30
|
+
error: "Unauthorized",
|
|
31
|
+
message: new ProsopoEnvError(error || "CONTRACT.CANNOT_FIND_KEYPAIR"),
|
|
32
|
+
});
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
catch (err) {
|
|
36
|
+
req.logger.error(() => ({ err, msg: "Auth Middleware Error" }));
|
|
37
|
+
res.status(401).json({ error: "Unauthorized", message: err });
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
const extractHeaders = (req) => {
|
|
43
|
+
const signature = req.headers.signature;
|
|
44
|
+
const timestamp = req.headers.timestamp;
|
|
45
|
+
if (!timestamp) {
|
|
46
|
+
throw new ProsopoApiError("GENERAL.INVALID_TIMESTAMP", {
|
|
47
|
+
context: { error: "Missing timestamp", code: 400 },
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
if (!signature) {
|
|
51
|
+
throw new ProsopoApiError("GENERAL.INVALID_SIGNATURE", {
|
|
52
|
+
context: { error: "Missing signature", code: 400 },
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
if (Array.isArray(signature) ||
|
|
56
|
+
Array.isArray(timestamp) ||
|
|
57
|
+
!isHex(signature)) {
|
|
58
|
+
throw new ProsopoApiError("CONTRACT.INVALID_DATA_FORMAT", {
|
|
59
|
+
context: { error: "Invalid header format", code: 400 },
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
const now = new Date().getTime();
|
|
63
|
+
const ts = Number.parseInt(timestamp);
|
|
64
|
+
if (now - ts > 300000) {
|
|
65
|
+
throw new ProsopoApiError("GENERAL.INVALID_TIMESTAMP", {
|
|
66
|
+
context: { error: "Timestamp is too old", code: 400 },
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
return { signature, timestamp };
|
|
70
|
+
};
|
|
71
|
+
export const verifySignature = (signature, message, pair) => {
|
|
72
|
+
const u8Sig = hexToU8a(signature);
|
|
73
|
+
if (!pair.verify(message, u8Sig, pair.publicKey)) {
|
|
74
|
+
throw new ProsopoApiError("GENERAL.INVALID_SIGNATURE", {
|
|
75
|
+
context: {
|
|
76
|
+
error: "Signature verification failed",
|
|
77
|
+
code: 401,
|
|
78
|
+
account: pair.address,
|
|
79
|
+
message,
|
|
80
|
+
signature,
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
//# sourceMappingURL=authMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/authMiddleware.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAInE,MAAM,CAAC,MAAM,cAAc,GAAG,CAC7B,IAA6B,EAC7B,WAAqC,EACpC,EAAE;IACH,OAAO,KAAK,EAAE,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;QAChE,IAAI,CAAC;YACJ,MAAM,EAAE,SAA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AAe,EACf,IAAiB,EAChB,EAAE;IACH,MAAM,KAAK,GAAG,QAAQ,CAAC,SAAS,CAAC,CAAC;IAElC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;QAClD,MAAM,IAAI,eAAe,CAAC,2BAA2B,EAAE;YACtD,OAAO,EAAE;gBACR,KAAK,EAAE,+BAA+B;gBACtC,IAAI,EAAE,GAAG;gBACT,OAAO,EAAE,IAAI,CAAC,OAAO;gBACrB,OAAO;gBACP,SAAS;aACT;SACD,CAAC,CAAC;IACJ,CAAC;AACF,CAAC,CAAC"}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ProviderEnvironment } from "@prosopo/env";
|
|
2
|
+
import type { NextFunction, Request, Response } from "express";
|
|
3
|
+
export declare function requestLoggerMiddleware(env: ProviderEnvironment): (req: Request, res: Response, next: NextFunction) => void;
|
|
4
|
+
//# sourceMappingURL=requestLoggerMiddleware.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requestLoggerMiddleware.d.ts","sourceRoot":"","sources":["../../src/middlewares/requestLoggerMiddleware.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,KAAK,EAAE,YAAY,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AAG/D,wBAAgB,uBAAuB,CAAC,GAAG,EAAE,mBAAmB,SAClD,OAAO,OAAO,QAAQ,QAAQ,YAAY,UAkBvD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { getLogger, parseLogLevel } from "@prosopo/common";
|
|
2
|
+
import { v4 as uuidv4 } from "uuid";
|
|
3
|
+
export function requestLoggerMiddleware(env) {
|
|
4
|
+
return (req, res, next) => {
|
|
5
|
+
const requestId = req.headers["x-request-id"] || `e-${uuidv4()}`;
|
|
6
|
+
const logger = getLogger(parseLogLevel(env.config.logLevel), "request-logger").with({
|
|
7
|
+
requestId,
|
|
8
|
+
});
|
|
9
|
+
req.logger = logger;
|
|
10
|
+
req.requestId = requestId;
|
|
11
|
+
next();
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=requestLoggerMiddleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"requestLoggerMiddleware.js","sourceRoot":"","sources":["../../src/middlewares/requestLoggerMiddleware.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAG3D,OAAO,EAAE,EAAE,IAAI,MAAM,EAAE,MAAM,MAAM,CAAC;AAEpC,MAAM,UAAU,uBAAuB,CAAC,GAAwB;IAC/D,OAAO,CAAC,GAAY,EAAE,GAAa,EAAE,IAAkB,EAAE,EAAE;QAC1D,MAAM,SAAS,GACb,GAAG,CAAC,OAAO,CAAC,cAAc,CAAY,IAAI,KAAK,MAAM,EAAE,EAAE,CAAC;QAE5D,MAAM,MAAM,GAAG,SAAS,CACvB,aAAa,CAAC,GAAG,CAAC,MAAM,CAAC,QAAQ,CAAC,EAClC,gBAAgB,CAChB,CAAC,IAAI,CAAC;YACN,SAAS;SACT,CAAC,CAAC;QAGH,GAAG,CAAC,MAAM,GAAG,MAAM,CAAC;QACpB,GAAG,CAAC,SAAS,GAAG,SAAS,CAAC;QAG1B,IAAI,EAAE,CAAC;IACR,CAAC,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authMiddleware.unit.test.d.ts","sourceRoot":"","sources":["../../../../src/tests/unit/middlewares/authMiddleware.unit.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { hexToU8a, isHex } from "@polkadot/util";
|
|
2
|
+
import { ProsopoApiError, ProsopoEnvError, getLogger, } from "@prosopo/common";
|
|
3
|
+
import { describe, expect, it, vi } from "vitest";
|
|
4
|
+
import { authMiddleware } from "../../../middlewares/authMiddleware.js";
|
|
5
|
+
const loggerOuter = getLogger("info", import.meta.url);
|
|
6
|
+
const mockLogger = {
|
|
7
|
+
debug: vi.fn().mockImplementation(loggerOuter.debug.bind(loggerOuter)),
|
|
8
|
+
log: vi.fn().mockImplementation(loggerOuter.log.bind(loggerOuter)),
|
|
9
|
+
info: vi.fn().mockImplementation(loggerOuter.info.bind(loggerOuter)),
|
|
10
|
+
error: vi.fn().mockImplementation(loggerOuter.error.bind(loggerOuter)),
|
|
11
|
+
trace: vi.fn().mockImplementation(loggerOuter.trace.bind(loggerOuter)),
|
|
12
|
+
fatal: vi.fn().mockImplementation(loggerOuter.fatal.bind(loggerOuter)),
|
|
13
|
+
warn: vi.fn().mockImplementation(loggerOuter.warn.bind(loggerOuter)),
|
|
14
|
+
};
|
|
15
|
+
vi.mock("@polkadot/util", async (importOriginal) => {
|
|
16
|
+
const actual = await importOriginal();
|
|
17
|
+
return {
|
|
18
|
+
...actual,
|
|
19
|
+
hexToU8a: vi.fn(),
|
|
20
|
+
isHex: vi.fn(),
|
|
21
|
+
};
|
|
22
|
+
});
|
|
23
|
+
const mockPair = {
|
|
24
|
+
publicKey: "mockPublicKey",
|
|
25
|
+
verify: vi.fn(),
|
|
26
|
+
};
|
|
27
|
+
const mockEnv = {
|
|
28
|
+
pair: mockPair,
|
|
29
|
+
authAccount: mockPair,
|
|
30
|
+
logger: mockLogger,
|
|
31
|
+
};
|
|
32
|
+
describe("authMiddleware", () => {
|
|
33
|
+
it("should call next() if signature is valid", async () => {
|
|
34
|
+
const mockLogger = {
|
|
35
|
+
debug: vi.fn().mockImplementation(loggerOuter.debug.bind(loggerOuter)),
|
|
36
|
+
log: vi.fn().mockImplementation(loggerOuter.log.bind(loggerOuter)),
|
|
37
|
+
info: vi.fn().mockImplementation(loggerOuter.info.bind(loggerOuter)),
|
|
38
|
+
error: vi.fn().mockImplementation(loggerOuter.error.bind(loggerOuter)),
|
|
39
|
+
trace: vi.fn().mockImplementation(loggerOuter.trace.bind(loggerOuter)),
|
|
40
|
+
fatal: vi.fn().mockImplementation(loggerOuter.fatal.bind(loggerOuter)),
|
|
41
|
+
warn: vi.fn().mockImplementation(loggerOuter.warn.bind(loggerOuter)),
|
|
42
|
+
};
|
|
43
|
+
const mockReq = {
|
|
44
|
+
url: "/v1/prosopo/provider/captcha/image",
|
|
45
|
+
originalUrl: "/v1/prosopo/provider/captcha/image",
|
|
46
|
+
headers: {
|
|
47
|
+
signature: "0x1234",
|
|
48
|
+
timestamp: new Date().getTime(),
|
|
49
|
+
},
|
|
50
|
+
logger: mockLogger,
|
|
51
|
+
};
|
|
52
|
+
const mockRes = {
|
|
53
|
+
status: vi.fn().mockReturnThis(),
|
|
54
|
+
json: vi.fn(),
|
|
55
|
+
};
|
|
56
|
+
const mockNext = vi.fn();
|
|
57
|
+
vi.mocked(isHex).mockReturnValue(true);
|
|
58
|
+
vi.mocked(hexToU8a).mockReturnValue(new Uint8Array());
|
|
59
|
+
vi.mocked(mockPair.verify).mockReturnValue(true);
|
|
60
|
+
const middleware = authMiddleware(mockEnv.pair, mockEnv.authAccount);
|
|
61
|
+
await middleware(mockReq, mockRes, mockNext);
|
|
62
|
+
expect(mockNext).toHaveBeenCalled();
|
|
63
|
+
expect(mockRes.status).not.toHaveBeenCalled();
|
|
64
|
+
});
|
|
65
|
+
it("should return 401 if signature is invalid", async () => {
|
|
66
|
+
const mockLogger = {
|
|
67
|
+
debug: vi.fn().mockImplementation(loggerOuter.debug.bind(loggerOuter)),
|
|
68
|
+
log: vi.fn().mockImplementation(loggerOuter.log.bind(loggerOuter)),
|
|
69
|
+
info: vi.fn().mockImplementation(loggerOuter.info.bind(loggerOuter)),
|
|
70
|
+
error: vi.fn().mockImplementation(loggerOuter.error.bind(loggerOuter)),
|
|
71
|
+
trace: vi.fn().mockImplementation(loggerOuter.trace.bind(loggerOuter)),
|
|
72
|
+
fatal: vi.fn().mockImplementation(loggerOuter.fatal.bind(loggerOuter)),
|
|
73
|
+
warn: vi.fn().mockImplementation(loggerOuter.warn.bind(loggerOuter)),
|
|
74
|
+
};
|
|
75
|
+
const mockReq = {
|
|
76
|
+
url: "/v1/prosopo/provider/captcha/image",
|
|
77
|
+
originalUrl: "/v1/prosopo/provider/captcha/image",
|
|
78
|
+
headers: {
|
|
79
|
+
signature: "0x1234",
|
|
80
|
+
timestamp: new Date().getTime(),
|
|
81
|
+
},
|
|
82
|
+
logger: mockLogger,
|
|
83
|
+
};
|
|
84
|
+
const mockRes = {
|
|
85
|
+
status: vi.fn().mockReturnThis(),
|
|
86
|
+
json: vi.fn(),
|
|
87
|
+
};
|
|
88
|
+
const mockNext = vi.fn();
|
|
89
|
+
vi.mocked(isHex).mockReturnValue(true);
|
|
90
|
+
vi.mocked(hexToU8a).mockReturnValue(new Uint8Array());
|
|
91
|
+
vi.mocked(mockPair.verify).mockReturnValue(false);
|
|
92
|
+
const middleware = authMiddleware(mockEnv.pair, mockEnv.authAccount);
|
|
93
|
+
await middleware(mockReq, mockRes, mockNext);
|
|
94
|
+
expect(mockNext).not.toHaveBeenCalled();
|
|
95
|
+
expect(mockRes.status).toHaveBeenCalledWith(401);
|
|
96
|
+
expect(mockRes.json).toHaveBeenCalledWith({
|
|
97
|
+
error: "Unauthorized",
|
|
98
|
+
message: expect.any(ProsopoApiError),
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
it("should return 401 if key pair is missing", async () => {
|
|
102
|
+
const mockLogger = {
|
|
103
|
+
debug: vi.fn().mockImplementation(loggerOuter.debug.bind(loggerOuter)),
|
|
104
|
+
log: vi.fn().mockImplementation(loggerOuter.log.bind(loggerOuter)),
|
|
105
|
+
info: vi.fn().mockImplementation(loggerOuter.info.bind(loggerOuter)),
|
|
106
|
+
error: vi.fn().mockImplementation(loggerOuter.error.bind(loggerOuter)),
|
|
107
|
+
trace: vi.fn().mockImplementation(loggerOuter.trace.bind(loggerOuter)),
|
|
108
|
+
fatal: vi.fn().mockImplementation(loggerOuter.fatal.bind(loggerOuter)),
|
|
109
|
+
warn: vi.fn().mockImplementation(loggerOuter.warn.bind(loggerOuter)),
|
|
110
|
+
};
|
|
111
|
+
const mockReq = {
|
|
112
|
+
url: "/v1/prosopo/provider/captcha/image",
|
|
113
|
+
originalUrl: "/v1/prosopo/provider/captcha/image",
|
|
114
|
+
headers: {
|
|
115
|
+
signature: "0x1234",
|
|
116
|
+
timestamp: new Date().getTime(),
|
|
117
|
+
},
|
|
118
|
+
logger: mockLogger,
|
|
119
|
+
};
|
|
120
|
+
const mockRes = {
|
|
121
|
+
status: vi.fn().mockReturnThis(),
|
|
122
|
+
json: vi.fn(),
|
|
123
|
+
};
|
|
124
|
+
const mockNext = vi.fn();
|
|
125
|
+
const middleware = authMiddleware(undefined, undefined);
|
|
126
|
+
await middleware(mockReq, mockRes, mockNext);
|
|
127
|
+
expect(mockNext).not.toHaveBeenCalled();
|
|
128
|
+
expect(mockRes.status).toHaveBeenCalledWith(401);
|
|
129
|
+
expect(mockRes.json).toHaveBeenCalledWith({
|
|
130
|
+
error: "Unauthorized",
|
|
131
|
+
message: expect.any(ProsopoEnvError),
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
//# sourceMappingURL=authMiddleware.unit.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authMiddleware.unit.test.js","sourceRoot":"","sources":["../../../../src/tests/unit/middlewares/authMiddleware.unit.test.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,EAEN,eAAe,EACf,eAAe,EACf,SAAS,GACT,MAAM,iBAAiB,CAAC;AAGzB,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,QAAQ,CAAC;AAClD,OAAO,EAAE,cAAc,EAAE,MAAM,wCAAwC,CAAC;AAExE,MAAM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": "dist/index.js",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -20,12 +20,13 @@
|
|
|
20
20
|
"build:cjs": "npx vite --config vite.cjs.config.ts build"
|
|
21
21
|
},
|
|
22
22
|
"dependencies": {
|
|
23
|
-
"@prosopo/api-route": "2.
|
|
24
|
-
"@prosopo/common": "
|
|
23
|
+
"@prosopo/api-route": "2.6.7",
|
|
24
|
+
"@prosopo/common": "3.0.2",
|
|
25
|
+
"uuid": "11.1.0",
|
|
25
26
|
"zod": "3.23.8"
|
|
26
27
|
},
|
|
27
28
|
"devDependencies": {
|
|
28
|
-
"@prosopo/config": "
|
|
29
|
+
"@prosopo/config": "3.1.0"
|
|
29
30
|
},
|
|
30
31
|
"author": "PROSOPO LIMITED <info@prosopo.io>",
|
|
31
32
|
"license": "Apache-2.0",
|
package/vite.test.config.ts
CHANGED