VerificationController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Provisioning_API\Controller;
  25. use InvalidArgumentException;
  26. use OC\Security\Crypto;
  27. use OCP\Accounts\IAccountManager;
  28. use OCP\AppFramework\Controller;
  29. use OCP\AppFramework\Http\TemplateResponse;
  30. use OCP\IL10N;
  31. use OCP\IRequest;
  32. use OCP\IUserManager;
  33. use OCP\IUserSession;
  34. use OCP\Security\VerificationToken\InvalidTokenException;
  35. use OCP\Security\VerificationToken\IVerificationToken;
  36. class VerificationController extends Controller {
  37. /** @var IVerificationToken */
  38. private $verificationToken;
  39. /** @var IUserManager */
  40. private $userManager;
  41. /** @var IL10N */
  42. private $l10n;
  43. /** @var IUserSession */
  44. private $userSession;
  45. /** @var IAccountManager */
  46. private $accountManager;
  47. /** @var Crypto */
  48. private $crypto;
  49. public function __construct(
  50. string $appName,
  51. IRequest $request,
  52. IVerificationToken $verificationToken,
  53. IUserManager $userManager,
  54. IL10N $l10n,
  55. IUserSession $userSession,
  56. IAccountManager $accountManager,
  57. Crypto $crypto
  58. ) {
  59. parent::__construct($appName, $request);
  60. $this->verificationToken = $verificationToken;
  61. $this->userManager = $userManager;
  62. $this->l10n = $l10n;
  63. $this->userSession = $userSession;
  64. $this->accountManager = $accountManager;
  65. $this->crypto = $crypto;
  66. }
  67. /**
  68. * @NoCSRFRequired
  69. * @NoAdminRequired
  70. * @NoSubAdminRequired
  71. */
  72. public function showVerifyMail(string $token, string $userId, string $key): TemplateResponse {
  73. if ($this->userSession->getUser()->getUID() !== $userId) {
  74. // not a public page, hence getUser() must return an IUser
  75. throw new InvalidArgumentException('Logged in user is not mail address owner');
  76. }
  77. $email = $this->crypto->decrypt($key);
  78. return new TemplateResponse(
  79. 'core', 'confirmation', [
  80. 'title' => $this->l10n->t('Email confirmation'),
  81. 'message' => $this->l10n->t('To enable the email address %s please click the button below.', [$email]),
  82. 'action' => $this->l10n->t('Confirm'),
  83. ], TemplateResponse::RENDER_AS_GUEST);
  84. }
  85. /**
  86. * @NoAdminRequired
  87. * @NoSubAdminRequired
  88. * @BruteForceProtection(action=emailVerification)
  89. */
  90. public function verifyMail(string $token, string $userId, string $key): TemplateResponse {
  91. $throttle = false;
  92. try {
  93. if ($this->userSession->getUser()->getUID() !== $userId) {
  94. throw new InvalidArgumentException('Logged in user is not mail address owner');
  95. }
  96. $email = $this->crypto->decrypt($key);
  97. $ref = \substr(hash('sha256', $email), 0, 8);
  98. $user = $this->userManager->get($userId);
  99. $this->verificationToken->check($token, $user, 'verifyMail' . $ref, $email);
  100. $userAccount = $this->accountManager->getAccount($user);
  101. $emailProperty = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)
  102. ->getPropertyByValue($email);
  103. if ($emailProperty === null) {
  104. throw new InvalidArgumentException($this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'));
  105. }
  106. $emailProperty->setLocallyVerified(IAccountManager::VERIFIED);
  107. $this->accountManager->updateAccount($userAccount);
  108. $this->verificationToken->delete($token, $user, 'verifyMail' . $ref);
  109. } catch (InvalidTokenException $e) {
  110. if ($e->getCode() === InvalidTokenException::TOKEN_EXPIRED) {
  111. $error = $this->l10n->t('Could not verify mail because the token is expired.');
  112. } else {
  113. $throttle = true;
  114. $error = $this->l10n->t('Could not verify mail because the token is invalid.');
  115. }
  116. } catch (InvalidArgumentException $e) {
  117. $error = $e->getMessage();
  118. } catch (\Exception $e) {
  119. $error = $this->l10n->t('An unexpected error occurred. Please contact your admin.');
  120. }
  121. if (isset($error)) {
  122. $response = new TemplateResponse(
  123. 'core', 'error', [
  124. 'errors' => [['error' => $error]]
  125. ], TemplateResponse::RENDER_AS_GUEST);
  126. if ($throttle) {
  127. $response->throttle();
  128. }
  129. return $response;
  130. }
  131. return new TemplateResponse(
  132. 'core', 'success', [
  133. 'title' => $this->l10n->t('Email confirmation successful'),
  134. 'message' => $this->l10n->t('Email confirmation successful'),
  135. ], TemplateResponse::RENDER_AS_GUEST);
  136. }
  137. }