oauth2-server/examples/relational/other_grants.php

115 lines
3.2 KiB
PHP
Raw Normal View History

2014-07-28 03:21:00 +05:30
<?php
use \Orno\Http\Request;
use \Orno\Http\Response;
use \Orno\Http\JsonResponse;
2014-08-04 19:49:17 +05:30
use \Orno\Http\Exception\NotFoundException;
use \League\OAuth2\Server\ResourceServer;
2014-07-28 03:21:00 +05:30
use \RelationalExample\Storage;
use \RelationalExample\Model;
2014-08-04 19:49:17 +05:30
use Illuminate\Database\Capsule\Manager as Capsule;
use \League\Event\Emitter;
2014-07-28 03:21:00 +05:30
include __DIR__.'/vendor/autoload.php';
// Routing setup
2014-08-04 19:49:17 +05:30
$request = (new Request)->createFromGlobals();
2014-07-28 03:21:00 +05:30
$router = new \Orno\Route\RouteCollection;
2014-08-04 19:49:17 +05:30
$router->setStrategy(\Orno\Route\RouteStrategyInterface::RESTFUL_STRATEGY);
2014-07-28 03:21:00 +05:30
2014-08-04 19:49:17 +05:30
// Set up the OAuth 2.0 authorization server
2014-07-28 03:21:00 +05:30
$server = new \League\OAuth2\Server\AuthorizationServer;
$server->setSessionStorage(new Storage\SessionStorage);
$server->setAccessTokenStorage(new Storage\AccessTokenStorage);
$server->setRefreshTokenStorage(new Storage\RefreshTokenStorage);
$server->setClientStorage(new Storage\ClientStorage);
$server->setScopeStorage(new Storage\ScopeStorage);
$server->setAuthCodeStorage(new Storage\AuthCodeStorage);
2014-08-04 19:49:17 +05:30
$clientCredentials = new \League\OAuth2\Server\Grant\ClientCredentialsGrant();
$server->addGrantType($clientCredentials);
2014-08-04 19:56:56 +05:30
2014-08-04 19:49:17 +05:30
$passwordGrant = new \League\OAuth2\Server\Grant\PasswordGrant();
2014-08-04 19:56:56 +05:30
$passwordGrant->setVerifyCredentialsCallback(function ($username, $password) {
$result = (new Model\Users())->get($username);
if (count($result) !== 1) {
return false;
}
if (password_verify($password, $result[0]['password'])) {
return $username;
}
return false;
});
2014-08-04 19:49:17 +05:30
$server->addGrantType($passwordGrant);
2014-07-28 03:21:00 +05:30
2014-08-04 19:56:47 +05:30
// $refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
// $server->addGrantType($refrehTokenGrant);
2014-08-04 19:49:17 +05:30
// Routing setup
2014-07-28 03:21:00 +05:30
$request = (new Request)->createFromGlobals();
2014-08-04 19:49:17 +05:30
$router = new \Orno\Route\RouteCollection;
2014-07-28 03:21:00 +05:30
$router->post('/access_token', function (Request $request) use ($server) {
try {
$response = $server->issueAccessToken();
return new Response(json_encode($response), 200);
} catch (\Exception $e) {
return new Response(
json_encode([
'error' => $e->errorType,
'message' => $e->getMessage()
]),
$e->httpStatusCode,
$e->getHttpHeaders()
);
}
});
$dispatcher = $router->getDispatcher();
2014-08-04 19:49:17 +05:30
try {
// A successful response
$response = $dispatcher->dispatch(
$request->getMethod(),
$request->getPathInfo()
);
} catch (\Orno\Http\Exception $e) {
// A failed response
$response = $e->getJsonResponse();
$response->setContent(json_encode(['status_code' => $e->getStatusCode(), 'message' => $e->getMessage()]));
} catch (\League\OAuth2\Server\Exception\OAuthException $e) {
$response = new Response(json_encode([
'error' => $e->errorType,
'message' => $e->getMessage()
]), $e->httpStatusCode);
foreach ($e->getHttpHeaders() as $header) {
$response->headers($header);
}
} catch (\Exception $e) {
$response = new Orno\Http\Response;
$response->setStatusCode(500);
$response->setContent(json_encode(['status_code' => 500, 'message' => $e->getMessage()]));
} finally {
// Return the response
$response->headers->set('Content-type', 'application/json');
$response->send();
}