Last active
December 12, 2015 00:18
-
-
Save caiguanhao/4682624 to your computer and use it in GitHub Desktop.
simplified version of https://github.com/bshaffer/oauth2-server-demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "public.php"; | |
$request->query = array( | |
'response_type' => 'code', | |
'client_id' => 'demoapp', | |
'redirect_uri' => '/' | |
); | |
$server->validateAuthorizeRequest($request); | |
$request->query = array( | |
'response_type' => 'code', | |
'client_id' => 'demoapp', | |
'redirect_uri' => '/' | |
); | |
$code = $server->handleAuthorizeRequest($request, true)->getHttpHeader('Location'); | |
// starting with /?code= | |
$code = substr($code, 7); | |
$request->query = array(); | |
$request->request = array( | |
'grant_type' => 'authorization_code', | |
'code' => $code, | |
'client_id' => 'demoapp', | |
'client_secret' => 'demopass', | |
'redirect_uri' => '/' | |
); | |
// need to be POST | |
$request->server['REQUEST_METHOD']='POST'; | |
$response = $server->handleGrantRequest($request); | |
$response = str_replace("\r\n", "\n", $response); | |
$response = explode("\n\n", $response, 2)[1]; | |
die($response); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- | |
-- Database Structure: | |
-- | |
CREATE TABLE IF NOT EXISTS `oauth_access_tokens` ( | |
`access_token` text, | |
`client_id` text, | |
`user_id` text, | |
`expires` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | |
`scope` text | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
CREATE TABLE IF NOT EXISTS `oauth_authorization_codes` ( | |
`authorization_code` text, | |
`client_id` text, | |
`user_id` text, | |
`redirect_uri` text, | |
`expires` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | |
`scope` text | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
CREATE TABLE IF NOT EXISTS `oauth_clients` ( | |
`client_id` text, | |
`client_secret` text, | |
`redirect_uri` text | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
INSERT INTO `oauth_clients` (`client_id`, `client_secret`, `redirect_uri`) VALUES | |
('demoapp', 'demopass', NULL); | |
CREATE TABLE IF NOT EXISTS `oauth_refresh_tokens` ( | |
`refresh_token` text, | |
`client_id` text, | |
`user_id` text, | |
`expires` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, | |
`scope` text | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "public.php"; | |
if (!$server->verifyAccessRequest($request)) { | |
$server->getResponse()->send(); | |
} else { | |
echo json_encode(array('results' => 'OK')); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "public.php"; | |
$response = $server->handleGrantRequest($request); | |
die($response); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<a href="authorize.php?response_type=code&client_id=demoapp&redirect_uri=<?php | |
echo urlencode("authorized.php"); ?>"> | |
Authorize | |
</a> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('oauth2-server-php/src/OAuth2/Autoloader.php'); | |
OAuth2_Autoloader::register(); | |
$storage = new OAuth2_Storage_Pdo( | |
array('dsn' => "mysql:host=localhost;dbname=fortest;charset=UTF8", | |
'username' => 'root', | |
'password' => 'root') | |
); | |
$server = new OAuth2_Server($storage); | |
$server->addGrantType(new OAuth2_GrantType_AuthorizationCode($storage)); | |
$request = OAuth2_Request::createFromGlobals(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thx, very nice gist ;) helped me a lot to understand the oauth2 plugin.
but i think in grant.php its much nicer to do the die() with die($response->getResponseBody()); so you just get the json without the 200 Header.