Repository navigation
Expand file tree
/
Copy pathcontent.js
More file actions
76 lines (65 loc) · 2.19 KB
/
Copy pathcontent.js
File metadata and controls
76 lines (65 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const models = require('../../../models');
const errors = require('@tryghost/errors');
const { limitService } = require('../../../services/limits');
const tpl = require('@tryghost/tpl');
const messages = {
invalidRequest: 'Invalid Request',
unknownContentApiKey: 'Unknown Content API Key',
invalidApiKeyType: 'Invalid API Key type',
};
const authenticateContentApiKey = async function authenticateContentApiKey(req, res, next) {
// allow fallthrough to other auth methods or final ensureAuthenticated check
if (!req.query || !req.query.key) {
return next();
}
if (req.query.key.constructor === Array) {
return next(
new errors.BadRequestError({
message: tpl(messages.invalidRequest),
code: 'INVALID_REQUEST',
}),
);
}
const key = req.query.key;
try {
const apiKey = await models.ApiKey.findOne({ secret: key }, { withRelated: ['integration'] });
if (!apiKey) {
return next(
new errors.UnauthorizedError({
message: tpl(messages.unknownContentApiKey),
code: 'UNKNOWN_CONTENT_API_KEY',
}),
);
}
if (apiKey.get('type') !== 'content') {
return next(
new errors.UnauthorizedError({
message: tpl(messages.invalidApiKeyType),
code: 'INVALID_API_KEY_TYPE',
}),
);
}
// CASE: blocking all non-internal: "custom" and "builtin" integration requests when the limit is reached
if (
limitService.isLimited('customIntegrations') &&
apiKey.relations.integration &&
!['internal', 'core'].includes(apiKey.relations.integration.get('type'))
) {
// NOTE: using "checkWouldGoOverLimit" instead of "checkIsOverLimit" here because flag limits don't have
// a concept of measuring if the limit has been surpassed
await limitService.errorIfWouldGoOverLimit('customIntegrations');
}
// authenticated OK, store the api key on the request for later checks and logging
req.api_key = apiKey;
next();
} catch (err) {
if (err instanceof errors.HostLimitError) {
next(err);
} else {
next(new errors.InternalServerError({ err }));
}
}
};
module.exports = {
authenticateContentApiKey,
};