1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Chubbyphp\Security\Authentication; |
4
|
|
|
|
5
|
|
|
use Chubbyphp\Model\RepositoryInterface; |
6
|
|
|
use Chubbyphp\Security\Authentication\Exception\EmptyPasswordException; |
7
|
|
|
use Chubbyphp\Security\Authentication\Exception\InvalidPasswordException; |
8
|
|
|
use Chubbyphp\Security\Authentication\Exception\UserNotFoundException; |
9
|
|
|
use Chubbyphp\Session\SessionInterface; |
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
11
|
|
|
|
12
|
|
|
final class FormAuthentication implements AuthenticationInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var SessionInterface |
16
|
|
|
*/ |
17
|
|
|
private $session; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var RepositoryInterface |
21
|
|
|
*/ |
22
|
|
|
private $userRepository; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param SessionInterface $session |
26
|
|
|
* @param RepositoryInterface $userRepository |
27
|
|
|
*/ |
28
|
|
|
public function __construct(SessionInterface $session, RepositoryInterface $userRepository) |
29
|
|
|
{ |
30
|
|
|
$this->session = $session; |
31
|
|
|
$this->userRepository = $userRepository; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param Request $request |
36
|
|
|
* |
37
|
|
|
* @throws InvalidPasswordException |
38
|
|
|
* @throws UserNotFoundException |
39
|
|
|
*/ |
40
|
|
|
public function login(Request $request) |
41
|
|
|
{ |
42
|
|
|
$data = $request->getParsedBody(); |
43
|
|
|
|
44
|
|
|
/** @var UserCredentialsInterface $user */ |
45
|
|
|
if (null === $user = $this->userRepository->findOneBy(['username' => $data['username']])) { |
46
|
|
|
throw UserNotFoundException::create($data['username']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!password_verify($data['password'], $user->getPassword())) { |
50
|
|
|
throw InvalidPasswordException::create(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
$this->session->set($request, self::USER_KEY, $user->getId()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param Request $request |
58
|
|
|
*/ |
59
|
|
|
public function logout(Request $request) |
60
|
|
|
{ |
61
|
|
|
$this->session->remove($request, self::USER_KEY); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param Request $request |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function isAuthenticated(Request $request): bool |
70
|
|
|
{ |
71
|
|
|
return null !== $this->getAuthenticatedUser($request); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param Request $request |
76
|
|
|
* |
77
|
|
|
* @return UserCredentialsInterface|null |
78
|
|
|
*/ |
79
|
|
|
public function getAuthenticatedUser(Request $request) |
80
|
|
|
{ |
81
|
|
|
if (!$this->session->has($request, self::USER_KEY)) { |
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$id = $this->session->get($request, self::USER_KEY); |
86
|
|
|
|
87
|
|
|
return $this->userRepository->find($id); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $password |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
* |
95
|
|
|
* @throws EmptyPasswordException |
96
|
|
|
*/ |
97
|
|
|
public function hashPassword(string $password): string |
98
|
|
|
{ |
99
|
|
|
if ('' === $password) { |
100
|
|
|
throw EmptyPasswordException::create(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return password_hash($password, PASSWORD_DEFAULT); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|