Skip to content
Merged
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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ OTP()->useProvider('users')
*/
OTP()->onlyConfirmToken()
->validate('+98900000000', 'token_123');

/*
|--------------------------------------------------------------------------
| You may wish to use a custom indicator
|--------------------------------------------------------------------------
*/
OTP()->indicator('custom_indicator')
->send('+98900000000');

OTP()->indicator('custom_indicator')
->onlyConfirmToken()
->validate('+98900000000', 'token_123');
```
## Installation

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddIndicatorToOTPTokensTable extends Migration
{
private string $tokenTable;

private string $defaultIndicator;

public function __construct()
{
$this->tokenTable = config('otp.token_table', 'otp_tokens');
$this->defaultIndicator = config('otp.prefix', 'otp_tokens');
}

public function up(): void
{
if (Schema::hasTable($this->tokenTable)) {
Schema::table($this->tokenTable, function (Blueprint $table): void {
$table->string('indicator')->default($this->defaultIndicator);
});
}
}

public function down(): void
{
if (Schema::hasTable($this->tokenTable)) {
Schema::table($this->tokenTable, static function (Blueprint $table): void {
$table->dropColumn('indicator');
});
}
}
}
12 changes: 6 additions & 6 deletions src/Contracts/AbstractTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public function __construct(protected int $expires, protected int $tokenLength)
{
}

public function create(OTPNotifiable $user): string
public function create(OTPNotifiable $user, string $indicator): string
{
$mobile = $user->getMobileForOTPNotification();

$this->deleteExisting($user);
$this->deleteExisting($user, $indicator);

$token = $this->createNewToken();

$this->save($mobile, $token);
$this->save($mobile, $indicator, $token);

return $token;
}
Expand All @@ -35,13 +35,13 @@ protected function tokenExpired(string $expiresAt): bool
return Carbon::parse($expiresAt)->isPast();
}

protected function getPayload(string $mobile, string $token): array
protected function getPayload(string $mobile, string $indicator, string $token): array
{
return ['mobile' => $mobile, 'token' => $token, 'sent_at' => now()->toDateTimeString()];
return ['mobile' => $mobile, 'indicator' => $indicator, 'token' => $token, 'sent_at' => now()->toDateTimeString()];
}

/**
* Insert into token storage.
*/
abstract protected function save(string $mobile, string $token): bool;
abstract protected function save(string $mobile, string $indicator, string $token): bool;
}
8 changes: 4 additions & 4 deletions src/Contracts/TokenRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ interface TokenRepositoryInterface
/**
* Create a new token record.
*/
public function create(OTPNotifiable $user): string;
public function create(OTPNotifiable $user, string $indicator): string;

/**
* Determine if a token record exists and is valid.
*/
public function exists(string $mobile): bool;
public function exists(string $mobile, string $indicator): bool;

/**
* Determine if the given token matches the provided one.
*/
public function isTokenMatching(OTPNotifiable $user, string $token): bool;
public function isTokenMatching(OTPNotifiable $user, string $indicator, string $token): bool;

/**
* Delete all existing tokens from the storage.
*/
public function deleteExisting(OTPNotifiable $user): bool;
public function deleteExisting(OTPNotifiable $user, string $indicator): bool;
}
25 changes: 20 additions & 5 deletions src/OTPBroker.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class OTPBroker
{
private array $channel;

private string $indicator;

private ?string $token = null;

private bool $onlyConfirm = false;
Expand All @@ -27,6 +29,7 @@ public function __construct(
private UserProviderResolver $providerResolver
) {
$this->channel = $this->getDefaultChannel();
$this->indicator = $this->getDefaultIndicator();
$this->userRepository = $this->resolveUserRepository();
}

Expand All @@ -42,7 +45,7 @@ public function send(string $mobile, bool $userExists = false): OTPNotifiable

$notifiable = $user ?? $this->makeNotifiable($mobile);

$this->token = $this->tokenRepository->create($notifiable);
$this->token = $this->tokenRepository->create($notifiable, $this->indicator);

$notifiable->sendOTPNotification(
$this->token,
Expand Down Expand Up @@ -77,6 +80,13 @@ public function onlyConfirmToken(bool $confirm = true): static
return $this;
}

public function indicator(string $indicator): static
{
$this->indicator = $indicator;

return $this;
}

public function getToken(): ?string
{
return $this->token;
Expand All @@ -101,7 +111,7 @@ public function channel($channel = ['']): static

public function revoke(OTPNotifiable $user): bool
{
return $this->tokenRepository->deleteExisting($user);
return $this->tokenRepository->deleteExisting($user, $this->indicator);
}

/**
Expand All @@ -126,7 +136,7 @@ private function findOrCreateUser(string $mobile): OTPNotifiable

private function findUserByMobile(string $mobile): ?OTPNotifiable
{
return $this->userRepository->findByMobile($mobile);
return $this->userRepository->findByMobile($mobile, $this->indicator);
}

private function getDefaultChannel(): array
Expand All @@ -138,12 +148,12 @@ private function getDefaultChannel(): array

public function verifyToken(OTPNotifiable $user, string $token): bool
{
return $this->tokenRepository->isTokenMatching($user, $token);
return $this->tokenRepository->isTokenMatching($user, $this->indicator, $token);
}

private function tokenExists(string $mobile): bool
{
return $this->tokenRepository->exists($mobile);
return $this->tokenRepository->exists($mobile, $this->indicator);
}

private function makeNotifiable(string $mobile): OTPNotifiable
Expand All @@ -152,4 +162,9 @@ private function makeNotifiable(string $mobile): OTPNotifiable

return $this->userRepository->getModel()->make([$mobileColumn => $mobile]);
}

private function getDefaultIndicator()
{
return config('otp.prefix');
}
}
32 changes: 19 additions & 13 deletions src/Token/CacheTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,53 @@
use Fouladgar\OTP\Contracts\AbstractTokenRepository;
use Fouladgar\OTP\Contracts\OTPNotifiable;
use Illuminate\Contracts\Cache\Repository as Cache;
use Psr\SimpleCache\InvalidArgumentException;

class CacheTokenRepository extends AbstractTokenRepository
{
public function __construct(
protected Cache $cache,
protected int $expires,
protected int $tokenLength,
protected string $prefix
) {
parent::__construct($expires, $tokenLength);
}

public function deleteExisting(OTPNotifiable $user): bool
public function deleteExisting(OTPNotifiable $user, string $indicator): bool
{
return $this->cache->forget($this->getSignatureKey($user->getMobileForOTPNotification()));
return $this->cache->forget($this->getSignatureKey($user->getMobileForOTPNotification(), $indicator));
}

public function exists(string $mobile): bool
/**
* @throws InvalidArgumentException
*/
public function exists(string $mobile, string $indicator): bool
{
return $this->cache->has($this->getSignatureKey($mobile));
return $this->cache->has($this->getSignatureKey($mobile, $indicator));
}

public function isTokenMatching(OTPNotifiable $user, string $token): bool
/**
* @throws InvalidArgumentException
*/
public function isTokenMatching(OTPNotifiable $user, string $indicator, string $token): bool
{
$exist = $this->exists($user->getMobileForOTPNotification());
$signature = $this->getSignatureKey($user->getMobileForOTPNotification());
$exist = $this->exists($user->getMobileForOTPNotification(), $indicator);
$signature = $this->getSignatureKey($user->getMobileForOTPNotification(), $indicator);

return $exist && $this->cache->get($signature)['token'] === $token;
}

protected function save(string $mobile, string $token): bool
protected function save(string $mobile, string $indicator, string $token): bool
{
return $this->cache->add(
$this->getSignatureKey($mobile),
$this->getPayload($mobile, $token),
$this->getSignatureKey($mobile, $indicator),
$this->getPayload($mobile, $indicator, $token),
now()->addMinutes($this->expires)
);
}

protected function getSignatureKey($mobile): string
protected function getSignatureKey($mobile, string $indicator): string
{
return $this->prefix.$mobile;
return sprintf('%s%s', $indicator, $mobile);
}
}
31 changes: 18 additions & 13 deletions src/Token/DatabaseTokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,19 @@ class DatabaseTokenRepository extends AbstractTokenRepository
{
public function __construct(
protected ConnectionInterface $connection,
protected int $expires,
protected int $tokenLength,
protected string $table
protected int $expires,
protected int $tokenLength,
protected string $table
) {
parent::__construct($expires, $tokenLength);
}

public function deleteExisting(OTPNotifiable $user): bool
public function deleteExisting(OTPNotifiable $user, string $indicator): bool
{
return (bool) optional($this->getTable()->where('mobile', $user->getMobileForOTPNotification()))->delete();
return (bool)optional($this->getTable()->where([
'mobile' => $user->getMobileForOTPNotification(),
'indicator' => $indicator,
]))->delete();
}

protected function getLatestRecord(array $filters): ?array
Expand All @@ -32,21 +35,22 @@ protected function getLatestRecord(array $filters): ?array
->latest()
->first();

return $record ? (array) $record : null;
return $record ? (array)$record : null;
}

public function exists(string $mobile): bool
public function exists(string $mobile, string $indicator): bool
{
$record = $this->getLatestRecord(['mobile' => $mobile]);
$record = $this->getLatestRecord(['mobile' => $mobile, 'indicator' => $indicator]);

return $record && ! $this->tokenExpired($record['expires_at']);
}

public function isTokenMatching(OTPNotifiable $user, string $token): bool
public function isTokenMatching(OTPNotifiable $user, string $indicator, string $token): bool
{
$record = $this->getLatestRecord([
'mobile' => $user->getMobileForOTPNotification(),
'token' => $token,
'indicator' => $indicator,
]);

return $record && ! $this->tokenExpired($record['expires_at']);
Expand All @@ -57,13 +61,14 @@ protected function getTable(): Builder
return $this->connection->table($this->table);
}

protected function save(string $mobile, string $token): bool
protected function save(string $mobile, string $indicator, string $token): bool
{
return $this->getTable()->insert($this->getPayload($mobile, $token));
return $this->getTable()->insert($this->getPayload($mobile, $indicator, $token));
}

protected function getPayload(string $mobile, string $token): array
protected function getPayload(string $mobile, string $indicator, string $token): array
{
return parent::getPayload($mobile, $token) + ['expires_at' => now()->addMinutes($this->expires)];
return parent::getPayload($mobile, $indicator, $token) +
['expires_at' => now()->addMinutes($this->expires), 'indicator' => $indicator];
}
}
1 change: 0 additions & 1 deletion src/Token/TokenRepositoryManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ protected function createCacheDriver(): TokenRepositoryInterface
$this->container->make(CacheRepository::class),
$this->config->get('otp.token_lifetime', 5),
$this->config->get('otp.token_length', 5),
$this->config->get('otp.prefix'),
);
}

Expand Down
Loading