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