Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
 
3
namespace Packback\Lti1p3;
4
 
5
/**
6
 * Used for migrations from LTI 1.1 to LTI 1.3
7
 *
8
 * @see IMigrationDatabase
9
 */
10
class Lti1p1Key
11
{
12
    private ?string $key;
13
    private ?string $secret;
14
 
15
    public function __construct(?array $key = null)
16
    {
17
        $this->key = $key['key'] ?? null;
18
        $this->secret = $key['secret'] ?? null;
19
    }
20
 
21
    public function getKey(): ?string
22
    {
23
        return $this->key;
24
    }
25
 
26
    public function setKey(string $key): self
27
    {
28
        $this->key = $key;
29
 
30
        return $this;
31
    }
32
 
33
    public function getSecret(): ?string
34
    {
35
        return $this->secret;
36
    }
37
 
38
    public function setSecret(string $secret): self
39
    {
40
        $this->secret = $secret;
41
 
42
        return $this;
43
    }
44
 
45
    /**
46
     * Create a signature using the key and secret
47
     *
48
     * @see https://www.imsglobal.org/spec/lti/v1p3/migr#oauth_consumer_key_sign
49
     */
50
    public function sign(string $deploymentId, string $iss, string $clientId, string $exp, string $nonce): string
51
    {
52
        $signatureComponents = [
53
            $this->getKey(),
54
            $deploymentId,
55
            $iss,
56
            $clientId,
57
            $exp,
58
            $nonce,
59
        ];
60
 
61
        $baseString = implode('&', $signatureComponents);
62
        $utf8String = mb_convert_encoding($baseString, 'utf8', mb_detect_encoding($baseString));
63
        $hash = hash_hmac('sha256', $utf8String, $this->getSecret(), true);
64
 
65
        return base64_encode($hash);
66
    }
67
 
68
}