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:<plain>' for deterministic test assertions. composer stan now passes --memory-limit=512M (default 128M overflows once larastan loads more rules).
This commit is contained in:
parent
eca73213f5
commit
bb38e544ee
10 changed files with 136 additions and 1 deletions
16
backend/app/Auth/BcryptPasswordHasher.php
Normal file
16
backend/app/Auth/BcryptPasswordHasher.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
class BcryptPasswordHasher implements PasswordHasher
|
||||
{
|
||||
public function hash(string $password): string
|
||||
{
|
||||
return password_hash($password, PASSWORD_DEFAULT);
|
||||
}
|
||||
|
||||
public function verify(string $password, string $hash): bool
|
||||
{
|
||||
return password_verify($password, $hash);
|
||||
}
|
||||
}
|
||||
10
backend/app/Auth/Clock.php
Normal file
10
backend/app/Auth/Clock.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
interface Clock
|
||||
{
|
||||
public function now(): DateTimeImmutable;
|
||||
}
|
||||
10
backend/app/Auth/PasswordHasher.php
Normal file
10
backend/app/Auth/PasswordHasher.php
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
interface PasswordHasher
|
||||
{
|
||||
public function hash(string $password): string;
|
||||
|
||||
public function verify(string $password, string $hash): bool;
|
||||
}
|
||||
11
backend/app/Auth/RandomTokenGenerator.php
Normal file
11
backend/app/Auth/RandomTokenGenerator.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
class RandomTokenGenerator implements TokenGenerator
|
||||
{
|
||||
public function generate(): string
|
||||
{
|
||||
return bin2hex(random_bytes(32));
|
||||
}
|
||||
}
|
||||
14
backend/app/Auth/SystemClock.php
Normal file
14
backend/app/Auth/SystemClock.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
|
||||
class SystemClock implements Clock
|
||||
{
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return new DateTimeImmutable('now', new DateTimeZone('UTC'));
|
||||
}
|
||||
}
|
||||
8
backend/app/Auth/TokenGenerator.php
Normal file
8
backend/app/Auth/TokenGenerator.php
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
interface TokenGenerator
|
||||
{
|
||||
public function generate(): string;
|
||||
}
|
||||
|
|
@ -49,7 +49,7 @@
|
|||
"npx concurrently -c \"#93c5fd,#c4b5fd,#fb7185\" \"php artisan serve\" \"php artisan queue:listen --tries=1\" \"php artisan pail --timeout=0\" --names=server,queue,logs --kill-others"
|
||||
],
|
||||
|
||||
"stan": "phpstan analyse --no-progress",
|
||||
"stan": "phpstan analyse --no-progress --memory-limit=512M",
|
||||
"cs:fix": "php-cs-fixer fix",
|
||||
"cs:check": "php-cs-fixer check --diff -vvv",
|
||||
|
||||
|
|
|
|||
21
backend/tests/Fakes/FakeClock.php
Normal file
21
backend/tests/Fakes/FakeClock.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\Clock;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class FakeClock implements Clock
|
||||
{
|
||||
public function __construct(private DateTimeImmutable $currentTime) {}
|
||||
|
||||
public function now(): DateTimeImmutable
|
||||
{
|
||||
return $this->currentTime;
|
||||
}
|
||||
|
||||
public function setTime(DateTimeImmutable $newTime): void
|
||||
{
|
||||
$this->currentTime = $newTime;
|
||||
}
|
||||
}
|
||||
18
backend/tests/Fakes/FakePasswordHasher.php
Normal file
18
backend/tests/Fakes/FakePasswordHasher.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\PasswordHasher;
|
||||
|
||||
class FakePasswordHasher implements PasswordHasher
|
||||
{
|
||||
public function hash(string $password): string
|
||||
{
|
||||
return 'hashed:'.$password;
|
||||
}
|
||||
|
||||
public function verify(string $password, string $hash): bool
|
||||
{
|
||||
return $this->hash($password) === $hash;
|
||||
}
|
||||
}
|
||||
27
backend/tests/Fakes/FakeTokenGenerator.php
Normal file
27
backend/tests/Fakes/FakeTokenGenerator.php
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Auth\TokenGenerator;
|
||||
use RuntimeException;
|
||||
|
||||
class FakeTokenGenerator implements TokenGenerator
|
||||
{
|
||||
private int $callCount = 0;
|
||||
|
||||
/**
|
||||
* @param string[] $tokens
|
||||
*/
|
||||
public function __construct(private array $tokens) {}
|
||||
|
||||
public function generate(): string
|
||||
{
|
||||
if ($this->callCount >= count($this->tokens)) {
|
||||
throw new RuntimeException('FakeTokenGenerator exhausted');
|
||||
}
|
||||
$token = $this->tokens[$this->callCount];
|
||||
$this->callCount++;
|
||||
|
||||
return $token;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue