|
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\Exception\OAuth2Exception; |
|
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 Psr\Http\Message\ServerRequestInterface; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @group TokenEndpoint |
|
28
|
|
|
* @group ClientAuthentication |
|
29
|
|
|
*/ |
|
30
|
|
|
class AuthenticationMethodManagerTest extends TestCase |
|
31
|
|
|
{ |
|
32
|
|
|
/** |
|
33
|
|
|
* @test |
|
34
|
|
|
*/ |
|
35
|
|
|
public function genericCalls() |
|
36
|
|
|
{ |
|
37
|
|
|
$manager = new AuthenticationMethodManager(); |
|
38
|
|
|
$manager |
|
39
|
|
|
->add(new None()) |
|
40
|
|
|
->add(new ClientSecretBasic('Realm')) |
|
41
|
|
|
; |
|
42
|
|
|
self::assertTrue($manager->has('none')); |
|
43
|
|
|
self::assertEquals(['none', 'client_secret_basic'], $manager->list()); |
|
44
|
|
|
self::assertInstanceOf(AuthenticationMethod::class, $manager->get('none')); |
|
45
|
|
|
self::assertEquals(2, count($manager->all())); |
|
46
|
|
|
self::assertEquals(['Basic realm="Realm",charset="UTF-8"'], $manager->getSchemesParameters()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @test |
|
51
|
|
|
*/ |
|
52
|
|
|
public function theClientCannotUseSeveralAuthenticationMethods() |
|
53
|
|
|
{ |
|
54
|
|
|
$manager = new AuthenticationMethodManager(); |
|
55
|
|
|
$manager |
|
56
|
|
|
->add(new ClientSecretBasic('My Service')) |
|
57
|
|
|
->add(new ClientSecretPost()) |
|
58
|
|
|
; |
|
59
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
|
60
|
|
|
$request->getHeader('Authorization')->willReturn(['Basic '.base64_encode('CLIENT_ID:CLIENT_SECRET')]); |
|
61
|
|
|
$request->getParsedBody()->willReturn([ |
|
62
|
|
|
'client_id' => 'CLIENT_ID', |
|
63
|
|
|
'client_secret' => 'CLIENT_SECRET', |
|
64
|
|
|
]); |
|
65
|
|
|
|
|
66
|
|
|
try { |
|
67
|
|
|
$manager->findClientIdAndCredentials($request->reveal(), $method, $credentials); |
|
68
|
|
|
$this->fail('An OAuth2 exception should be thrown.'); |
|
69
|
|
|
} catch (OAuth2Exception $e) { |
|
70
|
|
|
self::assertEquals(400, $e->getCode()); |
|
71
|
|
|
self::assertEquals([ |
|
72
|
|
|
'error' => 'invalid_request', |
|
73
|
|
|
'error_description' => 'Only one authentication method may be used to authenticate the client.', |
|
74
|
|
|
], $e->getData()); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @test |
|
80
|
|
|
*/ |
|
81
|
|
|
public function theClientCanUseSeveralAuthenticationMethodsWhenOneIsNone() |
|
82
|
|
|
{ |
|
83
|
|
|
$manager = new AuthenticationMethodManager(); |
|
84
|
|
|
$manager |
|
85
|
|
|
->add(new None()) |
|
86
|
|
|
->add(new ClientSecretPost()) |
|
87
|
|
|
; |
|
88
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
|
89
|
|
|
$request->getParsedBody()->willReturn([ |
|
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
|
|
|
|