Completed
Push — master ( 7a29a1...33d37f )
by Thomas
01:49
created

Throttle   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 58
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A addFailedLoginAttempt() 0 3 1
A putDelay() 0 5 1
A calculateDelayForUid() 0 3 1
A calculateDelayForIp() 0 3 1
A clearSuspiciousAttemptsForIp() 0 3 1
1
<?php
2
/**
3
 * @author Semih Serhat Karakaya <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Security;
23
24
use OCP\IDBConnection;
25
26
/**
27
 * Class Throttle
28
 * @package OCA\Secuity\Lib
29
 */
30
class Throttle {
31
32
    /**
33
     * @var \OCA\Security\Db\DbService $connection
34
     */
35
    protected $dbConnection;
36
37
    /**
38
     * @param IDBConnection $dbConnection
39
     */
40
    public function __construct($dbConnection) {
41
        $this->dbConnection = $dbConnection;
0 ignored issues
show
Documentation Bug introduced by
It seems like $dbConnection of type object<OCP\IDBConnection> is incompatible with the declared type object<OCA\Security\Db\DbService> of property $dbConnection.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
    }
43
44
    /**
45
     * @param string $uid
46
     * @param string ip
47
     * @return void
48
     */
49
    public function addFailedLoginAttempt($uid, $ip) {
50
        $this->dbConnection->addFailedLoginAttempt($uid, $ip);
51
    }
52
53
    /**
54
     * @param string $uid
55
     * @param string ip
56
     * @return void
57
     */
58
    public function putDelay($uid, $ip) {
0 ignored issues
show
Unused Code introduced by
The parameter $uid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
59
        //we determine appropriate delay time by using ip and username info
60
        //initially it return count of failed attempts for ip
61
        sleep($this->calculateDelayForIp($ip));
62
    }
63
64
    /**
65
     * @param string $uid
66
     * @return int
67
     */
68
    public function calculateDelayForUid($uid) {
69
        return $this->dbConnection->getSuspiciousActivityCountForUid($uid);
70
    }
71
72
    /**
73
     * @param string ip
74
     * @return int
75
     */
76
    public function calculateDelayForIp($ip) {
77
        return $this->dbConnection->getSuspiciousActivityCountForIp($ip);
78
    }
79
80
    /**
81
     * @param string ip
82
     * @return void
83
     */
84
    public function clearSuspiciousAttemptsForIp($ip) {
85
        $this->dbConnection->deleteSuspiciousAttemptsForIp($ip);
86
    }
87
}