Skip to content

Commit 5caa164

Browse files
committed
Merge branch 'feature/issue#46' into develop
2 parents 8370f4b + 3b58636 commit 5caa164

18 files changed

Lines changed: 367 additions & 23 deletions

appinfo/routes.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@
6161
"url" => "/settings/autocomplete/table/group",
6262
"verb" => "POST"
6363
],
64+
[
65+
"name" => "settings#cryptoParams",
66+
"url" => "/settings/crypto/params",
67+
"verb" => "GET"
68+
],
6469
]
6570
]
6671
);

css/settings.css

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@
3333
float: right;
3434
}
3535

36+
#user_sql .main .inner-fieldset {
37+
border-bottom: 1px solid var(--color-border);
38+
border-top: 1px solid var(--color-border);
39+
margin: 8px 0;
40+
padding: 8px 0 8px 16px;
41+
}
42+
3643
#user_sql .msg {
3744
left: 0;
3845
padding: 3px;
@@ -43,16 +50,23 @@
4350
}
4451

4552
#user_sql .msg.error {
46-
background-color: #d2322d;
47-
color: #fff;
53+
background-color: var(--color-error);
54+
color: var(--color-primary-text);
4855
}
4956

5057
#user_sql .msg.success {
51-
background-color: #47a447;
52-
color: #fff;
58+
background-color: var(--color-success);
59+
color: var(--color-primary-text);
5360
}
5461

5562
#user_sql .msg.waiting {
56-
background-color: #ff8f00;
57-
color: #fff;
58-
}
63+
background-color: var(--color-warning);
64+
color: var(--color-primary-text);
65+
}
66+
67+
#user_sql .loading {
68+
display: inline-block;
69+
height: 32px;
70+
margin: 5px 0;
71+
width: 32px;
72+
}

js/settings.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,49 @@ user_sql.adminSettingsUI = function () {
5858
});
5959
};
6060

61+
var cryptoParams = function () {
62+
var cryptoChanged = function () {
63+
var content = $("#opt-crypto_params_content");
64+
var loading = $("#opt-crypto_params_loading");
65+
66+
content.hide();
67+
loading.show();
68+
69+
$.get(OC.generateUrl("/apps/user_sql/settings/crypto/params"), {cryptoClass: $("#opt-crypto_class").val()},
70+
function (data) {
71+
content.empty();
72+
loading.hide();
73+
74+
if (data.status === "success") {
75+
for (var index = 0, length = data.data.length; index < length; ++index) {
76+
var param = $("<div></div>");
77+
var label = $("<label></label>").attr({for: "opt-crypto_param_" + index});
78+
var title = $("<span></span>").text(data.data[index]["name"]);
79+
var input = $("<input/>").attr({
80+
type: "number",
81+
id: "opt-crypto_param_" + index,
82+
name: "opt-crypto_param_" + index,
83+
step: 1,
84+
min: data.data[index]["min"],
85+
max: data.data[index]["max"],
86+
value: data.data[index]["value"]
87+
});
88+
89+
label.append(title);
90+
param.append(label);
91+
param.append(input);
92+
content.append(param);
93+
content.show();
94+
}
95+
}
96+
}, "json");
97+
};
98+
$("#opt-crypto_class").change(function () {
99+
cryptoChanged();
100+
});
101+
cryptoChanged();
102+
};
103+
61104
$("#user_sql-db_connection_verify").click(function (event) {
62105
return click(event, "/apps/user_sql/settings/db/verify");
63106
});
@@ -89,11 +132,13 @@ user_sql.adminSettingsUI = function () {
89132
"#db-table-group-column-admin, #db-table-group-column-name, #db-table-group-column-gid",
90133
"/apps/user_sql/settings/autocomplete/table/group"
91134
);
135+
136+
cryptoParams();
92137
}
93138
};
94139

95140
$(document).ready(function () {
96141
if ($(form_id)) {
97142
user_sql.adminSettingsUI();
98143
}
99-
});
144+
});

