oauth2-server/examples/public/protected_api.php

65 lines
1.7 KiB
PHP
Raw Normal View History

2016-01-17 20:26:51 +05:30
<?php
use League\OAuth2\Server\Middleware\ResourceServerMiddleware;
use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository;
use Slim\App;
use Slim\Http\Request;
use Slim\Http\Response;
2016-02-22 13:30:50 +05:30
include __DIR__ . '/../vendor/autoload.php';
2016-01-17 20:26:51 +05:30
// App
$app = new App([
'settings' => [
'displayErrorDetails' => true,
],
Server::class => function () {
// Init our repositories
$clientRepository = new ClientRepository();
$scopeRepository = new ScopeRepository();
$accessTokenRepository = new AccessTokenRepository();
2016-02-22 13:30:50 +05:30
$privateKeyPath = 'file://' . __DIR__ . '/../private.key';
$publicKeyPath = 'file://' . __DIR__ . '/../public.key';
2016-01-17 20:26:51 +05:30
// Setup the authorization server
$server = new Server(
$clientRepository,
$accessTokenRepository,
$scopeRepository,
$privateKeyPath,
$publicKeyPath
);
return $server;
2016-02-20 04:39:39 +05:30
},
2016-01-17 20:26:51 +05:30
]);
$app->add(new ResourceServerMiddleware($app->getContainer()->get(Server::class)));
$app->post('/api/example', function (Request $request, Response $response) {
$params = [];
if (in_array('basic', $request->getAttribute('oauth_scopes', []))) {
$params = [
'id' => 1,
'name' => 'Alex',
2016-02-20 04:39:39 +05:30
'city' => 'London',
2016-01-17 20:26:51 +05:30
];
}
if (in_array('email', $request->getAttribute('oauth_scopes', []))) {
$params['email'] = 'alex@example.com';
}
$response->getBody()->write(json_encode($params));
return $response;
});
$app->run();