apitally 0.1.0
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/LICENSE +21 -0
- package/README.md +136 -0
- package/dist/common/client.d.ts +37 -0
- package/dist/common/client.d.ts.map +1 -0
- package/dist/common/client.js +243 -0
- package/dist/common/client.js.map +1 -0
- package/dist/common/index.d.ts +2 -0
- package/dist/common/index.d.ts.map +1 -0
- package/dist/common/index.js +6 -0
- package/dist/common/index.js.map +1 -0
- package/dist/common/keyRegistry.d.ts +29 -0
- package/dist/common/keyRegistry.d.ts.map +1 -0
- package/dist/common/keyRegistry.js +90 -0
- package/dist/common/keyRegistry.js.map +1 -0
- package/dist/common/logging.d.ts +8 -0
- package/dist/common/logging.d.ts.map +1 -0
- package/dist/common/logging.js +13 -0
- package/dist/common/logging.js.map +1 -0
- package/dist/common/requestLogger.d.ts +10 -0
- package/dist/common/requestLogger.d.ts.map +1 -0
- package/dist/common/requestLogger.js +49 -0
- package/dist/common/requestLogger.js.map +1 -0
- package/dist/common/types.d.ts +56 -0
- package/dist/common/types.d.ts.map +1 -0
- package/dist/common/types.js +3 -0
- package/dist/common/types.js.map +1 -0
- package/dist/common/utils.d.ts +4 -0
- package/dist/common/utils.d.ts.map +1 -0
- package/dist/common/utils.js +23 -0
- package/dist/common/utils.js.map +1 -0
- package/dist/common/validationErrorLogger.d.ts +9 -0
- package/dist/common/validationErrorLogger.d.ts.map +1 -0
- package/dist/common/validationErrorLogger.js +40 -0
- package/dist/common/validationErrorLogger.js.map +1 -0
- package/dist/express/auth.d.ts +6 -0
- package/dist/express/auth.d.ts.map +1 -0
- package/dist/express/auth.js +66 -0
- package/dist/express/auth.js.map +1 -0
- package/dist/express/index.d.ts +3 -0
- package/dist/express/index.d.ts.map +1 -0
- package/dist/express/index.js +8 -0
- package/dist/express/index.js.map +1 -0
- package/dist/express/listEndpoints.d.ts +3 -0
- package/dist/express/listEndpoints.d.ts.map +1 -0
- package/dist/express/listEndpoints.js +166 -0
- package/dist/express/listEndpoints.js.map +1 -0
- package/dist/express/middleware.d.ts +4 -0
- package/dist/express/middleware.d.ts.map +1 -0
- package/dist/express/middleware.js +164 -0
- package/dist/express/middleware.js.map +1 -0
- package/dist/fastify/auth.d.ts +6 -0
- package/dist/fastify/auth.d.ts.map +1 -0
- package/dist/fastify/auth.js +66 -0
- package/dist/fastify/auth.js.map +1 -0
- package/dist/fastify/index.d.ts +3 -0
- package/dist/fastify/index.d.ts.map +1 -0
- package/dist/fastify/index.js +11 -0
- package/dist/fastify/index.js.map +1 -0
- package/dist/fastify/plugin.d.ts +16 -0
- package/dist/fastify/plugin.d.ts.map +1 -0
- package/dist/fastify/plugin.js +122 -0
- package/dist/fastify/plugin.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/koa/auth.d.ts +6 -0
- package/dist/koa/auth.d.ts.map +1 -0
- package/dist/koa/auth.js +67 -0
- package/dist/koa/auth.js.map +1 -0
- package/dist/koa/index.d.ts +3 -0
- package/dist/koa/index.d.ts.map +1 -0
- package/dist/koa/index.js +8 -0
- package/dist/koa/index.js.map +1 -0
- package/dist/koa/middleware.d.ts +4 -0
- package/dist/koa/middleware.d.ts.map +1 -0
- package/dist/koa/middleware.js +108 -0
- package/dist/koa/middleware.js.map +1 -0
- package/dist/nestjs/auth.d.ts +10 -0
- package/dist/nestjs/auth.d.ts.map +1 -0
- package/dist/nestjs/auth.js +84 -0
- package/dist/nestjs/auth.js.map +1 -0
- package/dist/nestjs/index.d.ts +3 -0
- package/dist/nestjs/index.d.ts.map +1 -0
- package/dist/nestjs/index.js +9 -0
- package/dist/nestjs/index.js.map +1 -0
- package/package.json +128 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.useApitally = void 0;
|
|
7
|
+
const perf_hooks_1 = require("perf_hooks");
|
|
8
|
+
const client_1 = require("../common/client");
|
|
9
|
+
const keyRegistry_1 = require("../common/keyRegistry");
|
|
10
|
+
const utils_1 = require("../common/utils");
|
|
11
|
+
const listEndpoints_1 = __importDefault(require("./listEndpoints"));
|
|
12
|
+
const useApitally = (app, config) => {
|
|
13
|
+
const client = new client_1.ApitallyClient(config);
|
|
14
|
+
const middleware = getMiddleware(client);
|
|
15
|
+
app.use(middleware);
|
|
16
|
+
setTimeout(() => {
|
|
17
|
+
client.setAppInfo(getAppInfo(app, config.appVersion));
|
|
18
|
+
}, 100);
|
|
19
|
+
};
|
|
20
|
+
exports.useApitally = useApitally;
|
|
21
|
+
const getMiddleware = (client) => {
|
|
22
|
+
const validatorInstalled = (0, utils_1.getPackageVersion)("express-validator") !== null;
|
|
23
|
+
const celebrateInstalled = (0, utils_1.getPackageVersion)("celebrate") !== null;
|
|
24
|
+
const nestInstalled = (0, utils_1.getPackageVersion)("@nestjs/core") !== null;
|
|
25
|
+
const classValidatorInstalled = (0, utils_1.getPackageVersion)("class-validator") !== null;
|
|
26
|
+
return (req, res, next) => {
|
|
27
|
+
try {
|
|
28
|
+
const startTime = perf_hooks_1.performance.now();
|
|
29
|
+
const originalJson = res.json;
|
|
30
|
+
res.json = (body) => {
|
|
31
|
+
res.locals.body = body;
|
|
32
|
+
return originalJson.call(res, body);
|
|
33
|
+
};
|
|
34
|
+
res.on("finish", () => {
|
|
35
|
+
try {
|
|
36
|
+
if (req.route) {
|
|
37
|
+
const responseTime = perf_hooks_1.performance.now() - startTime;
|
|
38
|
+
client.requestLogger.logRequest({
|
|
39
|
+
consumer: getConsumer(res),
|
|
40
|
+
method: req.method,
|
|
41
|
+
path: req.route.path,
|
|
42
|
+
statusCode: res.statusCode,
|
|
43
|
+
responseTime: responseTime,
|
|
44
|
+
});
|
|
45
|
+
if ((res.statusCode === 400 || res.statusCode === 422) &&
|
|
46
|
+
res.locals.body) {
|
|
47
|
+
const validationErrors = [];
|
|
48
|
+
if (validatorInstalled) {
|
|
49
|
+
validationErrors.push(...extractExpressValidatorErrors(res.locals.body));
|
|
50
|
+
}
|
|
51
|
+
if (celebrateInstalled) {
|
|
52
|
+
validationErrors.push(...extractCelebrateErrors(res.locals.body));
|
|
53
|
+
}
|
|
54
|
+
if (nestInstalled && classValidatorInstalled) {
|
|
55
|
+
validationErrors.push(...extractNestValidationErrors(res.locals.body));
|
|
56
|
+
}
|
|
57
|
+
validationErrors.forEach((error) => {
|
|
58
|
+
client.validationErrorLogger.logValidationError(Object.assign({ consumer: getConsumer(res), method: req.method, path: req.route.path }, error));
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
client.logger.error("Error while logging request in Apitally middleware.", { request: req, response: res, error });
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
catch (error) {
|
|
69
|
+
client.logger.error("Error in Apitally middleware.", {
|
|
70
|
+
request: req,
|
|
71
|
+
response: res,
|
|
72
|
+
error,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
finally {
|
|
76
|
+
next();
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
const getConsumer = (res) => {
|
|
81
|
+
if (res.locals.consumerIdentifier) {
|
|
82
|
+
return String(res.locals.consumerIdentifier);
|
|
83
|
+
}
|
|
84
|
+
if (res.locals.keyInfo && res.locals.keyInfo instanceof keyRegistry_1.KeyInfo) {
|
|
85
|
+
return `key:${res.locals.keyInfo.keyId}`;
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
};
|
|
89
|
+
const extractExpressValidatorErrors = (responseBody) => {
|
|
90
|
+
const errors = [];
|
|
91
|
+
if (responseBody &&
|
|
92
|
+
responseBody.errors &&
|
|
93
|
+
Array.isArray(responseBody.errors)) {
|
|
94
|
+
responseBody.errors.forEach((error) => {
|
|
95
|
+
if (error.location && error.path && error.msg && error.type) {
|
|
96
|
+
errors.push({
|
|
97
|
+
loc: `${error.location}.${error.path}`,
|
|
98
|
+
msg: error.msg,
|
|
99
|
+
type: error.type,
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
return errors;
|
|
105
|
+
};
|
|
106
|
+
const extractCelebrateErrors = (responseBody) => {
|
|
107
|
+
const errors = [];
|
|
108
|
+
if (responseBody && responseBody.validation) {
|
|
109
|
+
Object.values(responseBody.validation).forEach((error) => {
|
|
110
|
+
if (error.source &&
|
|
111
|
+
error.keys &&
|
|
112
|
+
Array.isArray(error.keys) &&
|
|
113
|
+
error.message) {
|
|
114
|
+
error.keys.forEach((key) => {
|
|
115
|
+
errors.push({
|
|
116
|
+
loc: `${error.source}.${key}`,
|
|
117
|
+
msg: subsetJoiMessage(error.message, key),
|
|
118
|
+
type: "",
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
return errors;
|
|
125
|
+
};
|
|
126
|
+
const extractNestValidationErrors = (responseBody) => {
|
|
127
|
+
const errors = [];
|
|
128
|
+
if (responseBody && Array.isArray(responseBody.message)) {
|
|
129
|
+
responseBody.message.forEach((message) => {
|
|
130
|
+
errors.push({
|
|
131
|
+
loc: "",
|
|
132
|
+
msg: message,
|
|
133
|
+
type: "",
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return errors;
|
|
138
|
+
};
|
|
139
|
+
const subsetJoiMessage = (message, key) => {
|
|
140
|
+
const messageWithKey = message
|
|
141
|
+
.split(". ")
|
|
142
|
+
.find((message) => message.includes(`"${key}"`));
|
|
143
|
+
return messageWithKey ? messageWithKey : message;
|
|
144
|
+
};
|
|
145
|
+
const getAppInfo = (app, appVersion) => {
|
|
146
|
+
const versions = [["nodejs", process.version]];
|
|
147
|
+
const expressVersion = (0, utils_1.getPackageVersion)("express");
|
|
148
|
+
const apitallyVersion = (0, utils_1.getPackageVersion)("../..");
|
|
149
|
+
if (expressVersion) {
|
|
150
|
+
versions.push(["express", expressVersion]);
|
|
151
|
+
}
|
|
152
|
+
if (apitallyVersion) {
|
|
153
|
+
versions.push(["apitally", apitallyVersion]);
|
|
154
|
+
}
|
|
155
|
+
if (appVersion) {
|
|
156
|
+
versions.push(["app", appVersion]);
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
paths: (0, listEndpoints_1.default)(app),
|
|
160
|
+
versions: new Map(versions),
|
|
161
|
+
client: "js:express",
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
//# sourceMappingURL=middleware.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.js","sourceRoot":"","sources":["../../src/express/middleware.ts"],"names":[],"mappings":";;;;;;AACA,2CAAyC;AAEzC,6CAAkD;AAClD,uDAAgD;AAEhD,2CAAoD;AACpD,oEAA4C;AAErC,MAAM,WAAW,GAAG,CAAC,GAAY,EAAE,MAAsB,EAAE,EAAE;IAClE,MAAM,MAAM,GAAG,IAAI,uBAAc,CAAC,MAAM,CAAC,CAAC;IAC1C,MAAM,UAAU,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;IACzC,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACpB,UAAU,CAAC,GAAG,EAAE;QACd,MAAM,CAAC,UAAU,CAAC,UAAU,CAAC,GAAG,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC,CAAC;IACxD,CAAC,EAAE,GAAG,CAAC,CAAC;AACV,CAAC,CAAC;AAPW,QAAA,WAAW,eAOtB;AAEF,MAAM,aAAa,GAAG,CAAC,MAAsB,EAAE,EAAE;IAC/C,MAAM,kBAAkB,GAAG,IAAA,yBAAiB,EAAC,mBAAmB,CAAC,KAAK,IAAI,C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UAAmB,EAAW,EAAE;IAChE,MAAM,QAAQ,GAA4B,CAAC,CAAC,QAAQ,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;IACxE,MAAM,cAAc,GAAG,IAAA,yBAAiB,EAAC,SAAS,CAAC,CAAC;IACpD,MAAM,eAAe,GAAG,IAAA,yBAAiB,EAAC,OAAO,CAAC,CAAC;IACnD,IAAI,cAAc,EAAE;QAClB,QAAQ,CAAC,IAAI,CAAC,CAAC,SAAS,EAAE,cAAc,CAAC,CAAC,CAAC;KAC5C;IACD,IAAI,eAAe,EAAE;QACnB,QAAQ,CAAC,IAAI,CAAC,CAAC,UAAU,EAAE,eAAe,CAAC,CAAC,CAAC;KAC9C;IACD,IAAI,UAAU,EAAE;QACd,QAAQ,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC;KACpC;IACD,OAAO;QACL,KAAK,EAAE,IAAA,uBAAa,EAAC,GAAG,CAAC;QACzB,QAAQ,EAAE,IAAI,GAAG,CAAC,QAAQ,CAAC;QAC3B,MAAM,EAAE,YAAY;KACrB,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DoneFuncWithErrOrRes, FastifyReply, FastifyRequest } from "fastify";
|
|
2
|
+
export declare const requireApiKey: ({ scopes, customHeader, }?: {
|
|
3
|
+
scopes?: string | string[] | undefined;
|
|
4
|
+
customHeader?: string | undefined;
|
|
5
|
+
}) => (request: FastifyRequest, reply: FastifyReply, done: DoneFuncWithErrOrRes) => Promise<void>;
|
|
6
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/fastify/auth.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG7E,eAAO,MAAM,aAAa;;;gBAQb,cAAc,SAChB,YAAY,QACb,oBAAoB,kBAqD7B,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.requireApiKey = void 0;
|
|
13
|
+
const client_1 = require("../common/client");
|
|
14
|
+
const requireApiKey = ({ scopes, customHeader, } = {}) => {
|
|
15
|
+
return (request, reply, done) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
let apiKey;
|
|
17
|
+
if (!customHeader) {
|
|
18
|
+
if (!request.headers.authorization) {
|
|
19
|
+
reply
|
|
20
|
+
.code(401)
|
|
21
|
+
.header("WWW-Authenticate", "ApiKey")
|
|
22
|
+
.send({ error: "Missing authorization header" });
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const authorizationParts = request.headers.authorization.split(" ");
|
|
26
|
+
if (authorizationParts.length === 2 &&
|
|
27
|
+
authorizationParts[0].toLowerCase() === "apikey") {
|
|
28
|
+
apiKey = authorizationParts[1];
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
reply
|
|
32
|
+
.code(401)
|
|
33
|
+
.header("WWW-Authenticate", "ApiKey")
|
|
34
|
+
.send({ error: "Invalid authorization scheme" });
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
else if (customHeader) {
|
|
39
|
+
const customHeaderValue = request.headers[customHeader.toLowerCase()];
|
|
40
|
+
if (typeof customHeaderValue === "string") {
|
|
41
|
+
apiKey = customHeaderValue;
|
|
42
|
+
}
|
|
43
|
+
else if (Array.isArray(customHeaderValue)) {
|
|
44
|
+
apiKey = customHeaderValue[0];
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (!apiKey) {
|
|
48
|
+
reply.code(403).send({ error: "Missing API key" });
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const client = client_1.ApitallyClient.getInstance();
|
|
52
|
+
const keyInfo = yield client.keyRegistry.get(apiKey);
|
|
53
|
+
if (!keyInfo) {
|
|
54
|
+
reply.code(403).send({ error: "Invalid API key" });
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (scopes && !keyInfo.hasScopes(scopes)) {
|
|
58
|
+
reply.code(403).send({ error: "Permission denied" });
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
request.keyInfo = keyInfo;
|
|
62
|
+
done();
|
|
63
|
+
});
|
|
64
|
+
};
|
|
65
|
+
exports.requireApiKey = requireApiKey;
|
|
66
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/fastify/auth.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,6CAAkD;AAE3C,MAAM,aAAa,GAAG,CAAC,EAC5B,MAAM,EACN,YAAY,MAIV,EAAE,EAAE,EAAE;IACR,OAAO,CACL,OAAuB,EACvB,KAAmB,EACnB,IAA0B,EAC1B,EAAE;QACF,IAAI,MAA0B,CAAC;QAE/B,IAAI,CAAC,YAAY,EAAE;YACjB,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE;gBAClC,KAAK;qBACF,IAAI,CAAC,GAAG,CAAC;qBACT,MAAM,CAAC,kBAAkB,EAAE,QAAQ,CAAC;qBACpC,IA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E,iBAAiB,EAAE,CAAC,CAAC;YACnD,OAAO;SACR;QACD,IAAI,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE;YACxC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC,CAAC;YACrD,OAAO;SACR;QAED,OAAO,CAAC,OAAO,GAAG,OAAO,CAAC;QAC1B,IAAI,EAAE,CAAC;IACT,CAAC,CAAA,CAAC;AACJ,CAAC,CAAC;AA/DW,QAAA,aAAa,iBA+DxB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/fastify/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,EAAE,OAAO,IAAI,cAAc,EAAE,MAAM,UAAU,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.apitallyPlugin = exports.requireApiKey = void 0;
|
|
7
|
+
var auth_1 = require("./auth");
|
|
8
|
+
Object.defineProperty(exports, "requireApiKey", { enumerable: true, get: function () { return auth_1.requireApiKey; } });
|
|
9
|
+
var plugin_1 = require("./plugin");
|
|
10
|
+
Object.defineProperty(exports, "apitallyPlugin", { enumerable: true, get: function () { return __importDefault(plugin_1).default; } });
|
|
11
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/fastify/index.ts"],"names":[],"mappings":";;;;;;AAAA,+BAAuC;AAA9B,qGAAA,aAAa,OAAA;AACtB,mCAAqD;AAA5C,yHAAA,OAAO,OAAkB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { FastifyInstance } from "fastify";
|
|
3
|
+
import { KeyInfo } from "../common/keyRegistry";
|
|
4
|
+
import { ApitallyConfig } from "../common/types";
|
|
5
|
+
declare module "fastify" {
|
|
6
|
+
interface FastifyReply {
|
|
7
|
+
payload: any;
|
|
8
|
+
}
|
|
9
|
+
interface FastifyRequest {
|
|
10
|
+
consumerIdentifier?: string;
|
|
11
|
+
keyInfo?: KeyInfo;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
declare const _default: (fastify: FastifyInstance<import("fastify").RawServerDefault, import("http").IncomingMessage, import("http").ServerResponse<import("http").IncomingMessage>, import("fastify").FastifyBaseLogger, import("fastify").FastifyTypeProviderDefault>, config: ApitallyConfig) => Promise<void>;
|
|
15
|
+
export default _default;
|
|
16
|
+
//# sourceMappingURL=plugin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.d.ts","sourceRoot":"","sources":["../../src/fastify/plugin.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,eAAe,EAAkB,MAAM,SAAS,CAAC;AAI1D,OAAO,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAChD,OAAO,EAAE,cAAc,EAA6B,MAAM,iBAAiB,CAAC;AAG5E,OAAO,QAAQ,SAAS,CAAC;IACvB,UAAU,YAAY;QACpB,OAAO,EAAE,GAAG,CAAC;KACd;IAED,UAAU,cAAc;QACtB,kBAAkB,CAAC,EAAE,MAAM,CAAC;QAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;KACnB;CACF;;AA4HD,wBAEG"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
16
|
+
const client_1 = require("../common/client");
|
|
17
|
+
const keyRegistry_1 = require("../common/keyRegistry");
|
|
18
|
+
const utils_1 = require("../common/utils");
|
|
19
|
+
const apitallyPlugin = (fastify, config) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const client = new client_1.ApitallyClient(config);
|
|
21
|
+
const routes = [];
|
|
22
|
+
fastify.decorateReply("payload", null);
|
|
23
|
+
fastify.addHook("onRoute", (routeOptions) => {
|
|
24
|
+
const methods = Array.isArray(routeOptions.method)
|
|
25
|
+
? routeOptions.method
|
|
26
|
+
: [routeOptions.method];
|
|
27
|
+
methods.forEach((method) => {
|
|
28
|
+
routeOptions.onSend;
|
|
29
|
+
if (method !== "HEAD") {
|
|
30
|
+
routes.push({
|
|
31
|
+
method: method,
|
|
32
|
+
path: routeOptions.url,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
fastify.addHook("onReady", () => {
|
|
38
|
+
client.setAppInfo(getAppInfo(routes, config.appVersion));
|
|
39
|
+
});
|
|
40
|
+
fastify.addHook("onSend", (request, reply, payload, done) => {
|
|
41
|
+
try {
|
|
42
|
+
reply.payload = JSON.parse(payload);
|
|
43
|
+
}
|
|
44
|
+
catch (error) { } // eslint-disable-line no-empty
|
|
45
|
+
done();
|
|
46
|
+
});
|
|
47
|
+
fastify.addHook("onResponse", (request, reply, done) => {
|
|
48
|
+
// Get path from routeOptions if available (from v4), otherwise fallback to deprecated routerPath
|
|
49
|
+
const path = "routeOptions" in request
|
|
50
|
+
? request.routeOptions.url
|
|
51
|
+
: request.routerPath;
|
|
52
|
+
client.requestLogger.logRequest({
|
|
53
|
+
consumer: getConsumer(request),
|
|
54
|
+
method: request.method,
|
|
55
|
+
path: path,
|
|
56
|
+
statusCode: reply.statusCode,
|
|
57
|
+
responseTime: reply.getResponseTime(),
|
|
58
|
+
});
|
|
59
|
+
if ((reply.statusCode === 400 || reply.statusCode === 422) &&
|
|
60
|
+
reply.payload &&
|
|
61
|
+
(!reply.payload.code || reply.payload.code === "FST_ERR_VALIDATION") &&
|
|
62
|
+
typeof reply.payload.message === "string") {
|
|
63
|
+
const validationErrors = extractAjvErrors(reply.payload.message);
|
|
64
|
+
validationErrors.forEach((error) => {
|
|
65
|
+
client.validationErrorLogger.logValidationError(Object.assign({ consumer: getConsumer(request), method: request.method, path: path }, error));
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
done();
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
const getAppInfo = (routes, appVersion) => {
|
|
72
|
+
const versions = [["nodejs", process.version]];
|
|
73
|
+
const fastifyVersion = (0, utils_1.getPackageVersion)("fastify");
|
|
74
|
+
const apitallyVersion = (0, utils_1.getPackageVersion)("../..");
|
|
75
|
+
if (fastifyVersion) {
|
|
76
|
+
versions.push(["fastify", fastifyVersion]);
|
|
77
|
+
}
|
|
78
|
+
if (apitallyVersion) {
|
|
79
|
+
versions.push(["apitally", apitallyVersion]);
|
|
80
|
+
}
|
|
81
|
+
if (appVersion) {
|
|
82
|
+
versions.push(["app", appVersion]);
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
paths: routes,
|
|
86
|
+
versions: new Map(versions),
|
|
87
|
+
client: "js:fastify",
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
const getConsumer = (request) => {
|
|
91
|
+
if (request.consumerIdentifier) {
|
|
92
|
+
return String(request.consumerIdentifier);
|
|
93
|
+
}
|
|
94
|
+
if (request.keyInfo && request.keyInfo instanceof keyRegistry_1.KeyInfo) {
|
|
95
|
+
return `key:${request.keyInfo.keyId}`;
|
|
96
|
+
}
|
|
97
|
+
return null;
|
|
98
|
+
};
|
|
99
|
+
const extractAjvErrors = (message) => {
|
|
100
|
+
const regex = /(?<=^|, )((?:headers|params|query|querystring|body)[/.][^ ]+)(?= )/g;
|
|
101
|
+
const matches = [];
|
|
102
|
+
let match;
|
|
103
|
+
while ((match = regex.exec(message)) !== null) {
|
|
104
|
+
matches.push({ match: match[0], index: match.index });
|
|
105
|
+
}
|
|
106
|
+
return matches.map((m, i) => {
|
|
107
|
+
const endIndex = i + 1 < matches.length ? matches[i + 1].index - 2 : message.length;
|
|
108
|
+
const matchSplit = m.match.split(/[/.]/);
|
|
109
|
+
if (matchSplit[0] === "querystring") {
|
|
110
|
+
matchSplit[0] = "query";
|
|
111
|
+
}
|
|
112
|
+
return {
|
|
113
|
+
loc: matchSplit.join("."),
|
|
114
|
+
msg: message.substring(m.index, endIndex),
|
|
115
|
+
type: "",
|
|
116
|
+
};
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
exports.default = (0, fastify_plugin_1.default)(apitallyPlugin, {
|
|
120
|
+
name: "apitally",
|
|
121
|
+
});
|
|
122
|
+
//# sourceMappingURL=plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../../src/fastify/plugin.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,oEAAgC;AAEhC,6CAAkD;AAClD,uDAAgD;AAEhD,2CAAoD;AAapD,MAAM,cAAc,GAAG,CACrB,OAAwB,EACxB,MAAsB,EACtB,EAAE;IACF,MAA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AE,CAAC,CAAC;KACvD;IAED,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QAC1B,MAAM,QAAQ,GACZ,CAAC,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,MAAM,CAAC;QACrE,MAAM,UAAU,GAAG,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;QACzC,IAAI,UAAU,CAAC,CAAC,CAAC,KAAK,aAAa,EAAE;YACnC,UAAU,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC;SACzB;QACD,OAAO;YACL,GAAG,EAAE,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC;YACzB,GAAG,EAAE,OAAO,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,EAAE,QAAQ,CAAC;YACzC,IAAI,EAAE,EAAE;SACT,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,kBAAe,IAAA,wBAAE,EAAC,cAAc,EAAE;IAChC,IAAI,EAAE,UAAU;CACjB,CAAC,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/koa/auth.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAItB,eAAO,MAAM,aAAa;;;YAOL,IAAI,OAAO,QAAQ,IAAI,IAAI,kBAqD/C,CAAC"}
|
package/dist/koa/auth.js
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.requireApiKey = void 0;
|
|
13
|
+
const client_1 = require("../common/client");
|
|
14
|
+
const requireApiKey = ({ scopes, customHeader, } = {}) => {
|
|
15
|
+
return (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
+
let apiKey;
|
|
17
|
+
if (!customHeader) {
|
|
18
|
+
if (!ctx.headers.authorization) {
|
|
19
|
+
ctx.status = 401;
|
|
20
|
+
ctx.set("WWW-Authenticate", "ApiKey");
|
|
21
|
+
ctx.body = { error: "Missing authorization header" };
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
const authorizationParts = ctx.headers.authorization.split(" ");
|
|
25
|
+
if (authorizationParts.length === 2 &&
|
|
26
|
+
authorizationParts[0].toLowerCase() === "apikey") {
|
|
27
|
+
apiKey = authorizationParts[1];
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
ctx.status = 401;
|
|
31
|
+
ctx.set("WWW-Authenticate", "ApiKey");
|
|
32
|
+
ctx.body = { error: "Invalid authorization scheme" };
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
else if (customHeader) {
|
|
37
|
+
const customHeaderValue = ctx.headers[customHeader.toLowerCase()];
|
|
38
|
+
if (typeof customHeaderValue === "string") {
|
|
39
|
+
apiKey = customHeaderValue;
|
|
40
|
+
}
|
|
41
|
+
else if (Array.isArray(customHeaderValue)) {
|
|
42
|
+
apiKey = customHeaderValue[0];
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (!apiKey) {
|
|
46
|
+
ctx.status = 403;
|
|
47
|
+
ctx.body = { error: "Missing API key" };
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const client = client_1.ApitallyClient.getInstance();
|
|
51
|
+
const keyInfo = yield client.keyRegistry.get(apiKey);
|
|
52
|
+
if (!keyInfo) {
|
|
53
|
+
ctx.status = 403;
|
|
54
|
+
ctx.body = { error: "Invalid API key" };
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (scopes && !keyInfo.hasScopes(scopes)) {
|
|
58
|
+
ctx.status = 403;
|
|
59
|
+
ctx.body = { error: "Permission denied" };
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
ctx.state.keyInfo = keyInfo;
|
|
63
|
+
yield next();
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
exports.requireApiKey = requireApiKey;
|
|
67
|
+
//# sourceMappingURL=auth.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.js","sourceRoot":"","sources":["../../src/koa/auth.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,6CAAkD;AAE3C,MAAM,aAAa,GAAG,CAAC,EAC5B,MAAM,EACN,YAAY,MAIV,EAAE,EAAE,EAAE;IACR,OAAO,CAAO,GAAgB,EAAE,IAAc,EAAE,EAAE;QAChD,IAAI,MAA0B,CAAC;QAE/B,IAAI,CAAC,YAAY,EAAE;YACjB,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,aAAa,EAAE;gBAC9B,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;gBACjB,GAAG,CAAC,GA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C,OAAO,EAAE;YACZ,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;YACjB,GAAG,CAAC,IAAI,GAAG,EAAE,KAAK,EAAE,iBAAiB,EAAE,CAAC;YACxC,OAAO;SACR;QACD,IAAI,MAAM,IAAI,CAAC,OAAO,CAAC,SAAS,CAAC,MAAM,CAAC,EAAE;YACxC,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC;YACjB,GAAG,CAAC,IAAI,GAAG,EAAE,KAAK,EAAE,mBAAmB,EAAE,CAAC;YAC1C,OAAO;SACR;QAED,GAAG,CAAC,KAAK,CAAC,OAAO,GAAG,OAAO,CAAC;QAC5B,MAAM,IAAI,EAAE,CAAC;IACf,CAAC,CAAA,CAAC;AACJ,CAAC,CAAC;AA5DW,QAAA,aAAa,iBA4DxB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/koa/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useApitally = exports.requireApiKey = void 0;
|
|
4
|
+
var auth_1 = require("./auth");
|
|
5
|
+
Object.defineProperty(exports, "requireApiKey", { enumerable: true, get: function () { return auth_1.requireApiKey; } });
|
|
6
|
+
var middleware_1 = require("./middleware");
|
|
7
|
+
Object.defineProperty(exports, "useApitally", { enumerable: true, get: function () { return middleware_1.useApitally; } });
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/koa/index.ts"],"names":[],"mappings":";;;AAAA,+BAAuC;AAA9B,qGAAA,aAAa,OAAA;AACtB,2CAA2C;AAAlC,yGAAA,WAAW,OAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../src/koa/middleware.ts"],"names":[],"mappings":"AAAA,OAAO,GAAG,MAAM,KAAK,CAAC;AAItB,OAAO,EAAE,cAAc,EAAqB,MAAM,iBAAiB,CAAC;AAGpE,eAAO,MAAM,WAAW,QAAS,GAAG,UAAU,cAAc,SAO3D,CAAC"}
|