lib/Backend/UserBackend.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,12 @@ public function checkPassword(string $uid, string $password)
343343
private function getPasswordAlgorithm()
344344
{
345345
$cryptoType = $this->properties[Opt::CRYPTO_CLASS];
346-
$passwordAlgorithm = new $cryptoType($this->localization);
346+
$cryptoParam0 = $this->properties[Opt::CRYPTO_PARAM_0];
347+
$cryptoParam1 = $this->properties[Opt::CRYPTO_PARAM_1];
348+
$cryptoParam2 = $this->properties[Opt::CRYPTO_PARAM_2];
349+
$passwordAlgorithm = new $cryptoType(
350+
$this->localization, $cryptoParam0, $cryptoParam1, $cryptoParam2
351+
);
347352

348353
if ($passwordAlgorithm === null) {
349354
$this->logger->error(

lib/Constant/Opt.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ final class Opt
3131
const APPEND_SALT = "opt.append_salt";
3232
const CASE_INSENSITIVE_USERNAME = "opt.case_insensitive_username";
3333
const CRYPTO_CLASS = "opt.crypto_class";
34+
const CRYPTO_PARAM_0 = "opt.crypto_param_0";
35+
const CRYPTO_PARAM_1 = "opt.crypto_param_1";
36+
const CRYPTO_PARAM_2 = "opt.crypto_param_2";
3437
const EMAIL_SYNC = "opt.email_sync";
3538
const HOME_LOCATION = "opt.home_location";
3639
const HOME_MODE = "opt.home_mode";

lib/Controller/SettingsController.php

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828
use OC\DB\ConnectionFactory;
2929
use OCA\UserSQL\Cache;
3030
use OCA\UserSQL\Constant\App;
31+
use OCA\UserSQL\Constant\Opt;
32+
use OCA\UserSQL\Crypto\IPasswordAlgorithm;
3133
use OCA\UserSQL\Platform\PlatformFactory;
3234
use OCA\UserSQL\Properties;
3335
use OCP\AppFramework\Controller;
3436
use OCP\IL10N;
3537
use OCP\ILogger;
3638
use OCP\IRequest;
39+
use ReflectionClass;
40+
use ReflectionException;
3741

3842
/**
3943
* The settings controller.
@@ -72,7 +76,8 @@ class SettingsController extends Controller
7276
public function __construct(
7377
$appName, IRequest $request, ILogger $logger, IL10N $localization,
7478
Properties $properties, Cache $cache
75-
) {
79+
)
80+
{
7681
parent::__construct($appName, $request);
7782
$this->appName = $appName;
7883
$this->logger = $logger;
@@ -193,6 +198,16 @@ public function saveProperties()
193198
];
194199
}
195200

201+
if (!$this->validateCryptoParams()) {
202+
return [
203+
"status" => "error", "data" => [
204+
"message" => $this->localization->t(
205+
"Hash algorithm parameter is out of range."
206+
)
207+
]
208+
];
209+
}
210+
196211
foreach ($properties as $key => $value) {
197212
$reqValue = $this->request->getParam(str_replace(".", "-", $key));
198213
$appValue = $this->properties[$key];
@@ -208,6 +223,9 @@ public function saveProperties()
208223
"Property '$key' has been set to: " . $value,
209224
["app" => $this->appName]
210225
);
226+
} elseif (!is_bool($appValue) && !isset($reqValue)) {
227+
unset($this->properties[$key]);
228+
211229
}
212230
}
213231

@@ -225,6 +243,48 @@ public function saveProperties()
225243
];
226244
}
227245

246+
/**
247+
* Validate request crypto params.
248+
*
249+
* @return bool TRUE if crypto params are correct FALSE otherwise.
250+
*/
251+
private function validateCryptoParams()
252+
{
253+
$cryptoClass = $this->request->getParam("opt-crypto_class");
254+
$configuration = $this->cryptoClassConfiguration($cryptoClass);
255+
256+
for ($i = 0; $i < count($configuration); ++$i) {
257+
$reqParam = $this->request->getParam(
258+
"opt-crypto_param_" . $i, null
259+
);
260+
$cryptoParam = $configuration[$i];
261+
262+
if (is_null($reqParam) || $reqParam < $cryptoParam->min
263+
|| $reqParam > $cryptoParam->max
264+
) {
265+
return false;
266+
}
267+
}
268+
269+
return true;
270+
}
271+
272+
/**
273+
* Get a crypto class configuration from request.
274+
*
275+
* @param $cryptoClass string Crypto class name.
276+
*
277+
* @return array A crypto class configuration.
278+
*/
279+
private function cryptoClassConfiguration($cryptoClass)
280+
{
281+
/**
282+
* @var $passwordAlgorithm IPasswordAlgorithm
283+
*/
284+
$passwordAlgorithm = new $cryptoClass($this->localization);
285+
return $passwordAlgorithm->configuration();
286+
}
287+
228288
/**
229289
* Clear the application cache memory.
230290
*
@@ -367,4 +427,40 @@ public function groupTableAutocomplete()
367427

368428
return $columns;
369429
}
430+
431+
/**
432+
* Get parameters for a password algorithm.
433+
*
434+
* @return array Password algorithm parameters.
435+
* @throws ReflectionException Whenever Opt class cannot be initiated.
436+
*/
437+
public function cryptoParams()
438+
{
439+
$this->logger->debug(
440+
"Entering cryptoParams()", ["app" => $this->appName]
441+
);
442+
443+
$cryptoClass = $this->request->getParam("cryptoClass");
444+
$configuration = $this->cryptoClassConfiguration($cryptoClass);
445+
446+
if ($cryptoClass === $this->properties[Opt::CRYPTO_CLASS]) {
447+
foreach ($configuration as $key => $value) {
448+
$opt = new ReflectionClass("OCA\UserSQL\Constant\Opt");
449+
$param = $this->properties[$opt->getConstant(
450+
"CRYPTO_PARAM_" . $key
451+
)];
452+
453+
if (!is_null($param)) {
454+
$value->value = $param;
455+
}
456+
}
457+
}
458+
459+
$this->logger->debug(
460+
"Returning cryptoParams(): count(" . count($configuration) . ")",
461+
["app" => $this->appName]
462+
);
463+
464+
return ["status" => "success", "data" => (array)$configuration];
465+
}
370466
}

lib/Crypto/AbstractAlgorithm.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,12 @@ public function checkPassword($password, $dbHash, $salt = null)
7474
* @inheritdoc
7575
*/
7676
public abstract function getPasswordHash($password, $salt = null);
77+
78+
/**
79+
* @inheritdoc
80+
*/
81+
public function configuration()
82+
{
83+
return [];
84+
}
7785
}

lib/Crypto/CryptArgon2.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,23 @@ public function getPasswordHash($password, $salt = null)
100100
);
101101
}
102102

