WWW-Authenticate Header Parser
This is a tiny Python library designed specifically for parsing complex `WWW-Authenticate` HTTP headers. It simplifies the extraction of authentication scheme names and their parameters, returning them in a `collections.OrderedDict`. The library's last release was in August 2015, indicating it is no longer actively developed or maintained.
Warnings
- breaking The library has not been updated since August 2015 (version 0.9.2). This means it likely does not incorporate modern HTTP authentication standards, security best practices, or bug fixes for issues discovered in the last nine years. It might be incompatible with newer Python versions or produce incorrect results for contemporary authentication schemes.
- gotcha This library only parses the `WWW-Authenticate` header. It does not handle the full authentication handshake, generate `Authorization` headers, or manage credentials. It is purely a parsing utility.
- gotcha The parsing logic might be brittle for malformed or non-standard compliant `WWW-Authenticate` headers, which can occur in the wild. Some newer or less common authentication schemes might not be parsed correctly or fully.
Install
-
pip install www-authenticate
Imports
- parse
import www_authenticate parsed_header = www_authenticate.parse(header_string)
Quickstart
import www_authenticate
# Simulate a WWW-Authenticate header from a server response
www_authenticate_header = 'Basic realm="Restricted Area", charset="UTF-8", Digest realm="api", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", opaque="5ccc069c403ebaf9f0171e9517f40e41"'
parsed_challenges = www_authenticate.parse(www_authenticate_header)
print("Parsed WWW-Authenticate Challenges:")
for scheme, params in parsed_challenges.items():
print(f" Scheme: {scheme}")
print(f" Parameters: {params}")
# Accessing specific parameters
if 'Basic' in parsed_challenges:
basic_params = parsed_challenges['Basic']
print(f"\nBasic Realm: {basic_params.get('realm')}")
if 'Digest' in parsed_challenges:
digest_params = parsed_challenges['Digest']
print(f"Digest Nonce: {digest_params.get('nonce')}")