WinKerberos
WinKerberos is a Python library providing a high-level interface to SSPI (Security Support Provider Interface) for Kerberos client authentication, exclusively for Windows platforms. It is maintained by MongoDB-labs and has a consistent release cadence, frequently updating to support new Python versions.
Warnings
- breaking Upgrading `winkerberos` may require a Python version upgrade. Version 0.11.0 dropped support for Python 3.7. Version 0.10.0 dropped support for Python 2.7, 3.5, and 3.6. Current versions (0.10.0+) require Python >=3.10.
- gotcha This library is designed exclusively for Windows environments, leveraging the Security Support Provider Interface (SSPI). It will not function on Linux, macOS, or other operating systems.
- gotcha Successful authentication with `winkerberos` depends on a correctly configured Kerberos environment on the Windows host. This includes domain membership, access to a Key Distribution Center (KDC), valid service principal names (SPNs), and potentially existing Kerberos tickets.
- gotcha Incorrectly formatted or invalid Service Principal Names (SPNs) for `winkerberos.auth()` will result in `GSSError`s during context initiation.
Install
-
pip install winkerberos
Imports
- auth
import winkerberos # ... context = winkerberos.auth(service_principal, hostname)
Quickstart
import winkerberos
import os
# Example Service Principal Name (SPN) for a hypothetical service.
# In a real scenario, this would be a valid SPN for the service you want to authenticate with.
# For example: 'HTTP/webserver.example.com@EXAMPLE.COM'
SERVICE_PRINCIPAL = os.environ.get('WINKERBEROS_SPN', 'HTTP/localhost@EXAMPLE.COM')
HOSTNAME = os.environ.get('WINKERBEROS_HOSTNAME', 'localhost')
try:
# winkerberos typically uses the current logged-in user's credentials
# on a domain-joined Windows machine, so 'user'/'password' are often not needed.
context = winkerberos.auth(SERVICE_PRINCIPAL, HOSTNAME)
if context:
print(f"Successfully initiated Kerberos context for SPN: {SERVICE_PRINCIPAL}")
print("Kerberos authentication context established.")
# In a real application, 'context' would be used to generate tokens
# for further communication (e.g., context.step('challenge')).
else:
print(f"Failed to initiate Kerberos context for SPN: {SERVICE_PRINCIPAL}")
print("Ensure Kerberos is configured, tickets are available, and the SPN is valid.")
except winkerberos.GSSError as e:
print(f"Kerberos GSSAPI Error: {e}")
print("This often indicates issues with the Kerberos configuration, "
"network, or an invalid Service Principal Name (SPN).")
except Exception as e:
print(f"An unexpected error occurred: {e}")