copy user entity and auth from ysv
This commit is contained in:
parent
9d5bfc33a6
commit
613180d459
24 changed files with 612 additions and 0 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;
|
||||
}
|
||||
16
backend/app/Auth/CreateSessionDto.php
Normal file
16
backend/app/Auth/CreateSessionDto.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use App\User\User;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class CreateSessionDto
|
||||
{
|
||||
public function __construct(
|
||||
public string $token,
|
||||
public User $user,
|
||||
public DateTimeImmutable $createdAt,
|
||||
public DateTimeImmutable $expiresAt,
|
||||
) {}
|
||||
}
|
||||
60
backend/app/Auth/EloquentSessionRepository.php
Normal file
60
backend/app/Auth/EloquentSessionRepository.php
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use App\User\UserRepository;
|
||||
use DateTimeImmutable;
|
||||
use DateTimeZone;
|
||||
|
||||
class EloquentSessionRepository implements SessionRepository
|
||||
{
|
||||
public function __construct(private UserRepository $userRepo) {}
|
||||
|
||||
public function create(CreateSessionDto $dto): Session
|
||||
{
|
||||
SessionModel::create([
|
||||
'token' => $dto->token,
|
||||
'user_id' => $dto->user->getId(),
|
||||
'created_at' => $dto->createdAt,
|
||||
'expires_at' => $dto->expiresAt,
|
||||
]);
|
||||
|
||||
return new Session(
|
||||
token: $dto->token,
|
||||
user: $dto->user,
|
||||
createdAt: $dto->createdAt,
|
||||
expiresAt: $dto->expiresAt,
|
||||
);
|
||||
}
|
||||
|
||||
public function findByToken(string $token): ?Session
|
||||
{
|
||||
$model = SessionModel::find($token);
|
||||
if ($model === null) {
|
||||
return null;
|
||||
}
|
||||
$user = $this->userRepo->find($model->user_id);
|
||||
if ($user === null) {
|
||||
return null;
|
||||
}
|
||||
$utc = new DateTimeZone('UTC');
|
||||
|
||||
return new Session(
|
||||
token: $model->token,
|
||||
user: $user,
|
||||
createdAt: new DateTimeImmutable(
|
||||
$model->created_at->toDateTimeString(),
|
||||
$utc
|
||||
),
|
||||
expiresAt: new DateTimeImmutable(
|
||||
$model->expires_at->toDateTimeString(),
|
||||
$utc
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function deleteByToken(string $token): void
|
||||
{
|
||||
SessionModel::where('token', $token)->delete();
|
||||
}
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
41
backend/app/Auth/Session.php
Normal file
41
backend/app/Auth/Session.php
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use App\User\User;
|
||||
use DateTimeImmutable;
|
||||
|
||||
class Session
|
||||
{
|
||||
public function __construct(
|
||||
private string $token,
|
||||
private User $user,
|
||||
private DateTimeImmutable $createdAt,
|
||||
private DateTimeImmutable $expiresAt,
|
||||
) {}
|
||||
|
||||
public function getToken(): string
|
||||
{
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
public function getUser(): User
|
||||
{
|
||||
return $this->user;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function getExpiresAt(): DateTimeImmutable
|
||||
{
|
||||
return $this->expiresAt;
|
||||
}
|
||||
|
||||
public function isExpired(DateTimeImmutable $now): bool
|
||||
{
|
||||
return $now >= $this->expiresAt;
|
||||
}
|
||||
}
|
||||
44
backend/app/Auth/SessionModel.php
Normal file
44
backend/app/Auth/SessionModel.php
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
/**
|
||||
* @property string $token
|
||||
* @property int $user_id
|
||||
* @property Carbon $created_at
|
||||
* @property Carbon $expires_at
|
||||
*
|
||||
* @method static Builder<static>|SessionModel newModelQuery()
|
||||
* @method static Builder<static>|SessionModel newQuery()
|
||||
* @method static Builder<static>|SessionModel query()
|
||||
*
|
||||
* @mixin \Eloquent
|
||||
*/
|
||||
class SessionModel extends Model
|
||||
{
|
||||
protected $table = 'sessions';
|
||||
|
||||
protected $primaryKey = 'token';
|
||||
|
||||
public $incrementing = false;
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'token',
|
||||
'user_id',
|
||||
'created_at',
|
||||
'expires_at',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'created_at' => 'datetime',
|
||||
'expires_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
12
backend/app/Auth/SessionRepository.php
Normal file
12
backend/app/Auth/SessionRepository.php
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth;
|
||||
|
||||
interface SessionRepository
|
||||
{
|
||||
public function create(CreateSessionDto $dto): Session;
|
||||
|
||||
public function findByToken(string $token): ?Session;
|
||||
|
||||
public function deleteByToken(string $token): void;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases\AuthenticateUser;
|
||||
|
||||
use App\Auth\PasswordHasher;
|
||||
use App\Exceptions\BadRequestException;
|
||||
use App\Exceptions\UnauthorizedException;
|
||||
use App\Shared\ValueObject\EmailAddress;
|
||||
use App\User\User;
|
||||
use App\User\UserRepository;
|
||||
|
||||
class AuthenticateUser
|
||||
{
|
||||
public function __construct(
|
||||
private UserRepository $userRepo,
|
||||
private PasswordHasher $hasher,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @throws BadRequestException
|
||||
* @throws UnauthorizedException
|
||||
*/
|
||||
public function execute(AuthenticateUserRequest $request): User
|
||||
{
|
||||
if ($request->email === null || $request->email === '') {
|
||||
throw new BadRequestException('email is required');
|
||||
}
|
||||
if ($request->password === null || $request->password === '') {
|
||||
throw new BadRequestException('password is required');
|
||||
}
|
||||
|
||||
$user = $this->userRepo->findByEmail(
|
||||
new EmailAddress($request->email)
|
||||
);
|
||||
if ($user === null) {
|
||||
throw new UnauthorizedException('invalid credentials');
|
||||
}
|
||||
|
||||
$passwordMatches = $this->hasher->verify(
|
||||
$request->password,
|
||||
$user->getPasswordHash(),
|
||||
);
|
||||
if (! $passwordMatches) {
|
||||
throw new UnauthorizedException('invalid credentials');
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases\AuthenticateUser;
|
||||
|
||||
class AuthenticateUserRequest
|
||||
{
|
||||
public function __construct(
|
||||
public ?string $email,
|
||||
public ?string $password,
|
||||
) {}
|
||||
}
|
||||
34
backend/app/Auth/UseCases/CreateSession/CreateSession.php
Normal file
34
backend/app/Auth/UseCases/CreateSession/CreateSession.php
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases\CreateSession;
|
||||
|
||||
use App\Auth\Clock;
|
||||
use App\Auth\CreateSessionDto;
|
||||
use App\Auth\Session;
|
||||
use App\Auth\SessionRepository;
|
||||
use App\Auth\TokenGenerator;
|
||||
use App\User\User;
|
||||
|
||||
class CreateSession
|
||||
{
|
||||
private const SESSION_LIFETIME = '+7 days';
|
||||
|
||||
public function __construct(
|
||||
private SessionRepository $sessionRepo,
|
||||
private TokenGenerator $tokenGenerator,
|
||||
private Clock $clock,
|
||||
) {}
|
||||
|
||||
public function execute(User $user): Session
|
||||
{
|
||||
$now = $this->clock->now();
|
||||
$expiresAt = $now->modify(self::SESSION_LIFETIME);
|
||||
|
||||
return $this->sessionRepo->create(new CreateSessionDto(
|
||||
token: $this->tokenGenerator->generate(),
|
||||
user: $user,
|
||||
createdAt: $now,
|
||||
expiresAt: $expiresAt,
|
||||
));
|
||||
}
|
||||
}
|
||||
17
backend/app/Auth/UseCases/Logout/Logout.php
Normal file
17
backend/app/Auth/UseCases/Logout/Logout.php
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Auth\UseCases\Logout;
|
||||
|
||||
use App\Auth\SessionRepository;
|
||||
|
||||
class Logout
|
||||
{
|
||||
public function __construct(
|
||||
private SessionRepository $sessionRepo,
|
||||
) {}
|
||||
|
||||
public function execute(string $token): void
|
||||
{
|
||||
$this->sessionRepo->deleteByToken($token);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue