koel/app/Services/AuthenticationService.php

86 lines
2.5 KiB
PHP
Raw Normal View History

2023-08-20 22:35:58 +00:00
<?php
namespace App\Services;
use App\Exceptions\InvalidCredentialsException;
use App\Models\User;
use App\Repositories\UserRepository;
use App\Values\CompositeToken;
2024-02-25 19:32:53 +00:00
use Illuminate\Auth\Events\PasswordReset;
use Illuminate\Auth\Passwords\PasswordBroker;
2024-04-03 14:48:52 +00:00
use Illuminate\Support\Facades\Cache;
2024-06-04 13:35:00 +00:00
use Illuminate\Support\Facades\Hash;
2024-02-25 19:32:53 +00:00
use Illuminate\Support\Facades\Password;
2023-08-20 22:35:58 +00:00
class AuthenticationService
{
public function __construct(
2024-04-18 14:36:28 +00:00
private readonly UserRepository $userRepository,
private readonly TokenManager $tokenManager,
private readonly PasswordBroker $passwordBroker
2023-08-20 22:35:58 +00:00
) {
}
public function login(string $email, string $password): CompositeToken
2023-08-20 22:35:58 +00:00
{
2024-06-04 13:35:00 +00:00
$user = $this->userRepository->findFirstWhere('email', $email);
2023-08-20 22:35:58 +00:00
2024-06-04 13:35:00 +00:00
if (!$user || !Hash::check($password, $user->password)) {
2023-08-20 22:35:58 +00:00
throw new InvalidCredentialsException();
}
2024-06-04 13:35:00 +00:00
if (Hash::needsRehash($user->password)) {
$user->password = Hash::make($password);
$user->save();
}
2024-03-30 16:49:25 +00:00
return $this->logUserIn($user);
}
public function logUserIn(User $user): CompositeToken
{
return $this->tokenManager->createCompositeToken($user);
2023-08-20 22:35:58 +00:00
}
public function logoutViaBearerToken(string $token): void
{
$this->tokenManager->deleteCompositionToken($token);
}
2024-02-25 19:32:53 +00:00
public function trySendResetPasswordLink(string $email): bool
{
return $this->passwordBroker->sendResetLink(['email' => $email]) === Password::RESET_LINK_SENT;
}
public function tryResetPasswordUsingBroker(string $email, string $password, string $token): bool
{
$credentials = [
'email' => $email,
'password' => $password,
'password_confirmation' => $password,
'token' => $token,
];
2024-06-04 13:35:00 +00:00
$status = $this->passwordBroker->reset($credentials, static function (User $user, string $password): void {
$user->password = Hash::make($password);
2024-02-25 19:32:53 +00:00
$user->save();
event(new PasswordReset($user));
});
return $status === Password::PASSWORD_RESET;
}
2024-04-03 14:48:52 +00:00
public function generateOneTimeToken(User $user): string
{
2024-04-19 13:25:08 +00:00
$token = bin2hex(random_bytes(12));
Cache::set("one-time-token.$token", encrypt($user->id), 60 * 10);
2024-04-03 14:48:52 +00:00
return $token;
}
2024-04-19 13:25:08 +00:00
public function loginViaOneTimeToken(string $token): CompositeToken
{
2024-04-24 21:58:19 +00:00
return $this->logUserIn($this->userRepository->getOne(decrypt(Cache::get("one-time-token.$token"))));
2024-04-19 13:25:08 +00:00
}
2023-08-20 22:35:58 +00:00
}