Password Reset System

Authentication → Included
💰 $0

Secure email-based password recovery and reset flow.

Technology iconTechnology iconTechnology icon

Password Reset System Module Overview

Purpose

The Password Reset System module is designed to provide a secure and user-friendly mechanism for recovering lost or forgotten passwords. It enables users to reset their passwords through an email-based verification process, ensuring both security and ease of use. This module handles the entire flow from initiating a password reset request to validating and updating the new password.

Benefits

Usage Scenarios

The Password Reset System module is essential in the following scenarios:

  1. Password Recovery: When users forget their login credentials, they can initiate a password reset process via email.
  2. Onboarding: Allows new users to set an initial password after receiving a confirmation email.
  3. Security Audits: Helps enforce security best practices by enabling periodic password resets or changes during audits.

Key Features

This module ensures a robust, secure, and scalable solution for handling password resets, making it an essential component of any authentication system.

Secure Token Generation

The Password Reset System generates unique, tamper-proof tokens for password reset requests using cryptographic algorithms. Each token is signed and includes an expiration time to ensure security.


Email-Based Password Reset Flow

Users can initiate the password reset process by providing their registered email. The system sends a reset link via email containing a secure token, allowing them to create a new password upon clicking.


IP Address Validation

The system checks if the IP address from which a reset request originates matches the user’s usual login location. This helps prevent unauthorized access and adds an extra layer of security.


Rate Limiting on Reset Requests

To mitigate brute force attacks, the module limits the number of password reset requests a user can make within a specific timeframe. Excessive attempts trigger temporary account lockouts.


Audit Logging of Reset Activities

Every password reset activity is recorded in audit logs, including details like request timestamps, user IDs, and IP addresses. This provides transparency and helps detect suspicious activities.


Expiring Tokens with Time Limits

Tokens generated for password resets expire after a configurable period (e.g., 24 hours). Expired tokens cannot be used to reset passwords, reducing the risk of unauthorized access.


CSRF Protection for Reset Requests

The system implements Cross-Site Request Forgery (CSRF) protection measures. This ensures that reset requests are not maliciously triggered by external websites or scripts.


Brute Force Detection and Lockout Mechanisms

The module detects brute force attempts on password reset tokens and locks accounts temporarily to prevent unauthorized access. Failed attempts trigger alerts for monitoring purposes.


Email Notifications on Reset Status

Users receive email notifications upon initiating a password reset, confirming successful completion, or if an attempt is detected from an unrecognized IP address.


Integration with Existing Systems

The Password Reset System integrates seamlessly with existing user authentication systems and security modules. It supports APIs for token generation, validation, and resets while maintaining compatibility with other system components.

Password Reset System Documentation

1. Module Overview

The Password Reset System module provides a secure mechanism for users to recover their passwords through an email-based reset flow. This module includes:

2. Code Samples

2.1 FastAPI Endpoint (Password Reset Request)
from fastapi import APIRouter, Depends, HTTPException
from typing import Annotated
from pydantic import EmailStr, BaseModel
from datetime import datetime, timedelta
import secrets
from jose import jwt

router = APIRouter()

# Pydantic Model for Password Reset Request
class PasswordResetRequest(BaseModel):
    email: EmailStr

# Dependency Injection for Token Generation
def get_token():
    return lambda: secrets.token_hex(32)

@router.post("/reset-password")
async def request_password_reset(
    request: Annotated[PasswordResetRequest, Depends()],
    token_generator: Annotated[str, Depends(get_token)]
):
    """
    Handles password reset requests by generating a secure token and sending it via email.
    """
    try:
        # Generate a random token
        token = token_generator()
        
        # Store the token in your database with expiration (e.g., 1 hour)
        expires_at = datetime.utcnow() + timedelta(hours=1)
        
        # Send reset link to user's email
        send_password_reset_email(request.email, token, expires_at)
        
        return {"message": "Password reset email sent successfully"}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
2.2 React UI Component (Password Reset Form)
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';

