|
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 $uid
|
|
79
|
|
|
* @param string $ip
|
|
80
|
|
|
* @throws LoginException
|
|
81
|
|
|
*/
|
|
82
|
|
|
public function applyBruteForcePolicy($uid ,$ip) {
|
|
83
|
|
|
$banPeriod = $this->config->getBruteForceProtectionBanPeriod();
|
|
84
|
|
|
$banUntil = $this->dbConnection->getLastFailedLoginAttemptTimeForIp($ip)+$banPeriod;
|
|
85
|
|
|
if($this->dbConnection->getSuspiciousActivityCountForUidIpCombination($uid, $ip) >=
|
|
86
|
|
|
$this->config->getBruteForceProtectionFailTolerance() &&
|
|
87
|
|
|
$banUntil > $this->timeFactory->getTime()) {
|
|
88
|
|
|
throw new LoginException($this->l->t("Too many failed login attempts. Try again in %s minutes.",
|
|
89
|
|
|
ceil($banPeriod/60))
|
|
90
|
|
|
);
|
|
91
|
|
|
}
|
|
92
|
|
|
}
|
|
93
|
|
|
|
|
94
|
|
|
/**
|
|
95
|
|
|
* @param string $ip
|
|
96
|
|
|
* @return void
|
|
97
|
|
|
*/
|
|
98
|
|
|
public function clearSuspiciousAttemptsForIp($ip) {
|
|
99
|
|
|
$this->dbConnection->deleteSuspiciousAttemptsForIp($ip);
|
|
100
|
|
|
}
|
|
101
|
|
|
|
|
102
|
|
|
/**
|
|
103
|
|
|
* @param string $ip
|
|
104
|
|
|
* @return void
|
|
105
|
|
|
*/
|
|
106
|
|
|
public function clearSuspiciousAttemptsForUidIpCombination($uid, $ip) {
|
|
107
|
|
|
$this->dbConnection->deleteSuspiciousAttemptsForUidIpCombination($uid, $ip);
|
|
108
|
|
|
}
|
|
109
|
|
|
} |