Skip to content

Commit 1a59222

Browse files
Merge pull request #54973 from nextcloud/backport/54653/stable32
[stable32] fix: Avoid internal error when logging in with the wrong account to verify email address
2 parents e3460ac + 98c3858 commit 1a59222

1 file changed

Lines changed: 26 additions & 10 deletions

File tree

apps/provisioning_api/lib/Controller/VerificationController.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
namespace OCA\Provisioning_API\Controller;
1111

12-
use InvalidArgumentException;
1312
use OC\Security\Crypto;
1413
use OCP\Accounts\IAccountManager;
1514
use OCP\AppFramework\Controller;
@@ -18,6 +17,7 @@
1817
use OCP\AppFramework\Http\Attribute\NoCSRFRequired;
1918
use OCP\AppFramework\Http\Attribute\OpenAPI;
2019
use OCP\AppFramework\Http\TemplateResponse;
20+
use OCP\HintException;
2121
use OCP\IL10N;
2222
use OCP\IRequest;
2323
use OCP\IUserManager;
@@ -51,11 +51,21 @@ public function __construct(
5151
#[NoAdminRequired]
5252
#[NoCSRFRequired]
5353
public function showVerifyMail(string $token, string $userId, string $key): TemplateResponse {
54-
if ($this->userSession->getUser()->getUID() !== $userId) {
55-
// not a public page, hence getUser() must return an IUser
56-
throw new InvalidArgumentException('Logged in account is not mail address owner');
54+
try {
55+
if ($this->userSession->getUser()?->getUID() !== $userId) {
56+
// not a public page, hence getUser() must return an IUser
57+
throw new HintException(
58+
'Logged in account is not mail address owner',
59+
$this->l10n->t('Logged in account is not mail address owner'),
60+
);
61+
}
62+
$email = $this->crypto->decrypt($key);
63+
} catch (HintException $e) {
64+
return new TemplateResponse(
65+
'core', 'error', [
66+
'errors' => [['error' => $e->getHint()]]
67+
], TemplateResponse::RENDER_AS_GUEST);
5768
}
58-
$email = $this->crypto->decrypt($key);
5969

6070
return new TemplateResponse(
6171
'core', 'confirmation', [
@@ -73,8 +83,11 @@ public function showVerifyMail(string $token, string $userId, string $key): Temp
7383
public function verifyMail(string $token, string $userId, string $key): TemplateResponse {
7484
$throttle = false;
7585
try {
76-
if ($this->userSession->getUser()->getUID() !== $userId) {
77-
throw new InvalidArgumentException('Logged in account is not mail address owner');
86+
if ($this->userSession->getUser()?->getUID() !== $userId) {
87+
throw new HintException(
88+
'Logged in account is not mail address owner',
89+
$this->l10n->t('Logged in account is not mail address owner'),
90+
);
7891
}
7992
$email = $this->crypto->decrypt($key);
8093
$ref = \substr(hash('sha256', $email), 0, 8);
@@ -87,7 +100,10 @@ public function verifyMail(string $token, string $userId, string $key): Template
87100
->getPropertyByValue($email);
88101

89102
if ($emailProperty === null) {
90-
throw new InvalidArgumentException($this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'));
103+
throw new HintException(
104+
'Email was already removed from account and cannot be confirmed anymore.',
105+
$this->l10n->t('Email was already removed from account and cannot be confirmed anymore.'),
106+
);
91107
}
92108
$emailProperty->setLocallyVerified(IAccountManager::VERIFIED);
93109
$this->accountManager->updateAccount($userAccount);
@@ -99,8 +115,8 @@ public function verifyMail(string $token, string $userId, string $key): Template
99115
$throttle = true;
100116
$error = $this->l10n->t('Could not verify mail because the token is invalid.');
101117
}
102-
} catch (InvalidArgumentException $e) {
103-
$error = $e->getMessage();
118+
} catch (HintException $e) {
119+
$error = $e->getHint();
104120
} catch (\Exception $e) {
105121
$error = $this->l10n->t('An unexpected error occurred. Please contact your admin.');
106122
}

0 commit comments

Comments
 (0)