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