Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame^] | 1 | <?php |
| 2 | |
| 3 | namespace OAuth2\GrantType; |
| 4 | |
| 5 | use OAuth2\Storage\Bootstrap; |
| 6 | use OAuth2\Server; |
| 7 | use OAuth2\Request\TestRequest; |
| 8 | use OAuth2\Request; |
| 9 | use OAuth2\Response; |
| 10 | use PHPUnit\Framework\TestCase; |
| 11 | |
| 12 | class ClientCredentialsTest extends TestCase |
| 13 | { |
| 14 | public function testInvalidCredentials() |
| 15 | { |
| 16 | $server = $this->getTestServer(); |
| 17 | $request = TestRequest::createPost(array( |
| 18 | 'grant_type' => 'client_credentials', // valid grant type |
| 19 | 'client_id' => 'Test Client ID', // valid client id |
| 20 | 'client_secret' => 'FakeSecret', // valid client secret |
| 21 | )); |
| 22 | $server->handleTokenRequest($request, $response = new Response()); |
| 23 | |
| 24 | $this->assertEquals($response->getStatusCode(), 400); |
| 25 | $this->assertEquals($response->getParameter('error'), 'invalid_client'); |
| 26 | $this->assertEquals($response->getParameter('error_description'), 'The client credentials are invalid'); |
| 27 | } |
| 28 | |
| 29 | public function testValidCredentials() |
| 30 | { |
| 31 | $server = $this->getTestServer(); |
| 32 | $request = TestRequest::createPost(array( |
| 33 | 'grant_type' => 'client_credentials', // valid grant type |
| 34 | 'client_id' => 'Test Client ID', // valid client id |
| 35 | 'client_secret' => 'TestSecret', // valid client secret |
| 36 | )); |
| 37 | $token = $server->grantAccessToken($request, new Response()); |
| 38 | |
| 39 | $this->assertNotNull($token); |
| 40 | $this->assertArrayHasKey('scope', $token); |
| 41 | $this->assertNull($token['scope']); |
| 42 | } |
| 43 | |
| 44 | public function testValidCredentialsWithScope() |
| 45 | { |
| 46 | $server = $this->getTestServer(); |
| 47 | $request = TestRequest::createPost(array( |
| 48 | 'grant_type' => 'client_credentials', // valid grant type |
| 49 | 'client_id' => 'Test Client ID', // valid client id |
| 50 | 'client_secret' => 'TestSecret', // valid client secret |
| 51 | 'scope' => 'scope1', |
| 52 | )); |
| 53 | $token = $server->grantAccessToken($request, new Response()); |
| 54 | |
| 55 | $this->assertNotNull($token); |
| 56 | $this->assertArrayHasKey('access_token', $token); |
| 57 | $this->assertArrayHasKey('scope', $token); |
| 58 | $this->assertEquals($token['scope'], 'scope1'); |
| 59 | } |
| 60 | |
| 61 | public function testValidCredentialsInvalidScope() |
| 62 | { |
| 63 | $server = $this->getTestServer(); |
| 64 | $request = TestRequest::createPost(array( |
| 65 | 'grant_type' => 'client_credentials', // valid grant type |
| 66 | 'client_id' => 'Test Client ID', // valid client id |
| 67 | 'client_secret' => 'TestSecret', // valid client secret |
| 68 | 'scope' => 'invalid-scope', |
| 69 | )); |
| 70 | $token = $server->grantAccessToken($request, $response = new Response()); |
| 71 | |
| 72 | $this->assertEquals($response->getStatusCode(), 400); |
| 73 | $this->assertEquals($response->getParameter('error'), 'invalid_scope'); |
| 74 | $this->assertEquals($response->getParameter('error_description'), 'An unsupported scope was requested'); |
| 75 | } |
| 76 | |
| 77 | public function testValidCredentialsInHeader() |
| 78 | { |
| 79 | // create with HTTP_AUTHORIZATION in header |
| 80 | $server = $this->getTestServer(); |
| 81 | $headers = array('HTTP_AUTHORIZATION' => 'Basic '.base64_encode('Test Client ID:TestSecret'), 'REQUEST_METHOD' => 'POST'); |
| 82 | $params = array('grant_type' => 'client_credentials'); |
| 83 | $request = new Request(array(), $params, array(), array(), array(), $headers); |
| 84 | $token = $server->grantAccessToken($request, new Response()); |
| 85 | |
| 86 | $this->assertNotNull($token); |
| 87 | $this->assertArrayHasKey('access_token', $token); |
| 88 | $this->assertNotNull($token['access_token']); |
| 89 | |
| 90 | // create using PHP Authorization Globals |
| 91 | $headers = array('PHP_AUTH_USER' => 'Test Client ID', 'PHP_AUTH_PW' => 'TestSecret', 'REQUEST_METHOD' => 'POST'); |
| 92 | $params = array('grant_type' => 'client_credentials'); |
| 93 | $request = new Request(array(), $params, array(), array(), array(), $headers); |
| 94 | $token = $server->grantAccessToken($request, new Response()); |
| 95 | |
| 96 | $this->assertNotNull($token); |
| 97 | $this->assertArrayHasKey('access_token', $token); |
| 98 | $this->assertNotNull($token['access_token']); |
| 99 | } |
| 100 | |
| 101 | public function testValidCredentialsInRequest() |
| 102 | { |
| 103 | $server = $this->getTestServer(); |
| 104 | $request = TestRequest::createPost(array( |
| 105 | 'grant_type' => 'client_credentials', // valid grant type |
| 106 | 'client_id' => 'Test Client ID', // valid client id |
| 107 | 'client_secret' => 'TestSecret', // valid client secret |
| 108 | )); |
| 109 | $token = $server->grantAccessToken($request, new Response()); |
| 110 | |
| 111 | $this->assertNotNull($token); |
| 112 | $this->assertArrayHasKey('access_token', $token); |
| 113 | $this->assertNotNull($token['access_token']); |
| 114 | } |
| 115 | |
| 116 | public function testValidCredentialsInQuerystring() |
| 117 | { |
| 118 | $server = $this->getTestServer(); |
| 119 | $request = TestRequest::createPost(array( |
| 120 | 'grant_type' => 'client_credentials', // valid grant type |
| 121 | 'client_id' => 'Test Client ID', // valid client id |
| 122 | 'client_secret' => 'TestSecret', // valid client secret |
| 123 | )); |
| 124 | $token = $server->grantAccessToken($request, new Response()); |
| 125 | |
| 126 | $this->assertNotNull($token); |
| 127 | $this->assertArrayHasKey('access_token', $token); |
| 128 | $this->assertNotNull($token['access_token']); |
| 129 | } |
| 130 | |
| 131 | public function testClientUserIdIsSetInAccessToken() |
| 132 | { |
| 133 | $server = $this->getTestServer(); |
| 134 | $request = TestRequest::createPost(array( |
| 135 | 'grant_type' => 'client_credentials', // valid grant type |
| 136 | 'client_id' => 'Client ID With User ID', // valid client id |
| 137 | 'client_secret' => 'TestSecret', // valid client secret |
| 138 | )); |
| 139 | $token = $server->grantAccessToken($request, new Response()); |
| 140 | |
| 141 | $this->assertNotNull($token); |
| 142 | $this->assertArrayHasKey('access_token', $token); |
| 143 | |
| 144 | // verify the user_id was associated with the token |
| 145 | $storage = $server->getStorage('client'); |
| 146 | $token = $storage->getAccessToken($token['access_token']); |
| 147 | $this->assertNotNull($token); |
| 148 | $this->assertArrayHasKey('user_id', $token); |
| 149 | $this->assertEquals($token['user_id'], 'brent@brentertainment.com'); |
| 150 | } |
| 151 | |
| 152 | private function getTestServer() |
| 153 | { |
| 154 | $storage = Bootstrap::getInstance()->getMemoryStorage(); |
| 155 | $server = new Server($storage); |
| 156 | $server->addGrantType(new ClientCredentials($storage)); |
| 157 | |
| 158 | return $server; |
| 159 | } |
| 160 | } |