Failed Conditions
Push — master ( 2eee97...42d85f )
by Florent
09:02
created

getSupportedKeyEncryptionAlgorithms()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace OAuth2Framework\Component\Server\Model\IdToken;
15
16
use Assert\Assertion;
17
use Jose\Component\Core\JWKSet;
18
use Jose\Component\Signature\JWSLoader;
19
20
final class IdTokenLoader
21
{
22
    /**
23
     * @var JWKSet
24
     */
25
    private $signatureKeySet;
26
27
    /**
28
     * @var JWSLoader
29
     */
30
    private $jwsLoader;
31
32
    /**
33
     * @var string[]
34
     */
35
    private $signatureAlgorithms;
36
37
    /**
38
     * IdTokenLoader constructor.
39
     *
40
     * @param JWSLoader $jwsLoader
41
     * @param JWKSet    $signatureKeySet
42
     * @param array     $signatureAlgorithms
43
     */
44
    public function __construct(JWSLoader $jwsLoader, JWKSet $signatureKeySet, array $signatureAlgorithms)
45
    {
46
        $this->signatureAlgorithms = $signatureAlgorithms;
47
        $this->signatureKeySet = $signatureKeySet;
48
        $this->jwsLoader = $jwsLoader;
49
    }
50
51
    /**
52
     * @return string[]
53
     */
54
    public function getSupportedSignatureAlgorithms(): array
55
    {
56
        return $this->jwsLoader->getSignatureAlgorithmManager()->list();
57
    }
58
59
    /**
60
     * @param IdTokenId $idTokenId
61
     *
62
     * @return IdToken|null
63
     */
64
    public function load(IdTokenId $idTokenId): ? IdToken
65
    {
66
        $value = $idTokenId->getValue();
67
        try {
68
            $jwt = $this->jwsLoader->load($value);
69
            $claims = json_decode($jwt->getPayload(), true);
70
            Assertion::isArray($claims, 'Invalid ID Token');
71
            $validSignature = $this->jwsLoader->verifyWithKeySet($jwt, $this->signatureKeySet);
72
            Assertion::inArray($jwt->getSignature($validSignature)->getProtectedHeader('alg'), $this->signatureAlgorithms);
73
            $idToken = IdToken::create($idTokenId, $claims);
74
75
            return $idToken;
76
        } catch (\Exception $e) {
77
            return null;
78
        }
79
    }
80
}
81