Failed Conditions
Push — master ( ed09c2...f0d0a7 )
by Florent
01:53
created

JWSLoader   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 92
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A loadAndVerifyWithKey() 0 6 1
A loadAndVerifyWithKeySet() 0 17 4
A processSignature() 0 10 2
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 Jose\Component\Signature;
15
16
use Jose\Component\Checker\HeaderCheckerManager;
17
use Jose\Component\Core\AlgorithmManager;
18
use Jose\Component\Core\JWK;
19
use Jose\Component\Core\JWKSet;
20
use Jose\Component\Signature\Serializer\JWSSerializerManager;
21
22
final class JWSLoader
23
{
24
    /**
25
     * @var JWSVerifier
26
     */
27
    private $jwsVerifier;
28
29
    /**
30
     * @var HeaderCheckerManager
31
     */
32
    private $headerCheckerManager;
33
34
    /**
35
     * @var JWSSerializerManager
36
     */
37
    private $serializerManager;
38
39
    /**
40
     * JWSLoader constructor.
41
     *
42
     * @param JWSSerializerManager $serializerManager
43
     * @param AlgorithmManager $jwsVerifier
44
     * @param HeaderCheckerManager $headerCheckerManager
45
     */
46
    public function __construct(JWSSerializerManager $serializerManager, AlgorithmManager $jwsVerifier, HeaderCheckerManager $headerCheckerManager)
47
    {
48
        $this->serializerManager = $serializerManager;
49
        $this->jwsVerifier = $jwsVerifier;
0 ignored issues
show
Documentation Bug introduced by
It seems like $jwsVerifier of type object<Jose\Component\Core\AlgorithmManager> is incompatible with the declared type object<Jose\Component\Signature\JWSVerifier> of property $jwsVerifier.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
50
        $this->headerCheckerManager = $headerCheckerManager;
51
    }
52
53
    /**
54
     * @param string $token
55
     * @param JWK $key
56
     * @param int $signature
57
     * @param null|string $payload
58
     *
59
     * @return JWS
60
     */
61
    public function loadAndVerifyWithKey(string $token, JWK $key, int &$signature, ?string $payload = null): JWS
62
    {
63
        $keyset = JWKSet::createFromKeys([$key]);
64
65
        return $this->loadAndVerifyWithKeySet($token, $keyset, $signature, $payload);
66
    }
67
68
    /**
69
     * @param string $token
70
     * @param JWKSet $keyset
71
     * @param int $signature
72
     * @param null|string $payload
73
     * @return JWS
74
     *
75
     * @throws \Exception
76
     */
77
    public function loadAndVerifyWithKeySet(string $token, JWKSet $keyset, int &$signature, ?string $payload = null): JWS
78
    {
79
        try {
80
            $jws = $this->serializerManager->unserialize($token);
81
            $nbSignatures = $jws->countSignatures();
82
            for ($i = 0; $i < $nbSignatures; $i++) {
83
                if ($this->processSignature($jws, $keyset, $i, $payload)) {
84
                    $signature = $i;
85
86
                    return $jws;
87
                }
88
            }
89
        } catch (\Exception $e) {
90
            // Nothing to do. Exception thrown just after
91
        }
92
        throw new \Exception('Unable to load the token.');
93
    }
94
95
    /**
96
     * @param JWS $jws
97
     * @param JWKSet $keyset
98
     * @param int $signature
99
     * @param null|string $payload
100
     *
101
     * @return bool
102
     */
103
    private function processSignature(JWS $jws, JWKSet $keyset, int $signature, ?string $payload): bool
104
    {
105
        try {
106
            $this->headerCheckerManager->check($jws, $signature);
107
108
            return $this->jwsVerifier->verifyWithKeySet($jws, $keyset, $signature, $payload);
109
        } catch (\Exception $e) {
110
            return false;
111
        }
112
    }
113
}
114