Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @MincDev
20 changes: 20 additions & 0 deletions .github/SECURITY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Security Policy

## Reporting a Vulnerability

If you discover a security vulnerability in this project, please do **not** open a public GitHub issue.

Instead, report it privately by emailing:

**christopher@mincdevelopment.co.za**

Please include:

- A description of the vulnerability
- Steps to reproduce it
- The potential impact
- Any suggested fix (if available)

I will acknowledge receipt as soon as possible and work to resolve verified issues promptly.

Thank you for helping keep this project secure.
26 changes: 26 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: 2

updates:
- package-ecosystem: "composer"
directory: "/"
schedule:
interval: "weekly"

open-pull-requests-limit: 2

commit-message:
prefix: "deps"

labels:
- "dependencies"

- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"

commit-message:
prefix: "ci"

labels:
- "github-actions"
35 changes: 35 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Run Tests

on:
push:
branches:
- master
pull_request:

jobs:
phpunit:
runs-on: ubuntu-latest

strategy:
matrix:
php: ['8.2', '8.3', '8.4']

name: PHP ${{ matrix.php }}

steps:
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
coverage: none

- name: Validate composer.json
run: composer validate --strict

- name: Install dependencies
run: composer install --prefer-dist --no-progress

- name: Run PHPUnit
run: vendor/bin/phpunit
29 changes: 28 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
# Composer
/vendor/
composer.lock

# Lock file (libraries typically don't commit this)
composer.lock

# PHPUnit
.phpunit.cache/

# IDEs
.idea/
.vscode/

# macOS
.DS_Store

# Windows
Thumbs.db

# Coverage reports
coverage/
coverage.xml

# PHPUnit reports
testdox.html
testdox.txt

# Xdebug profiling
cachegrind.out.*
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2021 MincDev
Copyright (c) 2021-2026 MincDev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
90 changes: 66 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,86 @@
# php-otpauth
# PHP-OtpAuth

A library for generating a 2 factor authentication QR code to use with Google Authenticator, Authy, etc.

### Secure QRCode Creation
[![Tests](https://github.com/mincdev/php-otpauth/actions/workflows/run-tests.yml/badge.svg)](https://github.com/mincdev/php-otpauth/actions/workflows/run-tests.yml)

This library has secure QRCode creation because of the fact that the QRCode is generated locally on your server. This means that the user's secret is not passed to any third party or remote server in order to generate a code. This was inspired by the stack overflow answer by **kravietz** as seen [here](https://stackoverflow.com/a/56737468/3948544)
A lightweight PHP library for generating and validating **Time-based One-Time Passwords (TOTP)** compatible with Google Authenticator, Microsoft Authenticator, Authy, 1Password, Bitwarden, and other RFC 6238 compatible authenticator applications.

### Installation (Composer)
## Features

```
- 🔒 Generate cryptographically secure TOTP secrets
- 📱 Generate QR codes for easy authenticator app provisioning
- ✅ Validate one-time passwords
- 🧪 Verified against the official **RFC 6238** test vectors
- 🖥️ QR codes are generated locally on your server — no secrets are sent to third-party services
- 🚀 Lightweight with minimal dependencies

## Installation

Install the package using Composer:

```bash
composer require mincdev/php-otpauth
```

### Dependencies
## Generating a Secret and QR Code

This library requires the **tc-lib-barcode** library found at https://github.com/tecnickcom/tc-lib-barcode.
```php
use MincDev\OtpAuth\OtpAuthenticator;

**Note:** The tc-lib-barcode library is maintained and owned by a separate entity.
$otp = new OtpAuthenticator();

#### Generating a QR Code
// Store the secret securely for the user.
$secret = $otp->newSecret();

You can generate a QR code which can be scanned by Google Authenticator, Authy, etc. by using the below.
$qrCode = $otp->getQR(
'john.doe@example.com',
'My Awesome App',
$secret
);
```

```php
$otpAuth = new OtpAuthenticator();
The returned value is a Base64 data URI that can be used directly in an image tag:

```html
<img src="<?= $qrCode ?>" alt="Authenticator QR Code">
```

$userName = "MrDoe";
$appName = "My Awesome App";
## Validating a Code

```php
use MincDev\OtpAuth\OtpAuthenticator;

// Store this secret somewhere safe, as you'll need it to validate the pin later
$userSecret = $otpAuth->newSecret();
$otp = new OtpAuthenticator();

$qrBase64 = $otpAuth->getQR($userName, $appName, $userSecret);
$isValid = $otp->validate($secret, $userEnteredCode);
```

#### Validating a PIN
## Security

Once your user logs in, you can validate their pin by making use of the following:
This library generates QR codes **entirely on your server**. The user's secret is never transmitted to an external QR code generation service, ensuring that sensitive authentication data remains under your control.

```php
$otpAuth = new OtpAuthenticator();
$isValid = $otpAuth->validate($userSecret, $pinCode);
QR code generation is powered by `tc-lib-barcode`.

## Compatibility

This library implements **RFC 6238 (TOTP)** and works with applications such as:

- Google Authenticator
- Microsoft Authenticator
- Authy
- 1Password
- Bitwarden
- and other compatible authenticator apps.

## Testing

The TOTP implementation is verified against the official **RFC 6238** test vectors to ensure standards-compliant code generation.

Run the test suite with:

```bash
composer test
```

## License

Released under the MIT License.
26 changes: 21 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
{
"name": "mincdev/php-otpauth",
"description": "A library for genrating a 2 factor authentication QR code",
"description": "PHP library for Time-based One-Time Password (TOTP) authentication compatible with Google Authenticator, Microsoft Authenticator and Authy.",
"type": "library",
"license": "MIT",
"version": "1.0.1",
"minimum-stability": "stable",
"keywords": [
"totp",
"otp",
"2fa",
"two-factor",
"google-authenticator",
"authenticator",
"security"
],
"authors": [
{
"name": "Christopher Smit",
"email": "christopher@mincdevelopment.co.za"
}
],
"require": {
"tecnickcom/tc-lib-barcode": "^1.17"
"php": "^8.2 || ^8.3 || ^8.4",
"tecnickcom/tc-lib-barcode": "^2.11"
},
"require-dev": {
"phpunit/phpunit": "^11.0"
},
"autoload": {
"psr-4": {
"MincDev\\OtpAuth\\": "src/"
"MincDev\\OtpAuth\\": "src/",
"MincDev\\OtpAuth\\Tests\\": "tests/"
}
},
"scripts": {
"test": "phpunit"
}
}
}
Loading
Loading