Pure SASL

0.6.2 · active · verified Thu Apr 09

`pure-sasl` is a pure Python client-side implementation of the Simple Authentication and Security Layer (SASL) protocol. It enables Python applications to authenticate securely against servers using various SASL mechanisms like PLAIN, SCRAM-SHA-1, SCRAM-SHA-256, and GSSAPI (with an optional dependency). The current version is 0.6.2, and it maintains a moderate release cadence, with the latest update in March 2024.

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize a `SASLClient` for the PLAIN mechanism and process an initial (potentially empty) server challenge. For multi-step mechanisms like SCRAM, you would repeat the `client.process()` call for subsequent server challenges and client responses until `client.complete` is true.

import os
from pure_sasl.client import SASLClient

# Retrieve credentials from environment variables or provide defaults
# In a real application, these would come from configuration or user input.
username = os.environ.get("SASL_USERNAME", "myuser")
password = os.environ.get("SASL_PASSWORD", "mypassword")

# 1. Initialize the SASL client with the desired mechanism and credentials.
# Common mechanisms: 'PLAIN', 'SCRAM-SHA-1', 'SCRAM-SHA-256'.
try:
    client = SASLClient(
        mechanism='PLAIN',
        username=username,
        password=password
    )

    print(f"SASLClient initialized for PLAIN mechanism with user: {username}")

    # 2. Process the initial challenge from the server.
    # For PLAIN, the initial challenge is typically an empty bytes string.
    # In a real network exchange, this comes from the server.
    server_challenge_1 = b''
    client_response_1 = client.process(server_challenge_1)
    print(f"Client response to initial challenge: {client_response_1!r}")

    # 3. If authentication requires more steps (e.g., SCRAM),
    # the server would send another challenge, which you'd pass to .process() again.
    # For PLAIN, the process is often complete after the first response.
    if client.complete:
        print("SASL PLAIN authentication sequence complete.")
    else:
        print("Authentication not yet complete. Awaiting further server challenges.")

except Exception as e:
    print(f"Error during SASL client operation: {e}")

view raw JSON →