From d90dc8f510d4a3c5a393e87b7bdc26b31c86df72 Mon Sep 17 00:00:00 2001 From: Aboozar Ghaffari Date: Thu, 16 Jan 2025 05:42:26 +0330 Subject: [PATCH] fixed some code styles --- config/otp.php | 21 +++--- resources/lang/en/messages.php | 8 +-- src/Otp.php | 124 ++++++++++++++++++++++----------- src/OtpFacade.php | 4 +- src/OtpServiceProvider.php | 8 +-- src/Rules/OtpValidate.php | 12 ++-- 6 files changed, 111 insertions(+), 66 deletions(-) diff --git a/config/otp.php b/config/otp.php index 0b1c7c2..f42fa9c 100644 --- a/config/otp.php +++ b/config/otp.php @@ -1,4 +1,5 @@ '123456789ABCDEFG@#$%', - /* + /* * The length of the password. */ - 'length' => env('OTP_LENGTH', 6), + 'length' => env('OTP_LENGTH', 6), /* * The separator of the password. @@ -42,13 +43,13 @@ 'separator' => '-', - /* + /* * Requiring correct input of uppercase and lowercase letters. */ - 'sensitive' => env('OTP_SENSITIVE', false), + 'sensitive' => env('OTP_SENSITIVE', false), - /* + /* * The expiry time of the password in minutes. */ @@ -60,7 +61,7 @@ 'attempts' => env('OTP_ATTEMPT_TIMES', 5), - /* + /* * The repeated password. * The previous password is valid when new password generated * until either one password used or itself expired. @@ -75,7 +76,7 @@ 'disposable' => true, - /* + /* * The prefix of the cache key to be used to store. */ @@ -86,6 +87,6 @@ */ 'demo' => env('OTP_DEMO', false), - 'demo_passwords' => ['1234','123456','12345678'] + 'demo_passwords' => ['1234', '123456', '12345678'], -]; \ No newline at end of file +]; diff --git a/resources/lang/en/messages.php b/resources/lang/en/messages.php index b23f4c9..584094f 100644 --- a/resources/lang/en/messages.php +++ b/resources/lang/en/messages.php @@ -1,7 +1,7 @@ 'The :attribute is not a valid code.', - 'expired' => 'The :attribute is expired.', - 'max_attempt' => 'The :attribute reached the maximum allowed attempts.', -]; \ No newline at end of file + 'invalid' => 'The :attribute is not a valid code.', + 'expired' => 'The :attribute is expired.', + 'max_attempt' => 'The :attribute reached the maximum allowed attempts.', +]; diff --git a/src/Otp.php b/src/Otp.php index 52f2341..7b3c84b 100644 --- a/src/Otp.php +++ b/src/Otp.php @@ -4,42 +4,55 @@ use Exception; use Illuminate\Support\Facades\Cache; -use Illuminate\Support\Facades\Facade; use Illuminate\Support\Str; -use Illuminate\Foundation\Application; class Otp { private string $format = 'numeric'; + private string $customize; + private int $length = 6; + private string $separator = '-'; + private bool $sensitive = false; + private int $expires = 15; + private int $attempts = 5; + private bool $repeated = true; + private bool $disposable = true; + private string $prefix = 'OTPPX_'; + private $data; + private bool $skip = false; + private bool $demo = false; + private array $demo_passwords = ['1234', '123456', '12345678']; public function __construct() { foreach (['format', 'customize', 'length', 'separator', 'sensitive', 'expires', 'attempts', 'repeated', 'disposable', 'prefix', 'data', 'demo', 'demo_passwords'] as $value) { - if (!empty(config('otp.' . $value))) $this->{$value} = config('otp.' . $value); + if (! empty(config('otp.'.$value))) { + $this->{$value} = config('otp.'.$value); + } } } public function __call(string $method, $params) { - if (!str_starts_with($method, 'set')) { + if (! str_starts_with($method, 'set')) { return; } $property = Str::camel(substr($method, 3)); - if (!property_exists($this, $property)) { + if (! property_exists($this, $property)) { return; } @@ -47,74 +60,94 @@ public function __call(string $method, $params) if ($property == 'customize') { $this->format = 'customize'; } + return $this; } /** * @throws Exception */ - public function generate(string $identifier = null, array $options = []): string + public function generate(?string $identifier = null, array $options = []): string { - if (!empty($options)) foreach (['format', 'customize', 'length', 'separator', 'sensitive', 'expires', 'repeated', 'prefix', 'data'] as $value) { - if (isset($options[$value])) $this->{$value} = $options[$value]; + if (! empty($options)) { + foreach (['format', 'customize', 'length', 'separator', 'sensitive', 'expires', 'repeated', 'prefix', 'data'] as $value) { + if (isset($options[$value])) { + $this->{$value} = $options[$value]; + } + } } - if ($identifier === null) $identifier = session()->getId(); + if ($identifier === null) { + $identifier = session()->getId(); + } $array = $this->repeated ? $this->readData($identifier, []) : []; $password = $this->generateNewPassword(); - if (!$this->sensitive) $password = strtoupper($password); + if (! $this->sensitive) { + $password = strtoupper($password); + } $array[md5($password)] = [ 'expires' => time() + $this->expires * 60, - 'data' => $this->data + 'data' => $this->data, ]; $this->writeData($identifier, $array); + return $password; } - public function validate(string $identifier = null, string $password = null, array $options = []): object + public function validate(?string $identifier = null, ?string $password = null, array $options = []): object { - if (!empty($options)) foreach (['attempts', 'sensitive', 'disposable', 'skip'] as $value) { - if (isset($options[$value])) $this->{$value} = $options[$value]; + if (! empty($options)) { + foreach (['attempts', 'sensitive', 'disposable', 'skip'] as $value) { + if (isset($options[$value])) { + $this->{$value} = $options[$value]; + } + } } if ($password === null) { if ($identifier === null) { - throw new Exception("Validate parameter can not be null"); + throw new Exception('Validate parameter can not be null'); } $password = $identifier; $identifier = null; } if ($this->demo && in_array($password, $this->demo_passwords)) { - return (object)[ + return (object) [ 'status' => true, - 'demo' => true + 'demo' => true, ]; } - if ($identifier === null) $identifier = session()->getId(); - $attempt = $this->readData('_attempt_' . $identifier, 0); + if ($identifier === null) { + $identifier = session()->getId(); + } + $attempt = $this->readData('_attempt_'.$identifier, 0); if ($attempt >= $this->attempts) { - return (object)[ + return (object) [ 'status' => false, 'error' => 'max_attempt', ]; } $codes = $this->readData($identifier, []); - if (!$this->sensitive) $password = strtoupper($password); + if (! $this->sensitive) { + $password = strtoupper($password); + } + + if (! isset($codes[md5($password)])) { + $this->writeData('_attempt_'.$identifier, $attempt + 1); - if (!isset($codes[md5($password)])) { - $this->writeData('_attempt_' . $identifier, $attempt + 1); - return (object)[ + return (object) [ 'status' => false, 'error' => 'invalid', ]; } if (time() > $codes[md5($password)]['expires']) { - $this->writeData('_attempt_' . $identifier, $attempt + 1); - return (object)[ + $this->writeData('_attempt_'.$identifier, $attempt + 1); + + return (object) [ 'status' => false, 'error' => 'expired', ]; @@ -124,37 +157,46 @@ public function validate(string $identifier = null, string $password = null, arr 'status' => true, ]; - if (!empty($codes[md5($password)]['data'])) { + if (! empty($codes[md5($password)]['data'])) { $response['data'] = $codes[md5($password)]['data']; } - if (!$this->skip) $this->forget($identifier, !$this->disposable ? $password : null); + if (! $this->skip) { + $this->forget($identifier, ! $this->disposable ? $password : null); + } $this->resetAttempt($identifier); - return (object)$response; + return (object) $response; } - public function forget(string $identifier = null, string $password = null): bool + public function forget(?string $identifier = null, ?string $password = null): bool { - if ($identifier === null) $identifier = session()->getId(); + if ($identifier === null) { + $identifier = session()->getId(); + } if ($password !== null) { $codes = $this->readData($identifier, []); - if (!isset($codes[md5($password)])) { + if (! isset($codes[md5($password)])) { return false; } unset($codes[md5($password)]); $this->writeData($identifier, $codes); + return true; } $this->deleteData($identifier); + return true; } - public function resetAttempt(string $identifier = null): bool + public function resetAttempt(?string $identifier = null): bool { - if ($identifier === null) $identifier = session()->getId(); - $this->deleteData('_attempt_' . $identifier); + if ($identifier === null) { + $identifier = session()->getId(); + } + $this->deleteData('_attempt_'.$identifier); + return true; } @@ -168,7 +210,7 @@ private function generateNewPassword(): string 'string' => $this->sensitive ? '23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ' : '23456789ABCDEFGHJKLMNPQRSTUVWXYZ', 'numeric' => '0123456789', 'numeric-no-zero' => '123456789', - 'customize' => $this->customize + 'customize' => $this->customize, ]; $lengths = is_array($this->length) ? $this->length : [$this->length]; @@ -180,22 +222,22 @@ private function generateNewPassword(): string return implode($this->separator, $password); } catch (Exception) { - throw new Exception("Fail to generate password, please check the format is correct."); + throw new Exception('Fail to generate password, please check the format is correct.'); } } private function writeData(string $key, mixed $value): void { - Cache::put($this->prefix . $key, $value, $this->expires * 60 * 3); + Cache::put($this->prefix.$key, $value, $this->expires * 60 * 3); } private function readData(string $key, mixed $default = null): mixed { - return Cache::get($this->prefix . $key, $default); + return Cache::get($this->prefix.$key, $default); } private function deleteData(string $key): void { - Cache::forget($this->prefix . $key); + Cache::forget($this->prefix.$key); } -} \ No newline at end of file +} diff --git a/src/OtpFacade.php b/src/OtpFacade.php index 2daf756..8de502b 100644 --- a/src/OtpFacade.php +++ b/src/OtpFacade.php @@ -8,11 +8,9 @@ class OtpFacade extends Facade { /** * Get the registered name of the component. - * - * @return string */ protected static function getFacadeAccessor(): string { return 'otp'; } -} \ No newline at end of file +} diff --git a/src/OtpServiceProvider.php b/src/OtpServiceProvider.php index 9ef9799..7945f47 100644 --- a/src/OtpServiceProvider.php +++ b/src/OtpServiceProvider.php @@ -2,8 +2,8 @@ namespace PhpMonsters\Otp; -use Illuminate\Support\ServiceProvider; use Illuminate\Foundation\Application; +use Illuminate\Support\ServiceProvider; class OtpServiceProvider extends ServiceProvider { @@ -25,10 +25,10 @@ public function register() public function boot() { $this->loadTranslationsFrom(__DIR__.'/../lang', 'otp'); - + $this->publishes([ __DIR__.'/../resources/lang' => $this->app->langPath('vendor/otp'), - __DIR__.'/../config/otp.php' => config_path('otp.php') + __DIR__.'/../config/otp.php' => config_path('otp.php'), ]); } -} \ No newline at end of file +} diff --git a/src/Rules/OtpValidate.php b/src/Rules/OtpValidate.php index 5b081be..f02fd4b 100644 --- a/src/Rules/OtpValidate.php +++ b/src/Rules/OtpValidate.php @@ -8,11 +8,14 @@ class OtpValidate implements Rule { protected string $identifier; + protected array $options; + protected string $attribute; + protected string $error; - public function __construct(string $identifier = null, array $options = []) + public function __construct(?string $identifier = null, array $options = []) { $this->identifier = $identifier ?: session()->getId(); $this->options = $options; @@ -24,6 +27,7 @@ public function passes($attribute, $value): bool if ($result->status !== true) { $this->attribute = $attribute; $this->error = $result->error; + return false; } @@ -32,8 +36,8 @@ public function passes($attribute, $value): bool public function message(): string { - return __('otp::messages.' . $this->error, [ - 'attribute' => $this->attribute + return __('otp::messages.'.$this->error, [ + 'attribute' => $this->attribute, ]); } -} \ No newline at end of file +}