Passed
Pull Request — master (#11)
by Pol
02:31
created

testUnpackAggregatedClaimsWithSignedJWT()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 44
nc 1
nop 0
dl 0
loc 65
rs 9.216
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\OpenIDClientTest\Claims;
6
7
use Facile\JoseVerifier\JWK\JwksProviderInterface;
8
use Facile\OpenIDClient\Claims\AggregateParser;
9
use Facile\OpenIDClient\Client\ClientInterface;
10
use Facile\OpenIDClient\Issuer\IssuerBuilderInterface;
11
use Facile\OpenIDClient\Issuer\IssuerInterface;
12
use Facile\OpenIDClient\Issuer\Metadata\IssuerMetadataInterface;
13
use Jose\Component\Core\AlgorithmManager;
14
use Jose\Component\Core\JWK;
15
use Jose\Component\KeyManagement\JWKFactory;
16
use Jose\Component\Signature\Algorithm\RS256;
17
use Jose\Component\Signature\JWS;
18
use Jose\Component\Signature\JWSBuilder;
19
use Jose\Component\Signature\JWSVerifier;
20
use Jose\Component\Signature\Serializer\CompactSerializer;
21
use Jose\Component\Signature\Serializer\JWSSerializer;
22
use function json_encode;
23
use Facile\OpenIDClientTest\TestCase;
24
use Prophecy\Argument;
25
26
class AggregatedClaimsTest extends TestCase
27
{
28
    public function testUnpackAggregatedClaimsWithNoClaimSources(): void
29
    {
30
        $algorithmManager = $this->prophesize(AlgorithmManager::class);
31
        $JWSVerifier = $this->prophesize(JWSVerifier::class);
32
        $JWSSerializer = $this->prophesize(JWSSerializer::class);
33
        $client = $this->prophesize(ClientInterface::class);
34
        $issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
35
36
        $service = new AggregateParser(
37
            $issuerBuilder->reveal(),
38
            $algorithmManager->reveal(),
39
            $JWSVerifier->reveal(),
40
            $JWSSerializer->reveal()
41
        );
42
43
        $claims = [
44
            'sub' => 'foo',
45
            '_claim_names' => [
46
                'eye_color' => 'src1',
47
                'shoe_size' => 'src1',
48
            ],
49
        ];
50
51
        $unpacked = $service->unpack($client->reveal(), $claims);
52
53
        static::assertSame($claims, $unpacked);
54
    }
55
56
    public function testUnpackAggregatedClaimsWithNoClaimNames(): void
57
    {
58
        $algorithmManager = $this->prophesize(AlgorithmManager::class);
59
        $JWSVerifier = $this->prophesize(JWSVerifier::class);
60
        $issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
61
        $client = $this->prophesize(ClientInterface::class);
62
63
        $service = new AggregateParser(
64
            $issuerBuilder->reveal(),
65
            $algorithmManager->reveal(),
66
            $JWSVerifier->reveal()
67
        );
68
69
        $claims = [
70
            'sub' => 'foo',
71
            '_claim_sources' => [
72
                'src1' => [
73
                    'JWT' => 'foo',
74
                ],
75
            ],
76
        ];
77
78
        $unpacked = $service->unpack($client->reveal(), $claims);
79
80
        static::assertSame($claims, $unpacked);
81
    }
82
83
    public function testUnpackAggregatedClaims(): void
84
    {
85
        $jwt = 'eyJhbGciOiJub25lIn0.eyJleWVfY29sb3IiOiAiYmx1ZSIsICJzaG9lX3NpemUiOiA4fQ.';
86
87
        $algorithmManager = $this->prophesize(AlgorithmManager::class);
88
        $JWSVerifier = $this->prophesize(JWSVerifier::class);
89
        $issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
90
        $client = $this->prophesize(ClientInterface::class);
91
        $issuer = $this->prophesize(IssuerInterface::class);
92
93
        $client->getIssuer()->willReturn($issuer->reveal());
94
95
        $service = new AggregateParser(
96
            $issuerBuilder->reveal(),
97
            $algorithmManager->reveal(),
98
            $JWSVerifier->reveal()
99
        );
100
101
        $claims = [
102
            'sub' => 'foo',
103
            '_claim_names' => [
104
                'eye_color' => 'src1',
105
                'shoe_size' => 'src1',
106
            ],
107
            '_claim_sources' => [
108
                'src1' => [
109
                    'JWT' => $jwt,
110
                ],
111
            ],
112
        ];
113
114
        $unpacked = $service->unpack($client->reveal(), $claims);
115
116
        static::assertSame('blue', $unpacked['eye_color'] ?? null);
117
        static::assertSame(8, $unpacked['shoe_size'] ?? null);
118
        static::assertArrayNotHasKey('_claim_names', $unpacked);
119
        static::assertArrayNotHasKey('_claim_sources', $unpacked);
120
    }
121
122
    public function testUnpackAggregatedClaimsWithSignedJWT(): void
123
    {
124
        $jwk = JWKFactory::createRSAKey(2048, ['alg' => 'RS256', 'use' => 'sig']);
125
        $jwkPublic = $jwk->toPublic();
126
127
        $jwsBuilder = new JWSBuilder(new AlgorithmManager([new RS256()]));
128
        $serializer = new CompactSerializer();
129
        $jws = $jwsBuilder->create()
130
            ->withPayload((string) json_encode([
131
                'eye_color' => 'blue',
132
            ]))
133
            ->addSignature($jwk, ['alg' => 'RS256', 'use' => 'sig'])
134
            ->build();
135
136
        $jwt = $serializer->serialize($jws, 0);
137
138
        $algorithmManager = $this->prophesize(AlgorithmManager::class);
139
        $JWSVerifier = $this->prophesize(JWSVerifier::class);
140
        $client = $this->prophesize(ClientInterface::class);
141
        $issuer = $this->prophesize(IssuerInterface::class);
142
        $issuerMetadata = $this->prophesize(IssuerMetadataInterface::class);
143
        $issuerBuilder = $this->prophesize(IssuerBuilderInterface::class);
144
        $issuerJwksProvider = $this->prophesize(JwksProviderInterface::class);
145
146
        $algorithm = new RS256();
147
        $algorithmManager->get('RS256')->willReturn($algorithm);
148
149
        $JWSVerifier->verifyWithKey(Argument::type(JWS::class), Argument::that(function (JWK $key) use ($jwkPublic) {
150
            return $jwkPublic->all() === $key->all();
151
        }), 0)
152
            ->willReturn(true);
153
154
        $client->getIssuer()->willReturn($issuer->reveal());
155
        $issuerMetadata->getIssuer()->willReturn('foo-issuer');
156
        $issuer->getMetadata()->willReturn($issuerMetadata->reveal());
157
        $issuer->getJwksProvider()->willReturn($issuerJwksProvider->reveal());
158
        $issuerJwksProvider->getJwks()->willReturn([
159
            'keys' => [
160
                $jwkPublic->all(),
161
            ],
162
        ]);
163
164
        $service = new AggregateParser(
165
            $issuerBuilder->reveal(),
166
            $algorithmManager->reveal(),
167
            $JWSVerifier->reveal()
168
        );
169
170
        $claims = [
171
            'sub' => 'foo',
172
            '_claim_names' => [
173
                'eye_color' => 'src1',
174
            ],
175
            '_claim_sources' => [
176
                'src1' => [
177
                    'JWT' => $jwt,
178
                ],
179
            ],
180
        ];
181
182
        $unpacked = $service->unpack($client->reveal(), $claims);
183
184
        static::assertSame('blue', $unpacked['eye_color'] ?? null);
185
        static::assertArrayNotHasKey('_claim_names', $unpacked);
186
        static::assertArrayNotHasKey('_claim_sources', $unpacked);
187
    }
188
}
189