blamodex / laravel-otp
A simple, Laravel-ready one-time password (OTP) package for rapid prototyping and lightweight use cases.
Installs: 2
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 0
Forks: 1
Open Issues: 0
pkg:composer/blamodex/laravel-otp
Requires
- php: ^8.2
- illuminate/config: ^12.0
- illuminate/database: ^12.0
- illuminate/queue: ^12.0
- illuminate/support: ^12.0
Requires (Dev)
- blamodex/laravel-ai-agents: ^1.0
- larastan/larastan: ^3.0
- orchestra/testbench: ^10.0
- phpunit/php-code-coverage: *
- phpunit/phpunit: ^11.5.3
- slevomat/coding-standard: ^8.0@dev
- squizlabs/php_codesniffer: 4.x-dev
README
A lightweight Laravel package to add one-time password (OTP) capabilities to any Eloquent model using polymorphic relationships.
Table of Contents
๐ Features
- 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
๐ฆ Installation
Install the package with Composer:
composer require blamodex/laravel-otp
Publish the config file:
php artisan vendor:publish --tag=blamodex-otp-config
Run the migrations:
php artisan migrate
โ๏ธ Configuration
Configuration 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 ];
๐งฉ Usage
1. Implement the interface and use the trait
use Blamodex\Otp\Traits\OneTimePasswordable; use Blamodex\Otp\Contracts\OneTimePasswordableInterface; class User extends Model implements OneTimePasswordableInterface { use OneTimePasswordable; }
2. Generate an OTP
$user = User::find(1); $otp = $user->generateOtp(); // returns raw password
3. Verify an OTP
if ($user->verifyOtp('123456')) { // Success } else { // Failure }
๐งช Testing
This package uses Orchestra Testbench and PHPUnit.
Run tests:
composer test
Check code style:
composer lint
Check code style and fix:
composer lint:fix
Check coverage (with Xdebug):
composer test:coverage
๐ Project Structure
src/
โโโ 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
๐ค Contributing
We welcome contributions! Please see CONTRIBUTING.md for details.
๐ Changelog
Please see CHANGELOG.md for recent changes.
๐ License
MIT ยฉ Blamodex
For more information, see the LICENSE file.