unify examples

This commit is contained in:
Julián Gutiérrez 2016-02-12 19:06:31 +01:00
parent 655f6b9771
commit 2bd45f2a6b
7 changed files with 169 additions and 121 deletions

View File

@ -16,10 +16,10 @@ Send the following cURL request:
curl -X "POST" "http://localhost:4444/client_credentials.php/access_token" \ curl -X "POST" "http://localhost:4444/client_credentials.php/access_token" \
-H "Content-Type: application/x-www-form-urlencoded" \ -H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: 1.0" \ -H "Accept: 1.0" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "client_id=myawesomeapp" \ --data-urlencode "client_id=myawesomeapp" \
--data-urlencode "scope=basic email" \
--data-urlencode "client_secret=abc123" \ --data-urlencode "client_secret=abc123" \
--data-urlencode "grant_type=client_credentials" --data-urlencode "scope=basic email"
``` ```
## Testing the password grant example ## Testing the password grant example
@ -30,12 +30,12 @@ Send the following cURL request:
curl -X "POST" "http://localhost:4444/password.php/access_token" \ curl -X "POST" "http://localhost:4444/password.php/access_token" \
-H "Content-Type: application/x-www-form-urlencoded" \ -H "Content-Type: application/x-www-form-urlencoded" \
-H "Accept: 1.0" \ -H "Accept: 1.0" \
--data-urlencode "grant_type=password" \
--data-urlencode "client_id=myawesomeapp" \ --data-urlencode "client_id=myawesomeapp" \
--data-urlencode "scope=basic email" \ --data-urlencode "client_secret=abc123" \
--data-urlencode "username=alex" \ --data-urlencode "username=alex" \
--data-urlencode "password=whisky" \ --data-urlencode "password=whisky" \
--data-urlencode "client_secret=abc123" \ --data-urlencode "scope=basic email"
--data-urlencode "grant_type=password"
``` ```
## Testing the refresh token grant example ## Testing the refresh token grant example
@ -51,4 +51,3 @@ curl -X "POST" "http://localhost:4444/refresh_token.php/access_token" \
--data-urlencode "client_secret=abc123" \ --data-urlencode "client_secret=abc123" \
--data-urlencode "refresh_token={{REFRESH_TOKEN}}" --data-urlencode "refresh_token={{REFRESH_TOKEN}}"
``` ```

View File

