Requests Kerberos Authentication
requests-kerberos is a Python library that provides a Kerberos authentication handler for the popular `requests` HTTP library. It enables applications to perform Kerberos/GSSAPI authentication, including mutual authentication, with web services. The current version is 0.15.0, with releases primarily driven by bug fixes, dependency updates, and feature enhancements related to Kerberos protocols.
Warnings
- breaking Dropped support for Python 2 and raised the minimum Python version to 3.6.
- breaking The underlying Kerberos dependency changed from older libraries to `pyspnego`. Additionally, `wrap_winrm` and `unwrap_winrm` functions were removed.
- gotcha The `context` attribute on `HTTPKerberosAuth` was renamed to `_context` to indicate it is for internal use only.
- gotcha Support for proxying `HTTPS` endpoints with Kerberos authentication is not available due to limitations of the underlying `requests` and `urllib3` libraries.
- gotcha By default, `HTTPKerberosAuth` requires mutual authentication (`mutual_authentication=REQUIRED`), meaning it will verify the server's identity. If verification fails, a `requests_kerberos.errors.MutualAuthenticationError` is raised. If a server emits an error which cannot be authenticated, the response content/headers might be stripped unless `sanitize_mutual_error_response=False` is set.
- gotcha For `requests-kerberos` to function, a Kerberos Ticket-Granting Ticket (TGT) must typically be available in the local credential cache (e.g., obtained via `kinit`). Without an active TGT, authentication attempts will fail.
- gotcha The implementation of Channel Binding Tokens (CBT) for `send_cbt` is now per-host. This might change behavior if your setup previously relied on a global CBT configuration.
Install
-
pip install requests-kerberos
Imports
- HTTPKerberosAuth
from requests_kerberos import HTTPKerberosAuth
Quickstart
import requests
import os
from requests_kerberos import HTTPKerberosAuth, REQUIRED
# NOTE: This example requires an active Kerberos Ticket-Granting Ticket (TGT)
# obtained via `kinit` or similar, or explicit principal/password (not shown).
# Replace 'http://your-kerberos-protected-service.example.com' with your actual URL.
KERBEROS_URL = os.environ.get('KERBEROS_PROTECTED_URL', 'http://your-kerberos-protected-service.example.com')
try:
# By default, mutual_authentication=REQUIRED (as explicitly shown here)
# means the client will verify the server's identity.
response = requests.get(KERBEROS_URL, auth=HTTPKerberosAuth(mutual_authentication=REQUIRED))
response.raise_for_status() # Raise an exception for HTTP errors (4xx or 5xx)
print(f"Successfully authenticated to {KERBEROS_URL}")
print(f"Status Code: {response.status_code}")
print(f"Response content snippet: {response.text[:200]}...")
except requests.exceptions.RequestException as e:
print(f"Error accessing Kerberos protected service: {e}")
except Exception as e:
print(f"An unexpected error occurred: {e}")