Kerberos (High-level GSSAPI Interface)

1.3.1 · active · verified Sat Apr 11

The `kerberos` library provides a high-level Python wrapper for Kerberos (GSSAPI) operations, specifically designed for client/server Kerberos authentication based on RFC 4559. It directly wraps the underlying Kerberos 5 C API, offering a limited set of functions for this purpose. The current version is 1.3.1, and it maintains an active release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the very basic client-side initialization of a Kerberos GSSAPI context using `kerberos.authGSSClientInit` and `kerberos.authGSSClientStep`. A real Kerberos environment with a Key Distribution Center (KDC), a configured service principal name (SPN), and an active Kerberos ticket (obtained via `kinit`) is required for successful operation. The process typically involves multiple `authGSSClientStep` calls, exchanging tokens with a server.

import kerberos
import os

# This is a simplified example. A real Kerberos setup with a KDC, service principal, and active tickets (kinit) is required.
# Set up a dummy service principal name for illustration.
# In a real scenario, this would be 'HTTP/your.service.com@REALM'
service_principal = os.environ.get('KERBEROS_SPN', 'HTTP/host.example.com@EXAMPLE.COM')

negotiate_token = None
try:
    # Initialize a Kerberos GSSAPI client context.
    # `gssflags` can be used to specify options like GSS_C_DELEG_FLAG.
    # `principal` can specify the client principal, if not using default cache.
    result, context = kerberos.authGSSClientInit(service_principal)

    # The client sends a 'token' (negotiate_token) to the server.
    # In a real HTTP exchange, this token would be part of the Authorization header.
    # This step simulates the client-side generation of the initial token.
    result = kerberos.authGSSClientStep(context, negotiate_token)

    # If successful, get the token to send to the server.
    negotiate_token = kerberos.authGSSClientResponse(context)
    print(f"Initial GSSAPI token generated: {negotiate_token[:30]}...")

    # In a real scenario, the server would send back its own token,
    # which the client would then process in subsequent authGSSClientStep calls.
    # For this quickstart, we just demonstrate the client init.

except kerberos.KerberosError as e:
    print(f"Kerberos Error: {e}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")
finally:
    # Always clean up the context to free resources.
    if 'context' in locals() and context is not None:
        kerberos.authGSSClientClean(context)

view raw JSON →