7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS CloudFront + S3 の環境に「複数の」Basic認証をかける

Posted at

概要

AWSのCloudFrontとS3で構成された環境に対して、Basic認証をかける方法自体は調べればたくさん出てきましたが、複数のBasic認証をかける方法がサクッと出てこなかったので、一応残しておこうと思いました。

ひとつのBasic認証の場合

こちらの記事がとてもわかりやすいので、ご覧ください。丸投げ!

複数のBasic認証の場合

上記の記事のコードを参考に、複数のBasic認証に対応。

lambda-basic-auth.js
'use strict';
exports.handler = (event, context, callback) => {

    // Get request and request headers
    const request = event.Records[0].cf.request;
    const headers = request.headers;

    // Configure authentication
    var authUsers = [
        {
            'user':'user1_name',
            'pass':'user1_pass',
        },
        {
            'user':'user2_name',
            'pass':'user2_pass',
        },
    ];

    var authString = '';

    // Require Basic authentication
    if (typeof headers.authorization != 'undefined') {
        authUsers.forEach(function(authUser){
            // Construct the Basic Auth string
            authString = 'Basic ' + new Buffer(authUser.user + ':' + authUser.pass).toString('base64');

            if (headers.authorization[0].value == authString) {
                // Continue request processing if authentication passed
                callback(null, request);
            }
        });
    }

    // Reject request
    const body = 'Unauthorized';
    const response = {
        status: '401',
        statusDescription: 'Unauthorized',
        body: body,
        headers: {
            'www-authenticate': [{key: 'WWW-Authenticate', value:'Basic'}]
        },
    };
    callback(null, response);
};

おしまい。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?