Passed
Branch master (92cda2)
by Sebastian
02:34
created

EnhancedAuthenticate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 4
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authentication;
13
14
use Linna\Session\Session;
15
use Linna\Shared\ClassOptionsTrait;
16
17
/**
18
 * Extend basic user authentication system with more security checks.
19
 */
20
class EnhancedAuthenticate extends Authenticate
21
{
22
    use ClassOptionsTrait;
23
    
24
    /**
25
     * @var array An associative array containing options
26
     */
27
    protected $options = [
28
        'maxAttemptsForUserName' => 5,
29
        'maxAttemptsForSessionId' => 10,
30
        'maxAttemptsForIpAddress' => 20,
31
        'maxAttemptsForSecond' => 40,
32
        'banTimeInSeconds' => 900 //15 minutes
33
    ];
34
    
35
    /**
36
     * @var EnhancedAuthenticateMapperInterface Enhanced Authenticate Mapper
37
     */
38
    private $enhancedAuthenticateMapper;
39
    
40
    /**
41
     * Class Constructor
42
     *
43
     * @param Session $session
44
     * @param Password $password
45
     * @param EnhancedAuthenticateMapperInterface $enhancedAuthenticateMapper
46
     * @param array $options
47
     */
48 21
    public function __construct(
49
            Session $session,
50
            Password $password,
51
            EnhancedAuthenticateMapperInterface $enhancedAuthenticateMapper,
52
            array $options = []
53
        ) {
54 21
        parent::__construct($session, $password);
55
        
56 21
        $this->enhancedAuthenticateMapper = $enhancedAuthenticateMapper;
57
        //set options
58 21
        $this->setOptions($options);
59 21
    }
60
    
61
    /**
62
     * Return how many attemps are left for incorrect password.
63
     *
64
     * @param string $userName
65
     *
66
     * @return int
67
     */
68 21
    public function getAttemptsLeftWithSameUser(string $userName) : int
69
    {
70 21
        $attemptsLeft = $this->options['maxAttemptsForUserName'] - $this->enhancedAuthenticateMapper->fetchAttemptsWithSameUser($userName, $this->options['banTimeInSeconds']);
71
72 21
        return ($attemptsLeft < 0) ? 0 : $attemptsLeft;
73
    }
74
    
75
    /**
76
     * Return how many attemps are left for same session id.
77
     *
78
     * @param string $sessionId
79
     *
80
     * @return int
81
     */
82 21
    public function getAttemptsLeftWithSameSession(string $sessionId) : int
83
    {
84 21
        $attemptsLeft = $this->options['maxAttemptsForSessionId'] - $this->enhancedAuthenticateMapper->fetchAttemptsWithSameSession($sessionId, $this->options['banTimeInSeconds']);
85
86 21
        return ($attemptsLeft < 0) ? 0 : $attemptsLeft;
87
    }
88
    
89
    /**
90
     * Return how many attemps are left for same ip.
91
     *
92
     * @param string $ipAddress
93
     *
94
     * @return int
95
     */
96 21
    public function getAttemptsLeftWithSameIp(string $ipAddress) : int
97
    {
98 21
        $attemptsLeft = $this->options['maxAttemptsForIpAddress'] - $this->enhancedAuthenticateMapper->fetchAttemptsWithSameIp($ipAddress, $this->options['banTimeInSeconds']);
99
100 21
        return ($attemptsLeft < 0) ? 0 : $attemptsLeft;
101
    }
102
    
103
    /**
104
     * Check if an user is banned from do login.
105
     *
106
     * @param string $userName
107
     *
108
     * @return bool
109
     */
110 21
    public function isUserBanned(string $userName) : bool
111
    {
112 21
        return !$this->getAttemptsLeftWithSameUser($userName);
113
    }
114
    
115
    /**
116
     * Check if a session id is banned from do login.
117
     *
118
     * @param string $sessionId
119
     *
120
     * @return bool
121
     */
122 21
    public function isSessionBanned(string $sessionId) : bool
123
    {
124 21
        return !$this->getAttemptsLeftWithSameSession($sessionId);
125
    }
126
    
127
    /**
128
     * Check if an ip address is banned from do login.
129
     *
130
     * @param string $ipAddress
131
     *
132
     * @return bool
133
     */
134 21
    public function isIpBanned(string $ipAddress) : bool
135
    {
136 21
        return !$this->getAttemptsLeftWithSameIp($ipAddress);
137
    }
138
}
139