WinKerberos

0.13.0 · active · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to initiate a Kerberos security context using `winkerberos.auth()`. This function leverages the underlying Windows SSPI to attempt authentication using the current user's Kerberos tickets. Success means a context object is returned, which can then be used for further token exchange in a client-server interaction.

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}")

view raw JSON →