• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / typo3-form-consent / 14044237598

24 Mar 2025 07:31PM UTC coverage: 96.506% (+3.5%) from 93.012%
14044237598

push

github

web-flow
[TASK] Update all dependencies

801 of 830 relevant lines covered (96.51%)

15.37 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.65
/Classes/Service/HashService.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the TYPO3 CMS extension "form_consent".
7
 *
8
 * Copyright (C) 2021-2025 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 2 of the License, or
13
 * (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 General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\Typo3FormConsent\Service;
25

26
use EliasHaeussler\Typo3FormConsent\Domain;
27
use EliasHaeussler\Typo3FormConsent\Event;
28
use Psr\EventDispatcher;
29
use TYPO3\CMS\Core;
30

31
/**
32
 * HashService
33
 *
34
 * @author Elias Häußler <elias@haeussler.dev>
35
 * @license GPL-2.0-or-later
36
 */
37
final class HashService
38
{
39
    public function __construct(
34✔
40
        private readonly EventDispatcher\EventDispatcherInterface $eventDispatcher,
41
    ) {}
34✔
42

43
    public function generate(Domain\Model\Consent $consent): string
32✔
44
    {
45
        $hashComponents = [
32✔
46
            $consent->getDate()->getTimestamp(),
32✔
47
        ];
32✔
48

49
        if ($consent->getData() !== null) {
32✔
50
            $hashComponents[] = (string)$consent->getData();
32✔
51
        }
52
        if ($consent->getValidUntil() !== null) {
32✔
53
            $hashComponents[] = $consent->getValidUntil()->getTimestamp();
32✔
54
        }
55

56
        /** @var Event\GenerateHashEvent $event */
57
        $event = $this->eventDispatcher->dispatch(new Event\GenerateHashEvent($hashComponents, $consent));
32✔
58

59
        if (null !== ($hash = $event->getHash())) {
32✔
60
            return $hash;
1✔
61
        }
62

63
        $hashInput = implode('_', $event->getComponents());
32✔
64
        $hashSecret = $consent->getEmail();
32✔
65

66
        // @todo Remove once support for TYPO3 v12 is dropped
67
        if (!\class_exists(Core\Crypto\HashService::class)) {
32✔
68
            return Core\Utility\GeneralUtility::hmac($hashInput, $hashSecret);
×
69
        }
70

71
        // @todo Use DI once support for TYPO3 v12 is dropped
72
        /** @var Core\Crypto\HashService $hashService */
73
        $hashService = Core\Utility\GeneralUtility::makeInstance(Core\Crypto\HashService::class);
32✔
74

75
        return $hashService->hmac($hashInput, $hashSecret);
32✔
76
    }
77

78
    public function isValid(Domain\Model\Consent $consent, ?string $hash = null): bool
29✔
79
    {
80
        $hash ??= $consent->getValidationHash();
29✔
81
        $newHash = $this->generate($consent);
29✔
82

83
        return $hash === $newHash;
29✔
84
    }
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc