Skip to content

Understanding TOTP with Laravel Implementation Guide

Published:

Table of contents

Open Table of contents

What is TOTP

TOTP short for Time-Based One-Time Password is a 2 Factor Authentication method used to add an extra layer of security to an online account.

How to use it

To set up TOTP for an online account, users typically receive either a QR code or a secret code from the website where they’re enabling 2FA.

Then they can scan the QR code or manually enter the secret code into an authenticator app like Google Authenticator to link it with the website.

Once linked, the app generates time-based codes, which expire in about 30 seconds.

During subsequent logins, user can retrieve the current code from the app and enter that when prompted for the TOTP code to complete the 2FA process.

How does it work

When the user sets up TOTP, we generate a unique key and store it in the database. This secret code can be displayed either as text or embedded in a QR code.

When the user needs to authenticate, their authenticator app generates a one-time password based on their secret key and the current time.

On the server, when the user attempts to authenticate with this one-time password, the server also calculates the OTP based on the stored secret and the current time. If the OTP generated by the server matches the one entered by the user, the authentication process is successful.

Embedding Secret in QR Code

To embed the necessary information in a QR code for TOTP setup, a specific format is used to ensure compatibility with authenticator apps.

Typically, the QR code contains a URL in the format of:

otpauth://totp/{app_name}:{user_email}?secret={secret_code}&issuer={app_name}&algorithm={algorithm_name}&digits={number_of_digits}&period={number_seconds_to_expire}

Here’s a breakdown of the components:

This standardized format ensures interoperability and ease of setup across various authenticator apps.

How to generate TOTP

To generate a one-time password (OTP) based on a secret key and the current time, the system follows a precise sequence of steps. First, it obtains the current time in Unix timestamp format. This timestamp is then divided by a predefined interval, typically 30 seconds, resulting in a sequence value. The secret key, unique to each user, is combined with this sequence value. The combination undergoes a cryptographic process known as HMAC-SHA1, which produces a hash value. This hash value undergoes dynamic truncation to extract a certain number of bits, which then form the final OTP

TOTP Generation Digram

What are the benefit of this

  1. Adds an extra layer of security beyond just a username and password, reducing the risk of unauthorized access, especially in cases where passwords may have been compromised.
  2. TOTP doesn’t require an internet connection to generate passwords, making it reliable even in low connectivity environments.
  3. It’s easy to implement and widely supported by various authentication apps and services, enhancing its usability across different platforms and devices.
  4. TOTP is based on open standards, promoting interoperability and transparency in its implementation, which contributes to its trustworthiness in the security community.

Example code for TOTP implementation using Laravel

If you found this article helpful consider supporting me by Buying me a Coffee Buy Me A Coffee


Next Post
Understanding and Creating Sitemaps for SEO