ParameterCheckerManagerTest   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 220
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 17
eloc 129
dl 0
loc 220
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A anAuthorizationRequestIsReceivedButTheResponseTypeIsNotSupportedByThisServer() 0 20 2
A anAuthorizationRequestIsReceivedAndIsValid() 0 22 1
A anAuthorizationRequestIsReceivedButTheDisplayParameterIsNotValid() 0 16 2
A anAuthorizationRequestIsReceivedButThePromptParameterIsNotValid() 0 16 2
A anAuthorizationRequestIsReceivedButTheResponseTypeIsNotAllowedForTheClient() 0 20 2
A anAuthorizationRequestIsReceivedButNoResponseTypeIsSet() 0 18 2
A getParameterCheckerManager() 0 22 2
A anAuthorizationRequestIsReceivedButThePromptParameterNoneMustBeUsedAlone() 0 16 2
A anAuthorizationRequestIsReceivedButNoRedirectUriIsSet() 0 14 2
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\AuthorizationEndpoint\Tests\ParameterChecker;
15
16
use OAuth2Framework\Component\AuthorizationEndpoint\AuthorizationRequest\AuthorizationRequest;
17
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException;
18
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\DisplayParameterChecker;
19
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ParameterCheckerManager;
20
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\PromptParameterChecker;
21
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\RedirectUriParameterChecker;
22
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\ResponseTypeParameterChecker;
23
use OAuth2Framework\Component\AuthorizationEndpoint\ParameterChecker\StateParameterChecker;
24
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\FragmentResponseMode;
25
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\QueryResponseMode;
26
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseMode\ResponseModeManager;
27
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType\ResponseType;
28
use OAuth2Framework\Component\AuthorizationEndpoint\ResponseType\ResponseTypeManager;
29
use OAuth2Framework\Component\Core\Client\Client;
30
use OAuth2Framework\Component\Core\Client\ClientId;
31
use PHPUnit\Framework\TestCase;
32
use Prophecy\PhpUnit\ProphecyTrait;
33
34
/**
35
 * @group ParameterCheckerManager
36
 *
37
 * @internal
38
 */
