Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/password_policy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorRoeland Jago Douma <roeland@famdouma.nl>2018-03-19 12:09:04 +0300
committerRoeland Jago Douma <roeland@famdouma.nl>2018-03-19 12:09:04 +0300
commit631afc808009da31c11034add25b8cc1e10aa8a2 (patch)
tree81275ad4ad11000156bb4885d358c7ad2582e0f2 /lib
parent6a96d59965d9fdba141b535a068b787ec635330e (diff)
Add generator
This can generate password that pass the password policy Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'lib')
-rw-r--r--lib/Generator.php96
1 files changed, 96 insertions, 0 deletions
diff --git a/lib/Generator.php b/lib/Generator.php
new file mode 100644
index 0000000..1d4dd5a
--- /dev/null
+++ b/lib/Generator.php
@@ -0,0 +1,96 @@
+<?php
+declare(strict_types=1);
+/**
+ * @copyright Copyright (c) 2018, Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace OCA\Password_Policy;
+
+use OC\HintException;
+use OCP\Security\ISecureRandom;
+
+class Generator {
+
+ /** @var PasswordPolicyConfig */
+ private $config;
+
+ /** @var PasswordValidator */
+ private $validator;
+
+ /** @var ISecureRandom */
+ private $random;
+
+ public function __construct(PasswordPolicyConfig $config,
+ PasswordValidator $validator,
+ ISecureRandom $random) {
+ $this->config = $config;
+ $this->validator = $validator;
+ $this->random = $random;
+ }
+
+ public function generate(): string {
+
+ $lenght = $this->config->getMinLength();
+
+ $password = '';
+ $chars = '';
+
+ while (true) {
+ if ($this->config->getEnforceUpperLowerCase()) {
+ $password .= $this->random->generate(1, ISecureRandom::CHAR_UPPER);
+ $password .= $this->random->generate(1, ISecureRandom::CHAR_LOWER);
+ $lenght -= 2;
+ $chars .= ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER;
+ }
+
+ if ($this->config->getEnforceNumericCharacters()) {
+ $password .= $this->random->generate(1, ISecureRandom::CHAR_DIGITS);
+ $lenght -= 1;
+ $chars .= ISecureRandom::CHAR_DIGITS;
+ }
+
+ if ($this->config->getEnforceSpecialCharacters()) {
+ $password .= $this->random->generate(1, ISecureRandom::CHAR_SYMBOLS);
+ $lenght -= 1;
+ $chars .= ISecureRandom::CHAR_SYMBOLS;
+ }
+
+ if ($chars === '') {
+ $chars = ISecureRandom::CHAR_HUMAN_READABLE;
+ }
+
+ $password .= $chars = $this->random->generate($lenght, $chars);
+
+ try {
+ $this->validator->validate($password);
+ break;
+ } catch (HintException $e) {
+ /*
+ * Invalid so lets go for another round
+ * Reset the length so we don't run below zero
+ */
+ $lenght = $this->config->getMinLength();
+ }
+ }
+
+ return $password;
+ }
+}