|
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\ClientCredentials; |
|
15
|
|
|
|
|
16
|
|
|
use OAuth2Framework\Component\ClientCredentialsGrant\ClientCredentialsGrantType; |
|
17
|
|
|
use OAuth2Framework\Component\Core\Client\Command; |
|
18
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @group Bundle |
|
22
|
|
|
* @group Functional |
|
23
|
|
|
* @group Grant |
|
24
|
|
|
* @group ClientCredentials |
|
25
|
|
|
*/ |
|
26
|
|
|
class ClientCredentialsGrantTest extends WebTestCase |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritdoc} |
|
30
|
|
|
*/ |
|
31
|
|
|
protected function setUp() |
|
32
|
|
|
{ |
|
33
|
|
|
if (!class_exists(ClientCredentialsGrantType::class)) { |
|
34
|
|
|
$this->markTestSkipped('The component "client-credentials-grant" is not installed.'); |
|
35
|
|
|
} |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @test |
|
40
|
|
|
*/ |
|
41
|
|
|
public function theRequestHasNoGrantType() |
|
42
|
|
|
{ |
|
43
|
|
|
$client = static::createClient(); |
|
44
|
|
|
$client->request('POST', '/token/get', [], [], ['HTTPS' => 'on'], null); |
|
45
|
|
|
$response = $client->getResponse(); |
|
46
|
|
|
self::assertEquals('{"error":"invalid_request","error_description":"The \"grant_type\" parameter is missing."}', $response->getContent()); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @test |
|
51
|
|
|
*/ |
|
52
|
|
|
public function theClientIsNotAuthenticated() |
|
53
|
|
|
{ |
|
54
|
|
|
$client = static::createClient(); |
|
55
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'client_credentials'], [], ['HTTPS' => 'on'], null); |
|
56
|
|
|
$response = $client->getResponse(); |
|
57
|
|
|
self::assertEquals(401, $response->getStatusCode()); |
|
58
|
|
|
self::assertEquals('Basic realm="My OAuth2 Server",charset="UTF-8",error="invalid_client",error_description="Client authentication failed."', $response->headers->get('www-authenticate')); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @test |
|
63
|
|
|
*/ |
|
64
|
|
|
public function theClientIsNotKnown() |
|
65
|
|
|
{ |
|
66
|
|
|
$client = static::createClient(); |
|
67
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'client_credentials', 'client_id' => 'UNKNOWN_CLIENT_ID'], [], ['HTTPS' => 'on'], null); |
|
68
|
|
|
$response = $client->getResponse(); |
|
69
|
|
|
self::assertEquals(401, $response->getStatusCode()); |
|
70
|
|
|
self::assertEquals('Basic realm="My OAuth2 Server",charset="UTF-8",error="invalid_client",error_description="Client authentication failed."', $response->headers->get('www-authenticate')); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @test |
|
75
|
|
|
*/ |
|
76
|
|
|
public function theGrantTypeIsNotAllowedForTheClient() |
|
77
|
|
|
{ |
|
78
|
|
|
$client = static::createClient(); |
|
79
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'client_credentials', 'client_id' => 'CLIENT_ID_1'], [], ['HTTPS' => 'on'], null); |
|
80
|
|
|
$response = $client->getResponse(); |
|
81
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
|
82
|
|
|
self::assertEquals('{"error":"unauthorized_client","error_description":"The grant type \"client_credentials\" is unauthorized for this client."}', $response->getContent()); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @test |
|
87
|
|
|
*/ |
|
88
|
|
|
public function theClientIsNotConfidential() |
|
89
|
|
|
{ |
|
90
|
|
|
$client = static::createClient(); |
|
91
|
|
|
$client->request('POST', '/token/get', ['grant_type' => 'client_credentials', 'client_id' => 'CLIENT_ID_2'], [], ['HTTPS' => 'on'], null); |
|
92
|
|
|
$response = $client->getResponse(); |
|
93
|
|
|
self::assertEquals(400, $response->getStatusCode()); |
|
94
|
|
|
self::assertEquals('{"error":"invalid_client","error_description":"The client is not a confidential client."}', $response->getContent()); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|