Skip to content
This repository was archived by the owner on Nov 3, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,31 +73,46 @@ module.exports = class SetupBasicAuthentication {
}

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) => {
// For each function which is marked as 'private', set the basic authenticator
// if it doesn't have a custom authenticator yet.

// If any of the functions' events is marked with the flag "basicAuth", then
// only the events with basicAuth=true are configured with basic auth.

const functions = this.serverless.service.functions || [];

const hasAnyBasicAuthConfig = Object.keys(functions).reduce(
(result, func) => result || Object.keys(func.events || []).reduce(
(hasBasicAuthConfig, event) => hasBasicAuthConfig || (event.http && 'basicAuth' in event.http),
false),
false);

Object.keys(functions).forEach((functionName) => {
// ignore our own function
if (functionName === 'basicAuthenticator') {
return;
}

// get all function configs
const fnctn = this.serverless.service.functions[functionName];
const fnctn = 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
Object.keys(fnctn.events).forEach((fnctnEvent) => {
// if http doesn't exist, skip
if (!('http' in this.serverless.service.functions[functionName].events[fnctnEvent])) {
if (!fnctn.events[fnctnEvent].http) {
return;
}

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
!fnctn.events[fnctnEvent].http.authorizer &&
(
fnctn.events[fnctnEvent].http.private === true ||
!hasAnyBasicAuthConfig ||
fnctn.events[fnctnEvent].http.basicAuth === true
)
) {
this.serverless.service.functions[functionName].events[fnctnEvent].http.authorizer = {
fnctn.events[fnctnEvent].http.authorizer = {
name: 'basicAuthenticator',
identitySource: '', // this is only valid if we set cache ttl to 0
resultTtlInSeconds: 0,
Expand Down
20 changes: 20 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ functions:
private: true
```

For more fine-grained control over which functions are enabled for basic auth, use the `basicAuth` attribute on events. If any of the functions' events is marked with the flag `basicAuth`, then only the events with `basicAuth=true` are configured with basic auth.

```
functions:
foo:
handler: handler.foo
events:
- http:
path: foo
method: get
basicAuth: true # Basic auth enabled
bar:
handler: handler.bar
events:
- http:
path: bar
method: get
private: true # Basic auth not enabled (may use some other default authorizer)
```

To send the correct header so that browsers will prompt for username and password, add a `GatewayResponse` to the `resources`:

```
Expand Down