1
0

VerificationController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2021 Arthur Schiwon <blizzz@arthur-schiwon.de>
  5. *
  6. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  7. * @author Kate Döen <kate.doeen@nextcloud.com>
  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 <https://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Provisioning_API\Controller;
  26. use InvalidArgumentException;
  27. use OC\Security\Crypto;
  28. use OCP\Accounts\IAccountManager;
  29. use OCP\AppFramework\Controller;
  30. use OCP\AppFramework\Http\Attribute\OpenAPI;
  31. use OCP\AppFramework\Http\TemplateResponse;
  32. use OCP\IL10N;
  33. use OCP\IRequest;
  34. use OCP\IUserManager;
  35. use OCP\IUserSession;
  36. use OCP\Security\VerificationToken\InvalidTokenException;
  37. use OCP\Security\VerificationToken\IVerificationToken;
  38. #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
  39. class VerificationController extends Controller {
  40. /** @var IVerificationToken */
  41. private $verificationToken;
  42. /** @var IUserManager */
  43. private $userManager;
  44. /** @var IL10N */
  45. private $l10n;
  46. /** @var IUserSession */
  47. private $userSession;
  48. /** @var IAccountManager */
  49. private $accountManager;
  50. /** @var Crypto */
  51. private $crypto;
  52. public function __construct(
  53. string $appName,
  54. IRequest $request,
  55. IVerificationToken $verificationToken,
  56. IUserManager $userManager,
  57. IL10N $l10n,
  58. IUserSession $userSession,
  59. IAccountManager $accountManager,
  60. Crypto $crypto
  61. ) {
  62. parent::__construct($appName, $request);
  63. $this->verificationToken = $verificationToken;
  64. $this->userManager = $userManager;
  65. $this->l10n = $l10n;
  66. $this->userSession = $userSession;
  67. $this->accountManager = $accountManager;
  68. $this->crypto = $crypto;
  69. }
  70. /**
  71. * @NoCSRFRequired
  72. * @NoAdminRequired
  73. * @NoSubAdminRequired
  74. */
  75. public function showVerifyMail(string $token, string $userId, string $key): TemplateResponse {
  76. if ($this->userSession->getUser()->getUID() !== $userId) {
  77. // not a public page, hence getUser() must return an IUser
  78. throw new InvalidArgumentException('Logged in account is not mail address owner');
  79. }
  80. $email = $this->crypto->decrypt($key);
  81. return new TemplateResponse(
  82. 'core', 'confirmation', [
  83. 'title' => $this->l10n->t('Email confirmation'),
  84. 'message' => $this->l10n->t('To enable the email address %s please click the button below.', [$email]),
  85. 'action' => $this->l10n->t('Confirm'),
  86. ], TemplateResponse::RENDER_AS_GUEST);
  87. }
  88. /**
  89. * @NoAdminRequired
  90. * @NoSubAdminRequired
  91. * @BruteForceProtection(action=emailVerification)
  92. */
  93. public function verifyMail(string $token, string $userId, string $key): TemplateResponse {
  94. $throttle = false;
  95. try {
  96. if ($this->userSession->getUser()->getUID() !== $userId) {
  97. throw new InvalidArgumentException('Logged in account is not mail address owner');
  98. }
  99. $email = $this->crypto->decrypt($key);
  100. $ref = \substr(hash('sha256', $email), 0, 8);
  101. $user = $this->userManager->get($userId);
  102. $this->verificationToken->check($token, $user, 'verifyMail' . $ref, $email);
  103. $userAccount = $this->accountManager->getAccount($user);
  104. $emailProperty = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)
  105. ->getPropertyByValue($email);
  106. if ($emailProperty === null) {
  107. throw new InvalidArgumentException($this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'));
  108. }
  109. $emailProperty->setLocallyVerified(IAccountManager::VERIFIED);
  110. $this->accountManager->updateAccount($userAccount);
  111. $this->verificationToken->delete($token, $user, 'verifyMail' . $ref);
  112. } catch (InvalidTokenException $e) {
  113. if ($e->getCode() === InvalidTokenException::TOKEN_EXPIRED) {
  114. $error = $this->l10n->t('Could not verify mail because the token is expired.');
  115. } else {
  116. $throttle = true;
  117. $error = $this->l10n->t('Could not verify mail because the token is invalid.');
  118. }
  119. } catch (InvalidArgumentException $e) {
  120. $error = $e->getMessage();
  121. } catch (\Exception $e) {
  122. $error = $this->l10n->t('An unexpected error occurred. Please contact your admin.');
  123. }
  124. if (isset($error)) {
  125. $response = new TemplateResponse(
  126. 'core', 'error', [
  127. 'errors' => [['error' => $error]]
  128. ], TemplateResponse::RENDER_AS_GUEST);
  129. if ($throttle) {
  130. $response->throttle();
  131. }
  132. return $response;
  133. }
  134. return new TemplateResponse(
  135. 'core', 'success', [
  136. 'title' => $this->l10n->t('Email confirmation successful'),
  137. 'message' => $this->l10n->t('Email confirmation successful'),
  138. ], TemplateResponse::RENDER_AS_GUEST);
  139. }
  140. }