Download the PHP package fouladgar/laravel-otp without Composer

On this page you can find all versions of the php package fouladgar/laravel-otp. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package laravel-otp

Laravel OTP (One-Time Password)

Latest Version on Packagist Test Status Code Style Status Total Downloads

Upgrading from v5? See the Upgrade Guide for a step-by-step migration walkthrough.

Introduction

Laravel OTP provides a secure, simple, and flexible way to generate, send, and validate One-Time Passwords. It supports multiple independent OTP flows through purpose-based tokens, ensuring that another cannot consume a token generated for one flow. OTP delivery is fully extensible and can use any notification channel, including SMS, email, or custom channels. The package is event-driven, providing lifecycle events that make it easy to integrate OTP flows with your application's business logic, monitoring, auditing, or external notification services.

Table of Contents

Basic Usage

Once the package is configured, sending and validating tokens is straightforward. The same API works whether the recipient is a mobile number, an email address, or another identifier supported by your notification channel.

Send a token:

Send to an email address (point otp.channel to mail):

Send through multiple channels for this call:

Validate a token (recipient must match the value used for send()):

Read the generated token after send():

Revoke a pending token:

send() and validate() return bool. On failure, the package throws Fouladgar\OTP\Exceptions\OTPException instead of returning false.

Every channel receives the same recipient value. Only combine channels that understand the same recipient format. For example, do not mix mail with SMS-only channels unless the recipient is an email address.

You can also inject Fouladgar\OTP\OTPBroker directly wherever you need it, such as in a controller or service class. The OTP() helper is a shortcut for app(OTPBroker::class) and returns the broker directly — useful when you need to chain calls without a static import.

Requirements

Installation

Install the package with Composer:

Thanks to Laravel's package auto-discovery, the service provider is registered automatically. No manual provider registration is required.

Configuration

Next, publish the configuration file. This creates config/otp.php, where you can choose the notification channel, token storage driver, lifetime, and token length.

Notification Channel

The package generates the token, builds a Fouladgar\OTP\Notifications\OTPNotification, and dispatches it through the channel configured in otp.channel. The default channel is otp_log, which writes tokens to Laravel's log — useful during development. Replace it with your real channel before going to production.

Write a standard Laravel notification channel and point otp.channel to it:

The recipient passed to send() is used as the routing target for every configured channel through Laravel's on-demand notifications. Built-in channels such as mail work too: OTP::channel('mail')->send('[email protected]').

Token Storage

The package stores generated tokens in either cache or database. The default driver is cache, which is usually enough for simple applications.

You can change the driver in config/otp.php:

Cache

The cache driver uses your application's configured Laravel cache store. If your app does not have a suitable cache store, use the database driver.

Database

The database driver stores tokens in a table created by the package migration.

Customize the table name if needed:

Then run the migrations:

Pruning Expired Tokens

The database driver never deletes an expired row on its own — a validated token is removed immediately, but a token nobody ever validated just sits there. Schedule the bundled otp:prune command to clean these up:

Running otp:prune while otp.token_storage is set to cache is a no-op, since cache entries already expire on their own via the store's TTL.

Token Lifetime

token_lifetime controls how long a pending token remains valid. While the current token has not expired, the package will not generate or send another token for the same recipient and purpose.

This prevents users from requesting several active codes for the same flow at the same time.

Token Length

token_length controls how many characters are generated for each token. The default value is 5, but you can increase or decrease it based on your application's verification requirements.

Token Generator

token_generator controls how a token's value is generated. By default, NumericAbstractTokenGenerator produces digits-only tokens, which works well for SMS delivery on a numeric keypad.

For channels where higher entropy per character is worth it — email, for example — swap in the bundled AlphanumericAbstractTokenGenerator, which generates uppercase letters and digits:

You can also provide your own class extending Fouladgar\OTP\Contracts\AbstractTokenGenerator:

Rate Limiting

validate() is rate-limited per recipient and purpose to slow down brute-force guessing. By default, at most 5 attempts are allowed within 60 seconds; exceeding that throws OTPException (reason TooManyAttempts) and fires the TooManyValidationAttempts event — regardless of whether the submitted token was actually correct. A successful validation clears the counter immediately.

