From 2114578b3cc88c583e229cce3be4f88c8fc9ea23 Mon Sep 17 00:00:00 2001 From: ibrahemkamal Date: Mon, 5 Feb 2024 23:41:07 +0200 Subject: [PATCH 1/2] - added otp handlers --- src/Contracts/OtpHandler.php | 10 ++++++++++ src/Otp.php | 24 +++++++++++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) create mode 100644 src/Contracts/OtpHandler.php diff --git a/src/Contracts/OtpHandler.php b/src/Contracts/OtpHandler.php new file mode 100644 index 0000000..2093234 --- /dev/null +++ b/src/Contracts/OtpHandler.php @@ -0,0 +1,10 @@ +model) { + if (!$this->model) { throw new \Exception('Model is required to generate otp'); } $otp = $otp ?? $this->generatePassword(); - if (! $this->validateOtpUniqueness($otp)) { + if (!$this->validateOtpUniqueness($otp)) { return $this->generate(); } /** @phpstan-ignore-next-line */ @@ -57,7 +57,7 @@ public function generate($otp = null): OtpCode 'otp' => $otp, 'phone' => $this->getPhone(), 'service' => $this->service, - 'expires_at' => now()->addMinutes(config('otp.services.'.$this->service.'.expires_in')), + 'expires_at' => now()->addMinutes(config('otp.services.' . $this->service . '.expires_in')), ]); } @@ -75,7 +75,7 @@ public function setGeneratorOptions($length = 4, $numbers = true, $letters = fal public function setService(string $service = 'default'): static { - if (! config('otp.services.'.$service)) { + if (!config('otp.services.' . $service)) { throw new \Exception('Service not found in the config file'); } $this->service = $service; @@ -116,17 +116,17 @@ public function setModel(HasOtp $model): static private function validateOtpUniqueness(string $otp): bool { - if (! $this->isValidateUniquenessAfterGeneration()) { + if (!$this->isValidateUniquenessAfterGeneration()) { return true; } - return ! $this->model->otpCodes()->where('phone', $this->phone) + return !$this->model->otpCodes()->where('phone', $this->phone) ->where('service', $this->service)->where('otp', $otp)->exists(); } public function verifyOtp(string $otp): ServiceResponse { - if (! $this->model) { + if (!$this->model) { throw new \Exception('Model is required to verify otp'); } $otpCode = $this->model->otpCodes()->where('otp', $otp)->where('phone', $this->phone) @@ -145,6 +145,12 @@ public function verifyOtp(string $otp): ServiceResponse } /** @phpstan-ignore-next-line */ $otpCode->verified_at = now(); + /** @phpstan-ignore-next-line */ + if ($handlers = config('otp.services.' . $otpCode->service . '.handlers')) { + foreach ($handlers as $handler) { + app($handler)::handle($otpCode); + } + } return $this->serviceResponse->setSuccess(true)->setData($otpCode); } @@ -154,7 +160,7 @@ public function verifyOtp(string $otp): ServiceResponse private function setDefaults(): void { - $generatorOptions = config('otp.services.'.$this->service.'.otp_generator_options'); + $generatorOptions = config('otp.services.' . $this->service . '.otp_generator_options'); if ($generatorOptions) { $this->setGeneratorOptions( @@ -171,7 +177,7 @@ private function setDefaults(): void symbols: config('otp.fallback_options.otp_generator_options.symbols'), ); } - $this->setValidateUniquenessAfterGeneration(config('otp.services.'.$this->service.'.validate_uniqueness_after_generation') ?? config('otp.fallback_options.validate_uniqueness_after_generation')); + $this->setValidateUniquenessAfterGeneration(config('otp.services.' . $this->service . '.validate_uniqueness_after_generation') ?? config('otp.fallback_options.validate_uniqueness_after_generation')); } public function getGeneratorOptions(): array From 3ac28df956e77113002f932be505cb3224268a1a Mon Sep 17 00:00:00 2001 From: ibrahem-kamal Date: Mon, 5 Feb 2024 21:48:19 +0000 Subject: [PATCH 2/2] Fix styling --- src/Otp.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/Otp.php b/src/Otp.php index fbd9984..a206ec0 100755 --- a/src/Otp.php +++ b/src/Otp.php @@ -45,11 +45,11 @@ public function setValidateUniquenessAfterGeneration(bool $validateUniquenessAft public function generate($otp = null): OtpCode { - if (!$this->model) { + if (! $this->model) { throw new \Exception('Model is required to generate otp'); } $otp = $otp ?? $this->generatePassword(); - if (!$this->validateOtpUniqueness($otp)) { + if (! $this->validateOtpUniqueness($otp)) { return $this->generate(); } @@ -58,7 +58,7 @@ public function generate($otp = null): OtpCode 'otp' => $otp, 'phone' => $this->getPhone(), 'service' => $this->service, - 'expires_at' => now()->addMinutes(config('otp.services.' . $this->service . '.expires_in')), + 'expires_at' => now()->addMinutes(config('otp.services.'.$this->service.'.expires_in')), ]); } @@ -76,7 +76,7 @@ public function setGeneratorOptions($length = 4, $numbers = true, $letters = fal public function setService(string $service = 'default'): static { - if (!config('otp.services.' . $service)) { + if (! config('otp.services.'.$service)) { throw new \Exception('Service not found in the config file'); } $this->service = $service; @@ -117,17 +117,17 @@ public function setModel(HasOtp $model): static private function validateOtpUniqueness(string $otp): bool { - if (!$this->isValidateUniquenessAfterGeneration()) { + if (! $this->isValidateUniquenessAfterGeneration()) { return true; } - return !$this->model->otpCodes()->where('phone', $this->phone) + return ! $this->model->otpCodes()->where('phone', $this->phone) ->where('service', $this->service)->where('otp', $otp)->exists(); } public function verifyOtp(string $otp): ServiceResponse { - if (!$this->model) { + if (! $this->model) { throw new \Exception('Model is required to verify otp'); } $otpCode = $this->model->otpCodes()->where('otp', $otp)->where('phone', $this->phone) @@ -147,7 +147,7 @@ public function verifyOtp(string $otp): ServiceResponse /** @phpstan-ignore-next-line */ $otpCode->verified_at = now(); /** @phpstan-ignore-next-line */ - if ($handlers = config('otp.services.' . $otpCode->service . '.handlers')) { + if ($handlers = config('otp.services.'.$otpCode->service.'.handlers')) { foreach ($handlers as $handler) { app($handler)::handle($otpCode); } @@ -161,7 +161,7 @@ public function verifyOtp(string $otp): ServiceResponse private function setDefaults(): void { - $generatorOptions = config('otp.services.' . $this->service . '.otp_generator_options'); + $generatorOptions = config('otp.services.'.$this->service.'.otp_generator_options'); if ($generatorOptions) { $this->setGeneratorOptions( @@ -178,7 +178,7 @@ private function setDefaults(): void symbols: config('otp.fallback_options.otp_generator_options.symbols'), ); } - $this->setValidateUniquenessAfterGeneration(config('otp.services.' . $this->service . '.validate_uniqueness_after_generation') ?? config('otp.fallback_options.validate_uniqueness_after_generation')); + $this->setValidateUniquenessAfterGeneration(config('otp.services.'.$this->service.'.validate_uniqueness_after_generation') ?? config('otp.fallback_options.validate_uniqueness_after_generation')); } public function getGeneratorOptions(): array