From a9bea81979041e84a5eea93d92331e23cef9f4a6 Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Sun, 29 Jun 2025 17:33:02 +0900 Subject: [PATCH 1/3] feat: token-validation-method-add --- .../PasswordResetTokenValidatorInterface.php | 11 +++++++ .../PasswordResetTokenValidatorService.php | 30 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/app/User/Domain/Service/PasswordResetTokenValidatorInterface.php create mode 100644 src/app/User/Infrastructure/Service/PasswordResetTokenValidatorService.php diff --git a/src/app/User/Domain/Service/PasswordResetTokenValidatorInterface.php b/src/app/User/Domain/Service/PasswordResetTokenValidatorInterface.php new file mode 100644 index 0000000..d44c6af --- /dev/null +++ b/src/app/User/Domain/Service/PasswordResetTokenValidatorInterface.php @@ -0,0 +1,11 @@ +isValidToken($userId, $token)) { + throw new InvalidArgumentException('Invalid password reset token.'); + } + } + + private function isValidToken($userId, $token): bool + { + return DB::table('password_reset_requests') + ->where('token', $token) + ->where('user_id', $userId) + ->where('created_at', '>=', now()->subMinutes(60)) + ->exists(); + } +} \ No newline at end of file From fe7e8b9a53984de118c20657dd0f5247287a4b44 Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Sun, 29 Jun 2025 17:34:35 +0900 Subject: [PATCH 2/3] feat: token-validation-method-test-add --- ...PasswordResetTokenValidatorServiceTest.php | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 src/app/User/Infrastructure/InfrastructureTest/Service/PasswordResetTokenValidatorServiceTest.php diff --git a/src/app/User/Infrastructure/InfrastructureTest/Service/PasswordResetTokenValidatorServiceTest.php b/src/app/User/Infrastructure/InfrastructureTest/Service/PasswordResetTokenValidatorServiceTest.php new file mode 100644 index 0000000..53228ea --- /dev/null +++ b/src/app/User/Infrastructure/InfrastructureTest/Service/PasswordResetTokenValidatorServiceTest.php @@ -0,0 +1,75 @@ +refresh(); + $this->service = new PasswordResetTokenValidatorService(); + $this->user = $this->createUser(); + $this->resetRequest = $this->createResetRequest(); + } + + protected function tearDown(): void + { + $this->refresh(); + parent::tearDown(); + } + + private function refresh(): void + { + if (env('APP_ENV') === 'testing') { + DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=0;'); + User::truncate(); + PasswordResetRequest::truncate(); + DB::connection('mysql')->statement('SET FOREIGN_KEY_CHECKS=1;'); + } + } + + private function createUser(): User + { + return User::Create([ + 'first_name' => 'Sergio', + 'last_name' => 'Ramos', + 'email' => "real-madrid".rand(). "@test.com", + 'password' => 'el-capitán-1234', + 'bio' => 'Real Madrid player', + 'location' => 'Madrid', + 'skills' => ['Football', 'Leadership'], + 'profile_image' => 'https://example.com/sergio.jpg' + ]); + } + + private function createResetRequest(): PasswordResetRequest + { + return PasswordResetRequest::create([ + 'user_id' => $this->user->id, + 'token' => bin2hex(random_bytes(32)), + 'requested_at' => now(), + 'expired_at' => now()->addMinutes(30), + ]); + } + + public function test_check_token_validate(): void + { + $this->expectNotToPerformAssertions(); + + $this->service->validate( + $this->user->id, + $this->resetRequest->token + ); + } +} \ No newline at end of file From 6c714decee2718a9fc3acf2f7424dc837933a078 Mon Sep 17 00:00:00 2001 From: Application-drop-up Date: Sun, 29 Jun 2025 17:38:28 +0900 Subject: [PATCH 3/3] feat: change-search-column-about-created_at_to_requested_at --- .../Service/PasswordResetTokenValidatorService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/User/Infrastructure/Service/PasswordResetTokenValidatorService.php b/src/app/User/Infrastructure/Service/PasswordResetTokenValidatorService.php index 68777da..b675e40 100644 --- a/src/app/User/Infrastructure/Service/PasswordResetTokenValidatorService.php +++ b/src/app/User/Infrastructure/Service/PasswordResetTokenValidatorService.php @@ -24,7 +24,7 @@ private function isValidToken($userId, $token): bool return DB::table('password_reset_requests') ->where('token', $token) ->where('user_id', $userId) - ->where('created_at', '>=', now()->subMinutes(60)) + ->where('requested_at', '>=', now()->subMinutes(60)) ->exists(); } } \ No newline at end of file