1
0

WebAuthnController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Richard Steinmetz <richard@steinmetz.cloud>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Core\Controller;
  27. use OC\Authentication\Login\LoginData;
  28. use OC\Authentication\Login\WebAuthnChain;
  29. use OC\Authentication\WebAuthn\Manager;
  30. use OC\URLGenerator;
  31. use OCP\AppFramework\Controller;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Http\Attribute\UseSession;
  34. use OCP\AppFramework\Http\JSONResponse;
  35. use OCP\IRequest;
  36. use OCP\ISession;
  37. use OCP\Util;
  38. use Psr\Log\LoggerInterface;
  39. use Webauthn\PublicKeyCredentialRequestOptions;
  40. class WebAuthnController extends Controller {
  41. private const WEBAUTHN_LOGIN = 'webauthn_login';
  42. private const WEBAUTHN_LOGIN_UID = 'webauthn_login_uid';
  43. public function __construct(
  44. string $appName,
  45. IRequest $request,
  46. private Manager $webAuthnManger,
  47. private ISession $session,
  48. private LoggerInterface $logger,
  49. private WebAuthnChain $webAuthnChain,
  50. private URLGenerator $urlGenerator,
  51. ) {
  52. parent::__construct($appName, $request);
  53. }
  54. /**
  55. * @NoAdminRequired
  56. * @PublicPage
  57. */
  58. #[UseSession]
  59. public function startAuthentication(string $loginName): JSONResponse {
  60. $this->logger->debug('Starting WebAuthn login');
  61. $this->logger->debug('Converting login name to UID');
  62. $uid = $loginName;
  63. Util::emitHook(
  64. '\OCA\Files_Sharing\API\Server2Server',
  65. 'preLoginNameUsedAsUserName',
  66. ['uid' => &$uid]
  67. );
  68. $this->logger->debug('Got UID: ' . $uid);
  69. $publicKeyCredentialRequestOptions = $this->webAuthnManger->startAuthentication($uid, $this->request->getServerHost());
  70. $this->session->set(self::WEBAUTHN_LOGIN, json_encode($publicKeyCredentialRequestOptions));
  71. $this->session->set(self::WEBAUTHN_LOGIN_UID, $uid);
  72. return new JSONResponse($publicKeyCredentialRequestOptions);
  73. }
  74. /**
  75. * @NoAdminRequired
  76. * @PublicPage
  77. */
  78. #[UseSession]
  79. public function finishAuthentication(string $data): JSONResponse {
  80. $this->logger->debug('Validating WebAuthn login');
  81. if (!$this->session->exists(self::WEBAUTHN_LOGIN) || !$this->session->exists(self::WEBAUTHN_LOGIN_UID)) {
  82. $this->logger->debug('Trying to finish WebAuthn login without session data');
  83. return new JSONResponse([], Http::STATUS_BAD_REQUEST);
  84. }
  85. // Obtain the publicKeyCredentialOptions from when we started the registration
  86. $publicKeyCredentialRequestOptions = PublicKeyCredentialRequestOptions::createFromString($this->session->get(self::WEBAUTHN_LOGIN));
  87. $uid = $this->session->get(self::WEBAUTHN_LOGIN_UID);
  88. $this->webAuthnManger->finishAuthentication($publicKeyCredentialRequestOptions, $data, $uid);
  89. //TODO: add other parameters
  90. $loginData = new LoginData(
  91. $this->request,
  92. $uid,
  93. ''
  94. );
  95. $this->webAuthnChain->process($loginData);
  96. return new JSONResponse([
  97. 'defaultRedirectUrl' => $this->urlGenerator->linkToDefaultPageUrl(),
  98. ]);
  99. }
  100. }