1
0

VerificationController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\IgnoreOpenAPI;
  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. #[IgnoreOpenAPI]
  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) {
  76. if ($this->userSession->getUser()->getUID() !== $userId) {
  77. // not a public page, hence getUser() must return an IUser
  78. throw new InvalidArgumentException('Logged in user 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. */
  92. public function verifyMail(string $token, string $userId, string $key) {
  93. try {
  94. if ($this->userSession->getUser()->getUID() !== $userId) {
  95. throw new InvalidArgumentException('Logged in user is not mail address owner');
  96. }
  97. $email = $this->crypto->decrypt($key);
  98. $ref = \substr(hash('sha256', $email), 0, 8);
  99. $user = $this->userManager->get($userId);
  100. $this->verificationToken->check($token, $user, 'verifyMail' . $ref, $email);
  101. $userAccount = $this->accountManager->getAccount($user);
  102. $emailProperty = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL)
  103. ->getPropertyByValue($email);
  104. if ($emailProperty === null) {
  105. throw new InvalidArgumentException($this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'));
  106. }
  107. $emailProperty->setLocallyVerified(IAccountManager::VERIFIED);
  108. $this->accountManager->updateAccount($userAccount);
  109. $this->verificationToken->delete($token, $user, 'verifyMail' . $ref);
  110. } catch (InvalidTokenException $e) {
  111. $error = $e->getCode() === InvalidTokenException::TOKEN_EXPIRED
  112. ? $this->l10n->t('Could not verify mail because the token is expired.')
  113. : $this->l10n->t('Could not verify mail because the token is invalid.');
  114. } catch (InvalidArgumentException $e) {
  115. $error = $e->getMessage();
  116. } catch (\Exception $e) {
  117. $error = $this->l10n->t('An unexpected error occurred. Please contact your admin.');
  118. }
  119. if (isset($error)) {
  120. return new TemplateResponse(
  121. 'core', 'error', [
  122. 'errors' => [['error' => $error]]
  123. ], TemplateResponse::RENDER_AS_GUEST);
  124. }
  125. return new TemplateResponse(
  126. 'core', 'success', [
  127. 'title' => $this->l10n->t('Email confirmation successful'),
  128. 'message' => $this->l10n->t('Email confirmation successful'),
  129. ], TemplateResponse::RENDER_AS_GUEST);
  130. }
  131. }