Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Configurable Hashing to OTP Generation and Validation #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions src/Otp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Carbon\Carbon;
use Exception;
use Ichtrojan\Otp\Models\Otp as Model;
use Illuminate\Support\Facades\Hash;

class Otp
{
Expand All @@ -31,9 +32,11 @@ public function generate(string $identifier, string $type, int $length = 4, int
throw new Exception("{$type} is not a supported type");
}

$useHashing = config('otp.use_hashing', false);

Model::create([
'identifier' => $identifier,
'token' => $token,
'token' => $useHashing ? Hash::make($token) : $token,
'validity' => $validity
]);

Expand All @@ -51,42 +54,53 @@ public function generate(string $identifier, string $type, int $length = 4, int
*/
public function validate(string $identifier, string $token): object
{
$otp = Model::where('identifier', $identifier)->where('token', $token)->first();
$otp = Model::where('identifier', $identifier)
->where('valid', true)
->latest('created_at')
->first();

if ($otp instanceof Model) {
if ($otp->valid) {
$now = Carbon::now();
$validity = $otp->created_at->addMinutes($otp->validity);

$otp->update(['valid' => false]);

if (strtotime($validity) < strtotime($now)) {
$otp->update(['valid' => false]);
return (object)[
'status' => false,
'message' => 'OTP Expired'
];
}

$useHashing = config('otp.use_hashing', false);
$isValid = $useHashing ? Hash::check($token, $otp->token) : $token === $otp->token;

if (!$isValid) {
return (object)[
'status' => false,
'message' => 'OTP is not valid'
];
}

$otp->update(['valid' => false]);

return (object)[
'status' => true,
'message' => 'OTP is valid'
];
}

$otp->update(['valid' => false]);
}

return (object)[
'status' => false,
'message' => 'OTP is not valid'
];
} else {
return (object)[
'status' => false,
'message' => 'OTP does not exist'
];
}

return (object)[
'status' => false,
'message' => 'OTP not found'
];
}

/**
Expand Down
9 changes: 8 additions & 1 deletion src/OtpServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class OtpServiceProvider extends ServiceProvider
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/config/otp.php', 'otp'
);
}

/**
Expand All @@ -22,8 +25,12 @@ public function register()
*/
public function boot()
{
$this->loadMigrationsFrom(__DIR__ . '/database/migrations');
$this->publishes([
__DIR__.'/config/otp.php' => config_path('otp.php'),
], 'otp-config');

$this->loadMigrationsFrom(__DIR__ . '/database/migrations');

$this->commands([
\Ichtrojan\Otp\Commands\CleanOtps::class,
]);
Expand Down
16 changes: 16 additions & 0 deletions src/config/otp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php


return [

/*
|--------------------------------------------------------------------------
| Use Hashing
|--------------------------------------------------------------------------
|
| This option controls whether OTPs should be hashed before storage.
| When set to true, OTPs will be hashed using Laravel's Hash facade.
|
*/
'use_hashing' => env('OTP_USE_HASHING', false),
];