1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2019 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace OAuth2Framework\Component\ClientRule; |
15
|
|
|
|
16
|
|
|
use Jose\Component\Core\JWKSet; |
17
|
|
|
use Jose\Component\KeyManagement\JKUFactory; |
18
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
19
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
20
|
|
|
|
21
|
|
|
class JwksRule implements Rule |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var null|JKUFactory |
25
|
|
|
*/ |
26
|
|
|
private $jkuFactory; |
27
|
|
|
|
28
|
|
|
public function __construct(?JKUFactory $jkuFactory) |
29
|
|
|
{ |
30
|
|
|
$this->jkuFactory = $jkuFactory; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function handle(ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters, RuleHandler $next): DataBag |
34
|
|
|
{ |
35
|
|
|
if ($commandParameters->has('jwks') && $commandParameters->has('jwks_uri')) { |
36
|
|
|
throw new \InvalidArgumentException('The parameters "jwks" and "jwks_uri" cannot be set together.'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($commandParameters->has('jwks')) { |
40
|
|
|
try { |
41
|
|
|
$keyset = JWKSet::createFromKeyData($commandParameters->get('jwks')); |
|
|
|
|
42
|
|
|
} catch (\Throwable $e) { |
43
|
|
|
throw new \InvalidArgumentException('The parameter "jwks" must be a valid JWKSet object.', 0, $e); |
44
|
|
|
} |
45
|
|
|
if (0 === \count($keyset)) { |
46
|
|
|
throw new \InvalidArgumentException('The parameter "jwks" must not be empty.'); |
47
|
|
|
} |
48
|
|
|
$validatedParameters->set('jwks', $commandParameters->get('jwks')); |
49
|
|
|
} |
50
|
|
|
if ($commandParameters->has('jwks_uri')) { |
51
|
|
|
if (null === $this->jkuFactory) { |
52
|
|
|
throw new \InvalidArgumentException('Distant key sets cannot be used. Please use "jwks" instead of "jwks_uri".'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
try { |
56
|
|
|
$jwks = $this->jkuFactory->loadFromUrl($commandParameters->get('jwks_uri')); |
|
|
|
|
57
|
|
|
} catch (\Throwable $e) { |
58
|
|
|
throw new \InvalidArgumentException('The parameter "jwks_uri" must be a valid uri to a JWKSet.', 0, $e); |
59
|
|
|
} |
60
|
|
|
if (0 === $jwks->count()) { |
61
|
|
|
throw new \InvalidArgumentException('The distant key set is empty.'); |
62
|
|
|
} |
63
|
|
|
$validatedParameters->set('jwks_uri', $commandParameters->get('jwks_uri')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $next->handle($clientId, $commandParameters, $validatedParameters); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|