public function testInstance_unsupported_grant() { try { $grantHandler = Akita_OAuth2_Server_GrantHandlers::getHandler('authorization_code', array('refresh_token', 'client_credentials', 'password')); } catch (Akita_OAuth2_Server_Error $error) { $this->assertEquals('400', $error->getOAuth2Code(), $error->getMessage()); $this->assertEquals('unsupported_grant_type', $error->getOAuth2Error(), $error->getMessage()); $this->assertEmpty($error->getOAuth2ErrorDescription(), $error->getMessage()); } $this->assertNotInstanceOf('Akita_OAuth2_Server_GrantHandler_AuthorizationCode', @$grantHandler); try { $grantHandler = Akita_OAuth2_Server_GrantHandlers::getHandler('refresh_token', array('authorization_code', 'client_credentials', 'password')); } catch (Akita_OAuth2_Server_Error $error) { $this->assertEquals('400', $error->getOAuth2Code(), $error->getMessage()); $this->assertEquals('unsupported_grant_type', $error->getOAuth2Error(), $error->getMessage()); $this->assertEmpty($error->getOAuth2ErrorDescription(), $error->getMessage()); } $this->assertNotInstanceOf('Akita_OAuth2_Server_GrantHandler_RefreshToken', @$grantHandler); try { $grantHandler = Akita_OAuth2_Server_GrantHandlers::getHandler('client_credentials', array('authorization_code', 'refresh_token', 'password')); } catch (Akita_OAuth2_Server_Error $error) { $this->assertEquals('400', $error->getOAuth2Code(), $error->getMessage()); $this->assertEquals('unsupported_grant_type', $error->getOAuth2Error(), $error->getMessage()); $this->assertEmpty($error->getOAuth2ErrorDescription(), $error->getMessage()); } $this->assertNotInstanceOf('Akita_OAuth2_Server_GrantHandler_ClientCredentials', @$grantHandler); try { $grantHandler = Akita_OAuth2_Server_GrantHandlers::getHandler('password', array('authorization_code', 'refresh_token', 'client_credentials')); } catch (Akita_OAuth2_Server_Error $error) { $this->assertEquals('400', $error->getOAuth2Code(), $error->getMessage()); $this->assertEquals('unsupported_grant_type', $error->getOAuth2Error(), $error->getMessage()); $this->assertEmpty($error->getOAuth2ErrorDescription(), $error->getMessage()); } $this->assertNotInstanceOf('Akita_OAuth2_Server_GrantHandler_Password', @$grantHandler); }
<?php require_once './lib/DataHandler.php'; // process request $headers = apache_request_headers(); $request = new Akita_OAuth2_Server_Request('authorization', $_SERVER, $_POST, $headers); $dataHandler = new Akita_OAuth2_Server_Sample_DataHandler($request); try { $grantHandler = Akita_OAuth2_Server_GrantHandlers::getHandler($request->param['grant_type']); $res = $grantHandler->handleRequest($dataHandler); } catch (Akita_OAuth2_Server_Error $error) { // error handling header('HTTP/1.1 ' . $error->getOAuth2Code()); header('Content-Type: application/json;charset=UTF-8'); header('Cache-Control: no-store'); header('Pragma: no-cache'); $res = array(); $res['error'] = $error->getOAuth2Error(); $desc = $error->getOAuth2ErrorDescription(); if (!empty($desc)) { $res['error_description'] = $desc; } echo json_encode($res); exit; } header('HTTP/1.1 200 OK'); header('Content-Type: application/json;charset=UTF-8'); header('Cache-Control: no-store'); header('Pragma: no-cache'); echo json_encode($res);