Overview
Inventario uses Django’s built-in session-based authentication with CSRF protection. The system also supports OAuth 2.0 via Google Sign-In through Django Allauth.Authentication Methods
Session Authentication
The primary authentication method uses Django sessions with cookies.Login
User’s username
User’s password
/dashboard/ with session cookie set
Logout
Google OAuth 2.0
Inventario supports Google Sign-In via Django Allauth.Initiate Google Login
CSRF Protection
All POST, PUT, DELETE requests require a valid CSRF token.Getting CSRF Token
- From Cookie: Django sets
csrftokencookie on first visit - From HTML Form: Extract from hidden input in rendered forms
Using CSRF Token
Form-Encoded Requests
AJAX/JSON Requests
User Registration
Create New User Account
Unique username
Valid email address (must be unique)
Password (minimum length validation applies)
Password confirmation (must match password1)
admin role by default, redirects to email verification
Email Verification
New users must verify their email address.Verification Flow
- User registers
- System generates 64-character token
- Verification email sent with link:
/verificar-email/<uidb64>/ - Token valid for 24 hours
- User clicks link to verify
Verify Email Endpoint
email_verified=True, redirects to login
Password Reset
Inventario implements a custom 6-digit code password reset flow.Step 1: Request Reset Code
Registered email address
Step 2: Verify Reset Code
6-digit verification code from email
Step 3: Set New Password
New password
Password confirmation
User Model
The custom User model extends Django’s AbstractUser:Unique user identifier
Unique username
Unique email address (required)
User role:
admin or vendedorFirst name (optional)
Last name (optional)
Phone number (optional)
Profile photo upload
Google profile photo URL (for OAuth users)
Email verification status
Whether user has completed tutorial
Forces password change on next login (for vendedor accounts)
Foreign key to admin user who created this account (for vendedor users)
Role-Based Access Control
Admin Role (rol='admin')
Full access to all endpoints:
- Create, edit, delete products
- Manage purchases
- Create/manage vendedor accounts
- Access all reports
- System configuration
Vendedor Role (rol='vendedor')
Limited access:
- View products (read-only)
- Create sales
- View own sales history
- Cannot modify products or purchases
Enforcing Permissions
Many endpoints use the@admin_required decorator:
Session Management
Session Cookie
Django sets asessionid cookie upon successful authentication:
Session Timeout
Configured in Django settings (default: 2 weeks)Checking Authentication Status
All views decorated with@login_required will redirect to /login/ if not authenticated.
/login/
Security Features
Password Validation
Custom validators inapplications.cuentas.validators:
- LongitudMinimaValidator - Minimum length requirement
- ContraseñaComunValidator - Prevents common passwords
- ContraseñaNumericaValidator - Prevents purely numeric passwords
CSRF Protection
Enabled viaCsrfViewMiddleware - all state-changing operations require valid CSRF token
Force Password Change
Vendedor users created by admin havedebe_cambiar_password=True and are redirected via ForzarCambioPasswordMiddleware until they set a new password.