Skip to main content

Lightweight HTTP authentication via environment variables

Project description

env-http-auth

PyPI - Version PyPI - Python Version

Lightweight HTTP authentication via environment variables for httpx, requests, and raw HTTP clients.

Features

  • Zero dependencies by default (stdlib only)
  • Universal: Works with requests, httpx, and any HTTP client
  • Multiple auth sources: Environment variables, netrc, keyring, config files
  • Per-host authentication with suffix patterns
  • Type safe: Full type hints for better development experience

Table of Contents

Installation

pip install env-http-auth

Quick Start

import os
from env_http_auth import get_auth

# Set environment variable
os.environ["HTTP_AUTH_TOKEN_example_com"] = "my-token"

# Get auth dict for URL
auth = get_auth("https://example.com/path")
# Returns: {'Authorization': 'Bearer my-token'}

Environment Variables

Environment variables are the primary way to configure authentication. Hostnames are normalized (lowercase, dots become underscores).

Variable Naming

Priority Variable Example Value Output
1 HTTP_AUTH_TOKEN_example_com mytoken Bearer mytoken
2 HTTP_AUTH_TOKEN__example_com mytoken Bearer mytoken (suffix)
3 HTTP_AUTH_HEADER_example_com Bearer token Bearer token
4 HTTP_AUTH_BASIC_example_com user:pass Basic dXNlcjpwYXNz
5 HTTP_AUTH_TOKEN token Bearer token
6 HTTP_AUTH_HEADER Bearer token Bearer token
7 HTTP_AUTH Custom creds Custom creds

Notes

  • Periods (.) in hostnames become underscores (_) in env var names
    • example.comHTTP_AUTH_TOKEN_example_com
    • artifactory.company.comHTTP_AUTH_TOKEN_artifactory_company_com
  • Matching is case-insensitive
  • Use config file for hosts with many periods

Suffix Matching

Double underscore (__) indicates suffix matching:

export HTTP_AUTH_TOKEN__example_com=my-token
# Matches: example.com, sub.example.com, api.example.com

Scheme Specifier

Override the auth scheme per host:

export HTTP_AUTH_SCHEME_example_com=bearer
export HTTP_AUTH_TOKEN_example_com=my-token
# Output: Bearer my-token

export HTTP_AUTH_SCHEME_example_com=basic
export HTTP_AUTH_TOKEN_example_com=user:pass
# Output: Basic dXNlcjpwYXNz

Usage Examples

Basic usage

from env_http_auth import get_auth, get_auth_header

# Get full auth dict
auth = get_auth("https://example.com/path")
# {'Authorization': 'Bearer my-token'}

# Get just the header value
header = get_auth_header("https://example.com/path")
# 'Bearer my-token'

Using requests

from env_http_auth import HTTPEnvAuth
import requests

# Set environment variable
# HTTP_AUTH_TOKEN_example_com=my-token

# Use with requests
response = requests.get(
    "https://example.com/api/data",
    auth=HTTPEnvAuth()
)

Using httpx

from env_http_auth import HTTPEnvAuth
import httpx

# Set environment variable
# HTTP_AUTH_TOKEN_example_com=my-token

# Use with httpx
client = httpx.Client(auth=HTTPEnvAuth())
response = client.get("https://example.com/api/data")

AuthResolver class

For more control over authentication sources:

from env_http_auth import AuthResolver

# Use only specific sources
resolver = AuthResolver(sources={"env", "netrc"})

# Query by URL or hostname
auth = resolver.for_url("https://example.com/path")
auth = resolver.for_hostname("example.com")

# Custom source order
resolver = AuthResolver(sources={"config", "env", "keyring"})

Priority Chain

When resolving authentication, sources are tried in this order:

  1. Exact host match - HTTP_AUTH_TOKEN_example_com
  2. Suffix match - HTTP_AUTH_TOKEN__example_com (double underscore)
  3. Global env vars - HTTP_AUTH_TOKEN
  4. Config file - ~/.env-http-auth.ini
  5. netrc - ~/.netrc
  6. keyring - System keyring

Configuration Sources

Environment Variables

Primary source. Supports per-host and global variables. See Environment Variables for details.

netrc

Falls back to ~/.netrc for Basic authentication:

machine example.com
login admin
password secret123

System Keyring

Stores credentials in the system keyring:

import keyring

# Store token
keyring.set_password("example.com", "token", "my-secret")

# Store Basic auth
keyring.set_password("example.com", "username", "admin")
keyring.set_password("example.com", "password", "secret")

Config File

INI format at ~/.env-http-auth.ini:

[example.com]
token = my-token
scheme = bearer

[other.company.com]
basic_user = admin
basic_pass = secret
scheme = basic

CLI

Get authentication headers from the command line:

# Basic usage
$ export HTTP_AUTH_TOKEN_example_com=my-token
$ env-http-auth https://example.com
Authorization: Bearer my-token

# Header only (value only)
$ env-http-auth --header-only https://example.com
Bearer my-token

# No auth found
$ env-http-auth https://unknown.com
No authentication found for https://unknown.com

CLI Options

env-http-auth [-h] [--header-only] url

Get HTTP authentication header for a URL

positional arguments:
  url                  URL to get authentication header for

options:
  -h, --help           show this help message and exit
  --header-only        Output only the header value

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

env-http-auth is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

env_http_auth-0.0.2.tar.gz (25.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

env_http_auth-0.0.2-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file env_http_auth-0.0.2.tar.gz.

File metadata

  • Download URL: env_http_auth-0.0.2.tar.gz
  • Upload date:
  • Size: 25.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for env_http_auth-0.0.2.tar.gz
Algorithm Hash digest
SHA256 9c4d0402b838ce1748ea1df5f8985a25bd89066b39d0ed25b873a8ab7ae936e1
MD5 c6d3c00c91f60d42b26f8e2f46a97cac
BLAKE2b-256 23fa1935756c8a4c8757f6c74a7a02b89193b1e3888057b74d44cb095a641c48

See more details on using hashes here.

Provenance

The following attestation bundles were made for env_http_auth-0.0.2.tar.gz:

Publisher: main.yaml on FlavioAmurrioCS/env-http-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file env_http_auth-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: env_http_auth-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for env_http_auth-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 49a182a9c9ce691e811d9cf0b0329acd42d4bc2a30d61f7ed370e6f634aa5600
MD5 5707b3c993ad7d7eaf0bd1aa4100b4b2
BLAKE2b-256 cd25e6e9bff3d5e7ade6b5d08e2004d9fed6557000d15a98d4c4a4c5aa7a9397

See more details on using hashes here.

Provenance

The following attestation bundles were made for env_http_auth-0.0.2-py3-none-any.whl:

Publisher: main.yaml on FlavioAmurrioCS/env-http-auth

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page