2016-03-24 20:57:55 +05:30
|
|
|
<?php
|
|
|
|
|
2016-04-17 17:11:28 +05:30
|
|
|
use League\OAuth2\Server\ResourceServer;
|
2016-03-24 20:57:55 +05:30
|
|
|
use OAuth2ServerExamples\Repositories\AccessTokenRepository;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
|
|
use Slim\App;
|
|
|
|
|
|
|
|
include __DIR__ . '/../vendor/autoload.php';
|
|
|
|
|
|
|
|
$app = new App([
|
2016-04-18 16:53:21 +05:30
|
|
|
// Add the resource server to the DI container
|
2016-04-17 17:24:49 +05:30
|
|
|
ResourceServer::class => function () {
|
2016-04-17 17:11:28 +05:30
|
|
|
$server = new ResourceServer(
|
2016-04-18 16:53:21 +05:30
|
|
|
new AccessTokenRepository(), // instance of AccessTokenRepositoryInterface
|
|
|
|
'file://' . __DIR__ . '/../public.key' // the authorization server's public key
|
2016-03-24 20:57:55 +05:30
|
|
|
);
|
|
|
|
|
|
|
|
return $server;
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
|
2016-04-18 16:53:21 +05:30
|
|
|
// Add the resource server middleware which will intercept and validate requests
|
2016-03-24 20:57:55 +05:30
|
|
|
$app->add(
|
|
|
|
new \League\OAuth2\Server\Middleware\ResourceServerMiddleware(
|
2016-04-17 17:24:49 +05:30
|
|
|
$app->getContainer()->get(ResourceServer::class)
|
2016-03-24 20:57:55 +05:30
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-04-18 16:53:21 +05:30
|
|
|
// An example endpoint secured with OAuth 2.0
|
|
|
|
$app->get(
|
|
|
|
'/users',
|
|
|
|
function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
|
|
|
|
$users = [
|
|
|
|
[
|
|
|
|
'id' => 123,
|
|
|
|
'name' => 'Alex',
|
|
|
|
'email' => 'alex@thephpleague.com',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 124,
|
|
|
|
'name' => 'Frank',
|
|
|
|
'email' => 'frank@thephpleague.com',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'id' => 125,
|
|
|
|
'name' => 'Phil',
|
|
|
|
'email' => 'phil@thephpleague.com',
|
|
|
|
],
|
|
|
|
];
|
2016-03-24 20:57:55 +05:30
|
|
|
|
2017-12-29 17:55:39 +05:30
|
|
|
$totalUsers = count($users);
|
|
|
|
|
2016-04-18 16:53:21 +05:30
|
|
|
// If the access token doesn't have the `basic` scope hide users' names
|
|
|
|
if (in_array('basic', $request->getAttribute('oauth_scopes')) === false) {
|
2017-12-29 17:55:39 +05:30
|
|
|
for ($i = 0; $i < $totalUsers; $i++) {
|
2016-04-18 16:53:21 +05:30
|
|
|
unset($users[$i]['name']);
|
|
|
|
}
|
2016-03-24 20:57:55 +05:30
|
|
|
}
|
|
|
|
|
2016-04-18 16:53:21 +05:30
|
|
|
// If the access token doesn't have the `email` scope hide users' email addresses
|
|
|
|
if (in_array('email', $request->getAttribute('oauth_scopes')) === false) {
|
2017-12-29 17:55:39 +05:30
|
|
|
for ($i = 0; $i < $totalUsers; $i++) {
|
2016-04-18 16:53:21 +05:30
|
|
|
unset($users[$i]['email']);
|
|
|
|
}
|
2016-03-24 20:57:55 +05:30
|
|
|
}
|
|
|
|
|
2016-04-18 16:53:21 +05:30
|
|
|
$response->getBody()->write(json_encode($users));
|
2016-03-24 20:57:55 +05:30
|
|
|
|
2016-04-18 16:53:21 +05:30
|
|
|
return $response->withStatus(200);
|
|
|
|
}
|
|
|
|
);
|
2016-03-24 20:57:55 +05:30
|
|
|
|
2016-09-13 19:47:09 +05:30
|
|
|
$app->run();
|