Blog | G5 Cyber Security

Guest Access: Authentication Without Registration

TL;DR

This guide shows you how to let users access your system without needing them to create an account first. We’ll use temporary, one-time codes sent by email or generated on demand.

1. Understand the Risks and Benefits

Benefits:

Risks:

This method is best suited for low-sensitivity applications.

2. Choose an Authentication Method

We’ll cover two main approaches:

3. Email Verification Implementation

  1. Generate a Unique Code: When a user requests access, create a random alphanumeric code (e.g., 6-8 characters). Ensure it’s unlikely to collide with existing codes.
  2. Store the Code and User Email: Temporarily store the code associated with the user’s email address in your database. Include an expiry timestamp (e.g., 15 minutes).
  3. Send the Email: Send an email containing the unique code to the provided email address.
  4. Verification Process: When the user enters the code, check if it matches the stored code for that email and hasn’t expired. If valid, grant access.
  5. Code Expiry: Regularly clean up expired codes from your database (e.g., using a scheduled task).

Example Python code snippet to generate a random code:

import secrets
import string

def generate_code(length=8):
  alphabet = string.ascii_letters + string.digits
  return ''.join(secrets.choice(alphabet) for i in range(length))

4. TOTP Implementation

  1. Install a TOTP Library: Use a library like pyotp (Python), or similar libraries in other languages.
  2. Generate a Secret Key: For each user requesting access, generate a unique secret key. Store this securely.
  3. Display the QR Code/Manual Input: Present the user with a QR code containing the secret key and your application’s name (for use in authenticator apps like Google Authenticator or Authy). Alternatively, provide the secret key as text for manual input.
  4. Verification Process: When the user enters the TOTP code from their app, verify it against the stored secret key using the library function.

Example Python code snippet to generate a QR code:

import pyotp
import qrcode

def generate_totp_qr(secret_key, application_name):
  totp = pyotp.TOTP(secret_key)
  uri = totp.provisioning_uri(application_name=application_name)
  qr = qrcode.QRCode(
    version=1,
    error_correction=qrcode.constants.ERROR_CORRECT_L,
    box_size=10,
    border=4,
  )
  qr.add_data(uri)
  qr.make(fit=True)
  return qr

5. Security Considerations

Exit mobile version