AbstractTokenVerifier::getIssuerJWKFromKid()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 4
nop 2
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
crap 3.0123
rs 10
c 1
b 0
f 0
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 13
    protected function getSigningJWKSet(ClientInterface $client, string $expectedAlg, ?string $kid = null): JWKSet
17
    {
18 13
        $metadata = $client->getMetadata();
19 13
        $issuer = $client->getIssuer();
20
21 13
        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 11
        $clientSecret = $metadata->getClientSecret();
29
30 11
        if (null === $clientSecret) {
31
            throw new RuntimeException('Unable to verify token without client_secret');
32
        }
33
34 11
        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