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

v1.0.0 2025-11-29 12:44 UTC

This package is auto-updated.

Last update: 2025-11-29 12:50:11 UTC


README

Latest Version on Packagist GitHub Tests Action Status Total Downloads License Laravel PHP

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_hash and password_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.

๐Ÿ”— Links