Passed
Push — master ( f350f2...411d2d )
by Morris
12:45
created

PasswordConfirmationMiddleware::beforeController()   B

Complexity

Conditions 7
Paths 8

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 12
nc 8
nop 2
dl 0
loc 19
rs 8.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright 2018, Roeland Jago Douma <[email protected]>
4
 *
5
 * @author Roeland Jago Douma <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
namespace OC\AppFramework\Middleware\Security;
24
25
use OC\AppFramework\Middleware\Security\Exceptions\NotConfirmedException;
26
use OC\AppFramework\Utility\ControllerMethodReflector;
27
use OCP\AppFramework\Controller;
28
use OCP\AppFramework\Middleware;
29
use OCP\AppFramework\Utility\ITimeFactory;
30
use OCP\ISession;
31
use OCP\IUserSession;
32
use OCP\User\Backend\IPasswordConfirmationBackend;
33
34
class PasswordConfirmationMiddleware extends Middleware {
35
	/** @var ControllerMethodReflector */
36
	private $reflector;
37
	/** @var ISession */
38
	private $session;
39
	/** @var IUserSession */
40
	private $userSession;
41
	/** @var ITimeFactory */
42
	private $timeFactory;
43
	/** @var array */
44
	private $excludedUserBackEnds = ['user_saml' => true, 'user_globalsiteselector' => true];
45
46
	/**
47
	 * PasswordConfirmationMiddleware constructor.
48
	 *
49
	 * @param ControllerMethodReflector $reflector
50
	 * @param ISession $session
51
	 * @param IUserSession $userSession
52
	 * @param ITimeFactory $timeFactory
53
	 */
54
	public function __construct(ControllerMethodReflector $reflector,
55
								ISession $session,
56
								IUserSession $userSession,
57
								ITimeFactory $timeFactory) {
58
		$this->reflector = $reflector;
59
		$this->session = $session;
60
		$this->userSession = $userSession;
61
		$this->timeFactory = $timeFactory;
62
	}
63
64
	/**
65
	 * @param Controller $controller
66
	 * @param string $methodName
67
	 * @throws NotConfirmedException
68
	 */
69
	public function beforeController($controller, $methodName) {
70
		if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) {
71
			$user = $this->userSession->getUser();
72
			$backendClassName = '';
73
			if ($user !== null) {
74
				$backend = $user->getBackend();
75
				if ($backend instanceof IPasswordConfirmationBackend) {
76
					if (!$backend->canConfirmPassword($user->getUID())) {
77
						return;
78
					}
79
				}
80
81
				$backendClassName = $user->getBackendClassName();
82
			}
83
84
			$lastConfirm = (int) $this->session->get('last-password-confirm');
85
			// we can't check the password against a SAML backend, so skip password confirmation in this case
86
			if (!isset($this->excludedUserBackEnds[$backendClassName]) && $lastConfirm < ($this->timeFactory->getTime() - (30 * 60 + 15))) { // allow 15 seconds delay
87
				throw new NotConfirmedException();
88
			}
89
		}
90
	}
91
}
92