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\Scope\Tests; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\Core\AccessToken\AccessToken; |
17
|
|
|
use OAuth2Framework\Component\Core\AccessToken\AccessTokenId; |
18
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
19
|
|
|
use OAuth2Framework\Component\Core\Client\ClientId; |
20
|
|
|
use OAuth2Framework\Component\Core\DataBag\DataBag; |
21
|
|
|
use OAuth2Framework\Component\Core\ResourceOwner\ResourceOwner; |
22
|
|
|
use OAuth2Framework\Component\Core\Exception\OAuth2Exception; |
23
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccountId; |
24
|
|
|
use OAuth2Framework\Component\Scope\Policy\NoScopePolicy; |
25
|
|
|
use OAuth2Framework\Component\Scope\Policy\ScopePolicyManager; |
26
|
|
|
use OAuth2Framework\Component\Scope\Scope; |
27
|
|
|
use OAuth2Framework\Component\Scope\ScopeRepository; |
28
|
|
|
use OAuth2Framework\Component\Scope\TokenEndpointScopeExtension; |
29
|
|
|
use OAuth2Framework\Component\TokenEndpoint\GrantType; |
30
|
|
|
use OAuth2Framework\Component\TokenEndpoint\GrantTypeData; |
31
|
|
|
use OAuth2Framework\Component\TokenEndpoint\TokenEndpoint; |
32
|
|
|
use PHPUnit\Framework\TestCase; |
33
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @group TokenEndpointScopeExtension |
37
|
|
|
*/ |
38
|
|
|
class TokenEndpointScopeExtensionTest extends TestCase |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @inheritdoc} |
42
|
|
|
*/ |
43
|
|
|
protected function setUp() |
44
|
|
|
{ |
45
|
|
|
if (!class_exists(TokenEndpoint::class)) { |
46
|
|
|
$this->markTestSkipped('The component "oauth2-framework/token-endpoint" is not installed.'); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @test |
52
|
|
|
*/ |
53
|
|
|
public function theRequestHasNoScope() |
54
|
|
|
{ |
55
|
|
|
$client = Client::createEmpty(); |
56
|
|
|
$client = $client->create( |
57
|
|
|
ClientId::create('CLIENT_ID'), |
58
|
|
|
DataBag::create([]), |
59
|
|
|
UserAccountId::create('USER_ACCOUNT_ID') |
60
|
|
|
); |
61
|
|
|
$client->eraseMessages(); |
62
|
|
|
|
63
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
64
|
|
|
$request->getParsedBody()->willReturn([]); |
65
|
|
|
$grantTypeData = GrantTypeData::create($client); |
66
|
|
|
$grantType = $this->prophesize(GrantType::class); |
67
|
|
|
$next = function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData { |
|
|
|
|
68
|
|
|
return $grantTypeData; |
69
|
|
|
}; |
70
|
|
|
|
71
|
|
|
$result = $this->getExtension()->beforeAccessTokenIssuance($request->reveal(), $grantTypeData, $grantType->reveal(), $next); |
72
|
|
|
self::assertSame($grantTypeData, $result); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @test |
77
|
|
|
*/ |
78
|
|
|
public function theRequestedScopeIsNotSupported() |
79
|
|
|
{ |
80
|
|
|
$client = Client::createEmpty(); |
81
|
|
|
$client = $client->create( |
82
|
|
|
ClientId::create('CLIENT_ID'), |
83
|
|
|
DataBag::create([]), |
84
|
|
|
UserAccountId::create('USER_ACCOUNT_ID') |
85
|
|
|
); |
86
|
|
|
$client->eraseMessages(); |
87
|
|
|
|
88
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
89
|
|
|
$request->getParsedBody()->willReturn([ |
90
|
|
|
'scope' => 'café', |
91
|
|
|
]); |
92
|
|
|
$grantTypeData = GrantTypeData::create($client); |
93
|
|
|
$grantType = $this->prophesize(GrantType::class); |
94
|
|
|
$next = function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData { |
|
|
|
|
95
|
|
|
return $grantTypeData; |
96
|
|
|
}; |
97
|
|
|
|
98
|
|
|
try { |
99
|
|
|
$this->getExtension()->beforeAccessTokenIssuance($request->reveal(), $grantTypeData, $grantType->reveal(), $next); |
100
|
|
|
} catch (OAuth2Exception $e) { |
101
|
|
|
self::assertEquals(400, $e->getCode()); |
102
|
|
|
self::assertEquals([ |
103
|
|
|
'error' => 'invalid_scope', |
104
|
|
|
'error_description' => 'An unsupported scope was requested. Available scope is/are: scope1 ,scope2.', |
105
|
|
|
], $e->getData()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @test |
111
|
|
|
*/ |
112
|
|
|
public function theRequestedScopeIsValid() |
113
|
|
|
{ |
114
|
|
|
$client = Client::createEmpty(); |
115
|
|
|
$client = $client->create( |
116
|
|
|
ClientId::create('CLIENT_ID'), |
117
|
|
|
DataBag::create([]), |
118
|
|
|
UserAccountId::create('USER_ACCOUNT_ID') |
119
|
|
|
); |
120
|
|
|
$client->eraseMessages(); |
121
|
|
|
|
122
|
|
|
$request = $this->prophesize(ServerRequestInterface::class); |
123
|
|
|
$request->getParsedBody()->willReturn([ |
124
|
|
|
'scope' => 'scope2 scope1', |
125
|
|
|
]); |
126
|
|
|
$grantTypeData = GrantTypeData::create($client); |
127
|
|
|
$grantType = $this->prophesize(GrantType::class); |
128
|
|
|
$next = function (ServerRequestInterface $request, GrantTypeData $grantTypeData, GrantType $grantType): GrantTypeData { |
|
|
|
|
129
|
|
|
return $grantTypeData; |
130
|
|
|
}; |
131
|
|
|
|
132
|
|
|
$result = $this->getExtension()->beforeAccessTokenIssuance($request->reveal(), $grantTypeData, $grantType->reveal(), $next); |
133
|
|
|
self::assertNotSame($grantTypeData, $result); |
134
|
|
|
self::assertTrue($result->hasParameter('scope')); |
135
|
|
|
self::assertEquals('scope2 scope1', $result->getParameter('scope')); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @test |
140
|
|
|
*/ |
141
|
|
|
public function after() |
142
|
|
|
{ |
143
|
|
|
$client = Client::createEmpty(); |
144
|
|
|
$client = $client->create( |
145
|
|
|
ClientId::create('CLIENT_ID'), |
146
|
|
|
DataBag::create([]), |
147
|
|
|
UserAccountId::create('USER_ACCOUNT_ID') |
148
|
|
|
); |
149
|
|
|
$client->eraseMessages(); |
150
|
|
|
$accessToken = AccessToken::createEmpty(); |
151
|
|
|
$accessToken = $accessToken->create( |
152
|
|
|
AccessTokenId::create('ACCESS_TOKEN_ID'), |
153
|
|
|
$client->getPublicId(), |
154
|
|
|
$client->getPublicId(), |
155
|
|
|
DataBag::create([]), |
156
|
|
|
DataBag::create([]), |
157
|
|
|
new \DateTimeImmutable('now +1 hour'), |
158
|
|
|
null |
159
|
|
|
); |
160
|
|
|
$accessToken->eraseMessages(); |
161
|
|
|
|
162
|
|
|
$next = function (Client $client, ResourceOwner $resourceOwner, AccessToken $accessToken): array { |
163
|
|
|
return $accessToken->getResponseData(); |
164
|
|
|
}; |
165
|
|
|
|
166
|
|
|
$result = $this->getExtension()->afterAccessTokenIssuance($client, $client, $accessToken, $next); |
167
|
|
|
self::assertEquals(2, count($result)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @var null|TokenEndpointScopeExtension |
172
|
|
|
*/ |
173
|
|
|
private $extension = null; |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return TokenEndpointScopeExtension |
177
|
|
|
*/ |
178
|
|
|
private function getExtension(): TokenEndpointScopeExtension |
179
|
|
|
{ |
180
|
|
|
if (null === $this->extension) { |
181
|
|
|
$scope1 = $this->prophesize(Scope::class); |
182
|
|
|
$scope1->name()->willReturn('scope1'); |
183
|
|
|
$scope1->__toString()->willReturn('scope1'); |
184
|
|
|
$scope2 = $this->prophesize(Scope::class); |
185
|
|
|
$scope2->name()->willReturn('scope2'); |
186
|
|
|
$scope2->__toString()->willReturn('scope2'); |
187
|
|
|
$scopeRepository = $this->prophesize(ScopeRepository::class); |
188
|
|
|
$scopeRepository->all()->willReturn([ |
189
|
|
|
$scope1->reveal(), |
190
|
|
|
$scope2->reveal(), |
191
|
|
|
]); |
192
|
|
|
|
193
|
|
|
$scopePolicyManager = new ScopePolicyManager(); |
194
|
|
|
$scopePolicyManager->add(new NoScopePolicy(), true); |
195
|
|
|
|
196
|
|
|
$this->extension = new TokenEndpointScopeExtension( |
197
|
|
|
$scopeRepository->reveal(), |
198
|
|
|
$scopePolicyManager |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this->extension; |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.