Requests Kerberos Authentication

0.15.0 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to make a GET request to a Kerberos-protected service. It uses `HTTPKerberosAuth` to handle the authentication handshake. Ensure you have an active Kerberos Ticket-Granting Ticket (TGT) on your system (e.g., by running `kinit`) for the default principal to be used. The `KERBEROS_PROTECTED_URL` environment variable can be used to specify the target URL, otherwise a placeholder is used.

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

view raw JSON →