The primary idea was to implement a Time-Based One-Time Password (TOTP) generator based on the RFC 6238 standard. So, here's a Python-based implementation of a Time-based One-Time Password (TOTP) authenticator similar to Google Authenticator. This app generates QR codes that can be scanned into authenticator apps like Google Authenticator to verify users via 6-digit codes.
- Project Overview
- Features
- Prerequisites
- Installation
- Usage
- Project Structure
- Flow of Code
- Contributing
This project demonstrates how to generate TOTP (Time-based One-Time Password) codes and display them in the form of a QR code, allowing users to scan it with authenticator apps (e.g., Google Authenticator). It also includes a basic token verification endpoint.
- Generates a QR code that users can scan with an authenticator app.
- Verifies a 6-digit TOTP token generated in the authenticator app.
- Built using Python, Flask, and libraries like
pyotpandqrcode.
Make sure you have the following installed:
- Python 3.7+
- pip (Python package manager)
Click to see the steps of installation
git clone https://github.com/your-username/google-authenticator-clone.git
cd google-authenticator-cloneTo keep dependencies isolated, create a virtual environment.
python3 -m venv venv
On Linux/Mac:
source venv/bin/activate
On Windows:
.\venv\Scripts\activate
Install the required Python libraries:
pip install pyotp qrcode[pil] flask
Running the App
After setting up the environment and installing dependencies, you can run the app.
python app.py
Open the App in Your Browser
Visit http://127.0.0.1:5000 in your browser. This page will display:
A QR code that can be scanned with Google Authenticator.
An input field where you can enter the TOTP token generated by the authenticator app.
Verify the Token
Scan the QR code in Google Authenticator.
Enter the 6-digit token displayed in the app into the input field on the page.
Submit the form to verify if the token is correct.
Project Structure
View Project Structure
google-authenticator-clone/
├── venv/ # Virtual environment (not included in Git)
├── app.py # Main application code
├── static/
│ └── qrcode.png # QR code generated for each TOTP session
├── templates/
│ └── index.html # HTML template for rendering the page
├── README.md # Project documentation
└── requirements.txt # List of dependencies
Flow of Code
View Flow of Code
Generate Secret Key: A unique TOTP secret key is generated using pyotp.random_base32() in app.py. This key is used for creating the TOTP code.
Generate QR Code:
The TOTP URI is generated with pyotp to make it compatible with Google Authenticator.
A QR code is created from this URI using qrcode.
The QR code is saved in the static folder as qrcode.png.
Render Template:
The index.html template displays the QR code and an input field for the TOTP token.
Verify Token:
When the user submits the token, the app verifies it against the TOTP instance.
If the token is correct, a success message is displayed. Otherwise, an error message appears.
Code Diagram
View Code Diagram
+--------------------+
| app.py |
|--------------------|
| SECRET Key |
| Generate QR |
| Render Template |
| Verify Token |
+--------------------+
|
v
+--------------------+
| Templates |
|--------------------|
| index.html |
| Displays QR Code |
| Token Input |
+--------------------+
Contributions are welcome! Please open an issue or submit a pull request for any changes, additions, or improvements. License
This project is licensed under the MIT License. See the LICENSE file for details. Additional Notes
If you encounter any issues, please refer to the following:
PEP 668 - Python environment management