oauth2-server/examples/public/api.php

72 lines
1.9 KiB
PHP
Raw Normal View History

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([
'settings' => [
'displayErrorDetails' => true,
],
2016-04-17 17:24:49 +05:30
ResourceServer::class => function () {
2016-03-24 20:57:55 +05:30
// Setup the authorization server
2016-04-17 17:11:28 +05:30
$server = new ResourceServer(
2016-03-24 20:57:55 +05:30
new AccessTokenRepository(),
'file://' . __DIR__ . '/../public.key'
);
return $server;
},
]);
$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
)
);
$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-04-17 17:11:28 +05:30
// If the access token doesn't have the `basic` scope hide users' names
2016-03-24 20:57:55 +05:30
if (in_array('basic', $request->getAttribute('oauth_scopes')) === false) {
for ($i = 0; $i < count($users); $i++) {
unset($users[$i]['name']);
}
}
2016-04-17 17:11:28 +05:30
// If the access token doesn't have the `emal` scope hide users' email addresses
2016-03-24 20:57:55 +05:30
if (in_array('email', $request->getAttribute('oauth_scopes')) === false) {
for ($i = 0; $i < count($users); $i++) {
unset($users[$i]['email']);
}
}
$response->getBody()->write(json_encode($users));
return $response->withStatus(200);
});
$app->run();