Passed
Push — master ( b39fb5...590849 )
by Roeland
11:59 queued 10s
created

WebAuthnController::startRegistration()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
rs 10
1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2020, Roeland Jago Douma <[email protected]>
5
 *
6
 * @author Roeland Jago Douma <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
namespace OCA\Settings\Controller;
26
27
use OC\Authentication\WebAuthn\Manager;
28
use OCA\Settings\AppInfo\Application;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http;
31
use OCP\AppFramework\Http\JSONResponse;
32
use OCP\ILogger;
33
use OCP\IRequest;
34
use OCP\ISession;
35
use OCP\IUserSession;
36
use Webauthn\PublicKeyCredentialCreationOptions;
37
38
class WebAuthnController extends Controller {
39
40
	private const WEBAUTHN_REGISTRATION = 'webauthn_registration';
41
42
	/** @var Manager */
43
	private $manager;
44
45
	/** @var IUserSession */
46
	private $userSession;
47
	/**
48
	 * @var ISession
49
	 */
50
	private $session;
51
	/**
52
	 * @var ILogger
53
	 */
54
	private $logger;
55
56
	public function __construct(IRequest $request, ILogger $logger, Manager $webAuthnManager, IUserSession $userSession, ISession $session) {
57
		parent::__construct(Application::APP_ID, $request);
58
59
		$this->manager = $webAuthnManager;
60
		$this->userSession = $userSession;
61
		$this->session = $session;
62
		$this->logger = $logger;
63
	}
64
65
	/**
66
	 * @NoAdminRequired
67
	 * @PasswordConfirmationRequired
68
	 * @UseSession
69
	 * @NoCSRFRequired
70
	 */
71
	public function startRegistration(): JSONResponse {
72
		$this->logger->debug('Starting WebAuthn registration');
73
74
		$credentialOptions = $this->manager->startRegistration($this->userSession->getUser(), $this->request->getServerHost());
75
76
		// Set this in the session since we need it on finish
77
		$this->session->set(self::WEBAUTHN_REGISTRATION, $credentialOptions);
78
79
		return new JSONResponse($credentialOptions);
80
	}
81
82
	/**
83
	 * @NoAdminRequired
84
	 * @PasswordConfirmationRequired
85
	 * @UseSession
86
	 */
87
	public function finishRegistration(string $name, string $data): JSONResponse {
88
		$this->logger->debug('Finishing WebAuthn registration');
89
90
		if (!$this->session->exists(self::WEBAUTHN_REGISTRATION)) {
91
			$this->logger->debug('Trying to finish WebAuthn registration without session data');
92
			return new JSONResponse([], Http::STATUS_BAD_REQUEST);
93
		}
94
95
		// Obtain the publicKeyCredentialOptions from when we started the registration
96
		$publicKeyCredentialCreationOptions = PublicKeyCredentialCreationOptions::createFromArray($this->session->get(self::WEBAUTHN_REGISTRATION));
97
98
		$this->session->remove(self::WEBAUTHN_REGISTRATION);
99
100
		return new JSONResponse($this->manager->finishRegister($publicKeyCredentialCreationOptions, $name, $data));
101
	}
102
103
	/**
104
	 * @NoAdminRequired
105
	 * @PasswordConfirmationRequired
106
	 */
107
	public function deleteRegistration(int $id): JSONResponse {
108
		$this->logger->debug('Finishing WebAuthn registration');
109
110
		$this->manager->deleteRegistration($this->userSession->getUser(), $id);
111
112
		return new JSONResponse([]);
113
	}
114
}
115