Exemplo n.º 1
0
require_once __DIR__ . '/../../vendor/autoload.php';
use Chadicus\Slim\OAuth2\Routes;
use Chadicus\Slim\OAuth2\Middleware;
use Slim\Slim;
use OAuth2\Server;
use OAuth2\Storage;
use OAuth2\GrantType;
$mongoDb = (new MongoClient())->selectDb('slim_oauth2');
$storage = new Storage\Mongo($mongoDb);
$storage->setClientDetails('librarian', 'secret', '/receive-code', null, 'bookCreate');
$storage->setClientDetails('student', 's3cr3t');
$server = new Server($storage, ['access_lifetime' => 3600], [new GrantType\ClientCredentials($storage), new GrantType\AuthorizationCode($storage)]);
$app = new Slim();
Routes\Token::register($app, $server);
Routes\Authorize::register($app, $server);
Routes\ReceiveCode::register($app);
$app->config('templates.path', __DIR__ . '/../../vendor/chadicus/slim-oauth2-routes/templates');
$authorization = new Middleware\Authorization($server);
$authorization->setApplication($app);
$app->get('/books', $authorization, function () use($app, $mongoDb) {
    $result = [];
    try {
        $limit = isset($_GET['limit']) ? (int) $_GET['limit'] : 5;
        $offset = isset($_GET['offset']) ? (int) $_GET['offset'] : 0;
        $books = $mongoDb->books->find([])->skip($offset)->limit($limit);
        $result = ['offset' => $offset, 'limit' => $books->count(true), 'total' => $books->count(), 'books' => []];
        foreach ($books as $book) {
            $result['books'][] = ['id' => (string) $book['_id'], 'url' => "/books/{$book['_id']}"];
        }
    } catch (\Exception $e) {
 /**
  * Verify basic behavior of register
  *
  * @test
  * @covers ::register
  *
  * @return void
  */
 public function register()
 {
     $storage = new \OAuth2\Storage\Memory([]);
     $server = new \OAuth2\Server($storage, [], []);
     \Slim\Environment::mock();
     $slim = new \Slim\Slim();
     Authorize::register($slim, $server);
     $route = $slim->router()->getNamedRoute('authorize');
     $this->assertInstanceOf('\\Slim\\Route', $route);
     $this->assertInstanceOf('\\Chadicus\\Slim\\OAuth2\\Routes\\Authorize', $route->getCallable());
     $this->assertSame([\Slim\Http\Request::METHOD_GET, \Slim\Http\Request::METHOD_POST], $route->getHttpMethods());
 }