Failed Conditions
Push — ng ( ca946d...fcb055 )
by Florent
10:43
created

aResourceServerIdIsSetButTheResourceServerCredentialsExpired()   B

Complexity

Conditions 2
Paths 3

Size

Total Lines 38
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
cc 2
eloc 29
nc 3
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2018 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\ResourceServerAuthentication\Tests;
15
16
use Psr\Http\Server\RequestHandlerInterface;
17
use OAuth2Framework\Component\Core\ResourceServer\ResourceServer;
18
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerId;
19
use OAuth2Framework\Component\Core\ResourceServer\ResourceServerRepository;
20
use OAuth2Framework\Component\Core\DataBag\DataBag;
21
use OAuth2Framework\Component\Core\Exception\OAuth2Exception;
22
use OAuth2Framework\Component\ResourceServerAuthentication\AuthenticationMethod;
23
use OAuth2Framework\Component\ResourceServerAuthentication\AuthenticationMethodManager;
24
use OAuth2Framework\Component\ResourceServerAuthentication\AuthenticationMiddleware;
25
use PHPUnit\Framework\TestCase;
26
use Prophecy\Argument;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
30
/**
31
 * @group TokenEndpoint
32
 * @group ResourceServerAuthenticationMiddleware
33
 */
34
class AuthenticationMiddlewareTest extends TestCase
35
{
36
    /**
37
     * @test
38
     */
39
    public function noResourceServerIsFoundInTheRequest()
40
    {
41
        $response = $this->prophesize(ResponseInterface::class);
42
        $request = $this->prophesize(ServerRequestInterface::class);
43
        $request->getHeader('Authorization')->willReturn([])->shouldBeCalled();
44
        $handler = $this->prophesize(RequestHandlerInterface::class);
45
        $ResourceServerRepository = $this->prophesize(ResourceServerRepository::class);
46
        $handler->handle(Argument::type(ServerRequestInterface::class))
47
            ->shouldBeCalled()
48
            ->willReturn($response->reveal())
49
        ;
50
51
        $this->getResourceServerAuthenticationMiddleware($ResourceServerRepository->reveal())->process($request->reveal(), $handler->reveal());
52
    }
53
54
    /**
55
     * @test
56
     */
57
    public function aResourceServerIdIsSetButTheResourceServerDoesNotExist()
58
    {
59
        $request = $this->prophesize(ServerRequestInterface::class);
60
        $request->getHeader('Authorization')
61
            ->willReturn([
62
                'Basic '.base64_encode('FOO:BAR'),
63
            ])
64
            ->shouldBeCalled();
65
        $handler = $this->prophesize(RequestHandlerInterface::class);
66
        $ResourceServerRepository = $this->prophesize(ResourceServerRepository::class);
67
        $ResourceServerRepository->find(Argument::type(ResourceServerId::class))->willReturn(null)->shouldBeCalled();
68
        $handler->handle(Argument::type(ServerRequestInterface::class))
69
            ->shouldNotBeCalled()
70
        ;
71
72
        try {
73
            $this->getResourceServerAuthenticationMiddleware($ResourceServerRepository->reveal())->process($request->reveal(), $handler->reveal());
74
            $this->fail('An OAuth2 exception should be thrown.');
75
        } catch (OAuth2Exception $e) {
76
            self::assertEquals(401, $e->getCode());
77
            self::assertEquals([
78
                'error' => 'invalid_ResourceServer',
79
                'error_description' => 'Unknown ResourceServer or ResourceServer not authenticated.',
80
            ], $e->getData());
81
        }
82
    }
83
84
    /**
85
     * @test
86
     */
87
    public function aResourceServerIdIsSetButTheResourceServerIsDeleted()
88
    {
89
        $ResourceServer = ResourceServer::createEmpty();
0 ignored issues
show
Bug introduced by
The method createEmpty() does not seem to exist on object<OAuth2Framework\C...eServer\ResourceServer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
90
        $ResourceServer = $ResourceServer->create(
91
            ResourceServerId::create('FOO'),
92
            DataBag::create([]),
93
            null
94
        );
95
        $ResourceServer = $ResourceServer->markAsDeleted();
96
        $ResourceServer->eraseMessages();
97
98
        $request = $this->prophesize(ServerRequestInterface::class);
99
        $request->getHeader('Authorization')
100
            ->willReturn([
101
                'Basic '.base64_encode('FOO:BAR'),
102
            ])
103
            ->shouldBeCalled();
104
        $handler = $this->prophesize(RequestHandlerInterface::class);
105
        $ResourceServerRepository = $this->prophesize(ResourceServerRepository::class);
106
        $ResourceServerRepository->find(Argument::type(ResourceServerId::class))->willReturn($ResourceServer)->shouldBeCalled();
107
        $handler->handle(Argument::type(ServerRequestInterface::class))
108
            ->shouldNotBeCalled()
109
        ;
110
111
        try {
112
            $this->getResourceServerAuthenticationMiddleware($ResourceServerRepository->reveal())->process($request->reveal(), $handler->reveal());
113
            $this->fail('An OAuth2 exception should be thrown.');
114
        } catch (OAuth2Exception $e) {
115
            self::assertEquals(401, $e->getCode());
116
            self::assertEquals([
117
                'error' => 'invalid_ResourceServer',
118
                'error_description' => 'Unknown ResourceServer or ResourceServer not authenticated.',
119
            ], $e->getData());
120
        }
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function aResourceServerIdIsSetButTheResourceServerCredentialsExpired()
127
    {
128
        $ResourceServer = ResourceServer::createEmpty();
0 ignored issues
show
Bug introduced by
The method createEmpty() does not seem to exist on object<OAuth2Framework\C...eServer\ResourceServer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
129
        $ResourceServer = $ResourceServer->create(
130
            ResourceServerId::create('FOO'),
131
            DataBag::create([
132
                'ResourceServer_authentication' => 'ResourceServer_secret_basic',
133
                'ResourceServer_secret' => 'BAR',
134
                'ResourceServer_secret_expires_at' => time() - 1,
135
            ]),
136
            null
137
        );
138
        $ResourceServer->eraseMessages();
139
140
        $request = $this->prophesize(ServerRequestInterface::class);
141
        $request->getHeader('Authorization')
142
            ->willReturn([
143
                'Basic '.base64_encode('FOO:BAR'),
144
            ])
145
            ->shouldBeCalled();
146
        $handler = $this->prophesize(RequestHandlerInterface::class);
147
        $ResourceServerRepository = $this->prophesize(ResourceServerRepository::class);
148
        $ResourceServerRepository->find(Argument::type(ResourceServerId::class))->willReturn($ResourceServer)->shouldBeCalled();
149
        $handler->handle(Argument::type(ServerRequestInterface::class))
150
            ->shouldNotBeCalled()
151
        ;
152
153
        try {
154
            $this->getResourceServerAuthenticationMiddleware($ResourceServerRepository->reveal())->process($request->reveal(), $handler->reveal());
155
            $this->fail('An OAuth2 exception should be thrown.');
156
        } catch (OAuth2Exception $e) {
157
            self::assertEquals(401, $e->getCode());
158
            self::assertEquals([
159
                'error' => 'invalid_ResourceServer',
160
                'error_description' => 'ResourceServer credentials expired.',
161
            ], $e->getData());
162
        }
163
    }
164
165
    /**
166
     * @test
167
     */
168
    public function aResourceServerIdIsSetButTheAuthenticationMethodIsNotSupportedByTheResourceServer()
169
    {
170
        $ResourceServer = ResourceServer::createEmpty();
0 ignored issues
show
Bug introduced by
The method createEmpty() does not seem to exist on object<OAuth2Framework\C...eServer\ResourceServer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
171
        $ResourceServer = $ResourceServer->create(
172
            ResourceServerId::create('FOO'),
173
            DataBag::create([
174
                'ResourceServer_authentication' => 'none',
175
            ]),
176
            null
177
        );
178
        $ResourceServer->eraseMessages();
179
180
        $request = $this->prophesize(ServerRequestInterface::class);
181
        $request->getHeader('Authorization')
182
            ->willReturn([
183
                'Basic '.base64_encode('FOO:BAR'),
184
            ])
185
            ->shouldBeCalled();
186
        $handler = $this->prophesize(RequestHandlerInterface::class);
187
        $ResourceServerRepository = $this->prophesize(ResourceServerRepository::class);
188
        $ResourceServerRepository->find(Argument::type(ResourceServerId::class))->willReturn($ResourceServer)->shouldBeCalled();
189
        $handler->handle(Argument::type(ServerRequestInterface::class))
190
            ->shouldNotBeCalled()
191
        ;
192
193
        try {
194
            $this->getResourceServerAuthenticationMiddleware($ResourceServerRepository->reveal())->process($request->reveal(), $handler->reveal());
195
            $this->fail('An OAuth2 exception should be thrown.');
196
        } catch (OAuth2Exception $e) {
197
            self::assertEquals(401, $e->getCode());
198
            self::assertEquals([
199
                'error' => 'invalid_ResourceServer',
200
                'error_description' => 'Unknown ResourceServer or ResourceServer not authenticated.',
201
            ], $e->getData());
202
        }
203
    }
204
205
    /**
206
     * @test
207
     */
208
    public function aResourceServerIdIsSetButTheResourceServerIsNotAuthenticated()
209
    {
210
        $ResourceServer = ResourceServer::createEmpty();
0 ignored issues
show
Bug introduced by
The method createEmpty() does not seem to exist on object<OAuth2Framework\C...eServer\ResourceServer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
211
        $ResourceServer = $ResourceServer->create(
212
            ResourceServerId::create('FOO'),
213
            DataBag::create([
214
                'ResourceServer_authentication' => 'ResourceServer_secret_basic',
215
                'ResourceServer_secret' => 'BAR',
216
            ]),
217
            null
218
        );
219
        $ResourceServer->eraseMessages();
220
221
        $request = $this->prophesize(ServerRequestInterface::class);
222
        $request->getHeader('Authorization')
223
            ->willReturn([
224
                'Basic '.base64_encode('FOO:BAD_SECRET'),
225
            ])
226
            ->shouldBeCalled();
227
        $handler = $this->prophesize(RequestHandlerInterface::class);
228
        $ResourceServerRepository = $this->prophesize(ResourceServerRepository::class);
229
        $ResourceServerRepository->find(Argument::type(ResourceServerId::class))->willReturn($ResourceServer)->shouldBeCalled();
230
        $handler->handle(Argument::type(ServerRequestInterface::class))
231
            ->shouldNotBeCalled()
232
        ;
233
234
        try {
235
            $this->getResourceServerAuthenticationMiddleware($ResourceServerRepository->reveal())->process($request->reveal(), $handler->reveal());
236
            $this->fail('An OAuth2 exception should be thrown.');
237
        } catch (OAuth2Exception $e) {
238
            self::assertEquals(401, $e->getCode());
239
            self::assertEquals([
240
                'error' => 'invalid_ResourceServer',
241
                'error_description' => 'Unknown ResourceServer or ResourceServer not authenticated.',
242
            ], $e->getData());
243
        }
244
    }
245
246
    /**
247
     * @test
248
     */
249
    public function aResourceServerIsFullyAuthenticated()
250
    {
251
        $ResourceServer = ResourceServer::createEmpty();
0 ignored issues
show
Bug introduced by
The method createEmpty() does not seem to exist on object<OAuth2Framework\C...eServer\ResourceServer>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
252
        $ResourceServer = $ResourceServer->create(
253
            ResourceServerId::create('FOO'),
254
            DataBag::create([
255
                'ResourceServer_authentication' => 'ResourceServer_secret_basic',
256
                'ResourceServer_secret' => 'BAR',
257
            ]),
258
            null
259
        );
260
        $ResourceServer->eraseMessages();
261
262
        $response = $this->prophesize(ResponseInterface::class);
263
        $request = $this->prophesize(ServerRequestInterface::class);
264
        $request->getHeader('Authorization')
265
            ->willReturn([
266
                'Basic '.base64_encode('FOO:BAR'),
267
            ])
268
            ->shouldBeCalled();
269
        $request->withAttribute('ResourceServer', $ResourceServer)->shouldBeCalled()->willReturn($request->reveal());
270
        $request->withAttribute('ResourceServer_authentication_method', Argument::type(AuthenticationMethod::class))->shouldBeCalled()->willReturn($request->reveal());
271
        $request->withAttribute('ResourceServer_credentials', 'BAR')->shouldBeCalled()->willReturn($request->reveal());
272
        $handler = $this->prophesize(RequestHandlerInterface::class);
273
        $ResourceServerRepository = $this->prophesize(ResourceServerRepository::class);
274
        $ResourceServerRepository->find(Argument::type(ResourceServerId::class))->willReturn($ResourceServer)->shouldBeCalled();
275
        $handler->handle(Argument::type(ServerRequestInterface::class))
276
            ->shouldBeCalled()
277
            ->willReturn($response->reveal())
278
        ;
279
280
        $this->getResourceServerAuthenticationMiddleware($ResourceServerRepository->reveal())->process($request->reveal(), $handler->reveal());
281
    }
282
283
    /**
284
     * @param ResourceServerRepository $ResourceServerRepository
285
     *
286
     * @return AuthenticationMiddleware
287
     */
288
    private function getResourceServerAuthenticationMiddleware(ResourceServerRepository $ResourceServerRepository): AuthenticationMiddleware
289
    {
290
        $authenticationMethodManager = new AuthenticationMethodManager();
291
        $authenticationMethodManager->add(new ResourceServerSecretBasic('Real'));
292
293
        $ResourceServerAuthenticationMiddleware = new AuthenticationMiddleware(
294
            $ResourceServerRepository,
295
            $authenticationMethodManager
296
        );
297
298
        return $ResourceServerAuthenticationMiddleware;
299
    }
300
}
301