Skip to content
Merged
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
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
charset = utf-8
trim_trailing_whitespace = true

[*.{js}]
indent_style = space
indent_size = 4

[*.{json,yml}]
indent_style = space
indent_size = 2
14 changes: 14 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
"extends": "airbnb-base",
"plugins": [
"import"
],
"rules": {
"arrow-body-style": "off",
"comma-dangle": ["error", "never"],
"import/no-dynamic-require": "off",
"indent": ["error", 4],
"no-prototype-builtins": "off",
"prefer-destructuring": "off"
}
};
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.nyc_output
8 changes: 8 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: node_js
node_js:
- 6
cache:
directories:
- "node_modules"
script:
- npm run test
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Serverless boilerplate for Static website hosting with Basic authentication
# Serverless boilerplate for Static website hosting with Basic authentication [![Build Status](https://travis-ci.org/k1LoW/serverless-static-hosting-with-basic-auth.svg?branch=master)](https://travis-ci.org/k1LoW/serverless-static-hosting-with-basic-auth)

## Architecture

Expand All @@ -19,7 +19,7 @@ $ npm install
$ AWS_PROFILE=XxxxxXXX WEBSITE_S3_BUCKET_NAME=sls-static-basic npm run deploy
```

### Synchronize src/* and Website
### Synchronize src/* -> Website

```
$ AWS_PROFILE=XxxxxXXX WEBSITE_S3_BUCKET_NAME=sls-static-basic npm run sync
Expand Down
20 changes: 11 additions & 9 deletions handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

const BASIC_AUTH_USERS = {
'user': 'pass'
user: 'pass'
};

module.exports.basicAuth = (event, context, callback) => {
Expand All @@ -11,22 +9,26 @@ module.exports.basicAuth = (event, context, callback) => {

if (authorization) {
const encoded = authorization[0].value.split(' ')[1];
const userAndPassword = new Buffer(encoded, 'base64').toString();
for (let user in BASIC_AUTH_USERS) {
const userAndPassword = Buffer.from(encoded, 'base64').toString();
const result = Object.keys(BASIC_AUTH_USERS).filter((user) => {
const password = BASIC_AUTH_USERS[user];
if (`${user}:${password}` === userAndPassword) {
callback(null, request);
return;
return true;
}
return false;
});
if (result.length > 0) {
callback(null, request);
return;
}
}

const response = {
status: '401',
statusDescription: 'Authorization Required',
headers: {
'www-authenticate': [{key: 'WWW-Authenticate', value: 'Basic'}],
'content-type': [{key: 'Content-Type', value: 'text/plain; charset=utf-8'}]
'www-authenticate': [{ key: 'WWW-Authenticate', value: 'Basic' }],
'content-type': [{ key: 'Content-Type', value: 'text/plain; charset=utf-8' }]
},
body: '401 Authorization Required'
};
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
"deploy": "sls deploy -v",
"remove": "sls remove",
"sync": "sls sync",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "nyc ava -v",
"lint": "eslint ."
},
"keywords": [
"serverless"
],
"author": "k1LoW <[email protected]> (https://github.com/k1LoW)",
"license": "MIT",
"devDependencies": {
"ava": "^0.24.0",
"eslint-config-airbnb-base": "^12.1.0",
"nyc": "^11.4.1",
"octopublish": "^0.5.0",
"path": "^0.12.7",
"serverless": "^1.25.0",
"serverless-plugin-cloudfront-lambda-edge": "^1.0.0",
"serverless-s3-sync": "^1.3.0"
Expand Down
31 changes: 31 additions & 0 deletions test/basicAuth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const test = require('ava');
const path = require('path');

const { basicAuth } = require(path.join(__dirname, '..', 'handler.js'));

test.cb('handler.basicAuth check user/pass', (t) => {
const event = {
Records: [
{
cf: {
request: {
headers: {
authorization: [{ key: 'Authorization', value: 'Basic dXNlcjpwYXNz' }]
}
}
}
}
]
};
const context = {};
const callback = (error, response) => {
t.deepEqual(response, {
headers: {
authorization: [{ key: 'Authorization', value: 'Basic dXNlcjpwYXNz' }]
}
});
t.end();
};

basicAuth(event, context, callback);
});