danielrobert/otp-generator

A Laravel Package for Generating and Validating Otp using database

v1.07 2023-03-19 13:55 UTC

This package is auto-updated.

Last update: 2024-09-05 16:47:52 UTC


README

Latest Version on Packagist Quality Score Code Quality Github Workflow Status

Licence

Installation

You may use the Composer package manager to install Otp Generator into your Laravel project:

composer require danielrobert/otp-generator

After installing the otp-generator, publish its configs using the otp:install Artisan command. After installing Otp Generator, you should also run the migrate command in order to create the tables needed to store OTPs:

php artisan otp:install
 
php artisan migrate

Configuration

After publishing Otp Generator's configs, its primary configuration file will be located at config/otp-generator.php. This configuration file allows you to configure your otps. Each configuration option includes a description of its purpose, so be sure to thoroughly explore this file.

Data Pruning

Data Pruning Without pruning, the otps table can accumulate records very quickly. To mitigate this, you can schedule the otp:prune Artisan command to run daily:

$schedule->command('otp:prune')->daily();

By default, all expired entries or entries older than 30 minutes as configured in the config/otp-generator.php will be pruned. You may use the minutes option when calling the command to determine how long to retain data. For example, the following command will delete all records created over 60 minutes ago:

$schedule->command('otp:prune --minutes=60')->daily();

Usage

use DanielRobert\Otp\Otp;
.
.
$otp =  Otp::generate($identifier);
.
$verify = Otp::validate($identifier, $otp->token);

// example response
{
  "status": true
  "message": "Otp is valid"
}

// to get an expiredAt time
$expires = Otp::expiredAt($identifier);

// example response 
+"status": true
+"expired_at": Illuminate\Support\Carbon @1611895244^ {
  ....
  #dumpLocale: null
  date: 2021-01-29 04:40:44.0 UTC (+00:00)
}

Advance Usage

In addition to configuring otps from the otp-generator.php config file you can also configure it directly

use DanielRobert\Otp\Otp;
.
.
$otp =  Otp::setValidity(30)  // otp validity time in mins
      ->setLength(4)  // Lenght of the generated otp
      ->setMaximumOtpsAllowed(10) // Number of times allowed to regenerate otps
      ->setOnlyDigits(false)  // generated otp contains mixed characters ex:ad2312
      ->setUseSameToken(true) // if you re-generate Otp, you will get same token
      ->generate($identifier);
.
$verify = Otp::setAllowedAttempts(10) // number of times they can allow to attempt with wrong token
    ->validate($identifier, $otp->token);

Testing

composer test

Changelog

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

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.