Updated implicit grant example

This commit is contained in:
Alex Bilbie 2016-04-10 14:31:21 +01:00
parent ba30e34511
commit d3a7b442ce

View File

@ -3,6 +3,7 @@
use League\OAuth2\Server\Exception\OAuthServerException; use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\Grant\ImplicitGrant; use League\OAuth2\Server\Grant\ImplicitGrant;
use League\OAuth2\Server\Server; use League\OAuth2\Server\Server;
use OAuth2ServerExamples\Entities\UserEntity;
use OAuth2ServerExamples\Repositories\AccessTokenRepository; use OAuth2ServerExamples\Repositories\AccessTokenRepository;
use OAuth2ServerExamples\Repositories\ClientRepository; use OAuth2ServerExamples\Repositories\ClientRepository;
use OAuth2ServerExamples\Repositories\ScopeRepository; use OAuth2ServerExamples\Repositories\ScopeRepository;
@ -38,21 +39,30 @@ $app = new App([
); );
// Enable the implicit grant on the server with a token TTL of 1 hour // Enable the implicit grant on the server with a token TTL of 1 hour
$server->enableGrantType( $server->enableGrantType(new ImplicitGrant($userRepository, new \DateInterval('PT1H')));
new ImplicitGrant($userRepository),
new \DateInterval('PT1H')
);
return $server; return $server;
}, },
]); ]);
$app->any('/authorize', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) { $app->get('/authorize', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
/* @var \League\OAuth2\Server\Server $server */ /* @var \League\OAuth2\Server\Server $server */
$server = $app->getContainer()->get(Server::class); $server = $app->getContainer()->get(Server::class);
try { try {
return $server->respondToRequest($request, $response); // Validate the HTTP request and return an AuthorizationRequest object.
// The auth request object can be serialized into a user's session
$authRequest = $server->validateAuthorizationRequest($request);
// Once the user has logged in set the user on the AuthorizationRequest
$authRequest->setUser(new UserEntity());
// Once the user has approved or denied the client update the status
// (true = approved, false = denied)
$authRequest->setAuthorizationApproved(true);
// Return the HTTP redirect response
return $server->completeAuthorizationRequest($authRequest, $response);
} catch (OAuthServerException $exception) { } catch (OAuthServerException $exception) {
return $exception->generateHttpResponse($response); return $exception->generateHttpResponse($response);
} catch (\Exception $exception) { } catch (\Exception $exception) {