serverless-basic-authentication
Advanced tools
Comparing version
160
index.js
@@ -1,109 +0,101 @@ | ||
'use strict' | ||
const fs = require('fs'); | ||
const chalk = require('chalk'); | ||
module.exports = class SetupBasicAuthentication { | ||
constructor(serverless, options) { | ||
this.options = options; | ||
this.serverless = serverless; | ||
class SetupBasicAuthentication { | ||
constructor (serverless, options) { | ||
// add the basic authentication function to the functions as soon as possible | ||
injectBasicAuthFunction(serverless); | ||
this.injectBasicAuthFunction(serverless); | ||
this.hooks = { | ||
'before:package:initialize': function () { | ||
// add our custom authenticator | ||
addAuthFileToPackage(serverless); | ||
'before:package:initialize': this.addAuthorizer.bind(this), | ||
'after:package:createDeploymentArtifacts': this.removeAuthorizer.bind(this), | ||
'before:deploy:deploy': this.configureApiGatewayKeySource.bind(this), | ||
}; | ||
} | ||
addAuthorizerFunctionToPrivateFunctions(serverless); | ||
}, | ||
'after:package:createDeploymentArtifacts': function () { | ||
// remove the custom authenticator | ||
removeFileFromPackage(serverless) | ||
}, | ||
'before:deploy:deploy': function() { | ||
// // add the basic authenticator function | ||
// injectBasicAuthFunction(serverless); | ||
addAuthorizer() { | ||
// add our custom authenticator | ||
this.addAuthFileToPackage(); | ||
// configure api gateway to check for the right place for the key | ||
configureApiGatewayKeySource(serverless); | ||
} | ||
} | ||
this.addAuthorizerFunctionToPrivateFunctions(); | ||
} | ||
} | ||
function removeFileFromPackage(serverless) { | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Removing Symlink for Basic Authenticator')); | ||
fs.unlinkSync(serverless.config.servicePath + "/basic_auth.py") | ||
} | ||
function addAuthFileToPackage(serverless) { | ||
if(!serverless.package) { | ||
serverless.package = {} | ||
removeAuthorizer() { | ||
this.serverless.cli.consoleLog(`Basic Authentication: ${chalk.yellow('Removing Symlink for Basic Authenticator')}`); | ||
fs.unlinkSync(`${this.serverless.config.servicePath}/basic_auth.py`); | ||
} | ||
if(!serverless.package.include) { | ||
serverless.package.include = [] | ||
} | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Adding Symlink for Basic Authenticator')); | ||
// @TODO: Make target filename randomized with something, to prevent overriding | ||
// any files | ||
addAuthFileToPackage() { | ||
if (!this.serverless.package) { | ||
this.serverless.package = {}; | ||
} | ||
// append our auth.py file to the package | ||
serverless.package.include.push(__dirname + "/auth.py") | ||
fs.symlinkSync(__dirname + "/basic_auth.py", serverless.config.servicePath + "/basic_auth.py") | ||
} | ||
if (!this.serverless.package.include) { | ||
this.serverless.package.include = []; | ||
} | ||
function injectBasicAuthFunction (serverless) { | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Adding function for Basic Authenticator')); | ||
var basicAuthenticator = { | ||
handler: 'basic_auth.basicAuth', | ||
runtime: 'python3.6' | ||
this.serverless.cli.consoleLog(`Basic Authentication: ${chalk.yellow('Adding Symlink for Basic Authenticator')}`); | ||
// @TODO: Make target filename randomized with something, to prevent overriding | ||
// any files | ||
// append our auth.py file to the package | ||
this.serverless.package.include.push(`${__dirname}/auth.py`); | ||
fs.symlinkSync(`${__dirname}/basic_auth.py`, `${this.serverless.config.servicePath}/basic_auth.py`); | ||
} | ||
// add the basic authenticator function | ||
serverless.service.functions.basicAuthenticator = basicAuthenticator; | ||
} | ||
injectBasicAuthFunction() { | ||
this.serverless.cli.consoleLog(`Basic Authentication: ${chalk.yellow('Adding function for Basic Authenticator')}`); | ||
const basicAuthenticator = { | ||
handler: 'basic_auth.basicAuth', | ||
runtime: 'python3.6', | ||
}; | ||
function addAuthorizerFunctionToPrivateFunctions(serverless) { | ||
// for each function which is marked as 'private', set the basic authenticator | ||
// if it doesn't have a custom authenticator yet | ||
for(let function_name in serverless.service.functions) { | ||
// add the basic authenticator function | ||
this.serverless.service.functions.basicAuthenticator = basicAuthenticator; | ||
} | ||
// ignore our own function | ||
if(function_name == 'basicAuthenticator') { | ||
continue; | ||
} | ||
addAuthorizerFunctionToPrivateFunctions() { | ||
// for each function which is marked as 'private', set the basic authenticator | ||
// if it doesn't have a custom authenticator yet | ||
Object.keys(this.serverless.service.functions).forEach((functionName) => { | ||
// ignore our own function | ||
if (functionName === 'basicAuthenticator') { | ||
return; | ||
} | ||
var fnctn = serverless.service.functions[function_name]; | ||
// get all function configs | ||
const fnctn = this.serverless.service.functions[functionName]; | ||
// check if any of the http events is marked as private, and if that event | ||
// also doesn't have a custom authorizer already, apply our authenticator | ||
for(let fnctn_event in fnctn['events']) { | ||
if( | ||
serverless.service.functions[function_name].events[fnctn_event].http != null && | ||
serverless.service.functions[function_name].events[fnctn_event].http.private == true && | ||
serverless.service.functions[function_name].events[fnctn_event].http.authorizer == null | ||
) { | ||
serverless.service.functions[function_name].events[fnctn_event].http.authorizer = { | ||
name: 'basicAuthenticator', | ||
identitySource: '', // this is only valid if we set cache ttl to 0 | ||
resultTtlInSeconds: 0, | ||
type: 'REQUEST' | ||
// check if any of the http events is marked as private, and if that event | ||
// also doesn't have a custom authorizer already, apply our authenticator | ||
Object.keys(fnctn.events).forEach((fnctnEvent) => { | ||
if ( | ||
this.serverless.service.functions[functionName].events[fnctnEvent].http != null | ||
&& this.serverless.service.functions[functionName].events[fnctnEvent].http.private === true | ||
&& this.serverless.service.functions[functionName].events[fnctnEvent].http.authorizer == null | ||
) { | ||
this.serverless.service.functions[functionName].events[fnctnEvent].http.authorizer = { | ||
name: 'basicAuthenticator', | ||
identitySource: '', // this is only valid if we set cache ttl to 0 | ||
resultTtlInSeconds: 0, | ||
type: 'REQUEST', | ||
}; | ||
this.serverless.cli.consoleLog(`Basic Authentication: ${chalk.yellow(`Enabled for ${functionName}`)}`); | ||
} | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Enabled for ' + function_name)); | ||
} | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
function configureApiGatewayKeySource(serverless) { | ||
var template = serverless.service.provider.compiledCloudFormationTemplate; | ||
if(template.Resources.ApiGatewayRestApi != null) { | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Configuring Api Gateway for Basic Authenticator')); | ||
template.Resources.ApiGatewayRestApi.Properties.ApiKeySourceType = 'AUTHORIZER' | ||
configureApiGatewayKeySource() { | ||
const template = this.serverless.service.provider.compiledCloudFormationTemplate; | ||
if (template.Resources.ApiGatewayRestApi != null) { | ||
this.serverless.cli.consoleLog( | ||
`Basic Authentication: ${chalk.yellow('Configuring Api Gateway for Basic Authenticator')}`, | ||
); | ||
template.Resources.ApiGatewayRestApi.Properties.ApiKeySourceType = 'AUTHORIZER'; | ||
} | ||
} | ||
} | ||
// now we need to make our plugin object available to the framework to execute | ||
module.exports = SetupBasicAuthentication | ||
}; |
{ | ||
"name": "serverless-basic-authentication", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"devDependencies": { | ||
"eslint": "^5.12.1", | ||
"eslint-config-airbnb-base": "^13.1.0", | ||
"eslint-plugin-import": "^2.14.0", | ||
"jest": "^22.4.3" | ||
@@ -6,0 +9,0 @@ }, |
Sorry, the diff of this file is not supported yet
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
8687
1.26%6
50%2
-33.33%4
300%132
-5.71%