VerificationController.php 4.7 KB

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