WebAuthnController.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. *
  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 OC\Core\Controller;
  26. use OC\Authentication\Login\LoginData;
  27. use OC\Authentication\Login\WebAuthnChain;
  28. use OC\Authentication\WebAuthn\Manager;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Http\JSONResponse;
  32. use OCP\IRequest;
  33. use OCP\ISession;
  34. use OCP\Util;
  35. use Psr\Log\LoggerInterface;
  36. use Webauthn\PublicKeyCredentialRequestOptions;
  37. class WebAuthnController extends Controller {
  38. private const WEBAUTHN_LOGIN = 'webauthn_login';
  39. private const WEBAUTHN_LOGIN_UID = 'webauthn_login_uid';
  40. private Manager $webAuthnManger;
  41. private ISession $session;
  42. private LoggerInterface $logger;
  43. private WebAuthnChain $webAuthnChain;
  44. public function __construct($appName, IRequest $request, Manager $webAuthnManger, ISession $session, LoggerInterface $logger, WebAuthnChain $webAuthnChain) {
  45. parent::__construct($appName, $request);
  46. $this->webAuthnManger = $webAuthnManger;
  47. $this->session = $session;
  48. $this->logger = $logger;
  49. $this->webAuthnChain = $webAuthnChain;
  50. }
  51. /**
  52. * @NoAdminRequired
  53. * @PublicPage
  54. * @UseSession
  55. */
  56. public function startAuthentication(string $loginName): JSONResponse {
  57. $this->logger->debug('Starting WebAuthn login');
  58. $this->logger->debug('Converting login name to UID');
  59. $uid = $loginName;
  60. Util::emitHook(
  61. '\OCA\Files_Sharing\API\Server2Server',
  62. 'preLoginNameUsedAsUserName',
  63. ['uid' => &$uid]
  64. );
  65. $this->logger->debug('Got UID: ' . $uid);
  66. $publicKeyCredentialRequestOptions = $this->webAuthnManger->startAuthentication($uid, $this->request->getServerHost());
  67. $this->session->set(self::WEBAUTHN_LOGIN, json_encode($publicKeyCredentialRequestOptions));
  68. $this->session->set(self::WEBAUTHN_LOGIN_UID, $uid);
  69. return new JSONResponse($publicKeyCredentialRequestOptions);
  70. }
  71. /**
  72. * @NoAdminRequired
  73. * @PublicPage
  74. * @UseSession
  75. */
  76. public function finishAuthentication(string $data): JSONResponse {
  77. $this->logger->debug('Validating WebAuthn login');
  78. if (!$this->session->exists(self::WEBAUTHN_LOGIN) || !$this->session->exists(self::WEBAUTHN_LOGIN_UID)) {
  79. $this->logger->debug('Trying to finish WebAuthn login without session data');
  80. return new JSONResponse([], Http::STATUS_BAD_REQUEST);
  81. }
  82. // Obtain the publicKeyCredentialOptions from when we started the registration
  83. $publicKeyCredentialRequestOptions = PublicKeyCredentialRequestOptions::createFromString($this->session->get(self::WEBAUTHN_LOGIN));
  84. $uid = $this->session->get(self::WEBAUTHN_LOGIN_UID);
  85. $this->webAuthnManger->finishAuthentication($publicKeyCredentialRequestOptions, $data, $uid);
  86. //TODO: add other parameters
  87. $loginData = new LoginData(
  88. $this->request,
  89. $uid,
  90. ''
  91. );
  92. $this->webAuthnChain->process($loginData);
  93. return new JSONResponse([]);
  94. }
  95. }