Updated example code examples

This commit is contained in:
Alex Bilbie 2014-08-04 15:19:17 +01:00
parent 69208fe0ac
commit b3da61822e
2 changed files with 109 additions and 14 deletions

View File

@ -33,6 +33,7 @@ $server = new ResourceServer(
$request = (new Request)->createFromGlobals(); $request = (new Request)->createFromGlobals();
$router = new \Orno\Route\RouteCollection; $router = new \Orno\Route\RouteCollection;
// GET /tokeninfo
$router->get('/tokeninfo', function (Request $request) use ($server) { $router->get('/tokeninfo', function (Request $request) use ($server) {
$token = [ $token = [
@ -47,12 +48,64 @@ $router->get('/tokeninfo', function (Request $request) use ($server) {
}); });
// GET /users
$router->get('/users', function (Request $request) use ($server) {
$results = (new Model\Users())->get();
$users = [];
foreach ($results as $result) {
$user = [
'username' => $result['username'],
'name' => $result['name']
];
if ($server->hasScope('email')) {
$user['email'] = $result['email'];
}
if ($server->hasScope('photo')) {
$user['photo'] = $result['photo'];
}
$users[] = $user;
}
return new Response(json_encode($users));
});
// GET /users/{username}
$router->get('/users/{username}', function (Request $request, $args) use ($server) {
$result = (new Model\Users())->get($args['username']);
if (count($result) === 0) {
throw new NotFoundException();
}
$user = [
'username' => $result[0]['username'],
'name' => $result[0]['name']
];
if ($server->hasScope('email')) {
$user['email'] = $result[0]['email'];
}
if ($server->hasScope('photo')) {
$user['photo'] = $result[0]['photo'];
}
return new Response(json_encode($user));
});
$dispatcher = $router->getDispatcher(); $dispatcher = $router->getDispatcher();
try { try {
// Check that access token is present // Check that access token is present
$server->isValidRequest(); $server->isValidRequest(false);
// A successful response // A successful response
$response = $dispatcher->dispatch( $response = $dispatcher->dispatch(

View File

@ -1,24 +1,22 @@
<?php <?php
namespace OAuth2Server\RelationalExample;
use \Orno\Http\Request; use \Orno\Http\Request;
use \Orno\Http\Response; use \Orno\Http\Response;
use \Orno\Http\JsonResponse; use \Orno\Http\JsonResponse;
use \Orno\Http\Exception\MethodNotAllowedException; use \Orno\Http\Exception\NotFoundException;
use \League\OAuth2\Server\ResourceServer;
use Illuminate\Database\Capsule\Manager as Capsule;
// use \League\OAuth2\Server\Exception;
use \RelationalExample\Storage; use \RelationalExample\Storage;
use \RelationalExample\Model; use \RelationalExample\Model;
use Illuminate\Database\Capsule\Manager as Capsule;
use \League\Event\Emitter;
include __DIR__.'/vendor/autoload.php'; include __DIR__.'/vendor/autoload.php';
// Routing setup // Routing setup
$request = (new Request)->createFromGlobals();
$router = new \Orno\Route\RouteCollection; $router = new \Orno\Route\RouteCollection;
$router->setStrategy(\Orno\Route\RouteStrategyInterface::RESTFUL_STRATEGY);
// Set up the OAuth 2.0 resource server // Set up the OAuth 2.0 authorization server
$server = new \League\OAuth2\Server\AuthorizationServer; $server = new \League\OAuth2\Server\AuthorizationServer;
$server->setSessionStorage(new Storage\SessionStorage); $server->setSessionStorage(new Storage\SessionStorage);
$server->setAccessTokenStorage(new Storage\AccessTokenStorage); $server->setAccessTokenStorage(new Storage\AccessTokenStorage);
@ -27,6 +25,12 @@ $server->setClientStorage(new Storage\ClientStorage);
$server->setScopeStorage(new Storage\ScopeStorage); $server->setScopeStorage(new Storage\ScopeStorage);
$server->setAuthCodeStorage(new Storage\AuthCodeStorage); $server->setAuthCodeStorage(new Storage\AuthCodeStorage);
$clientCredentials = new \League\OAuth2\Server\Grant\ClientCredentialsGrant();
$server->addGrantType($clientCredentials);
$passwordGrant = new \League\OAuth2\Server\Grant\PasswordGrant();
$server->addGrantType($passwordGrant);
$refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
$server->addGrantType($refrehTokenGrant);
$clientCredentials = new \League\OAuth2\Server\Grant\ClientCredentialsGrant(); $clientCredentials = new \League\OAuth2\Server\Grant\ClientCredentialsGrant();
$server->addGrantType($clientCredentials); $server->addGrantType($clientCredentials);
$passwordGrant = new \League\OAuth2\Server\Grant\PasswordGrant(); $passwordGrant = new \League\OAuth2\Server\Grant\PasswordGrant();
@ -34,8 +38,9 @@ $server->addGrantType($passwordGrant);
$refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant(); $refrehTokenGrant = new \League\OAuth2\Server\Grant\RefreshTokenGrant();
$server->addGrantType($refrehTokenGrant); $server->addGrantType($refrehTokenGrant);
// Routing setup
$request = (new Request)->createFromGlobals(); $request = (new Request)->createFromGlobals();
$server->setRequest($request); $router = new \Orno\Route\RouteCollection;
$router->post('/access_token', function (Request $request) use ($server) { $router->post('/access_token', function (Request $request) use ($server) {
@ -60,5 +65,42 @@ $router->post('/access_token', function (Request $request) use ($server) {
}); });
$dispatcher = $router->getDispatcher(); $dispatcher = $router->getDispatcher();
$response = $dispatcher->dispatch($request->getMethod(), $request->getPathInfo());
$response->send(); 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();
}