39
final class ParameterCheckerManagerTest extends TestCase
40
{
41
    use ProphecyTrait;
42
43
    /**
44
     * @var null|ParameterCheckerManager
45
     */
46
    private $parameterCheckerManager;
47
48
    /**
49
     * @test
50
     */
51
    public function anAuthorizationRequestIsReceivedButTheDisplayParameterIsNotValid()
52
    {
53
        $client = $this->prophesize(Client::class);
54
        $client->isPublic()->willReturn(false);
55
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
56
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
57
        $authorization = new AuthorizationRequest($client->reveal(), [
58
            'display' => 'foo',
59
        ]);
60
61
        try {
62
            $this->getParameterCheckerManager()->check($authorization);
63
            static::fail('An OAuth2 exception should be thrown.');
64
        } catch (OAuth2AuthorizationException $e) {
65
            static::assertEquals('invalid_request', $e->getMessage());
66
            static::assertEquals('Invalid parameter "display". Allowed values are page, popup, touch, wap', $e->getErrorDescription());
67
        }
68
    }
69
70
    /**
71
     * @test
72
     */
73
    public function anAuthorizationRequestIsReceivedButThePromptParameterIsNotValid()
74
    {
75
        $client = $this->prophesize(Client::class);
76
        $client->isPublic()->willReturn(false);
77
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
78
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
79
        $authorization = new AuthorizationRequest($client->reveal(), [
80
            'prompt' => 'foo',
81
        ]);
82
83
        try {
84
            $this->getParameterCheckerManager()->check($authorization);
85
            static::fail('An OAuth2 exception should be thrown.');
86
        } catch (OAuth2AuthorizationException $e) {
87
            static::assertEquals('invalid_request', $e->getMessage());
88
            static::assertEquals('Invalid parameter "prompt". Allowed values are none, login, consent, select_account', $e->getErrorDescription());
89
        }
90
    }
91
92
    /**
93
     * @test
94
     */
95
    public function anAuthorizationRequestIsReceivedButThePromptParameterNoneMustBeUsedAlone()
96
    {
97
        $client = $this->prophesize(Client::class);
98
        $client->isPublic()->willReturn(false);
99
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
100
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
101
        $authorization = new AuthorizationRequest($client->reveal(), [
102
            'prompt' => 'none login',
103
        ]);
104
105
        try {
106
            $this->getParameterCheckerManager()->check($authorization);
107
            static::fail('An OAuth2 exception should be thrown.');
108
        } catch (OAuth2AuthorizationException $e) {
109
            static::assertEquals('invalid_request', $e->getMessage());
110
            static::assertEquals('Invalid parameter "prompt". Prompt value "none" must be used alone.', $e->getErrorDescription());
111
        }
112
    }
113
114
    /**
115
     * @test
116
     */
117
    public function anAuthorizationRequestIsReceivedButNoRedirectUriIsSet()
118
    {
119
        $client = $this->prophesize(Client::class);
120
        $client->isPublic()->willReturn(false);
121
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
122
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
123
        $authorization = new AuthorizationRequest($client->reveal(), []);
124
125
        try {
126
            $this->getParameterCheckerManager()->check($authorization);
127
            static::fail('An OAuth2 exception should be thrown.');
128
        } catch (OAuth2AuthorizationException $e) {
129
            static::assertEquals('invalid_request', $e->getMessage());
130
            static::assertEquals('The parameter "redirect_uri" is missing.', $e->getErrorDescription());
131
        }
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function anAuthorizationRequestIsReceivedButNoResponseTypeIsSet()
138
    {
139
        $client = $this->prophesize(Client::class);
140
        $client->isPublic()->willReturn(false);
141
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
142
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
143
        $client->has('redirect_uris')->willReturn(true);
144
        $client->get('redirect_uris')->willReturn(['https://www.foo.bar/callback']);
145
        $authorization = new AuthorizationRequest($client->reveal(), [
146
            'redirect_uri' => 'https://www.foo.bar/callback',
147
        ]);
148
149
        try {
150
            $this->getParameterCheckerManager()->check($authorization);
151
            static::fail('An OAuth2 exception should be thrown.');
152
        } catch (OAuth2AuthorizationException $e) {
153
            static::assertEquals('invalid_request', $e->getMessage());
154
            static::assertEquals('The parameter "response_type" is mandatory.', $e->getErrorDescription());
155
        }
156
    }
157
158
    /**
159
     * @test
160
     */
161
    public function anAuthorizationRequestIsReceivedButTheResponseTypeIsNotSupportedByThisServer()
162
    {
163
        $client = $this->prophesize(Client::class);
164
        $client->isPublic()->willReturn(false);
165
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
166
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
167
        $client->has('redirect_uris')->willReturn(true);
168
        $client->get('redirect_uris')->willReturn(['https://www.foo.bar/callback']);
169
        $client->isResponseTypeAllowed('foo')->willReturn(true);
170
        $authorization = new AuthorizationRequest($client->reveal(), [
171
            'redirect_uri' => 'https://www.foo.bar/callback',
172
            'response_type' => 'bar',
173
        ]);
174
175
        try {
176
            $this->getParameterCheckerManager()->check($authorization);
177
            static::fail('An OAuth2 exception should be thrown.');
178
        } catch (OAuth2AuthorizationException $e) {
179
            static::assertEquals('invalid_request', $e->getMessage());
180
            static::assertEquals('The response type "bar" is not supported by this server', $e->getErrorDescription());
181
        }
182
    }
183
184
    /**
185
     * @test
186
     */
187
    public function anAuthorizationRequestIsReceivedButTheResponseTypeIsNotAllowedForTheClient()
188
    {
189
        $client = $this->prophesize(Client::class);
190
        $client->isPublic()->willReturn(false);
191
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
192
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
193
        $client->has('redirect_uris')->willReturn(true);
194
        $client->get('redirect_uris')->willReturn(['https://www.foo.bar/callback']);
195
        $client->isResponseTypeAllowed('foo')->willReturn(false);
196
        $authorization = new AuthorizationRequest($client->reveal(), [
197
            'redirect_uri' => 'https://www.foo.bar/callback',
198
            'response_type' => 'foo',
199
        ]);
200
201
        try {
202
            $this->getParameterCheckerManager()->check($authorization);
203
            static::fail('An OAuth2 exception should be thrown.');
204
        } catch (OAuth2AuthorizationException $e) {
205
            static::assertEquals('invalid_request', $e->getMessage());
206
            static::assertEquals('The response type "foo" is not allowed for this client.', $e->getErrorDescription());
207
        }
208
    }
209
210
    /**
211
     * @test
212
     */
213
    public function anAuthorizationRequestIsReceivedAndIsValid()
214
    {
215
        $client = $this->prophesize(Client::class);
216
        $client->isPublic()->willReturn(false);
217
        $client->getPublicId()->willReturn(new ClientId('CLIENT_ID'));
218
        $client->getClientId()->willReturn(new ClientId('CLIENT_ID'));
219
        $client->has('redirect_uris')->willReturn(true);
220
        $client->get('redirect_uris')->willReturn(['https://www.foo.bar/callback']);
221
        $client->isResponseTypeAllowed('foo')->willReturn(true);
222
        $authorization = new AuthorizationRequest($client->reveal(), [
223
            'redirect_uri' => 'https://www.foo.bar/callback',
224
            'response_type' => 'foo',
225
            'state' => '0123456789',
226
            'prompt' => 'login consent',
227
            'display' => 'wap',
228
            'response_mode' => 'fragment',
229
        ]);
230
231
        $this->getParameterCheckerManager()->check($authorization);
232
233
        static::assertEquals(['login', 'consent'], $authorization->getPrompt());
234
        static::assertFalse($authorization->hasPrompt('none'));
235
    }
236
237
    private function getParameterCheckerManager(): ParameterCheckerManager
238
    {
239
        if (null === $this->parameterCheckerManager) {
240
            $responseType = $this->prophesize(ResponseType::class);
241
            $responseType->name()->willReturn('foo');
242
            $responseType->getResponseMode()->willReturn('query');
243
            $responseTypeManager = new ResponseTypeManager();
244
            $responseTypeManager->add($responseType->reveal());
245
246
            $responseModeManager = new ResponseModeManager();
247
            $responseModeManager->add(new QueryResponseMode());
248
            $responseModeManager->add(new FragmentResponseMode());
249
250
            $this->parameterCheckerManager = new ParameterCheckerManager();
251
            $this->parameterCheckerManager->add(new DisplayParameterChecker());
252
            $this->parameterCheckerManager->add(new PromptParameterChecker());
253
            $this->parameterCheckerManager->add(new RedirectUriParameterChecker());
254
            $this->parameterCheckerManager->add(new ResponseTypeParameterChecker($responseTypeManager));
255
            $this->parameterCheckerManager->add(new StateParameterChecker());
256
        }
257
258
        return $this->parameterCheckerManager;
259
    }
260
}
261