const PasswordResetForm = () => {
    const [email, setEmail] = useState('');
    const [loading, setLoading] = useState(false);
    const [message, setMessage] = useState('');
    const navigate = useNavigate();

    const handleSubmit = async (e) => {
        e.preventDefault();
        setLoading(true);
        try {
            const response = await fetch('/api/reset-password', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ email }),
            });

            if (!response.ok) {
                throw new Error('Failed to reset password');
            }

            const data = await response.json();
            setMessage(data.message);
            // Optionally redirect after successful request
            setTimeout(() => navigate('/login'), 2000);
        } catch (error) {
            setMessage(error.message || 'An error occurred while resetting your password.');
        } finally {
            setLoading(false);
        }
    };

    return (
        <div className="reset-password-container">
            <h1>Reset Password</h1>
            {message && <p className={loading ? 'loading' : ''}>{message}</p>}
            <form onSubmit={handleSubmit}>
                <label htmlFor="email">Email</label>
                <input
                    type="email"
                    id="email"
                    value={email}
                    onChange={(e) => setEmail(e.target.value)}
                    required
                />
                <button type="submit" disabled={loading}>
                    {loading ? 'Resetting...' : 'Reset Password'}
                </button>
            </form>
        </div>
    );
};

export default PasswordResetForm;
2.3 Pydantic Data Schema (Password Reset Flow)
from pydantic import BaseModel
from typing import Optional

class ResetToken(BaseModel):
    token: str
    email: str
    expires_at: datetime

class PasswordResetRequestSchema(BaseModel):
    email: EmailStr

class PasswordResetResponseSchema(BaseModel):
    status: int
    message: str
    token: Optional[str] = None

3. Usage Instructions

  1. Backend Setup:

    • Add the FastAPI endpoint /reset-password to handle password reset requests.
    • Implement the send_password_reset_email function with your email service provider.
  2. Frontend Integration:

    • Use the React component PasswordResetForm in your authentication flow.
    • Handle loading states and error messages appropriately in the UI.
  3. Security Best Practices:

    • Store reset tokens securely with an expiration time (e.g., 1 hour).
    • Implement rate limiting on password reset requests to prevent brute-force attacks.
    • Include a nonce or unique identifier in reset tokens to prevent replay attacks.
  4. Environment Variables:

    # Example .env file
    SMTP_HOST=your.smtp.host
    SMTP_PORT=587
    SMTP_USER=your smtp username
    SMTP_PASSWORD=your smtp password
    RESET_TOKEN_EXPIRY=3600  # in seconds (1 hour)
    

4. Error Handling

5. Testing

  1. Unit Tests:

    • Test password reset request endpoint.
    • Test token generation and validation logic.
  2. Integration Tests:

    • Verify that reset emails are sent successfully.
    • Ensure tokens expire after the specified duration.
  3. Security Tests:

    • Attempt to guess tokens or replay requests to verify security measures.

6. Maintenance


This documentation provides a complete implementation of a secure password reset system with code examples for both backend and frontend components, ensuring robust security and user experience.

The Password Reset System module is designed to securely handle password recovery and reset processes via email. Here’s a structured overview of its key components and implementation considerations:

Overview:

  1. User Authentication: Handles standard login procedures.
  2. Email Service Integration: Manages sending emails for reset links.
  3. Security Measures: Implements encryption and token validation.
  4. Session Management: Manages user sessions post-reset.
  5. Audit Logging: Tracks password reset activities.

Use Cases:

  1. Forgot Password Flow: Users request reset links via email.
  2. Reset Password Flow: Users reset passwords after clicking the link.
  3. Admin-Initiated Reset: Administrators reset passwords for locked-out users.

Integration Tips:

  1. Email Configuration: Integrate with services like SendGrid, using environment variables for credentials.
  2. Token Security: Use short-lived tokens (e.g., 24 hours) and HTTPS for secure communication.
  3. Validation & Logging: Validate tokens on each use and log attempts to detect suspicious activity.

Configuration Options:

Implementation Considerations:

This module is crucial for user account recovery and should be integrated securely to protect user data. Proper setup ensures a robust password reset mechanism while maintaining system integrity.