basicauth library

1.0.0 · active · verified Fri Apr 17

The `basicauth` library provides an incredibly simple implementation for HTTP Basic Authentication in Python. It focuses purely on decoding and basic verification of the 'Authorization' header. The current version is 1.0.0. The project is stable and has a low release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `basicauth.decode` to extract username and password from an 'Authorization' header, and `basicauth.verify` to check credentials. It highlights handling `None` returns for invalid headers.

import basicauth

# Example Authorization header
auth_header_valid = 'Basic Zm9vOmJhcg==' # 'foo:bar'
auth_header_invalid = 'Bearer eyJh...' # Not Basic Auth
auth_header_malformed = 'Basic not_base64'

# --- Decoding a Basic Auth header ---
# Returns (username, password) or None
username, password = basicauth.decode(auth_header_valid)
if username and password:
    print(f"Decoded (valid): Username='{username}', Password='{password}'")
else:
    print(f"Failed to decode (valid): {username=}, {password=}")

username_invalid, password_invalid = basicauth.decode(auth_header_invalid)
if username_invalid is None:
    print(f"Decoded (invalid type, correctly None): {username_invalid=}, {password_invalid=}")

username_malformed, password_malformed = basicauth.decode(auth_header_malformed)
if username_malformed is None:
    print(f"Decoded (malformed, correctly None): {username_malformed=}, {password_malformed=}")

# --- Verifying a Basic Auth header ---
# Returns True or False
is_valid_auth = basicauth.verify(auth_header_valid, 'foo', 'bar')
print(f"Verification (correct credentials): {is_valid_auth}")

is_invalid_auth = basicauth.verify(auth_header_valid, 'wrong', 'credentials')
print(f"Verification (incorrect credentials): {is_invalid_auth}")

is_invalid_format = basicauth.verify(auth_header_invalid, 'foo', 'bar')
print(f"Verification (wrong header format): {is_invalid_format}")

view raw JSON →