aboutsummaryrefslogtreecommitdiffhomepage
path: root/services/app/src/Twig/CsrfExtension.php
blob: b6369a2890663c9f07a61d19e141cb83caa05cdc (plain)
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
<?php

declare(strict_types=1);

namespace Nsfisis\Albatross\Twig;

use Slim\Csrf\Guard;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;

final class CsrfExtension extends AbstractExtension implements GlobalsInterface
{
    public function __construct(private readonly Guard $csrf)
    {
    }

    /**
     * @return array{csrf: array{name_key: string, name: string, value_key: string, value: string}}
     */
    public function getGlobals(): array
    {
        $csrf_name_key = $this->csrf->getTokenNameKey();
        $csrf_name = $this->csrf->getTokenName();
        assert(
            isset($csrf_name),
            'It must be present here because the access is denied by Csrf\Guard middleware if absent.',
        );

        $csrf_value_key = $this->csrf->getTokenValueKey();
        $csrf_value = $this->csrf->getTokenValue();
        assert(
            isset($csrf_value),
            'It must be present here because the access is denied by Csrf\Guard middleware if absent.',
        );

        return [
            'csrf' => [
                'name_key' => $csrf_name_key,
                'name' => $csrf_name,
                'value_key' => $csrf_value_key,
                'value' => $csrf_value
            ]
        ];
    }
}