103+
/**
104+
* @inheritdoc
105+
*/
106+
public function configuration()
107+
{
108+
return [
109+
new CryptoParam(
110+
"Memory cost (KiB)", PASSWORD_ARGON2_DEFAULT_MEMORY_COST, 1,
111+
1048576
112+
),
113+
new CryptoParam(
114+
"Time cost", PASSWORD_ARGON2_DEFAULT_TIME_COST, 1, 1024
115+
),
116+
new CryptoParam("Threads", PASSWORD_ARGON2_DEFAULT_THREADS, 1, 1024)
117+
];
118+
}
119+
103120
/**
104121
* @inheritdoc
105122
*/

lib/Crypto/CryptBlowfish.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ public function getPasswordHash($password, $salt = null)
6767
);
6868
}
6969

70+
/**
71+
* @inheritdoc
72+
*/
73+
public function configuration()
74+
{
75+
return [new CryptoParam("Cost", 10, 4, 31)];
76+
}
77+
7078
/**
7179
* Get the algorithm name.
7280
*

lib/Crypto/CryptExtendedDES.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ public function __construct(IL10N $localization, $iterationCount = 1000)
4848
$this->iterationCount = $iterationCount;
4949
}
5050

51+
/**
52+
* @inheritdoc
53+
*/
54+
public function configuration()
55+
{
56+
return [new CryptoParam("Iterations", 1000, 0, 16777215)];
57+
}
58+
5159
/**
5260
* @inheritdoc
5361
*/

0 commit comments

Comments
 (0)