Laravel implementation of Rails's active_model_otp package.
Simple OTP generation.
$code = $user->otp(); // => "324650"
$user->verify($code); // => true
$user->verify($code); // => false
You can install the package via composer:
composer require graxmonzo/laravel-one-time-password
Your Eloquent models should use the GraxMonzo\OneTimePassWord\HasOTP
trait and the GraxMonzo\OneTimePassWord\OTPOptions
class.
The trait contains an abstract method otpOptions()
that you must implement yourself.
Your models' migrations should have a fields to save the OTP secret and counter to.
Here's an example of how to implement the trait:
namespace App;
use GraxMonzo\OneTimePassword\HasOTP;
use GraxMonzo\OneTimePassword\OTPOptions;
use Illuminate\Database\Eloquent\Model;
class YourEloquentModel extends Model
{
use HasOTP;
/**
* Get the options for generating OTP.
*/
public function otpOptions() : OTPOptions
{
return OTPOptions::create()
->saveToFields('otp_secret', 'otp_counter')
->digitsCount(6); // optional
}
}
With its migration:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateYourEloquentModelTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('your_eloquent_models', function (Blueprint $table) {
$table->increments('id');
$table->string('otp_secret');
$table->integer('otp_counter');
$table->timestamps();
});
}
}
$model = new YourEloquentModel();
$model->otp(); # => "186522"
$code = $model->otp(); # => "850738"
$model->verify($code); # => true
$model->verify($code); # => false
$model->verify($code); # => true
$model->otp_counter -= 1;
$model->verify($code); # => true
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.