PyKerberos (krb5)

0.9.0 · active · verified Thu Apr 09

krb5 is a Python library that provides direct bindings to the MIT Kerberos 5 C API, enabling Python applications to interact with Kerberos for authentication and security services at a low level. The current version is 0.9.0, with regular releases (typically a few times a year) maintaining compatibility and extending its API coverage.

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize a Kerberos context and parse a principal name using the krb5 library. It includes basic error handling for `Krb5Error` which wraps the underlying Kerberos C API errors. It's crucial to have system Kerberos development libraries installed for this to build and run successfully.

import krb5
from krb5.exceptions import Krb5Error
import os

try:
    # Initialize a Kerberos context
    ctx = krb5.init_context()
    print("Kerberos context initialized successfully.")

    # Example: Parse a principal name
    # Replace 'testuser@EXAMPLE.COM' with a valid principal in your Kerberos environment
    # or set KRB5_PRINCIPAL environment variable for example.
    principal_name = os.environ.get('KRB5_PRINCIPAL', 'host/localhost@REALM.TEST')
    
    # Using with statement ensures proper cleanup of the context and principal
    with ctx:
        with krb5.parse_name(ctx, principal_name) as principal:
            print(f"Parsed principal: {principal.name}")
            print(f"Principal Realm: {principal.realm}")

except Krb5Error as e:
    print(f"Kerberos Error: {e.message} (Code: {e.error_code})")
    print("Ensure Kerberos is configured correctly and system libraries are installed.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →