IdTokenAlgorithmsRuleTest   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 57
dl 0
loc 110
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getCallable() 0 4 1
A theIdTokenContentEncryptionAlgorithmsIsNotSupported() 0 14 1
A theIdTokenKeyEncryptionAlgorithmsIsNotSupported() 0 14 1
A getJWEBuilder() 0 11 1
A theIdTokenSignatureAlgorithmIsNotSupported() 0 13 1
A getJWSBuilder() 0 7 1
A theIdTokenAlgorithmsAreSupported() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 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\OpenIdConnect\Tests;
15
16
use Jose\Component\Core\Algorithm;
17
use Jose\Component\Core\AlgorithmManager;
18
use Jose\Component\Encryption\Compression\CompressionMethodManager;
19
use Jose\Component\Encryption\JWEBuilder;
20
use Jose\Component\Signature\JWSBuilder;
21
use OAuth2Framework\Component\ClientRule\RuleHandler;
22
use OAuth2Framework\Component\Core\Client\ClientId;
23
use OAuth2Framework\Component\Core\DataBag\DataBag;
24
use OAuth2Framework\Component\OpenIdConnect\Rule\IdTokenAlgorithmsRule;
25
use PHPUnit\Framework\TestCase;
26
use Prophecy\PhpUnit\ProphecyTrait;
27
28
/**
29
 * @group Tests
30
 *
31
 * @internal
32
 */
33
final class IdTokenAlgorithmsRuleTest extends TestCase
34
{
35
    use ProphecyTrait;
36
37
    /**
38
     * @test
39
     */
40
    public function theIdTokenAlgorithmsAreSupported()
41
    {
42
        $clientId = new ClientId('CLIENT_ID');
43
        $commandParameters = new DataBag([
44
            'id_token_signed_response_alg' => 'XS512',
45
            'id_token_encrypted_response_alg' => 'RSA_2_5',
46
            'id_token_encrypted_response_enc' => 'A512ECE+XS512',
47
        ]);
48
        $rule = new IdTokenAlgorithmsRule(
49
            $this->getJWSBuilder(),
50
            $this->getJWEBuilder()
51
        );
52
        $validatedParameters = $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
53
54
        static::assertTrue($validatedParameters->has('id_token_signed_response_alg'));
55
        static::assertTrue($validatedParameters->has('id_token_encrypted_response_alg'));
56
        static::assertTrue($validatedParameters->has('id_token_encrypted_response_enc'));
57
    }
58
59
    /**
60
     * @test
61
     */
62
    public function theIdTokenSignatureAlgorithmIsNotSupported()
63
    {
64
        $this->expectException(\InvalidArgumentException::class);
65
        $this->expectExceptionMessage('The parameter "id_token_signed_response_alg" must be an algorithm supported by this server. Please choose one of the following value(s): XS512');
66
        $clientId = new ClientId('CLIENT_ID');
67
        $commandParameters = new DataBag([
68
            'id_token_signed_response_alg' => 'foo',
69
        ]);
70
        $rule = new IdTokenAlgorithmsRule(
71
            $this->getJWSBuilder(),
72
            $this->getJWEBuilder()
73
        );
74
        $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
75
    }
76
77
    /**
78
     * @test
79
     */
80
    public function theIdTokenKeyEncryptionAlgorithmsIsNotSupported()
81
    {
82
        $this->expectException(\InvalidArgumentException::class);
83
        $this->expectExceptionMessage('The parameter "id_token_encrypted_response_alg" must be an algorithm supported by this server. Please choose one of the following value(s): RSA_2_5');
84
        $clientId = new ClientId('CLIENT_ID');
85
        $commandParameters = new DataBag([
86
            'id_token_encrypted_response_alg' => 'foo',
87
            'id_token_encrypted_response_enc' => 'foo',
88
        ]);
89
        $rule = new IdTokenAlgorithmsRule(
90
            $this->getJWSBuilder(),
91
            $this->getJWEBuilder()
92
        );
93
        $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
94
    }
95
96
    /**
97
     * @test
98
     */
99
    public function theIdTokenContentEncryptionAlgorithmsIsNotSupported()
100
    {
101
        $this->expectException(\InvalidArgumentException::class);
102
        $this->expectExceptionMessage('The parameter "id_token_encrypted_response_enc" must be an algorithm supported by this server. Please choose one of the following value(s): A512ECE+XS512');
103
        $clientId = new ClientId('CLIENT_ID');
104
        $commandParameters = new DataBag([
105
            'id_token_encrypted_response_alg' => 'RSA_2_5',
106
            'id_token_encrypted_response_enc' => 'foo',
107
        ]);
108
        $rule = new IdTokenAlgorithmsRule(
109
            $this->getJWSBuilder(),
110
            $this->getJWEBuilder()
111
        );
112
        $rule->handle($clientId, $commandParameters, new DataBag([]), $this->getCallable());
113
    }
114
115
    private function getJWSBuilder(): JWSBuilder
116
    {
117
        $algorithm = $this->prophesize(Algorithm::class);
118
        $algorithm->name()->willReturn('XS512');
119
120
        return new JWSBuilder(
121
            new AlgorithmManager([$algorithm->reveal()])
122
        );
123
    }
124
125
    private function getJWEBuilder(): JWEBuilder
126
    {
127
        $algorithm1 = $this->prophesize(Algorithm::class);
128
        $algorithm1->name()->willReturn('RSA_2_5');
129
        $algorithm2 = $this->prophesize(Algorithm::class);
130
        $algorithm2->name()->willReturn('A512ECE+XS512');
131
132
        return new JWEBuilder(
133
            new AlgorithmManager([$algorithm1->reveal()]),
134
            new AlgorithmManager([$algorithm2->reveal()]),
135
            new CompressionMethodManager([])
136
        );
137
    }
138
139
    private function getCallable(): RuleHandler
140
    {
141
        return new RuleHandler(function (ClientId $clientId, DataBag $commandParameters, DataBag $validatedParameters): DataBag {
142
            return $validatedParameters;
143
        });
144
    }
145
}
146