|
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\ServerBundle\Tests\Functional\Grant\AuthorizationCode; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeGrantType; |
|
17
|
|
|
use OAuth2Framework\Component\Core\UserAccount\UserAccountId; |
|
18
|
|
|
use OAuth2Framework\ServerBundle\Tests\Functional\DatabaseTestCase; |
|
19
|
|
|
use OAuth2Framework\ServerBundle\Tests\TestBundle\Entity\UserAccount; |
|
20
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
|
21
|
|
|
use Symfony\Component\BrowserKit\Cookie; |
|
22
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @group ServerBundle |
|
26
|
|
|
* @group Functional |
|
27
|
|
|
* @group Grant |
|
28
|
|
|
* @group AuthorizationCode |
|
29
|
|
|
*/ |
|
30
|
|
|
class AuthorizationEndpointTest extends DatabaseTestCase |
|
31
|
|
|
{ |
|
32
|
|
|
protected function setUp() |
|
33
|
|
|
{ |
|
34
|
|
|
if (!\class_exists(AuthorizationCodeGrantType::class)) { |
|
35
|
|
|
static::markTestSkipped('The component "oauth2-framework/authorization-code-grant" is not installed.'); |
|
36
|
|
|
} |
|
37
|
|
|
parent::setUp(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @test |
|
42
|
|
|
*/ |
|
43
|
|
|
public function theRequestIsValidButNoAccountIsSelected() |
|
44
|
|
|
{ |
|
45
|
|
|
$uri = $this->buildUri([ |
|
46
|
|
|
'client_id' => 'CLIENT_ID_2', |
|
47
|
|
|
'redirect_uri' => 'https://example.com/cb/?foo=bar', |
|
48
|
|
|
'response_type' => 'code', |
|
49
|
|
|
]); |
|
50
|
|
|
$client = static::createClient(); |
|
51
|
|
|
$this->logIn( |
|
52
|
|
|
$client, |
|
53
|
|
|
new UserAccount( |
|
54
|
|
|
new UserAccountId('john.1'), |
|
55
|
|
|
'admin', |
|
56
|
|
|
['ROLE_ADMIN', 'ROLE_USER'], |
|
57
|
|
|
new \DateTimeImmutable('now -25 hours'), |
|
58
|
|
|
new \DateTimeImmutable('now -15 days'), |
|
59
|
|
|
[ |
|
60
|
|
|
'address', [ |
|
61
|
|
|
'street_address' => '5 rue Sainte Anne', |
|
62
|
|
|
'region' => 'Île de France', |
|
63
|
|
|
'postal_code' => '75001', |
|
64
|
|
|
'locality' => 'Paris', |
|
65
|
|
|
'country' => 'France', |
|
66
|
|
|
], |
|
67
|
|
|
'name' => 'John Doe', |
|
68
|
|
|
'given_name' => 'John', |
|
69
|
|
|
'family_name' => 'Doe', |
|
70
|
|
|
'middle_name' => 'Jack', |
|
71
|
|
|
'nickname' => 'Little John', |
|
72
|
|
|
'profile' => 'https://profile.doe.fr/john/', |
|
73
|
|
|
'preferred_username' => 'j-d', |
|
74
|
|
|
'gender' => 'M', |
|
75
|
|
|
'phone_number' => '+0123456789', |
|
76
|
|
|
'phone_number_verified' => true, |
|
77
|
|
|
'zoneinfo' => 'Europe/Paris', |
|
78
|
|
|
'locale' => 'en', |
|
79
|
|
|
'picture' => 'https://www.google.com', |
|
80
|
|
|
'birthdate' => '1950-01-01', |
|
81
|
|
|
'email' => '[email protected]', |
|
82
|
|
|
'email_verified' => false, |
|
83
|
|
|
'website' => 'https://john.doe.com', |
|
84
|
|
|
'website#fr_fr' => 'https://john.doe.fr', |
|
85
|
|
|
'website#fr' => 'https://john.doe.fr', |
|
86
|
|
|
'picture#de' => 'https://john.doe.de/picture', |
|
87
|
|
|
] |
|
88
|
|
|
) |
|
89
|
|
|
); |
|
90
|
|
|
$client->request('GET', $uri, [], [], ['HTTPS' => 'on'], null); |
|
91
|
|
|
$response = $client->getResponse(); |
|
92
|
|
|
|
|
93
|
|
|
static::assertEquals(303, $response->getStatusCode()); |
|
94
|
|
|
static::assertTrue($response->headers->has('location')); |
|
95
|
|
|
|
|
96
|
|
|
static::assertStringEndsWith('/consent', $response->headers->get('location')); |
|
97
|
|
|
$client->followRedirect(); |
|
98
|
|
|
$response = $client->getResponse(); |
|
99
|
|
|
static::assertEquals('You are on the consent page', $response->getContent()); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private function buildUri(array $query): string |
|
103
|
|
|
{ |
|
104
|
|
|
$query = http_build_query($query); |
|
105
|
|
|
|
|
106
|
|
|
return empty($query) ? '/authorize' : \Safe\sprintf('/authorize?%s', $query); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function logIn(Client $client, UserAccount $userAccount): void |
|
110
|
|
|
{ |
|
111
|
|
|
$session = $client->getContainer()->get('session'); |
|
112
|
|
|
|
|
113
|
|
|
$firewallName = 'main'; |
|
114
|
|
|
$firewallContext = 'main'; |
|
115
|
|
|
|
|
116
|
|
|
$token = new UsernamePasswordToken($userAccount->getUsername(), null, $firewallName, ['ROLE_ADMIN']); |
|
117
|
|
|
$session->set('_security_'.$firewallContext, serialize($token)); |
|
118
|
|
|
$session->save(); |
|
119
|
|
|
|
|
120
|
|
|
$cookie = new Cookie($session->getName(), $session->getId()); |
|
121
|
|
|
$client->getCookieJar()->set($cookie); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|