| 1 |
efrain |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Packback\Lti1p3;
|
|
|
4 |
|
|
|
5 |
use Firebase\JWT\JWT;
|
|
|
6 |
use Packback\Lti1p3\Interfaces\IDatabase;
|
|
|
7 |
use Packback\Lti1p3\Interfaces\ILtiRegistration;
|
|
|
8 |
|
|
|
9 |
class JwksEndpoint
|
|
|
10 |
{
|
|
|
11 |
public function __construct(private array $keys)
|
|
|
12 |
{
|
|
|
13 |
}
|
|
|
14 |
|
|
|
15 |
public static function new(array $keys): self
|
|
|
16 |
{
|
|
|
17 |
return new JwksEndpoint($keys);
|
|
|
18 |
}
|
|
|
19 |
|
|
|
20 |
public static function fromIssuer(IDatabase $database, string $issuer): self
|
|
|
21 |
{
|
|
|
22 |
$registration = $database->findRegistrationByIssuer($issuer);
|
|
|
23 |
|
|
|
24 |
return new JwksEndpoint([$registration->getKid() => $registration->getToolPrivateKey()]);
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
public static function fromRegistration(ILtiRegistration $registration): self
|
|
|
28 |
{
|
|
|
29 |
return new JwksEndpoint([$registration->getKid() => $registration->getToolPrivateKey()]);
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public function getPublicJwks(): array
|
|
|
33 |
{
|
|
|
34 |
$jwks = [];
|
|
|
35 |
foreach ($this->keys as $kid => $private_key) {
|
|
|
36 |
$key_res = openssl_pkey_get_private($private_key);
|
|
|
37 |
$key_details = openssl_pkey_get_details($key_res);
|
|
|
38 |
$components = [
|
|
|
39 |
'kty' => 'RSA',
|
|
|
40 |
'alg' => 'RS256',
|
|
|
41 |
'use' => 'sig',
|
|
|
42 |
'e' => JWT::urlsafeB64Encode($key_details['rsa']['e']),
|
|
|
43 |
'n' => JWT::urlsafeB64Encode($key_details['rsa']['n']),
|
|
|
44 |
'kid' => $kid,
|
|
|
45 |
];
|
|
|
46 |
$jwks[] = $components;
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
return ['keys' => $jwks];
|
|
|
50 |
}
|
|
|
51 |
}
|