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\AuthorizationEndpoint\Authorization; |
17
|
|
|
use OAuth2Framework\Component\AuthorizationEndpoint\Exception\OAuth2AuthorizationException; |
18
|
|
|
use OAuth2Framework\Component\Core\Client\Client; |
19
|
|
|
use OAuth2Framework\Component\Scope\Policy\ScopePolicyManager; |
20
|
|
|
use OAuth2Framework\Component\Scope\ScopeParameterChecker; |
21
|
|
|
use OAuth2Framework\Component\Scope\ScopeRepository; |
22
|
|
|
use PHPUnit\Framework\TestCase; |
23
|
|
|
use Prophecy\Argument; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @group ScopeParameterChecker |
27
|
|
|
*/ |
28
|
|
|
class ScopeParameterCheckerTest extends TestCase |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @inheritdoc} |
32
|
|
|
*/ |
33
|
|
|
protected function setUp() |
34
|
|
|
{ |
35
|
|
|
if (!class_exists(Authorization::class)) { |
36
|
|
|
$this->markTestSkipped('Authorization Endpoint not available'); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @test |
42
|
|
|
*/ |
43
|
|
|
public function anAuthorizationRequestWithNoScopeParameterIsChecked() |
44
|
|
|
{ |
45
|
|
|
$client = $this->prophesize(Client::class); |
46
|
|
|
$authorization = $this->prophesize(Authorization::class); |
47
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
48
|
|
|
$authorization->hasQueryParam('scope')->willReturn(false)->shouldBeCalled(); |
49
|
|
|
$authorization->withResponseParameter('scope', Argument::any())->shouldNotBeCalled(); |
50
|
|
|
$this->getScopeParameterChecker()->check( |
51
|
|
|
$authorization->reveal() |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @test |
57
|
|
|
*/ |
58
|
|
|
public function anAuthorizationRequestWithScopeParameterIsChecked() |
59
|
|
|
{ |
60
|
|
|
$client = $this->prophesize(Client::class); |
61
|
|
|
$authorization = $this->prophesize(Authorization::class); |
62
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
63
|
|
|
$authorization->hasQueryParam('scope')->willReturn(true)->shouldBeCalled(); |
64
|
|
|
$authorization->getQueryParam('scope')->willReturn('scope1')->shouldBeCalled(); |
65
|
|
|
$authorization->withResponseParameter('scope', Argument::any())->willReturn($authorization)->shouldBeCalled(); |
66
|
|
|
$this->getScopeParameterChecker()->check( |
67
|
|
|
$authorization->reveal() |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @test |
73
|
|
|
*/ |
74
|
|
|
public function anAuthorizationRequestWithAnUnsupportedScopeParameterIsChecked() |
75
|
|
|
{ |
76
|
|
|
$client = $this->prophesize(Client::class); |
77
|
|
|
$authorization = $this->prophesize(Authorization::class); |
78
|
|
|
$authorization->getClient()->willReturn($client->reveal()); |
79
|
|
|
$authorization->hasQueryParam('scope')->willReturn(true)->shouldBeCalled(); |
80
|
|
|
$authorization->getQueryParam('scope')->willReturn('invalid_scope')->shouldBeCalled(); |
81
|
|
|
$authorization->withResponseParameter('scope', Argument::any())->shouldNotBeCalled(); |
82
|
|
|
|
83
|
|
|
try { |
84
|
|
|
$this->getScopeParameterChecker()->check( |
85
|
|
|
$authorization->reveal() |
86
|
|
|
); |
87
|
|
|
$this->fail('Expected exception nt thrown.'); |
88
|
|
|
} catch (OAuth2AuthorizationException $e) { |
89
|
|
|
self::assertEquals('invalid_scope', $e->getMessage()); |
90
|
|
|
self::assertEquals('An unsupported scope was requested. Available scopes for the client are scope1, scope2.', $e->getErrorDescription()); |
91
|
|
|
self::assertEquals(400, $e->getCode()); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @var ScopeParameterChecker|null |
97
|
|
|
*/ |
98
|
|
|
private $scopeParameterChecker = null; |
99
|
|
|
|
100
|
|
|
private function getScopeParameterChecker(): ScopeParameterChecker |
101
|
|
|
{ |
102
|
|
|
if (null === $this->scopeParameterChecker) { |
103
|
|
|
$scopeRepository = $this->prophesize(ScopeRepository::class); |
104
|
|
|
$scopeRepository->all()->willReturn([ |
105
|
|
|
'scope1', |
106
|
|
|
'scope2', |
107
|
|
|
]); |
108
|
|
|
$scopePolicyManager = $this->prophesize(ScopePolicyManager::class); |
109
|
|
|
$scopePolicyManager->apply(Argument::any(), Argument::type(Client::class))->willReturnArgument(0); |
110
|
|
|
|
111
|
|
|
$this->scopeParameterChecker = new ScopeParameterChecker( |
112
|
|
|
$scopeRepository->reveal(), |
113
|
|
|
$scopePolicyManager->reveal() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return $this->scopeParameterChecker; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|