From bb38e544ee2de299a31a104a5d0dcb347c8d2668 Mon Sep 17 00:00:00 2001 From: yisroel Date: Wed, 6 May 2026 15:11:19 +0300 Subject: [PATCH] add auth utility interfaces and impls Clock + SystemClock (DateTimeImmutable in UTC), TokenGenerator + RandomTokenGenerator (bin2hex(random_bytes(32)) -> 64-char hex), PasswordHasher + BcryptPasswordHasher (password_hash with PASSWORD_DEFAULT, password_verify). matching fakes: FakeClock with mutable setTime, FakeTokenGenerator with a pre-seeded queue (throws once exhausted), FakePasswordHasher returns 'hashed:' for deterministic test assertions. composer stan now passes --memory-limit=512M (default 128M overflows once larastan loads more rules). --- backend/app/Auth/BcryptPasswordHasher.php | 16 +++++++++++++ backend/app/Auth/Clock.php | 10 ++++++++ backend/app/Auth/PasswordHasher.php | 10 ++++++++ backend/app/Auth/RandomTokenGenerator.php | 11 +++++++++ backend/app/Auth/SystemClock.php | 14 +++++++++++ backend/app/Auth/TokenGenerator.php | 8 +++++++ backend/composer.json | 2 +- backend/tests/Fakes/FakeClock.php | 21 +++++++++++++++++ backend/tests/Fakes/FakePasswordHasher.php | 18 +++++++++++++++ backend/tests/Fakes/FakeTokenGenerator.php | 27 ++++++++++++++++++++++ 10 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 backend/app/Auth/BcryptPasswordHasher.php create mode 100644 backend/app/Auth/Clock.php create mode 100644 backend/app/Auth/PasswordHasher.php create mode 100644 backend/app/Auth/RandomTokenGenerator.php create mode 100644 backend/app/Auth/SystemClock.php create mode 100644 backend/app/Auth/TokenGenerator.php create mode 100644 backend/tests/Fakes/FakeClock.php create mode 100644 backend/tests/Fakes/FakePasswordHasher.php create mode 100644 backend/tests/Fakes/FakeTokenGenerator.php diff --git a/backend/app/Auth/BcryptPasswordHasher.php b/backend/app/Auth/BcryptPasswordHasher.php new file mode 100644 index 0000000..0bc4a46 --- /dev/null +++ b/backend/app/Auth/BcryptPasswordHasher.php @@ -0,0 +1,16 @@ +currentTime; + } + + public function setTime(DateTimeImmutable $newTime): void + { + $this->currentTime = $newTime; + } +} diff --git a/backend/tests/Fakes/FakePasswordHasher.php b/backend/tests/Fakes/FakePasswordHasher.php new file mode 100644 index 0000000..9e93325 --- /dev/null +++ b/backend/tests/Fakes/FakePasswordHasher.php @@ -0,0 +1,18 @@ +hash($password) === $hash; + } +} diff --git a/backend/tests/Fakes/FakeTokenGenerator.php b/backend/tests/Fakes/FakeTokenGenerator.php new file mode 100644 index 0000000..601eb2f --- /dev/null +++ b/backend/tests/Fakes/FakeTokenGenerator.php @@ -0,0 +1,27 @@ +callCount >= count($this->tokens)) { + throw new RuntimeException('FakeTokenGenerator exhausted'); + } + $token = $this->tokens[$this->callCount]; + $this->callCount++; + + return $token; + } +}