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\ResourceOwnerPasswordCredentialsGrant; |
15
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\ResourceOwnerPasswordCredentialsGrant\ResourceOwnerPasswordCredentialsGrantType; |
17
|
|
|
use OAuth2Framework\ServerBundle\Tests\Functional\DatabaseTestCase; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @group ServerBundle |
21
|
|
|
* @group Functional |
22
|
|
|
* @group Grant |
23
|
|
|
* @group ResourceOwnerPasswordCredentials |
24
|
|
|
*/ |
25
|
|
|
class ResourceOwnerPasswordCredentialsGrantTest extends DatabaseTestCase |
26
|
|
|
{ |
27
|
|
|
protected function setUp() |
28
|
|
|
{ |
29
|
|
|
if (!\class_exists(ResourceOwnerPasswordCredentialsGrantType::class)) { |
30
|
|
|
static::markTestSkipped('The component "oauth2-framework/resource-owner-password-credentials-grant" is not installed.'); |
31
|
|
|
} |
32
|
|
|
parent::setUp(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @test |
37
|
|
|
*/ |
38
|
|
|
public function theRequestHasNoGrantType() |
39
|
|
|
{ |
40
|
|
|
$client = static::createClient(); |
41
|
|
|
$client->request('POST', '/token/get', [], [], ['HTTPS' => 'on'], null); |
42
|
|
|
$response = $client->getResponse(); |
43
|
|
|
static::assertEquals('{"error":"invalid_request","error_description":"The \"grant_type\" parameter is missing."}', $response->getContent()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @test |
48
|
|
|
*/ |
49
|
|
|
public function theClientIsNotAuthenticated() |
50
|
|
|
{ |
51
|
|
|
$client = static::createClient(); |
52
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'password', 'username' => 'FOO', 'password' => 'FOO'], [], ['HTTPS' => 'on'], null); |
53
|
|
|
$response = $client->getResponse(); |
54
|
|
|
static::assertEquals(401, $response->getStatusCode()); |
55
|
|
|
static::assertEquals('Basic realm="My OAuth2 Server",charset="UTF-8",error="invalid_client",error_description="Client authentication failed."', $response->headers->get('www-authenticate')); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @test |
60
|
|
|
*/ |
61
|
|
|
public function theParametersAreMissing() |
62
|
|
|
{ |
63
|
|
|
$client = static::createClient(); |
64
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'password', 'client_id' => 'CLIENT_ID_1'], [], ['HTTPS' => 'on'], null); |
65
|
|
|
$response = $client->getResponse(); |
66
|
|
|
static::assertEquals(400, $response->getStatusCode()); |
67
|
|
|
static::assertEquals('{"error":"invalid_request","error_description":"Missing grant type parameter(s): username, password."}', $response->getContent()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @test |
72
|
|
|
*/ |
73
|
|
|
public function theClientIsNotKnown() |
74
|
|
|
{ |
75
|
|
|
$client = static::createClient(); |
76
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'password', 'username' => 'FOO', 'password' => 'FOO', 'client_id' => 'UNKNOWN_CLIENT_ID'], [], ['HTTPS' => 'on'], null); |
77
|
|
|
$response = $client->getResponse(); |
78
|
|
|
static::assertEquals(401, $response->getStatusCode()); |
79
|
|
|
static::assertEquals('Basic realm="My OAuth2 Server",charset="UTF-8",error="invalid_client",error_description="Client authentication failed."', $response->headers->get('www-authenticate')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @test |
84
|
|
|
*/ |
85
|
|
|
public function theGrantTypeIsNotAllowedForTheClient() |
86
|
|
|
{ |
87
|
|
|
$client = static::createClient(); |
88
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'password', 'username' => 'FOO', 'password' => 'FOO', 'client_id' => 'CLIENT_ID_1'], [], ['HTTPS' => 'on'], null); |
89
|
|
|
$response = $client->getResponse(); |
90
|
|
|
static::assertEquals(400, $response->getStatusCode()); |
91
|
|
|
static::assertEquals('{"error":"unauthorized_client","error_description":"The grant type \"password\" is unauthorized for this client."}', $response->getContent()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @test |
96
|
|
|
*/ |
97
|
|
|
public function theResourceOwnerPasswordCredentialsAreInvalid() |
98
|
|
|
{ |
99
|
|
|
$client = static::createClient(); |
100
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'password', 'username' => 'FOO', 'password' => 'FOO', 'client_id' => 'CLIENT_ID_3', 'client_secret' => 'secret'], [], ['HTTPS' => 'on'], null); |
101
|
|
|
$response = $client->getResponse(); |
102
|
|
|
static::assertEquals(400, $response->getStatusCode()); |
103
|
|
|
static::assertEquals('{"error":"invalid_grant","error_description":"Invalid username and password combination."}', $response->getContent()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @test |
108
|
|
|
*/ |
109
|
|
|
public function theAccessTokenIsIssued() |
110
|
|
|
{ |
111
|
|
|
$client = static::createClient(); |
112
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'password', 'username' => 'john.1', 'password' => 'password.1', 'client_id' => 'CLIENT_ID_3', 'client_secret' => 'secret'], [], ['HTTPS' => 'on'], null); |
113
|
|
|
$response = $client->getResponse(); |
114
|
|
|
static::assertEquals(200, $response->getStatusCode()); |
115
|
|
|
self::assertRegexp('/\{"token_type"\:"Bearer","access_token"\:"[0-9a-zA-Z-_]+","expires_in":[0-9]{4}\}/', $response->getContent()); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|