Setting up Basic authentication using Python @Lambda

Introduction

When I searched for Basic authentication with Lambda, I found only Node.js, so I wrote it. Also serves as a memorandum of my own. The Node.js version will come out as soon as you go around, so I won't touch it here.

code

It's not a big deal, but it's a rehash of the Node.js version. However, it is a little different in that it can support multiple accounts.

import json
import base64

accounts = [
    {
        "user": "user1",
        "pass": "pass1"
    },
    {
        "user": "user2",
        "pass": "pass2"
    }
    ]

def lambda_handler(event, context):
    request = event.get("Records")[0].get("cf").get("request")
    headers = request.get("headers")
    
    authorization_header = headers.get("authorization")
    
    if not check_authorization_header(authorization_header):
        return {
            'headers': {
                'www-authenticate': [
                    {
                        'key': 'WWW-Authenticate',
                        'value':'Basic'
                    }
                ]
            },
            'status': 401,
            'body': 'Unauthorized'
        }
            
        
    return request

def check_authorization_header(authorization_header: list) -> bool:
    if not authorization_header:
        return False
    
    for account in accounts:
        encoded_value = base64.b64encode("{}:{}".format(account.get("user"), account.get("pass")).encode('utf-8'))
        check_value = "Basic {}".format(encoded_value.decode(encoding='utf-8'))
        
        if authorization_header[0].get("value") == check_value:
            return True

    return False

Settings

Basically, It was easy! How to put BASIC authentication in CloudFront + S3 should be fine. There is a description of the ʻAuthorization` header that is missing from other sites here.

Recommended Posts

Setting up Basic authentication using Python @Lambda
Setting up Digest authentication using Python @Lambda
Achieve Basic Authentication with CloudFront Lambda @ Edge with Python 3.8
BASIC authentication with Python bottle
[Python] Using OpenCV with Python (Basic)
Summary if using AWS Lambda (Python)
Authentication using tweepy-User authentication and application authentication (Python)
Ansible playbook for setting up Python preferences using pyenv on Mac
[SAM] Try using RDS Proxy with Lambda (Python) [user/pass, IAM authentication]
[AWS] Using ini files with Lambda [Python]
[Python] Speeding up processing using cache tools
Install python library on Lambda using [/ tmp]
A memo when setting up a Docker container for using JUMAN ++, KNP, python
RF Python Basic_01
Beware of disable_existing_loggers when setting up Python logging
Start using Python
Basic Python writing
VBA user tried using Python / R: basic grammar
Python3 basic grammar
RF Python Basic_02
Send HTTP with Basic authentication header in Python
Python Https Authentication
Scraping using Python
Flask Basic authentication
[Introduction to Python] Basic usage of lambda expressions
From setting up Raspberry Pi to installing Python environment
How to set up a Python environment using pyenv
Pass the authentication proxy through communication using python urllib3
Check types_map when using mimetypes on AWS Lambda (Python)
VBA users tried using Python / R: basic arithmetic operations
Setting up Jupyter Lab in a Python 3.9 venv environment
Python basic course (12 functions)
Python I'm also basic
Python basic grammar / algorithm
Python Basic Course (7 Dictionary)
Operate Redmine using Python Redmine
Python basic course (2 Python installation)
Basic sorting in Python
Fibonacci sequence using Python
Python basic course (9 iterations)
Data analysis using Python 0
A python lambda expression ...
[python] class basic methods
Python basic course (6 sets)
Data cleaning using Python
Python3 cheat sheet (basic)
Python basic grammar (miscellaneous)
Python Basic Course (Introduction)
Using Python #external packages
Python basic memorandum part 2
python basic on windows ②
WiringPi-SPI communication using Python
Python basic memo --Part 2
Age calculation using python
Python basic course (13 classes)
Basic Python command memo
Python basic grammar note (4)
Python basic grammar note (3)
Basic knowledge of Python
Search Twitter using Python
Python basic grammar memo