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

SecurityConfig::setBruteForceProtectionBanPeriod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * @author Semih Serhat Karakaya <[email protected]>
4
 * @copyright Copyright (c) 2017, ownCloud GmbH.
5
 * @license AGPL-3.0
6
 *
7
 * This code is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License, version 3,
9
 * as published by the Free Software Foundation.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License, version 3,
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
18
 */
19
20
namespace OCA\Security;
21
22
use OCP\IConfig;
23
/**
24
 * Class Config
25
 *
26
 * read/write config of the security policies
27
 *
28
 * @package OCA\Security
29
 */
30
class SecurityConfig {
31
32
    /** @var IConfig */
33
    private $config;
34
35
    /**
36
     * Config constructor.
37
     *
38
     * @param IConfig $config
39
     */
40
    public function __construct(IConfig $config) {
41
        $this->config = $config;
42
    }
43
44
    /**
45
     * is brute force protection enabled
46
     *
47
     * @return array
48
     */
49
    public function getAllSecurityConfigs() {
50
        return [
51
            'bruteForceProtectionFailTolerance' => $this->getBruteForceProtectionFailTolerance(),
52
            'bruteForceProtectionTimeThreshold' => $this->getBruteForceProtectionTimeThreshold(),
53
            'bruteForceProtectionBanPeriod' => $this->getBruteForceProtectionBanPeriod(),
54
            'minPasswordLength' => $this->getMinPasswordLength(),
55
            'isUpperLowerCaseEnforced' => $this->getIsUpperLowerCaseEnforced(),
56
            'isNumericalCharsEnforced' => $this->getIsNumericCharactersEnforced(),
57
            'isSpecialCharsEnforced' => $this->getIsSpecialCharactersEnforced()
58
        ];
59
    }
60
61
    /**
62
     * ban after how many failed attempts
63
     *
64
     * @return int
65
     */
66
    public function getBruteForceProtectionFailTolerance() {
67
        $tolerance = $this->config->getAppValue(
68
            'security',
69
            'brute_force_protection_fail_tolerance',
70
            '3'
71
        );
72
        return intval($tolerance);
73
    }
74
75
    /**
76
     * Count failed login attempts over how many seconds
77
     *
78
     * @return int
79
     */
80
    public function getBruteForceProtectionTimeThreshold() {
81
        $timeThreshold = $this->config->getAppValue(
82
            'security',
83
            'brute_force_protection_time_threshold',
84
            '600'
85
        );
86
        return intval($timeThreshold);
87
    }
88
89
    /**
90
     * How many seconds to ban an attacker
91
     *
92
     * @return int
93
     */
94
    public function getBruteForceProtectionBanPeriod() {
95
        $banPeriod = $this->config->getAppValue(
96
            'security',
97
            'brute_force_protection_ban_period',
98
            '300'
99
        );
100
        return intval($banPeriod);
101
    }
102
103
    /**
104
     * get the enforced minimum length of passwords
105
     *
106
     * @return int
107
     */
108
    public function getMinPasswordLength() {
109
        $minLength = $this->config->getAppValue('security', 'min_password_length', '8');
110
        return intval($minLength);
111
    }
112
    /**
113
     * does the password need to contain upper and lower case characters
114
     *
115
     * @return bool
116
     */
117
    public function getIsUpperLowerCaseEnforced() {
118
        $enforceUpperLowerCase = $this->config->getAppValue(
119
            'security',
120
            'enforce_upper_lower_case',
121
            '0'
122
        );
123
        return boolval($enforceUpperLowerCase);
124
    }
125
    /**
126
     * does the password need to contain numeric characters
127
     *
128
     * @return bool
129
     */
130
    public function getIsNumericCharactersEnforced() {
131
        $enforceNumericCharacters = $this->config->getAppValue(
132
            'security',
133
            'enforce_numeric_characters',
134
            '0'
135
        );
136
        return boolval($enforceNumericCharacters);
137
    }
138
    /**
139
     * does the password need to contain special characters
140
     *
141
     * @return bool
142
     */
143
    public function getIsSpecialCharactersEnforced() {
144
        $enforceSpecialCharacters = $this->config->getAppValue(
145
            'security',
146
            'enforce_special_characters',
147
            '0'
148
        );
149
        return boolval($enforceSpecialCharacters);
150
    }
151
    /*
152
     * @param int $attempts
153
     */
154
    public function setBruteForceProtectionFailTolerance($attempts) {
155
        $this->config->setAppValue('security', 'brute_force_protection_fail_tolerance', $attempts);
156
    }
157
    /**
158
     * @param int $seconds
159
     */
160
    public function setBruteForceProtectionTimeThreshold($seconds) {
161
        $this->config->setAppValue('security', 'brute_force_protection_time_threshold', $seconds);
162
    }
163
    /**
164
     * @param int $seconds
165
     */
166
    public function setBruteForceProtectionBanPeriod($seconds) {
167
        $this->config->setAppValue('security', 'brute_force_protection_ban_period', $seconds);
168
    }
169
    /**
170
     * set minimal length of passwords
171
     *
172
     * @param int $minLength
173
     */
174
    public function setMinPasswordLength($minLength) {
175
        $this->config->setAppValue('security', 'min_password_length', $minLength);
176
    }
177
    /**
178
     * enforce upper and lower characters case
179
     *
180
     * @param bool $enforceUpperLowerCase
181
     */
182
    public function setIsUpperLowerCaseEnforced($enforceUpperLowerCase) {
183
        $value = $enforceUpperLowerCase === true ? '1' : '0';
184
        $this->config->setAppValue('security', 'enforce_upper_lower_case', $value);
185
    }
186
    /**
187
     * enforce numeric characters
188
     *
189
     * @param bool $enforceNumericCharacters
190
     */
191
    public function setIsNumericCharactersEnforced($enforceNumericCharacters) {
192
        $value = $enforceNumericCharacters === true ? '1' : '0';
193
        $this->config->setAppValue('security', 'enforce_numeric_characters', $value);
194
    }
195
    /**
196
     * enforce special characters
197
     *
198
     * @param bool $enforceSpecialCharacters
199
     */
200
    public function setIsSpecialCharactersEnforced($enforceSpecialCharacters) {
201
        $value = $enforceSpecialCharacters === true ? '1' : '0';
202
        $this->config->setAppValue('security', 'enforce_special_characters', $value);
203
    }
204
}