https://github.com/azozzalfiras/2fa-google
This PHP package offers an easy implementation of Google 2FA using the TOTP algorithm, allowing you to generate and verify OTPs, as well as create QR codes for authentication setup.
https://github.com/azozzalfiras/2fa-google
2fa 2fa-generator 2fa-php 2fa-security php
Last synced: 26 days ago
JSON representation
This PHP package offers an easy implementation of Google 2FA using the TOTP algorithm, allowing you to generate and verify OTPs, as well as create QR codes for authentication setup.
- Host: GitHub
- URL: https://github.com/azozzalfiras/2fa-google
- Owner: AzozzALFiras
- Created: 2025-03-09T12:15:09.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-03-09T12:17:41.000Z (over 1 year ago)
- Last Synced: 2025-12-19T12:28:24.193Z (6 months ago)
- Topics: 2fa, 2fa-generator, 2fa-php, 2fa-security, php
- Language: PHP
- Homepage:
- Size: 4.88 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# AzozzAlfiras 2FA Google - PHP Package
This PHP package provides a simple implementation for Google 2-Factor Authentication (2FA) using the Time-based One-Time Password (TOTP) algorithm. It includes functionalities for generating OTPs, verifying OTPs, and creating QR codes for authentication setup.
## Installation
You can install this package via Composer:
```bash
composer require azozzalfiras/2fa-google
```
## Usage
### Setup and Configuration
To use this package, instantiate the `TwoFactorAuth` class with the secret key, user, and issuer. Optionally, you can customize the user and issuer values.
```php
getSecretKey(); // This will get the generated secret key
// Initialize the object with secretKey, email, and issuer
$twoFactorAuth->initialize($generatedSecretKey, 'user@example.com', 'MyAppIssuer');
// Generate the QR code URL
$qrCodeUrl = $twoFactorAuth->generateQrCodeUrl();
echo "QR Code URL: " . $qrCodeUrl . "\n";
```
### Step-by-step Usage
To use the `Otp` class from the **AzozzAlfiras\TwoFactorAuth** namespace, follow these steps:
1. **Instantiate the Otp Class**: First, you need to create an instance of the `Otp` class. This class requires the secret key (Base32 encoded) as a parameter.
2. **Generate OTP**: Use the `generateOtp()` method to generate a one-time password (OTP) based on the provided secret key.
3. **Verify OTP**: Use the `verifyOtp()` method to verify if the entered OTP matches the generated OTP.
4. **Generate QR Code URL**: Use the `generateQrCodeUrl()` method to generate the URL needed to generate the QR code for Google Authenticator.
## Features
- **Instantiate the Otp Class**: Create an instance of the `Otp` class with the secret key.
- **Generate OTP**: Generate a one-time password using the `generateOtp()` method.
- **Verify OTP**: Verify the OTP using the `verifyOtp()` method.
- **Generate QR Code URL**: Generate the URL for the QR code using the `generateQrCodeUrl()` method.
## Example Usage
```php
generateOtp();
echo "Generated OTP: " . $generatedOtp . "\n";
// Verify the OTP (for example, entered by the user)
$enteredOtp = '123456'; // Replace with the OTP entered by the user
$isVerified = $otp->verifyOtp($enteredOtp);
if ($isVerified) {
echo "OTP is valid!\n";
} else {
echo "Invalid OTP!\n";
}
?>
```
## Explanation
- **Instantiate Otp**: The `Otp` class requires a Base32 encoded secret key. This secret key can be generated using the `TwoFactorAuth` class and is used to generate and verify OTPs.
- **Generate OTP**: The `generateOtp()` method calculates a one-time password based on the current time and the secret key.
- **Verify OTP**: The `verifyOtp()` method compares the OTP entered by the user with the generated OTP to confirm its validity.
- **Generate QR Code URL**: The `generateQrCodeUrl()` method provides a URL that can be used to generate a QR code for the user to scan using Google Authenticator or another authenticator app.
## License
This package is open-source and available under the MIT License.
### Key Highlights
- Sections like **Installation**, **Usage**, **Features**, and **Example Usage** are clearly defined with headers.
- Code examples are surrounded by triple backticks (```), making it easy to copy and understand.
- The **Step-by-step Usage** section explains each part of the process clearly.
- The **Features** section outlines the key functionalities of the package.
This `README.md` should provide everything needed for a user to understand, install, and use the package effectively.