Completed
Push — master ( e4de0d...f99e79 )
by Thomas
11s
created

Throttle::applyBruteForcePolicy()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 2
nop 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 OC\User\LoginException;
25
use OCA\Security\Db\DbService;
26
use OCP\AppFramework\Utility\ITimeFactory;
27
use OCP\IL10N;
28
29
/**
30
 * Class Throttle
31
 * @package OCA\Secuity\Lib
32
 */
33
class Throttle {
34
35
    /**
36
     * @var \OCA\Security\Db\DbService $connection
37
     */
38
    protected $dbConnection;
39
40
    /**
41
     * @var SecurityConfig $config
42
     */
43
    protected $config;
44
45
    /**
46
     * @var IL10N $l
47
     */
48
    protected $l;
49
50
    /**
51
     * @var ITimeFactory $timeFactory
52
     */
53
    protected $timeFactory;
54
55
    /**
56
     * @param \OCA\Security\Db\DbService $dbConnection
57
     * @param SecurityConfig $config
58
     * @param IL10N $l
59
     * @param ITimeFactory $timeFactory
60
     */
61
    public function __construct(DbService $dbConnection, SecurityConfig $config, IL10N $l, ITimeFactory $timeFactory) {
62
        $this->dbConnection = $dbConnection;
63
        $this->config = $config;
64
        $this->l = $l;
65
        $this->timeFactory = $timeFactory;
66
    }
67
68
    /**
69
     * @param string $uid
70
     * @param string $ip
71
     * @return void
72
     */
73
    public function addFailedLoginAttempt($uid, $ip) {
74
        $this->dbConnection->addFailedLoginAttempt($uid, $ip);
75
    }
76
77
    /**
78
     * @param string $ip
79
     * @throws LoginException
80
     */
81
    public function applyBruteForcePolicy($ip) {
82
        $banPeriod = $this->config->getBruteForceProtectionBanPeriod();
83
        $banUntil = $this->dbConnection->getLastFailedLoginAttemptTimeForIp($ip)+$banPeriod;
84
        if($this->dbConnection->getSuspiciousActivityCountForIp($ip) >=
85
            $this->config->getBruteForceProtectionFailTolerance() &&
86
            $banUntil > $this->timeFactory->getTime()) {
87
            throw new LoginException($this->l->t("Too many failed login attempts. Try again in %s minutes.",
88
                ceil($banPeriod/60))
89
            );
90
        }
91
    }
92
93
    /**
94
     * @param string $ip
95
     * @return void
96
     */
97
    public function clearSuspiciousAttemptsForIp($ip) {
98
        $this->dbConnection->deleteSuspiciousAttemptsForIp($ip);
99
    }
100
}