|
1 |
| -'use strict' |
2 | 1 | const fs = require('fs');
|
3 | 2 | const chalk = require('chalk');
|
4 | 3 |
|
5 |
| - |
6 |
| -class SetupBasicAuthentication { |
7 |
| - constructor (serverless, options) { |
| 4 | +module.exports = class SetupBasicAuthentication { |
| 5 | + constructor(serverless, options) { |
| 6 | + this.options = options; |
| 7 | + this.serverless = serverless; |
8 | 8 |
|
9 | 9 | // add the basic authentication function to the functions as soon as possible
|
10 |
| - injectBasicAuthFunction(serverless); |
| 10 | + this.injectBasicAuthFunction(serverless); |
11 | 11 |
|
12 | 12 | this.hooks = {
|
13 |
| - 'before:package:initialize': function () { |
14 |
| - // add our custom authenticator |
15 |
| - addAuthFileToPackage(serverless); |
16 |
| - |
17 |
| - addAuthorizerFunctionToPrivateFunctions(serverless); |
18 |
| - }, |
19 |
| - 'after:package:createDeploymentArtifacts': function () { |
20 |
| - // remove the custom authenticator |
21 |
| - removeFileFromPackage(serverless) |
22 |
| - }, |
23 |
| - 'before:deploy:deploy': function() { |
24 |
| - // // add the basic authenticator function |
25 |
| - // injectBasicAuthFunction(serverless); |
26 |
| - |
27 |
| - // configure api gateway to check for the right place for the key |
28 |
| - configureApiGatewayKeySource(serverless); |
29 |
| - } |
30 |
| - } |
| 13 | + 'before:package:initialize': this.addAuthorizer.bind(this), |
| 14 | + 'after:package:createDeploymentArtifacts': this.removeAuthorizer.bind(this), |
| 15 | + 'before:deploy:deploy': this.configureApiGatewayKeySource.bind(this), |
| 16 | + }; |
31 | 17 | }
|
32 |
| -} |
33 | 18 |
|
34 |
| -function removeFileFromPackage(serverless) { |
35 |
| - serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Removing Symlink for Basic Authenticator')); |
36 |
| - fs.unlinkSync(serverless.config.servicePath + "/basic_auth.py") |
37 |
| -} |
| 19 | + addAuthorizer() { |
| 20 | + // add our custom authenticator |
| 21 | + this.addAuthFileToPackage(); |
38 | 22 |
|
39 |
| -function addAuthFileToPackage(serverless) { |
40 |
| - if(!serverless.package) { |
41 |
| - serverless.package = {} |
| 23 | + this.addAuthorizerFunctionToPrivateFunctions(); |
42 | 24 | }
|
43 |
| - if(!serverless.package.include) { |
44 |
| - serverless.package.include = [] |
| 25 | + |
| 26 | + removeAuthorizer() { |
| 27 | + this.serverless.cli.consoleLog(`Basic Authentication: ${chalk.yellow('Removing Symlink for Basic Authenticator')}`); |
| 28 | + fs.unlinkSync(`${this.serverless.config.servicePath}/basic_auth.py`); |
45 | 29 | }
|
46 | 30 |
|
47 |
| - serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Adding Symlink for Basic Authenticator')); |
48 |
| - // @TODO: Make target filename randomized with something, to prevent overriding |
49 |
| - // any files |
| 31 | + addAuthFileToPackage() { |
| 32 | + if (!this.serverless.package) { |
| 33 | + this.serverless.package = {}; |
| 34 | + } |
| 35 | + |
| 36 | + if (!this.serverless.package.include) { |
| 37 | + this.serverless.package.include = []; |
| 38 | + } |
50 | 39 |
|
51 |
| - // append our auth.py file to the package |
52 |
| - serverless.package.include.push(__dirname + "/auth.py") |
53 |
| - fs.symlinkSync(__dirname + "/basic_auth.py", serverless.config.servicePath + "/basic_auth.py") |
54 |
| -} |
| 40 | + this.serverless.cli.consoleLog(`Basic Authentication: ${chalk.yellow('Adding Symlink for Basic Authenticator')}`); |
| 41 | + // @TODO: Make target filename randomized with something, to prevent overriding |
| 42 | + // any files |
55 | 43 |
|
56 |
| -function injectBasicAuthFunction (serverless) { |
57 |
| - serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Adding function for Basic Authenticator')); |
58 |
| - var basicAuthenticator = { |
59 |
| - handler: 'basic_auth.basicAuth', |
60 |
| - runtime: 'python3.6' |
| 44 | + // append our auth.py file to the package |
| 45 | + this.serverless.package.include.push(`${__dirname}/auth.py`); |
| 46 | + fs.symlinkSync(`${__dirname}/basic_auth.py`, `${this.serverless.config.servicePath}/basic_auth.py`); |
61 | 47 | }
|
62 | 48 |
|
63 |
| - // add the basic authenticator function |
64 |
| - serverless.service.functions.basicAuthenticator = basicAuthenticator; |
65 |
| -} |
66 |
| - |
67 |
| -function addAuthorizerFunctionToPrivateFunctions(serverless) { |
68 |
| - // for each function which is marked as 'private', set the basic authenticator |
69 |
| - // if it doesn't have a custom authenticator yet |
70 |
| - for(let function_name in serverless.service.functions) { |
| 49 | + injectBasicAuthFunction() { |
| 50 | + this.serverless.cli.consoleLog(`Basic Authentication: ${chalk.yellow('Adding function for Basic Authenticator')}`); |
| 51 | + const basicAuthenticator = { |
| 52 | + handler: 'basic_auth.basicAuth', |
| 53 | + runtime: 'python3.6', |
| 54 | + }; |
71 | 55 |
|
72 |
| - // ignore our own function |
73 |
| - if(function_name == 'basicAuthenticator') { |
74 |
| - continue; |
75 |
| - } |
| 56 | + // add the basic authenticator function |
| 57 | + this.serverless.service.functions.basicAuthenticator = basicAuthenticator; |
| 58 | + } |
76 | 59 |
|
77 |
| - var fnctn = serverless.service.functions[function_name]; |
78 |
| - |
79 |
| - // check if any of the http events is marked as private, and if that event |
80 |
| - // also doesn't have a custom authorizer already, apply our authenticator |
81 |
| - for(let fnctn_event in fnctn['events']) { |
82 |
| - if( |
83 |
| - serverless.service.functions[function_name].events[fnctn_event].http != null && |
84 |
| - serverless.service.functions[function_name].events[fnctn_event].http.private == true && |
85 |
| - serverless.service.functions[function_name].events[fnctn_event].http.authorizer == null |
86 |
| - ) { |
87 |
| - serverless.service.functions[function_name].events[fnctn_event].http.authorizer = { |
88 |
| - name: 'basicAuthenticator', |
89 |
| - identitySource: '', // this is only valid if we set cache ttl to 0 |
90 |
| - resultTtlInSeconds: 0, |
91 |
| - type: 'REQUEST' |
| 60 | + addAuthorizerFunctionToPrivateFunctions() { |
| 61 | + // for each function which is marked as 'private', set the basic authenticator |
| 62 | + // if it doesn't have a custom authenticator yet |
| 63 | + Object.keys(this.serverless.service.functions).forEach((functionName) => { |
| 64 | + // ignore our own function |
| 65 | + if (functionName === 'basicAuthenticator') { |
| 66 | + return; |
| 67 | + } |
92 | 68 |
|
| 69 | + // get all function configs |
| 70 | + const fnctn = this.serverless.service.functions[functionName]; |
| 71 | + |
| 72 | + // check if any of the http events is marked as private, and if that event |
| 73 | + // also doesn't have a custom authorizer already, apply our authenticator |
| 74 | + Object.keys(fnctn.events).forEach((fnctnEvent) => { |
| 75 | + if ( |
| 76 | + this.serverless.service.functions[functionName].events[fnctnEvent].http != null |
| 77 | + && this.serverless.service.functions[functionName].events[fnctnEvent].http.private === true |
| 78 | + && this.serverless.service.functions[functionName].events[fnctnEvent].http.authorizer == null |
| 79 | + ) { |
| 80 | + this.serverless.service.functions[functionName].events[fnctnEvent].http.authorizer = { |
| 81 | + name: 'basicAuthenticator', |
| 82 | + identitySource: '', // this is only valid if we set cache ttl to 0 |
| 83 | + resultTtlInSeconds: 0, |
| 84 | + type: 'REQUEST', |
| 85 | + }; |
| 86 | + this.serverless.cli.consoleLog(`Basic Authentication: ${chalk.yellow(`Enabled for ${functionName}`)}`); |
93 | 87 | }
|
94 |
| - serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Enabled for ' + function_name)); |
95 |
| - } |
96 |
| - } |
| 88 | + }); |
| 89 | + }); |
97 | 90 | }
|
98 |
| -} |
99 | 91 |
|
100 |
| -function configureApiGatewayKeySource(serverless) { |
101 |
| - var template = serverless.service.provider.compiledCloudFormationTemplate; |
102 |
| - if(template.Resources.ApiGatewayRestApi != null) { |
103 |
| - serverless.cli.consoleLog('Basic Authentication: ' + chalk.yellow('Configuring Api Gateway for Basic Authenticator')); |
104 |
| - template.Resources.ApiGatewayRestApi.Properties.ApiKeySourceType = 'AUTHORIZER' |
| 92 | + configureApiGatewayKeySource() { |
| 93 | + const template = this.serverless.service.provider.compiledCloudFormationTemplate; |
| 94 | + if (template.Resources.ApiGatewayRestApi != null) { |
| 95 | + this.serverless.cli.consoleLog( |
| 96 | + `Basic Authentication: ${chalk.yellow('Configuring Api Gateway for Basic Authenticator')}`, |
| 97 | + ); |
| 98 | + template.Resources.ApiGatewayRestApi.Properties.ApiKeySourceType = 'AUTHORIZER'; |
| 99 | + } |
105 | 100 | }
|
106 |
| -} |
107 |
| - |
108 |
| -// now we need to make our plugin object available to the framework to execute |
109 |
| -module.exports = SetupBasicAuthentication |
| 101 | +}; |
0 commit comments