Authentication module

This module handles user authentication. It includes essential security and usability features, including IP trust checks, session attempt limits, password hashing, and role-based access.

πŸ” Login Module

πŸ”§ Workflow

  1. GET Request (/login)

    • Renders the login form.

    • Displays a modal if the request is from an untrusted IP.

    show_modal = not is_ip_trusted(ip_address) and not request.session.get("pingid_ok", False)
    return {"show_modal": show_modal}
  2. POST Request (/login)

    • Validates IP, ping code, credentials, and email domain.

    • Limits login attempts to 5.

    • Verifies password using Argon2.

    • All POST forms are protected against CSRF.arrow-up-right

    if psw_hasher.verify(user.password, password):
        headers = remember(request, str(user.id))
        request.session['role'] = user.permission
        ...
        return HTTPFound(location=request.route_url('home'), headers=headers)

πŸ”’ Security Measures

  • Trusted Network Gate Users from unrecognized IPs must submit a ping code:

  • Password Protection Uses argon2.PasswordHasher() for hashing and verifying.

  • Session-based Brute Force Protection Tracks failed attempts in the session and logs them if necessary:


πŸ“ User Registration

User registration is an admin-only view that is used to register new users, making it easier than having to enter the database and adding them manually.

πŸ” Access Control

Admin-Only Route

Registration is protected with a permission requirement. Only authenticated users with "admin" permission can access it:

This is enforced for both GET (form display) and POST (form submission) methods.


πŸ”§ Registration Workflow

Step 1: Accessing the Form

A GET request renders the registration form. If the user is on an untrusted IP, a modal appears prompting for a ping code.

Step 2: Submitting the Form

Upon form submission (POST):

  • The IP and ping code are verified.

  • Inputs (email, password, etc.) are validated.

  • If all is valid, a new user is added and automatically logged in.

  • This and every other POST forms have protection against CSRF attacks.


πŸ” Security & Validation Details

βœ… IP Trust with Ping Code

Untrusted network access triggers a ping code prompt to prevent rogue signups, just as in login


βœ… Password Strength Enforcement

To promote security, passwords must meet strong validation criteria:

Rejected passwords return a list of human-readable errors:


βœ… Email Domain Restriction

To limit registration to internal users only, emails must end in @kochcc.com:


βœ… Password Confirmation

Passwords must be confirmed. If they don’t match, registration is halted:


βœ… Unique Username Generator

The system generates a unique username using parts of the user's name and a random number:

It checks the database for uniqueness and retries until success. This ensures all usernames are unique.


βœ… Duplicate Email Check

Before creating a user, the system verifies that the email hasn’t already been registered:


βœ… Secure Storage with Argon2

Passwords are securely hashed using Argon2 before saving to the database:


🀝 Post-Registration Login

Once a user is registered:

  • They are automatically logged in.

  • Their session stores their role and an expiration timestamp.

  • Ping code trust and form data are cleared from the session.


πŸ“‚ Template Integration

  • Template: /templates/register.jinja2

  • Passed Context:

    • show_modal: Whether to show the ping code modal

    • error_ping: Any validation or process errors

    • form_data: Pre-filled data if registration fails mid-way


🧠 Design Rationale

Feature
Purpose

Admin-Only Access

Prevents arbitrary user creation

Strong Password Policy

Reduces risk of credential compromise

Domain Filtering

Limits registration to official emails

IP Trust Mechanism

Adds friction against unknown device signups

Username Auto-Generation

Simplifies onboarding, ensures unique identifiers

Auto-login After Sign-Up

Improves UX by eliminating a second login step

Last updated