aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/LTI13a/NonceGenerator.php
blob: 8550709dabb958c56740083f708b1cc37c6a3b4e (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
<?php

namespace Studip\LTI13a;

use OAT\Library\Lti1p3Core\Security\Nonce\NonceGeneratorInterface;
use OAT\Library\Lti1p3Core\Security\Nonce\NonceInterface;
use OAT\Library\Lti1p3Core\Security\Nonce\Nonce;

class NonceGenerator implements NonceGeneratorInterface
{
    public function __construct(
        protected bool $pass_nonce_from_request = false
    ) {
    }

    #[\Override]
    public function generate(?int $ttl = null): NonceInterface
    {
        $expiration = new \DateTime();
        $expiration = $expiration->add(new \DateInterval('PT5M'));
        if ($this->pass_nonce_from_request) {
            return new Nonce(
                \Request::get('nonce'),
                $expiration
            );
        } else {
            $nonce = md5(random_bytes(16) . 'lti13a_nonce');
            return new Nonce(
                $nonce,
                $expiration
            );
        }
    }
}