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
string
required
User’s username
string
required
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
string
required
Unique username
string
required
Valid email address (must be unique)
string
required
Password (minimum length validation applies)
string
required
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
string
required
Registered email address
Step 2: Verify Reset Code
string
required
6-digit verification code from email
Step 3: Set New Password
string
required
New password
string
required
Password confirmation
User Model
The custom User model extends Django’s AbstractUser:integer
Unique user identifier
string
Unique username
string
Unique email address (required)
string
User role:
admin or vendedorstring
First name (optional)
string
Last name (optional)
string
Phone number (optional)
file
Profile photo upload
string
Google profile photo URL (for OAuth users)
boolean
Email verification status
boolean
Whether user has completed tutorial
boolean
Forces password change on next login (for vendedor accounts)
integer
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.