|
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\Bundle\Tests\Functional\Grant\AuthorizationCode; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\AuthorizationCodeGrant\AuthorizationCodeGrantType; |
|
17
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @group Bundle |
|
21
|
|
|
* @group Functional |
|
22
|
|
|
* @group Grant |
|
23
|
|
|
* @group AuthorizationCode |
|
24
|
|
|
*/ |
|
25
|
|
|
class AuthorizationCodeGrantTest extends WebTestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* {@inheritdoc} |
|
29
|
|
|
*/ |
|
30
|
|
|
protected function setUp() |
|
31
|
|
|
{ |
|
32
|
|
|
if (!class_exists(AuthorizationCodeGrantType::class)) { |
|
33
|
|
|
$this->markTestSkipped('The component "oauth2-framework/authrization-code-grant" is not installed.'); |
|
34
|
|
|
} |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @test |
|
39
|
|
|
*/ |
|
40
|
|
|
public function theRequestHasNoGrantType() |
|
41
|
|
|
{ |
|
42
|
|
|
$client = static::createClient(); |
|
43
|
|
|
$client->request('POST', '/token/get', [], [], ['HTTPS' => 'on'], null); |
|
44
|
|
|
$response = $client->getResponse(); |
|
45
|
|
|
self::assertEquals('{"error":"invalid_request","error_description":"The \"grant_type\" parameter is missing."}', $response->getContent()); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @test |
|
50
|
|
|
*/ |
|
51
|
|
|
public function theClientIsNotAuthenticated() |
|
52
|
|
|
{ |
|
53
|
|
|
$client = static::createClient(); |
|
54
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/callback', 'code' => 'FOO'], [], ['HTTPS' => 'on'], null); |
|
55
|
|
|
$response = $client->getResponse(); |
|
56
|
|
|
self::assertEquals(401, $response->getStatusCode()); |
|
57
|
|
|
self::assertEquals('Basic realm="My OAuth2 Server",charset="UTF-8",error="invalid_client",error_description="Client authentication failed."', $response->headers->get('www-authenticate')); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @test |
|
62
|
|
|
*/ |
|
63
|
|
|
public function theAuthorizationCodeParameterIsMissing() |
|
64
|
|
|
{ |
|
65
|
|
|
$client = static::createClient(); |
|
66
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'authorization_code', 'client_id' => 'CLIENT_ID_1'], [], ['HTTPS' => 'on'], null); |
|
67
|
|
|
$response = $client->getResponse(); |
|
68
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
|
69
|
|
|
self::assertEquals('{"error":"invalid_request","error_description":"Missing grant type parameter(s): code, redirect_uri."}', $response->getContent()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @test |
|
74
|
|
|
*/ |
|
75
|
|
|
public function theClientIsNotKnown() |
|
76
|
|
|
{ |
|
77
|
|
|
$client = static::createClient(); |
|
78
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/callback', 'code' => 'FOO', 'client_id' => 'UNKNOWN_CLIENT_ID'], [], ['HTTPS' => 'on'], null); |
|
79
|
|
|
$response = $client->getResponse(); |
|
80
|
|
|
self::assertEquals(401, $response->getStatusCode()); |
|
81
|
|
|
self::assertEquals('Basic realm="My OAuth2 Server",charset="UTF-8",error="invalid_client",error_description="Client authentication failed."', $response->headers->get('www-authenticate')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @test |
|
86
|
|
|
*/ |
|
87
|
|
|
public function theGrantTypeIsNotAllowedForTheClient() |
|
88
|
|
|
{ |
|
89
|
|
|
$client = static::createClient(); |
|
90
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/callback', 'code' => 'FOO', 'client_id' => 'CLIENT_ID_1'], [], ['HTTPS' => 'on'], null); |
|
91
|
|
|
$response = $client->getResponse(); |
|
92
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
|
93
|
|
|
self::assertEquals('{"error":"unauthorized_client","error_description":"The grant type \"authorization_code\" is unauthorized for this client."}', $response->getContent()); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @test |
|
98
|
|
|
*/ |
|
99
|
|
|
public function theAuthorizationCodeExpired() |
|
100
|
|
|
{ |
|
101
|
|
|
$client = static::createClient(); |
|
102
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/callback', 'code' => 'EXPIRED_AUTHORIZATION_CODE', 'client_id' => 'CLIENT_ID_3', 'client_secret' => 'secret'], [], ['HTTPS' => 'on'], null); |
|
103
|
|
|
$response = $client->getResponse(); |
|
104
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
|
105
|
|
|
self::assertEquals('{"error":"invalid_grant","error_description":"The authorization code expired."}', $response->getContent()); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @test |
|
110
|
|
|
*/ |
|
111
|
|
|
public function theAuthorizationCodeIsNotForThatClient() |
|
112
|
|
|
{ |
|
113
|
|
|
$client = static::createClient(); |
|
114
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/callback', 'code' => 'VALID_AUTHORIZATION_CODE', 'client_id' => 'CLIENT_ID_2'], [], ['HTTPS' => 'on'], null); |
|
115
|
|
|
$response = $client->getResponse(); |
|
116
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
|
117
|
|
|
self::assertEquals('{"error":"invalid_grant","error_description":"The parameter \"code\" is invalid."}', $response->getContent()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @test |
|
122
|
|
|
*/ |
|
123
|
|
|
public function theAuthorizationCodeIsRevoked() |
|
124
|
|
|
{ |
|
125
|
|
|
$client = static::createClient(); |
|
126
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/callback', 'code' => 'REVOKED_AUTHORIZATION_CODE', 'client_id' => 'CLIENT_ID_3', 'client_secret' => 'secret'], [], ['HTTPS' => 'on'], null); |
|
127
|
|
|
$response = $client->getResponse(); |
|
128
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
|
129
|
|
|
self::assertEquals('{"error":"invalid_grant","error_description":"The parameter \"code\" is invalid."}', $response->getContent()); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @test |
|
134
|
|
|
*/ |
|
135
|
|
|
public function theAccessTokenIsIssued() |
|
136
|
|
|
{ |
|
137
|
|
|
$client = static::createClient(); |
|
138
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'authorization_code', 'redirect_uri' => 'http://localhost/callback', 'code' => 'VALID_AUTHORIZATION_CODE', 'client_id' => 'CLIENT_ID_3', 'client_secret' => 'secret'], [], ['HTTPS' => 'on'], null); |
|
139
|
|
|
$response = $client->getResponse(); |
|
140
|
|
|
self::assertEquals(200, $response->getStatusCode()); |
|
141
|
|
|
self::assertRegexp('/\{"token_type"\:"Bearer","access_token"\:"[0-9a-zA-Z-_]+","expires_in":[0-9]{4}\}/', $response->getContent()); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|