-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathEmail.php
More file actions
217 lines (202 loc) · 7.4 KB
/
Email.php
File metadata and controls
217 lines (202 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Libresign\Service\IdentifyMethod;
use OCA\Libresign\Db\FileElementMapper;
use OCA\Libresign\Db\IdentifyMethodMapper;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\IdentifyMethod\SignatureMethod\ISignatureMethod;
use OCA\Libresign\Service\SessionService;
use OCP\AppFramework\Utility\ITimeFactory;
use OCP\Files\IRootFolder;
use OCP\IUser;
use OCP\IUserSession;
use Psr\Log\LoggerInterface;
class Email extends AbstractIdentifyMethod {
public array $availableSignatureMethods = [
ISignatureMethod::SIGNATURE_METHOD_CLICK_TO_SIGN,
ISignatureMethod::SIGNATURE_METHOD_EMAIL_TOKEN,
];
public string $defaultSignatureMethod = ISignatureMethod::SIGNATURE_METHOD_EMAIL_TOKEN;
public function __construct(
protected IdentifyService $identifyService,
private IdentifyMethodMapper $identifyMethodMapper,
private IRootFolder $root,
private ITimeFactory $timeFactory,
private SessionService $sessionService,
private FileElementMapper $fileElementMapper,
private IUserSession $userSession,
private LoggerInterface $logger,
) {
// TRANSLATORS Name of possible authenticator method. This signalize that the signer could be identified by email
$this->setFriendlyName($this->identifyService->getL10n()->t('Email'));
parent::__construct(
$identifyService,
);
}
public function validateToRequest(): void {
$this->throwIfInvalidEmail();
}
public function validateToIdentify(): void {
$this->throwIfAccountAlreadyExists();
$this->throwIfIsAuthenticatedWithDifferentAccount();
$this->throwIfMaximumValidityExpired();
$this->throwIfRenewalIntervalExpired();
$this->throwIfNeedToCreateAccount();
$this->throwIfFileNotFound();
$this->throwIfAlreadySigned();
$this->renewSession();
$this->updateIdentifiedAt();
}
public function validateToSign(): void {
$this->throwIfAccountAlreadyExists();
$this->throwIfIsAuthenticatedWithDifferentAccount();
$this->throwIfInvalidToken();
$this->throwIfMaximumValidityExpired();
$this->throwIfRenewalIntervalExpired();
$this->throwIfNeedToCreateAccount();
$this->throwIfFileNotFound();
$this->throwIfAlreadySigned();
$this->renewSession();
$this->updateIdentifiedAt();
}
protected function throwIfNeedToCreateAccount(): void {
$user = $this->userSession->getUser();
if ($user instanceof IUser) {
return;
}
$settings = $this->getSettings();
if (!$settings['enabled']) {
$this->logger->debug('Email identification method is disabled');
throw new LibresignException(json_encode([
'action' => JSActions::ACTION_SHOW_ERROR,
// TRANSLATORS This signalize that the identification method by email is disabled and the signer can't be identified
'errors' => [['message' => $this->identifyService->getL10n()->t('Invalid identification method')]],
]));
return;
}
if (!$settings['can_create_account']) {
return;
}
if ($this->identifyService->getSessionService()->getSignStartTime()) {
return;
}
$email = $this->getEntity()->getIdentifierValue();
$signer = $this->identifyService->getUserManager()->getByEmail($email);
if (!$signer) {
throw new LibresignException(json_encode([
'action' => JSActions::ACTION_CREATE_ACCOUNT,
'settings' => ['accountHash' => md5($email)],
'message' => $this->identifyService->getL10n()->t('You need to create an account to sign this file.'),
]));
}
$signRequest = $this->identifyService->getSignRequestMapper()->getById($this->getEntity()->getSignRequestId());
$errors = [$this->identifyService->getL10n()->t('User already exists. Please login.')];
if ($this->userSession->isLoggedIn()) {
$errors[] = $this->identifyService->getL10n()->t('This is not your file');
$this->userSession->logout();
}
throw new LibresignException(json_encode([
'action' => JSActions::ACTION_REDIRECT,
'errors' => $errors,
'redirect' => $this->identifyService->getUrlGenerator()->linkToRoute('core.login.showLoginForm', [
'redirect_url' => $this->identifyService->getUrlGenerator()->linkToRoute(
'libresign.page.sign',
['uuid' => $signRequest->getUuid()]
),
]),
]));
}
private function throwIfIsAuthenticatedWithDifferentAccount(): void {
$user = $this->userSession->getUser();
if (!$user instanceof IUser) {
return;
}
$email = $this->entity->getIdentifierValue();
if (!empty($user->getEMailAddress()) && $user->getEMailAddress() === $email) {
return;
}
if ($this->getEntity()->getCode() && !$this->getEntity()->getIdentifiedAtDate()) {
return;
}
throw new LibresignException(json_encode([
'action' => JSActions::ACTION_DO_NOTHING,
'errors' => [['message' => $this->identifyService->getL10n()->t('This document is not yours. Log out and use the sign link again.')]],
]));
}
private function throwIfAccountAlreadyExists(): void {
$user = $this->userSession->getUser();
if (!$user instanceof IUser) {
return;
}
$email = $this->entity->getIdentifierValue();
$signer = $this->identifyService->getUserManager()->getByEmail($email);
if (!$signer) {
return;
}
foreach ($signer as $s) {
if ($s->getUID() === $user->getUID()) {
return;
}
}
$signRequest = $this->identifyService->getSignRequestMapper()->getById($this->getEntity()->getSignRequestId());
$errors = [$this->identifyService->getL10n()->t('User already exists. Please login.')];
if ($this->userSession->isLoggedIn()) {
$errors[] = $this->identifyService->getL10n()->t('This is not your file');
$this->userSession->logout();
}
throw new LibresignException(json_encode([
'action' => JSActions::ACTION_REDIRECT,
'errors' => $errors,
'redirect' => $this->identifyService->getUrlGenerator()->linkToRoute('core.login.showLoginForm', [
'redirect_url' => $this->identifyService->getUrlGenerator()->linkToRoute(
'libresign.page.sign',
['uuid' => $signRequest->getUuid()]
),
]),
]));
}
public function validateToCreateAccount(string $value): void {
$this->throwIfInvalidEmail();
$this->throwIfNotAllowedToCreateAccount();
if ($this->identifyService->getUserManager()->userExists($value)) {
throw new LibresignException($this->identifyService->getL10n()->t('User already exists'));
}
if ($this->getEntity()->getIdentifierValue() !== $value) {
throw new LibresignException($this->identifyService->getL10n()->t('This is not your file'));
}
}
private function throwIfNotAllowedToCreateAccount(): void {
$settings = $this->getSettings();
if (!$settings['can_create_account']) {
throw new LibresignException(json_encode([
'action' => JSActions::ACTION_SHOW_ERROR,
'errors' => [['message' => $this->identifyService->getL10n()->t('It is not possible to create new accounts.')]],
]));
}
}
private function throwIfInvalidEmail(): void {
if (!filter_var($this->entity->getIdentifierValue(), FILTER_VALIDATE_EMAIL)) {
throw new LibresignException($this->identifyService->getL10n()->t('Invalid email'));
}
}
public function getSettings(): array {
if (!empty($this->settings)) {
return $this->settings;
}
$this->settings = parent::getSettingsFromDatabase(
default: [
'enabled' => false,
'can_create_account' => true,
],
immutable: [
'test_url' => $this->identifyService->getUrlGenerator()->linkToRoute('settings.MailSettings.sendTestMail'),
]
);
return $this->settings;
}
}