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

AbstractTokenVerifier   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 39
ccs 18
cts 20
cp 0.9
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getSigningJWKSet() 0 19 4
A getIssuerJWKFromKid() 0 16 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace TMV\OpenIdClient\Token;
6
7
use Jose\Component\Core\JWK;
8
use Jose\Component\Core\JWKSet;
9
use TMV\OpenIdClient\Client\ClientInterface;
10
use TMV\OpenIdClient\Exception\RuntimeException;
11
use TMV\OpenIdClient\Issuer\IssuerInterface;
12
use function TMV\OpenIdClient\jose_secret_key;
13
14
abstract class AbstractTokenVerifier
15
{
16 10
    protected function getSigningJWKSet(ClientInterface $client, string $expectedAlg, ?string $kid = null): JWKSet
17
    {
18 10
        $metadata = $client->getMetadata();
19 10
        $issuer = $client->getIssuer();
20
21 10
        if (0 !== strpos($expectedAlg, 'HS')) {
22
            // not symmetric key
23 2
            return null !== $kid
24 1
                ? new JWKSet([$this->getIssuerJWKFromKid($issuer, $kid)])
25 2
                : $issuer->getJwks();
26
        }
27
28 8
        $clientSecret = $metadata->getClientSecret();
29
30 8
        if (null === $clientSecret) {
31
            throw new RuntimeException('Unable to verify token without client_secret');
32
        }
33
34 8
        return new JWKSet([jose_secret_key($clientSecret)]);
35
    }
36
37 1
    protected function getIssuerJWKFromKid(IssuerInterface $issuer, string $kid): JWK
38
    {
39 1
        $jwks = $issuer->getJwks();
40
41 1
        $jwk = $jwks->selectKey('sig', null, ['kid' => $kid]);
42
43 1
        if (null === $jwk) {
44 1
            $issuer->updateJwks();
45 1
            $jwk = $issuer->getJwks()->selectKey('sig', null, ['kid' => $kid]);
46
        }
47
48 1
        if (null === $jwk) {
49
            throw new RuntimeException('Unable to find the jwk with the provided kid: ' . $kid);
50
        }
51
52 1
        return $jwk;
53
    }
54
}
55