Issues (2037)

plugin/lti_provider/tool/jwks.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\PluginBundle\Entity\LtiProvider\PlatformKey;
5
use Firebase\JWT\JWT;
6
use phpseclib\Crypt\RSA;
7
8
$cidReset = true;
9
10
require_once __DIR__.'/../../../main/inc/global.inc.php';
11
12
$plugin = LtiProviderPlugin::create();
13
14
if ('true' !== $plugin->get('enabled')) {
15
    exit;
16
}
17
18
/** @var PlatformKey $platformKey */
19
$platformKey = Database::getManager()
20
    ->getRepository('ChamiloPluginBundle:LtiProvider\PlatformKey')
21
    ->findOneBy([]);
22
23
if (!$platformKey) {
0 ignored issues
show
$platformKey is of type PlatformKey, thus it always evaluated to true.
Loading history...
24
    exit;
25
}
26
27
$privateKey = $platformKey->getPrivateKey();
28
29
$jwks = [];
30
31
$key = new RSA();
32
$key->setHash('sha256');
33
$key->loadKey($platformKey->getPrivateKey());
34
$key->setPublicKey(false, RSA::PUBLIC_FORMAT_PKCS8);
35
36
if ($key->publicExponent) {
37
    $jwks = [
38
        'kty' => 'RSA',
39
        'alg' => 'RS256',
40
        'use' => 'sig',
41
        'e' => JWT::urlsafeB64Encode($key->publicExponent->toBytes()),
42
        'n' => JWT::urlsafeB64Encode($key->modulus->toBytes()),
43
        'kid' => $platformKey->getKid(),
44
    ];
45
}
46
47
header('Content-Type: application/json');
48
49
echo json_encode(['keys' => [$jwks]]);
50