Passed
Pull Request — master (#7)
by Marco Aurélio
09:15 queued 04:38
created

TokenParser   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 2
b 0
f 0
dl 0
loc 44
ccs 18
cts 18
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A loadAndVerifyWithKeySet() 0 11 1
A unverifiedPayload() 0 5 1
A parse() 0 12 1
1
<?php declare(strict_types=1);
2
3
namespace CustomerGauge\Cognito;
4
5
use Jose\Component\Checker\ClaimCheckerManager;
6
use Jose\Component\Checker\ExpirationTimeChecker;
7
use Jose\Component\Checker\IssuerChecker;
8
use Jose\Component\Core\AlgorithmManager;
9
use Jose\Component\Core\JWKSet;
10
use Jose\Component\Signature\Algorithm\RS256;
11
use Jose\Component\Signature\JWS;
12
use Jose\Component\Signature\JWSLoader;
13
use Jose\Component\Signature\JWSVerifier;
14
use Jose\Component\Signature\Serializer\CompactSerializer;
15
use Jose\Component\Signature\Serializer\JWSSerializerManager;
16
17
final class TokenParser
18
{
19
    private $keyResolver;
20
21 4
    public function __construct(KeyResolver $keyResolver)
22
    {
23 4
        $this->keyResolver = $keyResolver;
24
    }
25
    
26
    /**
27
     * This public method is useful for parsing the token from PHPUnit. It is not intended for production use.
28
     */
29 4
    public function unverifiedPayload(string $token): array
30
    {
31 4
        $jws = $this->loadAndVerifyWithKeySet($token);
32
33 3
        return json_decode($jws->getPayload(), true);
0 ignored issues
show
Bug introduced by
It seems like $jws->getPayload() can also be of type null; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        return json_decode(/** @scrutinizer ignore-type */ $jws->getPayload(), true);
Loading history...
34
    }
35
36 4
    public function parse(string $token)
37
    {
38 4
        $payload = $this->unverifiedPayload($token);
39
40 3
        $claimCheckerManager = new ClaimCheckerManager([
41 3
            new IssuerChecker([$this->keyResolver->issuer()->toString()]),
42 3
            new ExpirationTimeChecker,
43
        ]);
44
45 3
        $claimCheckerManager->check($payload);
46
47 3
        return $payload;
48
    }
49
50 4
    private function loadAndVerifyWithKeySet(string $token): JWS
51
    {
52 4
        $jwsVerifier = new JWSVerifier(new AlgorithmManager([new RS256()]));
53
54 4
        $serializerManager = new JWSSerializerManager([new CompactSerializer()]);
55
56 4
        $jwsLoader = new JWSLoader($serializerManager, $jwsVerifier, null);
57
58 4
        $jwkset = JWKSet::createFromJson($this->keyResolver->jwkset());
59
60 4
        return $jwsLoader->loadAndVerifyWithKeySet($token, $jwkset, $signature);
61
    }
62
}
63