PHP code example of maksimovic / slim-oauth2-routes

1. Go to this page and download the library: Download maksimovic/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/ */

    

maksimovic / 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(['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/maksimovic/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');

$app->run();

use Chadicus\Slim\OAuth2\Routes\UserIdProviderInterface;
use Psr\Http\Message\ServerRequestInterface;

class ArgumentUserIdProvider implements UserIdProviderInterface
{
    public function getUserId(ServerRequestInterface $request, array $arguments = [])
    {
        return isset($arguments['user_id']) ? $arguments['user_id'] : null;
    }
}

$authorizeRoute = new Routes\Authorize($server, $view, 'authorize.phtml', new ArgumentUserIdProvider());
$app->map(['GET', 'POST'], Routes\Authorize::ROUTE, $authorizeRoute)->setName('authorize');