serverless-global-authorizer
Version:
Serverless framework plugin which allows to configure API Gateway authorizer globally and applies it for all http/httpApi lambda function events
84 lines • 4.12 kB
JavaScript
;
const serverless_global_authorizer_error_1 = require("./serverless-global-authorizer-error");
const define_serverless_schema_1 = require("./define-serverless-schema");
class ServerlessGlobalAuthorizerPlugin {
constructor(serverless) {
this.serverless = serverless;
this.hooks = {
'before:package:initialize': this.init.bind(this),
};
serverless.getProvider('aws');
(0, define_serverless_schema_1.defineServerlessSchema)(serverless);
}
init() {
(this.serverless.service.getAllFunctions() || []).forEach(this.processFunction.bind(this));
}
processFunction(functionName) {
(this.serverless.service.getAllEventsInFunction(functionName) || []).forEach(this.processEvent.bind(this));
}
processEvent(event) {
if ('http' in event &&
event.http &&
event.http.authorizer === undefined &&
ServerlessGlobalAuthorizerPlugin.isGlobalAuthorizerEnabledAtEventLevel(event.http)) {
event.http = this.applyRestApiAuthorizer(event.http);
}
else if ('httpApi' in event &&
event.httpApi &&
event.httpApi.authorizer === undefined &&
ServerlessGlobalAuthorizerPlugin.isGlobalAuthorizerEnabledAtEventLevel(event.httpApi)) {
event.httpApi = this.applyHttpApiAuthorizer(event.httpApi);
}
}
applyRestApiAuthorizer(event) {
// if event specified in shorthand syntax
if (typeof event === 'string') {
const [method, path] = event.split(' ');
event = { method, path };
}
event.authorizer = this.getRestApiAuthorizerConfig();
return event;
}
applyHttpApiAuthorizer(event) {
// if event specified in shorthand syntax
if (typeof event === 'string') {
// if default route
if (event === '*') {
return {
method: '*',
path: '*',
authorizer: this.getHttpApiAuthorizerConfig(),
};
}
const [method, path] = event.split(' ');
event = { method, path };
}
event.authorizer = this.getHttpApiAuthorizerConfig();
return event;
}
getRestApiAuthorizerConfig() {
var _a, _b, _c, _d;
if (!((_c = (_b = (_a = this.serverless.service.custom) === null || _a === void 0 ? void 0 : _a.globalAuthorizer) === null || _b === void 0 ? void 0 : _b.restApi) === null || _c === void 0 ? void 0 : _c.authorizer)) {
throw new serverless_global_authorizer_error_1.ServerlessGlobalAuthorizerError('Missing global authorizer configuration for REST API Gateway');
}
return (_d = this.serverless.service.custom.globalAuthorizer.restApi) === null || _d === void 0 ? void 0 : _d.authorizer;
}
getHttpApiAuthorizerConfig() {
var _a, _b, _c, _d;
if (!((_c = (_b = (_a = this.serverless.service.custom) === null || _a === void 0 ? void 0 : _a.globalAuthorizer) === null || _b === void 0 ? void 0 : _b.httpApi) === null || _c === void 0 ? void 0 : _c.authorizer)) {
throw new serverless_global_authorizer_error_1.ServerlessGlobalAuthorizerError('Missing global authorizer configuration for HTTP API Gateway');
}
return (_d = this.serverless.service.custom.globalAuthorizer.httpApi) === null || _d === void 0 ? void 0 : _d.authorizer;
}
static isGlobalAuthorizerEnabledAtEventLevel(event) {
if (typeof event === 'string' || event.globalAuthorizerEnabled === undefined) {
return true;
}
if (event.globalAuthorizerEnabled !== false && event.globalAuthorizerEnabled !== true) {
throw new serverless_global_authorizer_error_1.ServerlessGlobalAuthorizerError(`"globalAuthorizerEnabled" property needs to be of boolean type. "${event.globalAuthorizerEnabled}" passed as a value`);
}
return event.globalAuthorizerEnabled;
}
}
module.exports = ServerlessGlobalAuthorizerPlugin;
//# sourceMappingURL=index.js.map