amreljako/laravel-otp

Advanced, secure OTP (Email/SMS/WhatsApp) package for Laravel with hashed codes, TTL, rate limiting, one-time use, events, and drivers.

Maintainers

Package info

github.com/amreljako/laravel-otp

pkg:composer/amreljako/laravel-otp

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 14

Open Issues: 0

v0.1.0 2025-08-15 15:44 UTC

This package is auto-updated.

Last update: 2026-03-15 19:02:00 UTC


README

Advanced, secure OTP (Email/SMS/WhatsApp) for Laravel. Hashed codes, TTL, one-time use, rate limiting, drivers, rules, and clean API.

License

Features

  • Random OTP with configurable digits and TTL
  • Store only hashed codes
  • One-time use via consumed_at
  • Rate limit sending/verification
  • Attempts counter & lockout pattern
  • HMAC signature binding (purpose + destination)
  • Channels (Mail by default) + extend SMS/WhatsApp
  • Migration, Config publish, Facade, Rule
  • Framework-agnostic tests via Testbench + Pest

Install

composer require amreljako/laravel-otp
php artisan vendor:publish --tag=otp-config
php artisan migrate

Quick Start

use Otp;

Otp::send([
  'destination' => 'user@example.com',
  'purpose' => 'login',
  'channel' => 'mail', // or your sms/whatsapp driver
  // 'ttl' => 300, 'digits' => 6, 'max_attempts' => 5,
]);

Verify:

$ok = Otp::verify('user@example.com', 'login', $request->code);
if ($ok) { /* grant access */ } else { /* error */ }

Validation Rule

$request->validate([
  'email' => ['required','email'],
  'code'  => ['required', new \Amreljako\Otp\Rules\ValidOtp('email','login')],
]);

Create your own SMS/WhatsApp channel

class MySmsChannel implements \Amreljako\Otp\Contracts\OtpChannel {
  public function send(\Amreljako\Otp\DTO\OtpPayload $p): bool {
    // call provider API using $p->destination and $p->message()
    return true;
  }
}

Then register in config/otp.php:

'channels' => [
  'mail' => \Amreljako\Otp\Channels\MailChannel::class,
  'sms'  => \App\Otp\Channels\MySmsChannel::class,
],

Security

  • No plaintext codes stored
  • Expires with expires_at
  • One-time consumption
  • Throttle abuse with RateLimiter
  • Optional HMAC signature

See SECURITY.md to report vulnerabilities.

Testing

composer install
vendor/bin/pest

License

MIT © 2025 Amr Elsayed