JwsIdTokenParser::parse()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 24
ccs 11
cts 12
cp 0.9167
rs 9.6111
cc 5
nc 4
nop 2
crap 5.0144
1
<?php
2
3
namespace Parroauth2\Client\OpenID\IdToken;
4
5
use Jose\Component\Core\JWKSet;
6
use Jose\Component\KeyManagement\JWKFactory;
7
use Parroauth2\Client\Client;
8
use Parroauth2\Client\ClientInterface;
9
use Parroauth2\Client\Jwt\JWA;
10
use Parroauth2\Client\Jwt\JwtDecoder;
11
12
/**
13
 * Parse the ID Token in a JWS format
14
 */
15
final class JwsIdTokenParser implements IdTokenParserInterface
16
{
17
    /**
18
     * @var JwtDecoder
19
     */
20
    private $decoder;
21
22
23
    /**
24
     * JwsIdTokenParser constructor.
25
     *
26
     * @param JwtDecoder|null $decoder
27
     */
28 117
    public function __construct(?JwtDecoder $decoder = null)
29
    {
30 117
        $this->decoder = $decoder ?? new JwtDecoder();
31 117
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 19
    public function parse(ClientInterface $client, string $idToken): IdToken
37
    {
38 19
        $keySet = $client->keySet();
39 19
        $decoder = $this->decoder;
40
41 19
        if ($supportedAlg = $client->option('id_token_signing_alg_values_supported')) {
42
            $decoder = $decoder->supportedAlgorithms($supportedAlg);
43
        }
44
45
        // Add client secret key to the set for HMAC signature
46
        // @see https://openid.net/specs/openid-connect-core-1_0.html#rfc.section.10.1
47 19
        if ($client->secret() && $hmacAlgorithms = $decoder->jwa()->algorithmsByType(JWA::TYPE_HMAC)) {
48 18
            $keySet = $keySet->all();
49
50 18
            foreach ($hmacAlgorithms as $alg) {
51 18
                $keySet[] = JWKFactory::createFromSecret($client->secret(), ['alg' => $alg, 'use' => 'sig']);
52
            }
53
54 18
            $keySet = new JWKSet($keySet);
55
        }
56
57 19
        $jwt = $decoder->decode($idToken, $keySet);
58
59 17
        return new IdToken($idToken, $jwt->payload(), $jwt->headers());
60
    }
61
}
62