@ -3,31 +3,31 @@
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\AuthCodeGrant; use League\OAuth2\Server\Grant\AuthCodeGrant;
use League\OAuth2\Server\Server; use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Repositories\AccessTokenRepository; use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\AuthCodeRepository; use OAuth2ServerExamples\Repositories\AuthCodeRepository;
use OAuth2ServerExamples\Repositories\ClientRepository; use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\RefreshTokenRepository; use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository; use OAuth2ServerExamples\Repositories\ScopeRepository;
use OAuth2ServerExamples\Repositories\UserRepository; use OAuth2ServerExamples\Repositories\UserRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App; use Slim\App;
use Slim\Http\Request; use Zend\Diactoros\Stream;
use Slim\Http\Response;
include(__DIR__ . '/../vendor/autoload.php'); include(__DIR__ . '/../vendor/autoload.php');
// App
$app = new App([ $app = new App([
'settings' => [
'displayErrorDetails' => true,
],
Server::class => function () { Server::class => function () {
// Init our repositories // Init our repositories
$clientRepository = new ClientRepository(); $clientRepository = new ClientRepository();
$scopeRepository = new ScopeRepository(); $scopeRepository = new ScopeRepository();
$accessTokenRepository = new AccessTokenRepository(); $accessTokenRepository = new AccessTokenRepository();
$userRepository = new UserRepository();
$refreshTokenRepository = new RefreshTokenRepository();
$authCodeRepository = new AuthCodeRepository(); $authCodeRepository = new AuthCodeRepository();
$refreshTokenRepository = new RefreshTokenRepository();
$userRepository = new UserRepository();
$privateKeyPath = 'file://' . __DIR__ . '/../private.key'; $privateKeyPath = 'file://' . __DIR__ . '/../private.key';
$publicKeyPath = 'file://' . __DIR__ . '/../public.key'; $publicKeyPath = 'file://' . __DIR__ . '/../public.key';
@ -41,7 +41,7 @@ $app = new App([
$publicKeyPath $publicKeyPath
); );
// Enable the password grant on the server with a token TTL of 1 hour // Enable the authentication code grant on the server with a token TTL of 1 hour
$server->enableGrantType( $server->enableGrantType(
new AuthCodeGrant( new AuthCodeGrant(
$authCodeRepository, $authCodeRepository,
@ -56,27 +56,19 @@ $app = new App([
}, },
]); ]);
$app->any('/authorize', function (Request $request, Response $response) { $app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
/** @var Server $server */ /* @var \League\OAuth2\Server\Server $server */
$server = $this->get(Server::class); $server = $app->getContainer()->get(Server::class);
try {
return $server->respondToRequest($request, $response);
} catch (OAuthServerException $e) {
return $e->generateHttpResponse($response);
} catch (\Exception $e) {
return $response->withStatus(500)->write($e->getMessage());
}
});
$app->post('/access_token', function (Request $request, Response $response) {
/** @var Server $server */
$server = $this->get(Server::class);
try { try {
return $server->respondToRequest($request, $response); return $server->respondToRequest($request, $response);
} catch (OAuthServerException $e) { } catch (OAuthServerException $exception) {
return $e->generateHttpResponse($response); return $exception->generateHttpResponse($response);
} catch (\Exception $e) { } catch (\Exception $exception) {
return $response->withStatus(500)->write($e->getMessage()); $body = new Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
} }
}); });

View File

@ -3,25 +3,25 @@
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\ClientCredentialsGrant; use League\OAuth2\Server\Grant\ClientCredentialsGrant;
use League\OAuth2\Server\Server; use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Repositories\AccessTokenRepository; use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository; use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository; use OAuth2ServerExamples\Repositories\ScopeRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App; use Slim\App;
use Slim\Http\Request; use Zend\Diactoros\Stream;
use Slim\Http\Response;
include(__DIR__ . '/../vendor/autoload.php'); include(__DIR__ . '/../vendor/autoload.php');
// App
$app = new App([ $app = new App([
'settings' => [
'displayErrorDetails' => true,
],
Server::class => function () { Server::class => function () {
// Init our repositories // Init our repositories
$clientRepository = new ClientRepository(); $clientRepository = new ClientRepository();
$scopeRepository = new ScopeRepository();
$accessTokenRepository = new AccessTokenRepository(); $accessTokenRepository = new AccessTokenRepository();
$scopeRepository = new ScopeRepository();
$privateKeyPath = 'file://' . __DIR__ . '/../private.key'; $privateKeyPath = 'file://' . __DIR__ . '/../private.key';
$publicKeyPath = 'file://' . __DIR__ . '/../public.key'; $publicKeyPath = 'file://' . __DIR__ . '/../public.key';
@ -36,21 +36,28 @@ $app = new App([
); );
// Enable the client credentials grant on the server with a token TTL of 1 hour // Enable the client credentials grant on the server with a token TTL of 1 hour
$server->enableGrantType(new ClientCredentialsGrant(), new \DateInterval('PT1H')); $server->enableGrantType(
new ClientCredentialsGrant(),
new \DateInterval('PT1H')
);
return $server; return $server;
} }
]); ]);
$app->post('/access_token', function (Request $request, Response $response) { $app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
/** @var Server $server */ /* @var \League\OAuth2\Server\Server $server */
$server = $this->get(Server::class); $server = $app->getContainer()->get(Server::class);
try { try {
return $server->respondToRequest($request, $response); return $server->respondToRequest($request, $response);
} catch (OAuthServerException $e) { } catch (OAuthServerException $exception) {
return $e->generateHttpResponse($response); return $exception->generateHttpResponse($response);
} catch (\Exception $e) { } catch (\Exception $exception) {
return $response->withStatus(500)->write($e->getMessage()); $body = new Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
} }
}); });

View File

@ -1,33 +1,35 @@
<?php <?php
use League\OAuth2\Server\Grant\PasswordGrant; use League\OAuth2\Server\Grant\AuthCodeGrant;
use League\OAuth2\Server\Grant\RefreshTokenGrant; use League\OAuth2\Server\Grant\RefreshTokenGrant;
use League\OAuth2\Server\Middleware\AuthenticationServerMiddleware; use League\OAuth2\Server\Middleware\AuthenticationServerMiddleware;
use League\OAuth2\Server\Middleware\ResourceServerMiddleware;
use League\OAuth2\Server\Server; use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Repositories\AccessTokenRepository; use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\AuthCodeRepository;
use OAuth2ServerExamples\Repositories\ClientRepository; use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\RefreshTokenRepository; use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository; use OAuth2ServerExamples\Repositories\ScopeRepository;
use OAuth2ServerExamples\Repositories\UserRepository; use OAuth2ServerExamples\Repositories\UserRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App; use Slim\App;
use Zend\Diactoros\Stream;
include(__DIR__ . '/../vendor/autoload.php'); include(__DIR__ . '/../vendor/autoload.php');
// App
$app = new App([ $app = new App([
'settings' => [ 'settings' => [
'displayErrorDetails' => true, 'displayErrorDetails' => true,
], ],
Server::class => function () { Server::class => function () {
// Init our repositories // Init our repositories
$clientRepository = new ClientRepository(); $clientRepository = new ClientRepository();
$accessTokenRepository = new AccessTokenRepository(); $accessTokenRepository = new AccessTokenRepository();
$scopeRepository = new ScopeRepository(); $scopeRepository = new ScopeRepository();
$userRepository = new UserRepository(); $authCodeRepository = new AuthCodeRepository();
$refreshTokenRepository = new RefreshTokenRepository(); $refreshTokenRepository = new RefreshTokenRepository();
$userRepository = new UserRepository();
$privateKeyPath = 'file://' . __DIR__ . '/../private.key'; $privateKeyPath = 'file://' . __DIR__ . '/../private.key';
$publicKeyPath = 'file://' . __DIR__ . '/../public.key'; $publicKeyPath = 'file://' . __DIR__ . '/../public.key';
@ -41,21 +43,53 @@ $app = new App([
$publicKeyPath $publicKeyPath
); );
// Enable the grants // Enable the authentication code grant on the server with a token TTL of 1 hour
$server->enableGrantType( $server->enableGrantType(
new PasswordGrant($userRepository, $refreshTokenRepository), new AuthCodeGrant(
$authCodeRepository,
$refreshTokenRepository,
$userRepository,
new \DateInterval('PT10M')
),
new \DateInterval('PT1H') new \DateInterval('PT1H')
); );
// Enable the refresh token grant on the server with a token TTL of 1 month
$server->enableGrantType( $server->enableGrantType(
new RefreshTokenGrant($refreshTokenRepository), new RefreshTokenGrant($refreshTokenRepository),
new \DateInterval('PT1H') new \DateInterval('PT1M')
); );
return $server; return $server;
} }
]); ]);
// Access token issuer
$app->post('/access_token', function () { $app->post('/access_token', function () {
})->add(new AuthenticationServerMiddleware($app->getContainer()->get(Server::class))); })->add(new AuthenticationServerMiddleware($app->getContainer()->get(Server::class)));
// Secured API
$app->group('/api', function() {
$this->get('/user', function (ServerRequestInterface $request, ResponseInterface $response) {
$params = [];
if (in_array('basic', $request->getAttribute('oauth_scopes', []))) {
$params = [
'id' => 1,
'name' => 'Alex',
'city' => 'London'
];
}
if (in_array('email', $request->getAttribute('oauth_scopes', []))) {
$params['email'] = 'alex@example.com';
}
$body = new Stream('php://temp', 'r+');
$body->write(json_encode($params));
return $response->withBody($body);
});
})->add(new ResourceServerMiddleware($app->getContainer()->get(Server::class)));
$app->run(); $app->run();

View File

@ -3,27 +3,27 @@
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\PasswordGrant; use League\OAuth2\Server\Grant\PasswordGrant;
use League\OAuth2\Server\Server; use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Repositories\AccessTokenRepository; use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository; use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\RefreshTokenRepository; use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository; use OAuth2ServerExamples\Repositories\ScopeRepository;
use OAuth2ServerExamples\Repositories\UserRepository; use OAuth2ServerExamples\Repositories\UserRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App; use Slim\App;
use Slim\Http\Request; use Zend\Diactoros\Stream;
use Slim\Http\Response;
include(__DIR__ . '/../vendor/autoload.php'); include(__DIR__ . '/../vendor/autoload.php');
// App
$app = new App([ $app = new App([
'settings' => [
'displayErrorDetails' => true,
],
Server::class => function () { Server::class => function () {
// Init our repositories // Init our repositories
$clientRepository = new ClientRepository(); $clientRepository = new ClientRepository();
$scopeRepository = new ScopeRepository();
$accessTokenRepository = new AccessTokenRepository(); $accessTokenRepository = new AccessTokenRepository();
$scopeRepository = new ScopeRepository();
$userRepository = new UserRepository(); $userRepository = new UserRepository();
$refreshTokenRepository = new RefreshTokenRepository(); $refreshTokenRepository = new RefreshTokenRepository();
@ -49,15 +49,19 @@ $app = new App([
} }
]); ]);
$app->post('/access_token', function (Request $request, Response $response) { $app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
/** @var Server $server */ /* @var \League\OAuth2\Server\Server $server */
$server = $this->get(Server::class); $server = $app->getContainer()->get(Server::class);
try { try {
return $server->respondToRequest($request, $response); return $server->respondToRequest($request, $response);
} catch (OAuthServerException $e) { } catch (OAuthServerException $exception) {
return $e->generateHttpResponse($response); return $exception->generateHttpResponse($response);
} catch (\Exception $e) { } catch (\Exception $exception) {
return $response->withStatus(500)->write($e->getMessage()); $body = new Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
} }
}); });

View File

@ -3,57 +3,63 @@
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\RefreshTokenGrant; use League\OAuth2\Server\Grant\RefreshTokenGrant;
use League\OAuth2\Server\Server; use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Repositories\AccessTokenRepository; use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository; use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\RefreshTokenRepository; use OAuth2ServerExamples\Repositories\RefreshTokenRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository; use OAuth2ServerExamples\Repositories\ScopeRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App; use Slim\App;
use Slim\Http\Request; use Zend\Diactoros\Stream;
use Slim\Http\Response;
include(__DIR__ . '/../vendor/autoload.php'); include(__DIR__ . '/../vendor/autoload.php');
$app = new App([
'settings' => [
'displayErrorDetails' => true,
],
Server::class => function () {
// Init our repositories
$clientRepository = new ClientRepository();
$accessTokenRepository = new AccessTokenRepository();
$scopeRepository = new ScopeRepository();
$refreshTokenRepository = new RefreshTokenRepository();
$privateKeyPath = 'file://' . __DIR__ . '/../private.key';
$publicKeyPath = 'file://' . __DIR__ . '/../public.key';
// App // Setup the authorization server
$app = new App([Server::class => function () { $server = new Server(
// Init our repositories $clientRepository,
$clientRepository = new ClientRepository(); $accessTokenRepository,
$scopeRepository = new ScopeRepository(); $scopeRepository,
$accessTokenRepository = new AccessTokenRepository(); $privateKeyPath,
$refreshTokenRepository = new RefreshTokenRepository(); $publicKeyPath
);
$privateKeyPath = 'file://' . __DIR__ . '/../private.key'; // Enable the refresh token grant on the server with a token TTL of 1 hour
$publicKeyPath = 'file://' . __DIR__ . '/../public.key'; $server->enableGrantType(
new RefreshTokenGrant($refreshTokenRepository),
new \DateInterval('PT1H')
);
// Setup the authorization server return $server;
$server = new Server( }
$clientRepository, ]);
$accessTokenRepository,
$scopeRepository,
$privateKeyPath,
$publicKeyPath
);
// Enable the refresh token grant on the server $app->post('/access_token', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
$server->enableGrantType(new RefreshTokenGrant($refreshTokenRepository), new \DateInterval('PT1H')); /* @var \League\OAuth2\Server\Server $server */
$server = $app->getContainer()->get(Server::class);
return $server;
}]);
$app->post('/access_token', function (Request $request, Response $response) {
/** @var Server $server */
$server = $this->get(Server::class);
try { try {
return $server->respondToRequest($request, $response); return $server->respondToRequest($request, $response);
} catch (OAuthServerException $e) { } catch (OAuthServerException $exception) {
return $e->generateHttpResponse($response); return $exception->generateHttpResponse($response);
} catch (\Exception $e) { } catch (\Exception $exception) {
return $response->withStatus(500)->write( $body = new Stream('php://temp', 'r+');
sprintf('<h1>%s</h1><p>%s</p>', get_class($e), $e->getMessage()) $body->write($exception->getMessage());
);
return $response->withStatus(500)->withBody($body);
} }
}); });

