-
-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathEmailToken.php
More file actions
81 lines (68 loc) · 2.48 KB
/
EmailToken.php
File metadata and controls
81 lines (68 loc) · 2.48 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
<?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\SignatureMethod;
use OCA\Libresign\Exception\LibresignException;
use OCA\Libresign\Helper\JSActions;
use OCA\Libresign\Service\IdentifyMethod\IdentifyService;
use OCA\Libresign\Vendor\Wobeto\EmailBlur\Blur;
class EmailToken extends AbstractSignatureMethod implements IToken {
public function __construct(
protected IdentifyService $identifyService,
protected TokenService $tokenService,
) {
// TRANSLATORS Name of possible authenticator method. This signalize that the signer could be identified by email
$this->setFriendlyName($this->identifyService->getL10n()->t('Email token'));
parent::__construct(
$identifyService,
);
}
#[\Override]
public function validateToSign(): void {
$this->throwIfInvalidToken();
}
#[\Override]
public function toArray(): array {
$entity = $this->getEntity();
$email = match ($entity->getIdentifierKey()) {
'email' => $entity->getIdentifierValue(),
'account' => $this->identifyService->getUserManager()->get($entity->getIdentifierValue())
?->getEMailAddress() ?? '',
default => '',
};
$emailLowercase = strtolower($email);
$code = $entity->getCode();
$identifiedAt = $entity->getIdentifiedAtDate();
$codeSentByUser = $this->codeSentByUser;
$hasConfirmCode = !empty($code);
$needCode = empty($code)
|| empty($identifiedAt)
|| empty($codeSentByUser);
$return = parent::toArray();
$return['identifyMethod'] = $entity->getIdentifierKey();
$return['needCode'] = $needCode;
$return['hasConfirmCode'] = $hasConfirmCode;
$return['blurredEmail'] = $emailLowercase ? $this->blurEmail($emailLowercase) : '';
$return['hashOfEmail'] = $emailLowercase ? md5($emailLowercase) : '';
return $return;
}
private function blurEmail(string $email): string {
$blur = new Blur($email);
return $blur->make();
}
#[\Override]
public function requestCode(string $identifier, string $method): void {
$signRequestMapper = $this->identifyService->getSignRequestMapper();
$signRequest = $signRequestMapper->getById($this->getEntity()->getSignRequestId());
$displayName = $signRequest->getDisplayName();
if ($identifier === $displayName) {
$displayName = '';
}
$code = $this->tokenService->sendCodeByEmail($identifier, $displayName);
$this->getEntity()->setCode($code);
$this->identifyService->save($this->getEntity());
}
}