Python PAM

2.0.2 · active · verified Sat Apr 11

python-pam is a Python module that provides an interface to the Pluggable Authentication Modules (PAM) system on Unix-like operating systems using ctypes. It enables Python applications to perform user authentication tasks by leveraging the underlying system's PAM configuration. The current version is 2.0.2. Releases are primarily driven by bug fixes and compatibility updates.

Warnings

Install

Imports

Quickstart

Initializes the PAM authentication object and attempts to authenticate a user with provided credentials. The `authenticate()` method returns `True` or `False`, and additional details are available in `p.code` and `p.reason`.

import pam
import os

p = pam.authenticate()

# For demonstration, use environment variables or default values.
# On a real system, you would get these from user input.
username = os.environ.get('PAM_TEST_USERNAME', 'testuser')
password = os.environ.get('PAM_TEST_PASSWORD', 'testpassword')

# Note: For successful authentication, 'testuser' and 'testpassword'
# must be valid credentials on the system where this code runs,
# and the PAM configuration must allow 'python-pam' to authenticate.
if p.authenticate(username, password):
    print(f"Authentication successful for {username}. Code: {p.code}, Reason: {p.reason}")
else:
    print(f"Authentication failed for {username}. Code: {p.code}, Reason: {p.reason}")

view raw JSON →