|
1
|
|
|
<?php
|
|
2
|
|
|
/**
|
|
3
|
|
|
|
|
4
|
|
|
*
|
|
5
|
|
|
* @author Semih Serhat Karakaya
|
|
6
|
|
|
* @copyright Copyright (c) 2017, ownCloud GmbH
|
|
7
|
|
|
* @license AGPL-3.0
|
|
8
|
|
|
*
|
|
9
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
10
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
11
|
|
|
* as published by the Free Software Foundation.
|
|
12
|
|
|
*
|
|
13
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16
|
|
|
* GNU Affero General Public License for more details.
|
|
17
|
|
|
*
|
|
18
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
19
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
20
|
|
|
*
|
|
21
|
|
|
*/
|
|
22
|
|
|
|
|
23
|
|
|
namespace OCA\Security\Db;
|
|
24
|
|
|
|
|
25
|
|
|
use OC\AppFramework\Utility\TimeFactory;
|
|
26
|
|
|
use OCA\Security\SecurityConfig;
|
|
27
|
|
|
use OCP\IDBConnection;
|
|
28
|
|
|
|
|
29
|
|
|
/**
|
|
30
|
|
|
* Class DBService
|
|
31
|
|
|
* @package OCA\Security\Db
|
|
32
|
|
|
*/
|
|
33
|
|
|
class DbService {
|
|
34
|
|
|
|
|
35
|
|
|
/**
|
|
36
|
|
|
* @var IDBConnection
|
|
37
|
|
|
*/
|
|
38
|
|
|
private $connection;
|
|
39
|
|
|
|
|
40
|
|
|
/**
|
|
41
|
|
|
* @var TimeFactory
|
|
42
|
|
|
*/
|
|
43
|
|
|
private $factory;
|
|
44
|
|
|
/**
|
|
45
|
|
|
* @var SecurityConfig
|
|
46
|
|
|
*/
|
|
47
|
|
|
private $config;
|
|
48
|
|
|
|
|
49
|
|
|
/**
|
|
50
|
|
|
* DBService constructor.
|
|
51
|
|
|
*
|
|
52
|
|
|
* @param IDBConnection $connection
|
|
53
|
|
|
* @param TimeFactory $factory
|
|
54
|
|
|
* @param SecurityConfig $config
|
|
55
|
|
|
*/
|
|
56
|
|
|
public function __construct(IDBConnection $connection, TimeFactory $factory, SecurityConfig $config) {
|
|
57
|
|
|
$this->connection = $connection;
|
|
58
|
|
|
$this->factory = $factory;
|
|
59
|
|
|
$this->config = $config;
|
|
60
|
|
|
}
|
|
61
|
|
|
|
|
62
|
|
|
/**
|
|
63
|
|
|
* @param string $uid
|
|
64
|
|
|
*/
|
|
65
|
|
|
public function addFailedLoginAttempt($uid, $ip){
|
|
66
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
67
|
|
|
$builder->insert('failed_login_attempts')
|
|
68
|
|
|
->setValue('ip', $builder->createNamedParameter($ip))
|
|
69
|
|
|
->setValue('uid', $builder->createNamedParameter($uid))
|
|
70
|
|
|
->setValue('attempted_at', $builder->createNamedParameter($this->factory->getTime()))
|
|
71
|
|
|
->execute();
|
|
72
|
|
|
}
|
|
73
|
|
|
|
|
74
|
|
|
/**
|
|
75
|
|
|
* @param string $uid
|
|
76
|
|
|
* @param string $ip
|
|
77
|
|
|
* @return int
|
|
78
|
|
|
*/
|
|
79
|
|
|
public function getSuspiciousActivityCountForUidIpCombination($uid, $ip) {
|
|
80
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
81
|
|
|
$thresholdTime = (new \DateTime())->modify("-". $this->config->getBruteForceProtectionTimeThreshold() . "second")->getTimestamp();
|
|
82
|
|
|
$attempts = $builder->selectAlias($builder->createFunction('COUNT(*)'), 'count')
|
|
83
|
|
|
->from('failed_login_attempts')
|
|
84
|
|
|
->where($builder->expr()->gt('attempted_at', $builder->createNamedParameter($thresholdTime)))
|
|
85
|
|
|
->andWhere($builder->expr()->eq('uid', $builder->createNamedParameter($uid)))
|
|
86
|
|
|
->andWhere($builder->expr()->eq('ip', $builder->createNamedParameter($ip)))
|
|
87
|
|
|
->execute()
|
|
88
|
|
|
->fetch();
|
|
89
|
|
|
return intval($attempts['count']);
|
|
90
|
|
|
}
|
|
91
|
|
|
|
|
92
|
|
|
/**
|
|
93
|
|
|
* @param string $uid
|
|
94
|
|
|
* @return int
|
|
95
|
|
|
*/
|
|
96
|
|
View Code Duplication |
public function getSuspiciousActivityCountForUid($uid) {
|
|
|
|
|
|
|
97
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
98
|
|
|
$thresholdTime = (new \DateTime())->modify("-". $this->config->getBruteForceProtectionTimeThreshold() . "second")->getTimestamp();
|
|
99
|
|
|
$attempts = $builder->selectAlias($builder->createFunction('COUNT(*)'), 'count')
|
|
100
|
|
|
->from('failed_login_attempts')
|
|
101
|
|
|
->where($builder->expr()->gt('attempted_at', $builder->createNamedParameter($thresholdTime)))
|
|
102
|
|
|
->andWhere($builder->expr()->eq('uid', $builder->createNamedParameter($uid)))
|
|
103
|
|
|
->execute()
|
|
104
|
|
|
->fetch();
|
|
105
|
|
|
return intval($attempts['count']);
|
|
106
|
|
|
}
|
|
107
|
|
|
|
|
108
|
|
|
/**
|
|
109
|
|
|
* @param string $ip
|
|
110
|
|
|
* @return int
|
|
111
|
|
|
*/
|
|
112
|
|
View Code Duplication |
public function getSuspiciousActivityCountForIp($ip) {
|
|
|
|
|
|
|
113
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
114
|
|
|
$thresholdTime = (new \DateTime())->modify("-". $this->config->getBruteForceProtectionTimeThreshold() . "second")->getTimestamp();
|
|
115
|
|
|
$attempts = $builder->selectAlias($builder->createFunction('COUNT(*)'), 'count')
|
|
116
|
|
|
->from('failed_login_attempts')
|
|
117
|
|
|
->where($builder->expr()->gt('attempted_at', $builder->createNamedParameter($thresholdTime)))
|
|
118
|
|
|
->andWhere($builder->expr()->eq('ip', $builder->createNamedParameter($ip)))
|
|
119
|
|
|
->execute()
|
|
120
|
|
|
->fetch();
|
|
121
|
|
|
return intval($attempts['count']);
|
|
122
|
|
|
}
|
|
123
|
|
|
|
|
124
|
|
|
/**
|
|
125
|
|
|
* @param string $ip
|
|
126
|
|
|
* @return int
|
|
127
|
|
|
*/
|
|
128
|
|
View Code Duplication |
public function getLastFailedLoginAttemptTimeForIp($ip) {
|
|
|
|
|
|
|
129
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
130
|
|
|
$thresholdTime = (new \DateTime())->modify("-". $this->config->getBruteForceProtectionTimeThreshold() . "second")->getTimestamp();
|
|
131
|
|
|
$lastAttempt = $builder->select('attempted_at')
|
|
132
|
|
|
->from('failed_login_attempts')
|
|
133
|
|
|
->where($builder->expr()->gt('attempted_at', $builder->createNamedParameter($thresholdTime)))
|
|
134
|
|
|
->andWhere($builder->expr()->eq('ip', $builder->createNamedParameter($ip)))
|
|
135
|
|
|
->orderBy('attempted_at','DESC')
|
|
136
|
|
|
->setMaxResults(1)
|
|
137
|
|
|
->execute()
|
|
138
|
|
|
->fetch();
|
|
139
|
|
|
return intval($lastAttempt['attempted_at']);
|
|
140
|
|
|
}
|
|
141
|
|
|
|
|
142
|
|
|
/**
|
|
143
|
|
|
* @param string $ip
|
|
144
|
|
|
*/
|
|
145
|
|
|
public function deleteSuspiciousAttemptsForIp($ip) {
|
|
146
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
147
|
|
|
$builder->delete('failed_login_attempts')
|
|
148
|
|
|
->where($builder->expr()->eq('ip',$builder->createNamedParameter($ip)))
|
|
149
|
|
|
->execute();
|
|
150
|
|
|
}
|
|
151
|
|
|
|
|
152
|
|
|
/**
|
|
153
|
|
|
* @param string $uid
|
|
154
|
|
|
* @param string $ip
|
|
155
|
|
|
*/
|
|
156
|
|
|
public function deleteSuspiciousAttemptsForUidIpCombination($uid, $ip) {
|
|
157
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
158
|
|
|
$builder->delete('failed_login_attempts')
|
|
159
|
|
|
->where($builder->expr()->eq('uid',$builder->createNamedParameter($uid)))
|
|
160
|
|
|
->andWhere($builder->expr()->eq('ip', $builder->createNamedParameter($ip)))
|
|
161
|
|
|
->execute();
|
|
162
|
|
|
}
|
|
163
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.