A lightweight Laravel package to add one-time password (OTP) capabilities to any Eloquent model using polymorphic relationships.
- Attach OTP functionality to any model using a trait
- Polymorphic support for multiple model types
- Configurable alphabet, length, expiry, and hash algorithm
- Secure hashing via
password_hashandpassword_verify - Auto-expiration and one-time use enforcement
- Clean architecture: trait, model, service, generator
Install the package with Composer:
composer require blamodex/laravel-otpPublish the config file:
php artisan vendor:publish --tag=blamodex-otp-configRun the migrations:
php artisan migrateConfiguration lives in config/otp.php:
return [
/* PASSWORD ALGORITHM, SEE https://www.php.net/manual/en/function.password-hash.php FOR MORE INFO */
'algorithm' => PASSWORD_BCRYPT,
/* PASSWORD ALPHABET */
//NUMBERS ONLY ALPHABET
'alphabet' => '0123456789',
//"WORD SAFE" ALPHABET
//'alphabet' => '256789BCDFGHJKMNPQRSTVXW',
//ALPHANUM ALPHABET
//'alphabet' => '0123456789ABCDEFGHIJKLMNOPQRSTUV';
/* PASSWORD LENGTH */
'length' => 6,
/* PASSWORD EXPIRY */
'expiry' => 600
];use Blamodex\Otp\Traits\OneTimePasswordable;
use Blamodex\Otp\Contracts\OneTimePasswordableInterface;
class User extends Model implements OneTimePasswordableInterface
{
use OneTimePasswordable;
}$user = User::find(1);
$otp = $user->generateOtp(); // returns raw passwordif ($user->verifyOtp('123456')) {
// Success
} else {
// Failure
}This package uses Orchestra Testbench and PHPUnit.
Run tests:
composer testCheck code style:
composer lintCheck code style and fix:
composer lint:fixCheck coverage (with Xdebug):
composer test:coveragesrc/
├── Models/
│ └── OneTimePassword.php
├── Data/
│ └── OtpData.php
├── Services/
│ ├── OtpGenerator.php
│ └── OtpService.php
├── Traits/
│ └── OneTimePasswordable.php
├── Contracts/
│ └── OneTimePasswordableInterface.php
├── config/
│ └── otp.php
└── database/
└── migrations/
└── 202x_xx_xx_create_one_time_passwords_table.php
tests/
├── Unit/
├── Fixtures/
└── TestCase.php
We welcome contributions! Please see CONTRIBUTING.md for details.
Please see CHANGELOG.md for recent changes.
MIT © Blamodex
For more information, see the LICENSE file.