|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace TMV\OpenIdClient\Service; |
|
6
|
|
|
|
|
7
|
|
|
use Http\Discovery\Psr17FactoryDiscovery; |
|
8
|
|
|
use Http\Discovery\Psr18ClientDiscovery; |
|
9
|
|
|
use Psr\Http\Client\ClientExceptionInterface; |
|
10
|
|
|
use Psr\Http\Client\ClientInterface; |
|
11
|
|
|
use Psr\Http\Message\RequestFactoryInterface; |
|
12
|
|
|
use function TMV\OpenIdClient\check_server_response; |
|
13
|
|
|
use TMV\OpenIdClient\Client\ClientInterface as OpenIDClient; |
|
14
|
|
|
use TMV\OpenIdClient\Exception\RuntimeException; |
|
15
|
|
|
use function TMV\OpenIdClient\get_endpoint_uri; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* RFC 7009 Token Revocation |
|
19
|
|
|
* |
|
20
|
|
|
* @link https://tools.ietf.org/html/rfc7009 RFC 7009 |
|
21
|
|
|
*/ |
|
22
|
|
|
class RevocationService |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ClientInterface */ |
|
25
|
|
|
private $client; |
|
26
|
|
|
|
|
27
|
|
|
/** @var RequestFactoryInterface */ |
|
28
|
|
|
private $requestFactory; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
?ClientInterface $client = null, |
|
32
|
|
|
?RequestFactoryInterface $requestFactory = null |
|
33
|
|
|
) { |
|
34
|
|
|
$this->client = $client ?: Psr18ClientDiscovery::find(); |
|
35
|
|
|
$this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function revoke(OpenIDClient $client, string $token, array $params = []): void |
|
39
|
|
|
{ |
|
40
|
|
|
$endpointUri = get_endpoint_uri($client, 'revocation_endpoint'); |
|
41
|
|
|
|
|
42
|
|
|
$authMethod = $client->getAuthMethodFactory() |
|
43
|
|
|
->create($client->getMetadata()->getRevocationEndpointAuthMethod()); |
|
44
|
|
|
|
|
45
|
|
|
$tokenRequest = $this->requestFactory->createRequest('POST', $endpointUri) |
|
46
|
|
|
->withHeader('content-type', 'application/x-www-form-urlencoded'); |
|
47
|
|
|
|
|
48
|
|
|
$tokenRequest = $authMethod->createRequest($tokenRequest, $client, $params); |
|
49
|
|
|
$tokenRequest->getBody()->write(http_build_query(array_merge($params, [ |
|
50
|
|
|
'token' => $token, |
|
51
|
|
|
]))); |
|
52
|
|
|
|
|
53
|
|
|
$httpClient = $client->getHttpClient() ?: $this->client; |
|
54
|
|
|
|
|
55
|
|
|
try { |
|
56
|
|
|
$response = $httpClient->sendRequest($tokenRequest); |
|
57
|
|
|
check_server_response($response, 200); |
|
58
|
|
|
} catch (ClientExceptionInterface $e) { |
|
59
|
|
|
throw new RuntimeException('Unable to get revocation response', 0, $e); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|