View File

@ -1,48 +1,54 @@
<?php <?php
use League\OAuth2\Server\Middleware\ResourceServerMiddleware; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Server; use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Repositories\AccessTokenRepository; use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository; use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository; use OAuth2ServerExamples\Repositories\ScopeRepository;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Slim\App; use Slim\App;
use Slim\Http\Request; use Zend\Diactoros\Stream;
use Slim\Http\Response;
include(__DIR__ . '/../vendor/autoload.php'); include(__DIR__ . '/../vendor/autoload.php');
// App
$app = new App([ $app = new App([
'settings' => [ 'settings' => [
'displayErrorDetails' => true, 'displayErrorDetails' => true,
], ],
Server::class => function () { Server::class => function () {
// Init our repositories // Init our repositories
$clientRepository = new ClientRepository(); $clientRepository = new ClientRepository();
$scopeRepository = new ScopeRepository();
$accessTokenRepository = new AccessTokenRepository(); $accessTokenRepository = new AccessTokenRepository();
$scopeRepository = new ScopeRepository();
$privateKeyPath = 'file://' . __DIR__ . '/../private.key'; $privateKeyPath = 'file://' . __DIR__ . '/../private.key';
$publicKeyPath = 'file://' . __DIR__ . '/../public.key'; $publicKeyPath = 'file://' . __DIR__ . '/../public.key';
// Setup the authorization server // Setup the authorization server
$server = new Server( return new Server(
$clientRepository, $clientRepository,
$accessTokenRepository, $accessTokenRepository,
$scopeRepository, $scopeRepository,
$privateKeyPath, $privateKeyPath,
$publicKeyPath $publicKeyPath
); );
return $server;
} }
]); ]);
$app->add(new ResourceServerMiddleware($app->getContainer()->get(Server::class))); $app->get('/user', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
$app->post('/api/example', function (Request $request, Response $response) { $server = $app->getContainer()->get(Server::class);
$body = new Stream('php://temp', 'r+');
try {
$request = $server->validateRequest($request);
} catch (OAuthServerException $exception) {
return $exception->generateHttpResponse($response);
} catch (\Exception $exception) {
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
}
$params = []; $params = [];
@ -58,9 +64,9 @@ $app->post('/api/example', function (Request $request, Response $response) {
$params['email'] = 'alex@example.com'; $params['email'] = 'alex@example.com';
} }
$response->getBody()->write(json_encode($params)); $body->write(json_encode($params));
return $response; return $response->withBody($body);
}); });
$app->run(); $app->run();