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

HistoryCompliance.php « lib - github.com/nextcloud/password_policy.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 413f0b1fc5661b59e574ee3933329ab7b5c2eeaa (plain)
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
<?php
declare(strict_types=1);
/**
 * @copyright Copyright (c) 2020 Arthur Schiwon <blizzz@arthur-schiwon.de>
 *
 * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
 *
 * @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\IConfig;
use OCP\IL10N;
use OCP\ILogger;
use OCP\IUser;
use OCP\IUserSession;
use OCP\PreConditionNotMetException;
use OCP\Security\IHasher;

class HistoryCompliance {
	/** @var string */
	protected $uid;

	/** @var PasswordPolicyConfig */
	private $policyConfig;
	/** @var IConfig */
	private $config;
	/** @var IUserSession */
	private $session;
	/** @var IHasher */
	private $hasher;
	/** @var IL10N */
	private $l;
	/** @var ILogger */
	private $logger;

	public function __construct(
		PasswordPolicyConfig $policyConfig,
		IConfig $config,
		IUserSession $session,
		IHasher $hasher,
		IL10N $l,
		ILogger $logger
	) {
		$this->policyConfig = $policyConfig;
		$this->config = $config;
		$this->session = $session;
		$this->hasher = $hasher;
		$this->l = $l;
		$this->logger = $logger;
	}

	/**
	 * @throws HintException
	 */
	public function audit(IUser $user, string $password): void {
		if ($this->policyConfig->getHistorySize() === 0) {
			return;
		}

		$history = $this->getHistory($user);

		foreach ($history as $hash) {
			if ($this->hasher->verify($password, $hash)) {
				$message = 'Password must not have been used recently before.';
				$message_t = $this->l->t(
					'Password must not have been used recently before.'
				);
				throw new HintException($message, $message_t);
			}
		}
	}

	/**
	 * @throws PreConditionNotMetException
	 */
	public function update(IUser $user, string $password): void {
		$historySize = $this->policyConfig->getHistorySize();
		if($historySize === 0) {
			$this->config->deleteUserValue($user->getUID(), 'password_policy', 'passwordHistory');
			return;
		}

		$history = $this->getHistory($user);
		array_unshift($history, $this->hasher->hash($password));
		$history = \array_slice($history, 0, $historySize);

		$this->config->setUserValue(
			$user->getUID(),
			'password_policy',
			'passwordHistory',
			\json_encode($history)
		);
	}

	protected function getHistory(IUser $user): array {
		$history = $this->config->getUserValue(
			$user->getUID(),
			'password_policy',
			'passwordHistory',
			'[]'
		);
		$history = \json_decode($history, true);
		if (!is_array($history)) {
			$this->logger->warning(
				'Received password history of {uid} had the unexpected value of {history}, resetting.',
				['app' => 'password_policy', 'uid' => $user->getUID(), 'history' => $history]
			);
			$history = [];
		}
		$history = \array_slice($history, 0, $this->policyConfig->getHistorySize());

		return $history;
	}

}