|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Parroauth2\Client\Extension\JwtAccessToken; |
|
4
|
|
|
|
|
5
|
|
|
use Parroauth2\Client\ClientInterface; |
|
6
|
|
|
use Parroauth2\Client\EndPoint\Introspection\IntrospectionEndPoint; |
|
7
|
|
|
use Parroauth2\Client\EndPoint\Introspection\IntrospectionResponse; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Try to perform a local introspection by deserialize a JWT access token |
|
11
|
|
|
* If the deserialization failed, the base introspection endpoint will be called |
|
12
|
|
|
*/ |
|
13
|
|
|
class LocalIntrospectionEndPoint extends IntrospectionEndPoint |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var JwtParserInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
private $parser; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var ClientInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $client; |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* LocalIntrospectionEndPoint constructor. |
|
28
|
|
|
* |
|
29
|
|
|
* @param ClientInterface $client |
|
30
|
|
|
* @param JwtParserInterface $parser |
|
31
|
|
|
*/ |
|
32
|
20 |
|
public function __construct(ClientInterface $client, JwtParserInterface $parser) |
|
33
|
|
|
{ |
|
34
|
20 |
|
parent::__construct($client); |
|
35
|
|
|
|
|
36
|
20 |
|
$this->parser = $parser; |
|
37
|
20 |
|
$this->client = $client; |
|
38
|
20 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* {@inheritdoc} |
|
42
|
|
|
*/ |
|
43
|
10 |
|
public function call(): IntrospectionResponse |
|
44
|
|
|
{ |
|
45
|
10 |
|
if ($this->get('token_type_hint') !== null && $this->get('token_type_hint') !== self::TYPE_ACCESS_TOKEN) { |
|
46
|
1 |
|
return $this->networkCall(); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
try { |
|
50
|
9 |
|
$claims = $this->parser->parse($this->get('token'), $this->client); |
|
|
|
|
|
|
51
|
3 |
|
} catch (\InvalidArgumentException $e) { |
|
52
|
3 |
|
return $this->networkCall(); |
|
|
|
|
|
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
6 |
|
$expired = $claims['exp'] ?? 0; |
|
56
|
|
|
|
|
57
|
6 |
|
$claims += ['token_type' => 'bearer', 'iss' => null]; |
|
58
|
|
|
|
|
59
|
6 |
|
if (!isset($claims['client_id']) && !empty($claims['aud'])) { |
|
60
|
5 |
|
$claims['client_id'] = is_array($claims['aud']) ? $claims['aud'][0] : $claims['aud']; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
// We check here if the issuer is right and if the token is not expired |
|
64
|
6 |
|
if ($claims['iss'] !== $this->client->provider()->issuer() || ($expired >= 0 && $expired < time())) { |
|
65
|
2 |
|
$response = new IntrospectionResponse(['active' => false]); |
|
66
|
|
|
} else { |
|
67
|
4 |
|
$response = new IntrospectionResponse(['active' => true] + $claims); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
6 |
|
$this->callResponseListeners($response); |
|
71
|
|
|
|
|
72
|
6 |
|
return $response; |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Try to call the endpoint, if configured |
|
77
|
|
|
* If not, an inactive introspection response is returned |
|
78
|
|
|
*/ |
|
79
|
4 |
|
private function networkCall(): IntrospectionResponse |
|
80
|
|
|
{ |
|
81
|
4 |
|
if ($this->client->provider()->supportsEndpoint(self::NAME)) { |
|
82
|
3 |
|
return parent::call(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
$response = new IntrospectionResponse(['active' => false]); |
|
86
|
1 |
|
$this->callResponseListeners($response); |
|
87
|
|
|
|
|
88
|
1 |
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: