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
GET Request (/login)
Renders the login form.
Displays a modal if the request is from an untrusted IP.
if psw_hasher.verify(user.password, password): headers =remember(request,str(user.id)) request.session['role']= user.permission...returnHTTPFound(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
if not is_ip_trusted(ip_address) and not request.session.get("pingid_ok", False):
if not is_valid_ping_code(ping_code):
return {"show_modal": True, "error_ping": "Incorrect code."}
if request.session["current_attempt"] >= MAX_ATTEMPTS:
request.dbsession.add(ActivityLog(...))
return {"error_ping": "Too many failed attempts."}
show_modal = not is_ip_trusted(ip_address) and not request.session.get("pingid_ok", False)
def validate_password(password):
if len(password) < 8: ...
if not search(r'[A-Z]', password): ...
if not search(r'\d', password): ...
if not search(r'\W', password): ...