1<?php
2
3declare(strict_types = 1);
4
5/**
6 * Nextcloud - U2F 2FA
7 *
8 * This file is licensed under the Affero General Public License version 3 or
9 * later. See the COPYING file.
10 *
11 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
12 * @copyright Christoph Wurst 2018
13 */
14
15namespace OCA\TwoFactorU2F\Controller;
16
17require_once(__DIR__ . '/../../vendor/yubico/u2flib-server/src/u2flib_server/U2F.php');
18
19use OCA\TwoFactorU2F\Service\U2FManager;
20use OCP\AppFramework\Http\JSONResponse;
21use OCP\Authentication\TwoFactorAuth\ALoginSetupController;
22use OCP\IRequest;
23use OCP\IUserSession;
24
25class SettingsController extends ALoginSetupController {
26
27	/** @var U2FManager */
28	private $manager;
29
30	/** @var IUserSession */
31	private $userSession;
32
33	public function __construct(string $appName, IRequest $request, U2FManager $manager, IUserSession $userSession) {
34		parent::__construct($appName, $request);
35		$this->manager = $manager;
36		$this->userSession = $userSession;
37	}
38
39	/**
40	 * @NoAdminRequired
41	 */
42	public function state(): JSONResponse {
43		return new JSONResponse([
44			'devices' => $this->manager->getDevices($this->userSession->getUser())
45		]);
46	}
47
48	/**
49	 * @NoAdminRequired
50	 * @PasswordConfirmationRequired
51	 * @UseSession
52	 */
53	public function startRegister(): JSONResponse {
54		return new JSONResponse($this->manager->startRegistration($this->userSession->getUser()));
55	}
56
57	/**
58	 * @NoAdminRequired
59	 * @PasswordConfirmationRequired
60	 *
61	 * @param string $registrationData
62	 * @param string $clientData
63	 * @param string|null $name device name, given by user
64	 */
65	public function finishRegister(string $registrationData, string $clientData, string $name = null): JSONResponse {
66		return new JSONResponse($this->manager->finishRegistration($this->userSession->getUser(), $registrationData, $clientData, $name));
67	}
68
69	/**
70	 * @NoAdminRequired
71	 * @PasswordConfirmationRequired
72	 *
73	 * @param int $id
74	 */
75	public function remove(int $id): JSONResponse {
76		return new JSONResponse($this->manager->removeDevice($this->userSession->getUser(), $id));
77	}
78}
79