Blog | G5 Cyber Security

Secure Account Auto-Login

TL;DR

Automatically logging into an account using a random URL is extremely insecure and should never be done with Personally Identifiable Information (PII). This guide explains why, then details how to implement a secure auto-login system using standard techniques like session cookies and tokens. We’ll focus on best practices for security.

Why Random URLs are Dangerous

Using a random URL as part of an auto-login process creates several major vulnerabilities:

Do not use random URLs for auto-login with PII.

Secure Auto-Login Implementation

Here’s a step-by-step guide to implementing secure auto-login:

1. Authentication and Session Creation

  1. Standard Login: Implement a standard username/password login form.
  2. Authentication Check: Verify the user’s credentials against your database or authentication provider.
  3. Session Cookie: Upon successful authentication, create a secure session cookie. This is the core of auto-login.
    // Example (PHP) - Set a secure and HTTPOnly cookie
    session_start();
    $_SESSION['user_id'] = $user_id;
    setcookie('session_token', bin2hex(random_bytes(32)), [
      'expires' => time() + 3600, // Cookie expires in 1 hour
      'path' => '/',
      'domain' => '.yourdomain.com',
      'secure' => true,
      'httponly' => true,
      'samesite' => 'Strict'
    ]);

    Important:

    • Use session_start() at the beginning of each page requiring authentication.
    • Set the secure flag to true for HTTPS only.
    • Set the httponly flag to true to prevent JavaScript access (mitigates XSS attacks).
    • Use a strong, randomly generated session ID.
    • Consider using the ‘Strict’ SameSite attribute to protect against CSRF attacks.

2. Remembering the User (Persistent Login)

  1. “Remember Me” Checkbox: Add a “Remember me” checkbox on your login form.
  2. Token Generation: If the user checks the box, generate a unique, long-lived token.
    // Example (Python) - Generate a secure token using secrets module
    import secrets
    token = secrets.token_hex(32)
    
  3. Token Storage: Store the token securely in your database, associated with the user’s account.

    Important: Hash and salt the token before storing it.

  4. Cookie Setting: Set a cookie containing the token. Set an expiration date for this cookie (e.g., 30 days).
    // Example (JavaScript) - Setting a cookie with an expiry date
    document.cookie = "remember_token=" + token + "; expires=Fri, 31 Dec 9999 23:59:59 GMT; path=/; domain=.yourdomain.com; secure=true; samesite=Strict";

3. Auto-Login Process

  1. Cookie Check: On each page load, check if the “remember_token” cookie exists.
  2. Token Validation: If the cookie exists, retrieve the token from the database and validate it.
    • Verify that the token matches the stored hashed value.
    • Check if the token has expired.
  3. Session Restoration: If the token is valid, restore the user’s session by setting the appropriate session variables (e.g., $_SESSION['user_id'] in PHP).

4. Security Considerations

Exit mobile version