Completed
Push — master ( 20c2be...9be769 )
by Dominik
02:56 queued 56s
created

FormAuthentication   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 4
dl 0
loc 94
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A login() 0 15 3
A logout() 0 4 1
A isAuthenticated() 0 4 1
A getAuthenticatedUser() 0 10 2
A hashPassword() 0 8 2
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