Overview
FootyCollect API uses token-based authentication provided by Django REST Framework. Each user can have a unique authentication token that must be included in the HTTP headers for protected endpoints.Authentication Methods
The API supports two authentication methods:| Method | Use Case | Implementation |
|---|---|---|
| Token Authentication | API clients, mobile apps | rest_framework.authentication.TokenAuthentication |
| Session Authentication | Browser-based requests | rest_framework.authentication.SessionAuthentication |
Most API clients should use Token Authentication. Session authentication is primarily for browser-based interactions and the Django admin interface.
Obtaining an Authentication Token
To obtain an authentication token, send a POST request to the token endpoint with your username and password.Endpoint
Request
Response
Success (200 OK)Using the Authentication Token
Once you have a token, include it in theAuthorization header of your API requests using the Token scheme.
Header Format
Example Requests
Response
Authentication Errors
Missing Token
If you attempt to access a protected endpoint without a token: Response (401 Unauthorized)Invalid Token
If you provide an invalid or expired token: Response (401 Unauthorized)Insufficient Permissions
If your token is valid but you lack permissions: Response (403 Forbidden)Django Allauth Integration
FootyCollect uses django-allauth for account management, which provides:- Email verification for new accounts
- Password reset functionality
- Social authentication (Google OAuth)
- Multi-factor authentication (MFA) support
- Account lockout after failed login attempts
Account Security
The following security measures are enforced:| Feature | Configuration |
|---|---|
| Email Verification | Mandatory for new accounts (ACCOUNT_EMAIL_VERIFICATION = "mandatory") |
| Login Rate Limiting | 5 failed attempts per 5 minutes (ACCOUNT_RATE_LIMITS = {"login_failed": "5/5m"}) |
| Password Requirements | Minimum 8 characters, not similar to username, not commonly used |
API token authentication bypasses some django-allauth features like MFA. For maximum security, ensure tokens are stored securely and rotated regularly.
Token Management
Token Persistence
Tokens are stored in the database and persist until explicitly deleted. Each user has one token that remains valid indefinitely unless revoked.Regenerating Tokens
To regenerate a token (e.g., if compromised), delete the existing token through the Django admin interface:- Navigate to
/admin/authtoken/tokenproxy/ - Find the user’s token
- Delete it
- Obtain a new token via
/api/auth-token/
Best Practices
Store tokens securely
Store tokens securely
- Use environment variables or secure credential storage
- Never commit tokens to version control
- Never expose tokens in client-side code
- Use HTTPS to prevent token interception
Rotate tokens regularly
Rotate tokens regularly
- Implement token rotation for long-running applications
- Regenerate tokens if you suspect compromise
- Delete tokens for deactivated users
Handle token expiration gracefully
Handle token expiration gracefully
- Implement proper error handling for 401 responses
- Provide a mechanism to reauthenticate
- Log authentication failures for monitoring
Use HTTPS in production
Use HTTPS in production
- Always use HTTPS to encrypt token transmission
- Configure
SECURE_SSL_REDIRECT = Truein Django settings - Set appropriate security headers
Example: Complete Authentication Flow
Here’s a complete example demonstrating authentication and API usage:Next Steps
Now that you understand authentication, learn about:- Rate Limiting - Understanding API rate limits
- API Introduction - Available endpoints and usage