Skip to content

Instantly share code, notes, and snippets.

@manjeshpv
Created October 23, 2015 11:14
Show Gist options
  • Save manjeshpv/219601f17314cd4d103a to your computer and use it in GitHub Desktop.
Save manjeshpv/219601f17314cd4d103a to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: ManjeshV
* Date: 10/23/2015
* Time: 12:19 PM
*/
$dsn = "mysql:host=localhost;dbname=mig";
$username = "root";
$password = "";
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
$server = new OAuth2\Server($storage);
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage)); // or any grant type you like!
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
// now you can perform storage functions, such as the one below
//$storage->setClientDetails($client_id, $client_secret, $redirect_uri);
//Authorize
//handleAuthorizeRequest
//validateAuthorizeRequest
//Resource
//verifyResourceRequest
//getAccessTokenData
//Token
//grantAccessToken
//handleTokenRequest
$app->post(
'/token',
function () {
// include our OAuth2 Server object
// require_once __DIR__.'/oauth2.php';
$dsn = "mysql:host=localhost;dbname=mig";
$username = "root";
$password = "";
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
$server = new OAuth2\Server($storage);
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage)); // or any grant type you like!
$server->addGrantType(new OAuth2\GrantType\RefreshToken($storage)); // or any grant type you like!
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
$server->addGrantType(new OAuth2\GrantType\UserCredentials($storage));
// Handle a request for an OAuth2.0 Access Token and send the response to the client
$server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
// require_once 'server.php';
// $server->handleTokenRequest(OAuth2\Request::createFromGlobals())->send();
}
);
$app->map(
'/authorize',
function () {
// include our OAuth2 Server object
// require_once __DIR__.'/oauth2.php';
$dsn = "mysql:host=localhost;dbname=mig";
$username = "root";
$password = "";
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
$server = new OAuth2\Server($storage, array('allow_implicit' => true));
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage)); // or any grant type you like!
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
$request = OAuth2\Request::createFromGlobals();
$response = new OAuth2\Response();
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
// display an authorization form
if (empty($_POST)) {
exit('
<form method="post">
<label>Do You Authorize TestClient?</label><br />
<input type="submit" name="authorized" value="yes">
<input type="submit" name="authorized" value="no">
</form>');
}
// print the authorization code if the user has authorized your client
$is_authorized = ($_POST['authorized'] === 'yes');
$server->handleAuthorizeRequest($request, $response, $is_authorized);
if ($is_authorized) {
// this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
exit("SUCCESS! Authorization Code: $code");
}
$response->send();
}
)->via('GET', 'POST')->name('authorize');;
/// users limit
$dsn = "mysql:host=localhost;dbname=mig";
$username = "root";
$password = "";
$storage = new OAuth2\Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
$server = new OAuth2\Server($storage);
$server->addGrantType(new OAuth2\GrantType\AuthorizationCode($storage)); // or any grant type you like!
$server->addGrantType(new OAuth2\GrantType\ClientCredentials($storage));
if (!$server->verifyResourceRequest(OAuth2\Request::createFromGlobals())) {
$server->getResponse()->send();
die;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment