1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Parroauth2\Client\EndPoint\Token; |
4
|
|
|
|
5
|
|
|
use Parroauth2\Client\ClientInterface; |
6
|
|
|
use Parroauth2\Client\EndPoint\CallableEndPointInterface; |
7
|
|
|
use Parroauth2\Client\EndPoint\EndPointParametersTrait; |
8
|
|
|
use Parroauth2\Client\EndPoint\EndPointResponseListenerTrait; |
9
|
|
|
use Parroauth2\Client\EndPoint\EndPointTransformerInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The token revocation endpoint |
14
|
|
|
* |
15
|
|
|
* @see https://tools.ietf.org/html/rfc7009 |
16
|
|
|
* |
17
|
|
|
* @implements CallableEndPointInterface<ResponseInterface> |
18
|
|
|
*/ |
19
|
|
|
class RevocationEndPoint implements CallableEndPointInterface |
20
|
|
|
{ |
21
|
|
|
use EndPointParametersTrait; |
22
|
|
|
/** @use EndPointResponseListenerTrait<ResponseInterface> */ |
23
|
|
|
use EndPointResponseListenerTrait; |
24
|
|
|
|
25
|
|
|
public const NAME = 'revocation'; |
26
|
|
|
|
27
|
|
|
public const TYPE_ACCESS_TOKEN = 'access_token'; |
28
|
|
|
public const TYPE_REFRESH_TOKEN = 'refresh_token'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var ClientInterface |
32
|
|
|
* @readonly |
33
|
|
|
*/ |
34
|
|
|
private $client; |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* RevocationEndPoint constructor. |
39
|
|
|
* |
40
|
|
|
* @param ClientInterface $client |
41
|
|
|
*/ |
42
|
142 |
|
public function __construct(ClientInterface $client) |
43
|
|
|
{ |
44
|
142 |
|
$this->client = $client; |
45
|
142 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
* |
50
|
|
|
* @psalm-mutation-free |
51
|
|
|
*/ |
52
|
142 |
|
public function name(): string |
53
|
|
|
{ |
54
|
142 |
|
return self::NAME; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
3 |
|
public function apply(EndPointTransformerInterface $transformer): RevocationEndPoint |
61
|
|
|
{ |
62
|
3 |
|
return $transformer->onRevocation($this); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Define the token to revoke |
67
|
|
|
* |
68
|
|
|
* @param string $token Token to revoke. May be an access token or a refresh token |
69
|
|
|
* |
70
|
|
|
* @return static |
71
|
|
|
* |
72
|
|
|
* @psalm-mutation-free |
73
|
|
|
*/ |
74
|
5 |
|
public function token(string $token): RevocationEndPoint |
75
|
|
|
{ |
76
|
5 |
|
return $this->set('token', $token); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Define the token type |
81
|
|
|
* The server can ignore this value if not match with the token |
82
|
|
|
* |
83
|
|
|
* @param string $type the token type |
84
|
|
|
* |
85
|
|
|
* @return static |
86
|
|
|
* |
87
|
|
|
* @psalm-mutation-free |
88
|
|
|
*/ |
89
|
5 |
|
public function typeHint(string $type): RevocationEndPoint |
90
|
|
|
{ |
91
|
5 |
|
return $this->set('token_type_hint', $type); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Try to revoke an access token |
96
|
|
|
* |
97
|
|
|
* @param string $token The access token |
98
|
|
|
* |
99
|
|
|
* @return static |
100
|
|
|
* |
101
|
|
|
* @psalm-mutation-free |
102
|
|
|
*/ |
103
|
4 |
|
public function accessToken(string $token): RevocationEndPoint |
104
|
|
|
{ |
105
|
4 |
|
return $this->token($token)->typeHint(self::TYPE_ACCESS_TOKEN); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Try to revoke a refresh token |
110
|
|
|
* |
111
|
|
|
* @param string $token The refresh token |
112
|
|
|
* |
113
|
|
|
* @return static |
114
|
|
|
* |
115
|
|
|
* @psalm-mutation-free |
116
|
|
|
*/ |
117
|
1 |
|
public function refreshToken(string $token): RevocationEndPoint |
118
|
|
|
{ |
119
|
1 |
|
return $this->token($token)->typeHint(self::TYPE_REFRESH_TOKEN); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
5 |
|
public function call(): ResponseInterface |
126
|
|
|
{ |
127
|
5 |
|
$request = $this->client->endPoints() |
128
|
5 |
|
->request('POST', $this) |
129
|
5 |
|
->withHeader( |
130
|
5 |
|
'Authorization', |
131
|
5 |
|
'Basic ' . base64_encode($this->client->clientId() . ':' . $this->client->secret()) |
132
|
|
|
) |
133
|
|
|
; |
134
|
|
|
|
135
|
5 |
|
$response = $this->client->provider()->sendRequest($request); |
136
|
5 |
|
$this->callResponseListeners($response); |
137
|
|
|
|
138
|
5 |
|
return $response; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: