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\ClientAuthentication\Tests; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
17
|
|
|
use OAuth2Framework\Component\Core\Message\OAuth2Message; |
18
|
|
|
use OAuth2Framework\Component\ClientAuthentication\AuthenticationMethod; |
19
|
|
|
use OAuth2Framework\Component\ClientAuthentication\AuthenticationMethodManager; |
20
|
|
|
use OAuth2Framework\Component\ClientAuthentication\ClientSecretBasic; |
21
|
|
|
use OAuth2Framework\Component\ClientAuthentication\ClientSecretPost; |
22
|
|
|
use OAuth2Framework\Component\ClientAuthentication\None; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
use Prophecy\Prophecy\ObjectProphecy; |
25
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
26
|
|
|
use Psr\Http\Message\StreamInterface; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @group TokenEndpoint |
30
|
|
|
* @group ClientAuthentication |
31
|
|
|
*/ |
32
|
|
|
final class AuthenticationMethodManagerTest extends TestCase |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @test |
36
|
|
|
*/ |
37
|
|
|
public function genericCalls() |
38
|
|
|
{ |
39
|
|
|
$manager = new AuthenticationMethodManager(); |
40
|
|
|
$manager |
41
|
|
|
->add(new None()) |
42
|
|
|
->add(new ClientSecretBasic('Realm')) |
43
|
|
|
; |
44
|
|
|
self::assertTrue($manager->has('none')); |
45
|
|
|
self::assertEquals(['none', 'client_secret_basic'], $manager->list()); |
46
|
|
|
self::assertInstanceOf(AuthenticationMethod::class, $manager->get('none')); |
47
|
|
|
self::assertEquals(2, count($manager->all())); |
48
|
|
|
self::assertEquals(['Basic realm="Realm",charset="UTF-8"'], $manager->getSchemesParameters()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @test |
53
|
|
|
*/ |
54
|
|
|
public function theClientCannotUseSeveralAuthenticationMethods() |
55
|
|
|
{ |
56
|
|
|
$manager = new AuthenticationMethodManager(); |
57
|
|
|
$manager |
58
|
|
|
->add(new ClientSecretBasic('My Service')) |
59
|
|
|
->add(new ClientSecretPost()) |
60
|
|
|
; |
61
|
|
|
$request = $this->buildRequest([ |
62
|
|
|
'client_id' => 'CLIENT_ID', |
63
|
|
|
'client_secret' => 'CLIENT_SECRET', |
64
|
|
|
]); |
65
|
|
|
$request->getHeader('Authorization')->willReturn(['Basic '.base64_encode('CLIENT_ID:CLIENT_SECRET')]); |
66
|
|
|
|
67
|
|
|
try { |
68
|
|
|
$manager->findClientIdAndCredentials($request->reveal(), $method, $credentials); |
69
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
70
|
|
|
} catch (OAuth2Message $e) { |
71
|
|
|
self::assertEquals(400, $e->getCode()); |
72
|
|
|
self::assertEquals([ |
73
|
|
|
'error' => 'invalid_request', |
74
|
|
|
'error_description' => 'Only one authentication method may be used to authenticate the client.', |
75
|
|
|
], $e->getData()); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @test |
81
|
|
|
*/ |
82
|
|
|
public function theClientCanUseSeveralAuthenticationMethodsWhenOneIsNone() |
83
|
|
|
{ |
84
|
|
|
$manager = new AuthenticationMethodManager(); |
85
|
|
|
$manager |
86
|
|
|
->add(new None()) |
87
|
|
|
->add(new ClientSecretPost()) |
88
|
|
|
; |
89
|
|
|
$request = $this->buildRequest([ |
90
|
|
|
'client_id' => 'CLIENT_ID', |
91
|
|
|
'client_secret' => 'CLIENT_SECRET', |
92
|
|
|
]); |
93
|
|
|
|
94
|
|
|
$clientId = $manager->findClientIdAndCredentials($request->reveal(), $method, $credentials); |
95
|
|
|
self::assertInstanceOf(ClientSecretPost::class, $method); |
96
|
|
|
self::assertInstanceOf(ClientId::class, $clientId); |
97
|
|
|
self::assertEquals('CLIENT_SECRET', $credentials); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function buildRequest(array $data): ObjectProphecy |
101
|
|
|
{ |
102
|
|
|
$body = $this->prophesize(StreamInterface::class); |
103
|
|
|
$body->getContents()->willReturn(http_build_query($data)); |
104
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
105
|
|
|
$request->hasHeader('Content-Type')->willReturn(true); |
106
|
|
|
$request->getHeader('Content-Type')->willReturn(['application/x-www-form-urlencoded']); |
107
|
|
|
$request->getBody()->willReturn($body->reveal()); |
108
|
|
|
$request->getParsedBody()->willReturn([]); |
109
|
|
|
|
110
|
|
|
return $request; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|