diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ed7fef2 --- /dev/null +++ b/.editorconfig @@ -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 diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..b91720a --- /dev/null +++ b/.eslintrc.js @@ -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" + } +}; diff --git a/.gitignore b/.gitignore index b512c09..c9106a7 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules \ No newline at end of file +node_modules +.nyc_output diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..3b6cde9 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: node_js +node_js: + - 6 +cache: + directories: + - "node_modules" +script: + - npm run test diff --git a/README.md b/README.md index c75515a..49c18e3 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/handler.js b/handler.js index 8726ed0..371c445 100644 --- a/handler.js +++ b/handler.js @@ -1,7 +1,5 @@ -'use strict'; - const BASIC_AUTH_USERS = { - 'user': 'pass' + user: 'pass' }; module.exports.basicAuth = (event, context, callback) => { @@ -11,13 +9,17 @@ 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; } } @@ -25,8 +27,8 @@ module.exports.basicAuth = (event, context, callback) => { 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' }; diff --git a/package.json b/package.json index f597e9e..806b629 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "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" @@ -15,7 +16,11 @@ "author": "k1LoW (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" diff --git a/test/basicAuth.test.js b/test/basicAuth.test.js new file mode 100644 index 0000000..a47705a --- /dev/null +++ b/test/basicAuth.test.js @@ -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); +});