When the limit is exceeded, validate() throws OTPException::whenTooManyAttempts() and dispatches Fouladgar\OTP\Events\TooManyValidationAttempts.

Set max_attempts to null or 0 to disable validation rate limiting:

Successful validation clears the attempt counter for that recipient/purpose, so the next send/validate cycle starts with a fresh allowance.

Validation Timebox

Failed validation calls are padded to a minimum duration to reduce timing differences between invalid, expired, and rate-limited attempts. Successful validations are not delayed.

Set the value to 0 to disable the validation timebox.

Purpose

Every token is generated for a recipient and a purpose. The purpose is a short scope tag that lets multiple OTP flows exist for the same recipient without colliding.

For example, a user may request a login token and then request a password reset token before validating the first one. With separate purposes, both tokens can exist independently for the same phone number.

Disabling Notifications

Use withNotify(false) when this package should generate the token but another part of your system should deliver it. This is useful when SMS or email delivery is handled by a separate notification service.

In this mode, send() only generates and stores the token. No notification channel is required, and no notification is dispatched.

To disable notifications globally for all calls, set with_notify to false in config/otp.php:

After calling send(), you can read the token directly with getToken(). In a decoupled setup, listening for TokenCreated is usually cleaner because a listener can publish the token to a queue or call your notification service.

withNotify(false) only affects send(). validate() and revoke() behave the same either way.

Handling Exceptions

send() and validate() throw Fouladgar\OTP\Exceptions\OTPException for expected OTP failures. Each exception includes a typed reason so your application can respond without parsing translated messages.

Available reasons are:

Customization

Notification Customization

You can customize the SMS and mail messages by registering closures in your App\Providers\AppServiceProvider::boot(). Each closure receives the recipient and the generated token.

Events

OTPBroker dispatches events throughout the token lifecycle. Listen for them with Laravel's normal event tools: Event::listen(), listener classes, or App\Providers\EventServiceProvider.

Events are useful for audit logs, security monitoring, and decoupled notification delivery. For example, you can listen for TokenCreated and publish the token to a queue or notification service.

Token Events

Fouladgar\OTP\Events\CreatingToken

Fouladgar\OTP\Events\TokenCreated

Fouladgar\OTP\Events\TokenCreationFailed

Fouladgar\OTP\Events\TokenValidated

Fouladgar\OTP\Events\TokenValidationFailed

Fouladgar\OTP\Events\TooManyValidationAttempts

Fouladgar\OTP\Events\TokenRevoked

Notification Events

Fouladgar\OTP\Events\SendingNotification

Fouladgar\OTP\Events\NotificationSent

TokenCreationFailed, TokenValidationFailed, and TooManyValidationAttempts are useful security signals for repeated OTP requests or brute-force attempts. Missing channel configuration is not emitted as an event; it is reported immediately through Fouladgar\OTP\Exceptions\OTPException.

Translations

Publish the translation file with:

The package ships with English and Persian translations. You can customize the published language files:

Testing

Faking a Token in Your Application's Tests

When testing a flow in your own application, such as an OTP login endpoint, you usually need a valid token without actually sending SMS or email.

Use OTP::fake() instead of reaching into the cache or database directly. It stores a real, valid token using your current configuration, including storage driver and purpose.

fake() skips the "already sent" check and does not dispatch events. It is only a test setup helper, not part of the normal send()/validate() flow.

Running Tests

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security-related issues, please email [email protected] instead of using the issue tracker.

License

Laravel-OTP is released under the MIT License. See the bundled LICENSE file for details.

Built with ❤️ for you.


All versions of laravel-otp with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
illuminate/database Version ^10.0|^11.0|^12.0|^13.0
illuminate/support Version ^10.0|^11.0|^12.0|^13.0
illuminate/notifications Version ^10.0|^11.0|^12.0|^13.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package fouladgar/laravel-otp contains the following files

Loading the files please wait ...