Overview
The Inventory Management System uses a secure authentication system that manages user login, password reset, and session management. The authentication flow is handled by the Auth module and supports future JWT token-based authentication.Authentication Architecture
The authentication system is built using a clean architecture pattern:- Controller Layer:
backend/Auth/Adapters/auth_controller.py - Service Layer:
backend/Auth/Domain/auth_service.py - Middleware:
backend/CommonLayer/middleware/auth_middleware.py
Login Flow
Endpoint
Request Body
Authentication Process
- Credential Validation: The system checks if the username exists in the database
- Password Verification: Uses
werkzeug.security.check_password_hashto verify the password - Account Status Check: Validates that the user account is active
- User Data Response: Returns user information on successful authentication
Response
Success (200)Code Example
Fromauth_controller.py:14-46:
Security Features
Password Hashing
Password Hashing
Passwords are hashed using
werkzeug.security.generate_password_hash before storage. The system never stores plain text passwords.Generic Error Messages
Generic Error Messages
Login errors return the same message “Usuario o contraseña incorrectos” to prevent user enumeration attacks.
Active Account Validation
Active Account Validation
Inactive accounts cannot log in. The system returns: “Esta cuenta ha sido desactivada. Contacte a un administrador.”
Password Reset Flow
The system provides a secure password reset mechanism using time-limited tokens.Step 1: Request Password Reset
EndpointToken Generation
Fromauth_service.py:42-59:
itsdangerous.URLSafeTimedSerializer with:
- Secret key from Flask config
- Salt:
'password-reset-salt' - Default expiration: 3600 seconds (1 hour)
Step 2: Reset Password with Token
EndpointToken Validation
Fromauth_service.py:61-86:
Session Management
Current Implementation (Header-Based)
The system currently uses a temporary authentication mechanism via HTTP headers: Required Headerauth_middleware.py:11-65:
Valid Roles
Fromauth_middleware.py:8:
This header-based authentication is temporary. JWT token authentication will be implemented in Epic 3.2.
Future: JWT Token-Based Authentication
The system is designed to support JWT tokens. When implemented:- Login will return a JWT token containing user claims (id, username, role)
- The
@require_roledecorator will extract the role from the JWT token instead of headers - Tokens will have configurable expiration times
- Refresh token mechanism for extended sessions
User Flow Screenshots
Login Screen
User enters username and password credentials. The form validates required fields before submission.
Authentication
System validates credentials against the database, checks password hash, and verifies account status.
Password Reset Request
User clicks “Forgot Password” and enters their username. System sends reset link via email (currently mocked).
Error Handling
| Error Code | Scenario | Message |
|---|---|---|
| 400 | Missing credentials | ”Username y password son requeridos.” |
| 401 | Invalid credentials | ”Usuario o contraseña incorrectos.” |
| 401 | Inactive account | ”Esta cuenta ha sido desactivada. Contacte a un administrador.” |
| 401 | Missing auth header | ”Se requiere autenticación. Proporcione el header X-User-Role.” |
| 400 | Invalid reset token | ”Token de recuperación inválido.” |
| 400 | Expired reset token | ”El enlace de recuperación ha expirado. Por favor, solicite uno nuevo.” |
| 500 | Server error | ”Error interno del servidor” |
Best Practices
Password Requirements
Enforce strong passwords with minimum length, uppercase, lowercase, numbers, and special characters.
Token Expiration
Reset tokens expire after 1 hour. Always validate token age before accepting password resets.
Account Lockout
Consider implementing account lockout after multiple failed login attempts to prevent brute force attacks.
Audit Logging
All authentication events are logged for security auditing and compliance.
Next Steps
Roles & Permissions
Learn about the three user roles and their permissions
User Management
Create and manage user accounts