You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebAuthnController.php 3.5KB

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