Passed
Push — master ( 106006...c1ddb5 )
by Thomas Mauro
06:38 queued 11s
created

RevocationService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
dl 0
loc 36
ccs 0
cts 23
cp 0
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A revoke() 0 20 2
A __construct() 0 6 3
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
        try {
54
            $response = $this->client->sendRequest($tokenRequest);
55
            check_server_response($response, 200);
56
        } catch (ClientExceptionInterface $e) {
57
            throw new RuntimeException('Unable to get revocation response', 0, $e);
58
        }
59
    }
60
}
61