serverless-basic-authentication
Advanced tools
Comparing version 0.0.2 to 0.5.1
41
index.js
'use strict' | ||
const fs = require('fs'); | ||
const chalk = require('chalk'); | ||
@@ -7,12 +8,14 @@ | ||
constructor (serverless, options) { | ||
this.consoleLog = serverless.cli.consoleLog; | ||
// add the basic authentication function to the functions as soon as possible | ||
injectBasicAuthFunction(serverless); | ||
this.hooks = { | ||
'before:package:initialize': function () { | ||
// add the basic authenticator function | ||
injectBasicAuthFunction(serverless); | ||
// add our custom authenticator | ||
addAuthFileToPackage(serverless); | ||
addAuthorizerFunctionToPrivateFunctions(serverless); | ||
}, | ||
'after:package:finalize': function () { | ||
'after:package:createDeploymentArtifacts': function () { | ||
// remove the custom authenticator | ||
@@ -22,2 +25,5 @@ removeFileFromPackage(serverless) | ||
'before:deploy:deploy': function() { | ||
// // add the basic authenticator function | ||
// injectBasicAuthFunction(serverless); | ||
// configure api gateway to check for the right place for the key | ||
@@ -31,4 +37,4 @@ configureApiGatewayKeySource(serverless); | ||
function removeFileFromPackage(serverless) { | ||
this.consoleLog('Removing Symlink for Basic Authenticator'); | ||
fs.unlinkSync(serverless.config.servicePath + "/auth.py") | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Removing Symlink for Basic Authenticator')); | ||
fs.unlinkSync(serverless.config.servicePath + "/basic_auth.py") | ||
} | ||
@@ -44,3 +50,3 @@ | ||
this.consoleLog('Adding Symlink for Basic Authenticator'); | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Adding Symlink for Basic Authenticator')); | ||
// @TODO: Make target filename randomized with something, to prevent overriding | ||
@@ -51,8 +57,9 @@ // any files | ||
serverless.package.include.push(__dirname + "/auth.py") | ||
fs.symlinkSync(__dirname + "/auth.py", serverless.config.servicePath + "/auth.py") | ||
fs.symlinkSync(__dirname + "/basic_auth.py", serverless.config.servicePath + "/basic_auth.py") | ||
} | ||
function injectBasicAuthFunction (serverless) { | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Adding function for Basic Authenticator')); | ||
var basicAuthenticator = { | ||
handler: 'auth.basicAuth', | ||
handler: 'basic_auth.basicAuth', | ||
runtime: 'python3.6' | ||
@@ -62,4 +69,6 @@ } | ||
// add the basic authenticator function | ||
serverless.service.functions['basicAuthenticator'] = basicAuthenticator; | ||
serverless.service.functions.basicAuthenticator = basicAuthenticator; | ||
} | ||
function addAuthorizerFunctionToPrivateFunctions(serverless) { | ||
// for each function which is marked as 'private', set the basic authenticator | ||
@@ -80,7 +89,7 @@ // if it doesn't have a custom authenticator yet | ||
if( | ||
fnctn.events[fnctn_event].http.private == true && | ||
fnctn.events[fnctn_event].http.authorizer == null | ||
serverless.service.functions[function_name].events[fnctn_event].http.private == true && | ||
serverless.service.functions[function_name].events[fnctn_event].http.authorizer == null | ||
) { | ||
fnctn.events[fnctn_event].http.authorizer = 'basicAuthenticator' | ||
this.consoleLog(yellow('Basic Authentication') + ' enabled for ' + function_name); | ||
serverless.service.functions[function_name].events[fnctn_event].http.authorizer = 'basicAuthenticator' | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Enabled for ' + function_name)); | ||
} | ||
@@ -94,3 +103,3 @@ } | ||
if(template.Resources.ApiGatewayRestApi != null) { | ||
this.consoleLog('Configuring Api Gateway for Basic Authenticator') | ||
serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Configuring Api Gateway for Basic Authenticator')); | ||
template.Resources.ApiGatewayRestApi.Properties.ApiKeySourceType = 'AUTHORIZER' | ||
@@ -97,0 +106,0 @@ } |
{ | ||
"name": "serverless-basic-authentication", | ||
"version": "0.0.2", | ||
"version": "0.5.1", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "jest": "^22.4.2" |
Serverless Basic Authentication (http basic auth) | ||
-------------------------------------------- | ||
Sometimes you need to integrate your api with some outside system, and you are not capable of setting up custom headers with keys. | ||
Sometimes you need to integrate your api with some outside system, and you are not capable of setting up custom headers with keys. Almost all systems support Basic Authentication out of the box though. Which is where this plugin comes in. | ||
That is where this plugin comes in. This will install a custom authenticator for your specified functions, and use the AWS Api Gateway API Keys (so no user management required), as http basic username and password. | ||
This plugin will install a custom authenticator for the functions you specify as being private, and use the API Keys (so no user management required) as http basic username and password. | ||
@@ -8,0 +8,0 @@ Installation |
6938
119