1. Go to this page and download the library: Download chadicus/slim-oauth2-routes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
chadicus / slim-oauth2-routes example snippets
use Chadicus\Slim\OAuth2\Routes;
use OAuth2;
use OAuth2\GrantType;
use OAuth2\Storage;
use Slim;
use Slim\Views;
//Set-up the OAuth2 Server
$storage = new Storage\Pdo(array('dsn' => $dsn, 'username' => $username, 'password' => $password));
$server = new OAuth2\Server($storage);
$server->addGrantType(new GrantType\AuthorizationCode($storage));
$server->addGrantType(new GrantType\ClientCredentials($storage));
//Set-up the Slim Application
$app = new Slim\App(
[
'view' => new Views\PhpRenderer('/path/to/chadicus/slim-oauth2-routes/templates'),
]
);
$container = $app->getContainer();
$app->map(['GET', 'POST'], Routes\Authorize::ROUTE, new Routes\Authorize($server, $container['view']))->setName('authorize');
$app->post(Routes\Token::ROUTE, new Routes\Token($server))->setName('token');
$app->map(['GET', 'POST'], Routes\ReceiveCode::ROUTE, new Routes\ReceiveCode($container['view']))->setName('receive-code');
$app->post(Routes\Revoke::ROUTE, new Routes\Revoke($server))->setName('revoke');
//Add custom routes
$slim->get('/foo', function($request, $response, $args) {
$authorization = $request->getHeaderLine('Authorization');
//validate access token against your storage
return $response->withStatus(200);
});
//run the app
$app->run();
class ArgumentUserIdProvider implements UserIdProviderInterface
{
public function getUserId(ServerRequestInterface $request, array $arguments)
{
return isset($arguments['user_id']) ? $arguments['user_id'] : null;
}
}
//middleware to add user_id to route parameters
$loginMiddelware = function ($request, $response, $next) {
// Validate the user credentials
$userId = MyUserService::getUserIdIfValidCredentials($request);
if ($userId === false) {
return $response->withStatus(303);
}
//Put user_id into the route parameters
$route = $request->getAttribute('route');
$route->setArgument('user_id', $userId);
//Credentials are valid, continue so the authorization code can be sent to the clients callback_uri
return $next($request, $response);
};
$authorizeRoute = new Routes\Authorize($server, $view, 'authorize.phtml', new ArgumentUserIdProvider());
$app->map(
['GET', 'POST'],
Routes\Authorize::ROUTE,
$authorizeRoute
)->add($loginMiddleware)->setName('authorize');
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.