Skip to content

Authentication Module

Authentication Functions

auth

This module provides authentication utilities for GenAIPot, including password hashing and credential checking.

check_credentials(username, password)

Check if the provided credentials match the stored credentials.

Parameters:

Name Type Description Default
username str

The username to check.

required
password str

The password to check.

required

Returns:

Name Type Description
bool

True if the credentials match, False otherwise.

Source code in src/auth.py
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def check_credentials(username, password):
    """
    Check if the provided credentials match the stored credentials.

    Args:
        username (str): The username to check.
        password (str): The password to check.

    Returns:
        bool: True if the credentials match, False otherwise.
    """
    stored_username = config.get('server', 'username')
    stored_password = config.get('server', 'password')  # This is the hashed password

    # Hash the provided password to compare with stored hash
    hashed_password = hash_password(password)

    if config.getboolean('server', 'debug', fallback=False):
        logger.debug("Checking credentials for user: %s", username)
        logger.debug("Provided password (hashed): %s", hashed_password)
        logger.debug("Stored username: %s", stored_username)
        logger.debug("Stored password (hashed): %s", stored_password)

    return username == stored_username and hashed_password == stored_password

hash_password(password)

Hash the provided password using SHA-256.

Parameters:

Name Type Description Default
password str

The password to hash.

required

Returns:

Name Type Description
str

The hashed password.

Source code in src/auth.py
35
36
37
38
39
40
41
42
43
44
45
def hash_password(password):
    """
    Hash the provided password using SHA-256.

    Args:
        password (str): The password to hash.

    Returns:
        str: The hashed password.
    """
    return hashlib.sha256(password.encode()).hexdigest()