|
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 OCP\IDBConnection;
|
|
27
|
|
|
|
|
28
|
|
|
/**
|
|
29
|
|
|
* Class DBService
|
|
30
|
|
|
* @package OCA\Security\Db
|
|
31
|
|
|
*/
|
|
32
|
|
|
class DbService {
|
|
33
|
|
|
|
|
34
|
|
|
/**
|
|
35
|
|
|
* @var IDBConnection
|
|
36
|
|
|
*/
|
|
37
|
|
|
private $connection;
|
|
38
|
|
|
|
|
39
|
|
|
/**
|
|
40
|
|
|
* @var TimeFactory
|
|
41
|
|
|
*/
|
|
42
|
|
|
private $factory;
|
|
43
|
|
|
|
|
44
|
|
|
/**
|
|
45
|
|
|
* DBService constructor.
|
|
46
|
|
|
*
|
|
47
|
|
|
* @param IDBConnection $connection
|
|
48
|
|
|
*/
|
|
49
|
|
|
public function __construct(IDBConnection $connection, TimeFactory $factory) {
|
|
50
|
|
|
$this->connection = $connection;
|
|
51
|
|
|
$this->factory = $factory;
|
|
52
|
|
|
}
|
|
53
|
|
|
|
|
54
|
|
|
/**
|
|
55
|
|
|
* @param string $uid
|
|
56
|
|
|
*/
|
|
57
|
|
|
public function addFailedLoginAttempt($uid, $ip){
|
|
58
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
59
|
|
|
$builder->insert('failed_login_attempts')
|
|
60
|
|
|
->setValue('ip', $builder->createNamedParameter($ip))
|
|
61
|
|
|
->setValue('uid', $builder->createNamedParameter($uid))
|
|
62
|
|
|
->setValue('attempted_at', $builder->createNamedParameter($this->factory->getTime()))
|
|
63
|
|
|
->execute();
|
|
64
|
|
|
}
|
|
65
|
|
|
|
|
66
|
|
|
/**
|
|
67
|
|
|
* @param string $uid
|
|
68
|
|
|
* @return int
|
|
69
|
|
|
*/
|
|
70
|
|
View Code Duplication |
public function getSuspiciousActivityCountForUid($uid) {
|
|
|
|
|
|
|
71
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
72
|
|
|
$thresholdTime = (new \DateTime())->modify("-86400 second")->getTimestamp();
|
|
73
|
|
|
$attempts = $builder->select('*')
|
|
74
|
|
|
->from('failed_login_attempts')
|
|
75
|
|
|
->where($builder->expr()->gt('attempted_at', $builder->createNamedParameter($thresholdTime)))
|
|
76
|
|
|
->andWhere($builder->expr()->eq('uid', $builder->createNamedParameter($uid)))
|
|
77
|
|
|
->execute()
|
|
78
|
|
|
->fetchAll();
|
|
79
|
|
|
return count($attempts);
|
|
80
|
|
|
}
|
|
81
|
|
|
|
|
82
|
|
|
/**
|
|
83
|
|
|
* @param string $ip
|
|
84
|
|
|
* @return int
|
|
85
|
|
|
*/
|
|
86
|
|
View Code Duplication |
public function getSuspiciousActivityCountForIp($ip) {
|
|
|
|
|
|
|
87
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
88
|
|
|
$thresholdTime = (new \DateTime())->modify("-86400 second")->getTimestamp();
|
|
89
|
|
|
$attempts = $builder->select('*')
|
|
90
|
|
|
->from('failed_login_attempts')
|
|
91
|
|
|
->where($builder->expr()->gt('attempted_at', $builder->createNamedParameter($thresholdTime)))
|
|
92
|
|
|
->andWhere($builder->expr()->eq('ip', $builder->createNamedParameter($ip)))
|
|
93
|
|
|
->execute()
|
|
94
|
|
|
->fetchAll();
|
|
95
|
|
|
return count($attempts);
|
|
96
|
|
|
}
|
|
97
|
|
|
|
|
98
|
|
|
public function deleteSuspiciousAttemptsForIp($ip) {
|
|
99
|
|
|
$builder = $this->connection->getQueryBuilder();
|
|
100
|
|
|
$builder->delete('failed_login_attempts')
|
|
101
|
|
|
->where($builder->expr()->eq('ip',$builder->createNamedParameter($ip)))
|
|
102
|
|
|
->execute();
|
|
103
|
|
|
}
|
|
104
|
|
|
} |
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.