Passed
Push — master ( 6f1745...57b5b6 )
by
unknown
34:44 queued 14:32
created

FormSession   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 16
c 1
b 0
f 0
dl 0
loc 62
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A generateIdentifier() 0 3 1
A getIdentifier() 0 3 1
A validateIdentifier() 0 8 2
A getAuthenticatedIdentifier() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the TYPO3 CMS project.
7
 *
8
 * It is free software; you can redistribute it and/or modify it under
9
 * the terms of the GNU General Public License, either version 2
10
 * of the License, or any later version.
11
 *
12
 * For the full copyright and license information, please read the
13
 * LICENSE.txt file that was distributed with this source code.
14
 *
15
 * The TYPO3 project - inspiring people to share!
16
 */
17
18
namespace TYPO3\CMS\Form\Domain\Runtime\FormRuntime;
19
20
use TYPO3\CMS\Core\Crypto\Random;
21
use TYPO3\CMS\Core\Error\Http\BadRequestException;
22
use TYPO3\CMS\Core\Utility\GeneralUtility;
23
use TYPO3\CMS\Extbase\Security\Cryptography\HashService;
24
use TYPO3\CMS\Extbase\Security\Exception\InvalidArgumentForHashGenerationException;
25
use TYPO3\CMS\Extbase\Security\Exception\InvalidHashException;
26
27
/**
28
 * @internal
29
 */
30
class FormSession
31
{
32
    protected $identifier;
33
34
    /**
35
     * Factory to create the form session from the current state
36
     *
37
     * @param string|null $authenticatedIdentifier
38
     * @throws BadRequestException
39
     */
40
    public function __construct(string $authenticatedIdentifier = null)
41
    {
42
        if ($authenticatedIdentifier === null) {
43
            $this->identifier = $this->generateIdentifier();
44
        } else {
45
            $this->identifier = $this->validateIdentifier($authenticatedIdentifier);
46
        }
47
    }
48
49
    /**
50
     * @return string
51
     * @internal
52
     */
53
    public function getIdentifier(): string
54
    {
55
        return $this->identifier;
56
    }
57
58
    /**
59
     * Consumed by TYPO3\CMS\Form\ViewHelpers\FormViewHelper
60
     *
61
     * @return string
62
     * @internal
63
     */
64
    public function getAuthenticatedIdentifier(): string
65
    {
66
        return GeneralUtility::makeInstance(HashService::class)
67
            // restrict string expansion by adding some char ('|')
68
            ->appendHmac($this->identifier . '|');
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    protected function generateIdentifier(): string
75
    {
76
        return GeneralUtility::makeInstance(Random::class)->generateRandomHexString(40);
77
    }
78
79
    /**
80
     * @param string $authenticatedIdentifier
81
     * @return string
82
     * @throws BadRequestException
83
     */
84
    protected function validateIdentifier(string $authenticatedIdentifier): string
85
    {
86
        try {
87
            $identifier = GeneralUtility::makeInstance(HashService::class)
88
                ->validateAndStripHmac($authenticatedIdentifier);
89
            return rtrim($identifier, '|');
90
        } catch (InvalidHashException | InvalidArgumentForHashGenerationException $e) {
91
            throw new BadRequestException('The HMAC of the form session could not be validated.', 1613300274);
92
        }
93
    }
94
}
95