sonrac/lumen-league-oauth2
Installation
composer require sonrac/lumen-league-oauth2
Usages
Resister service provider first:
Add to your bootstrap/app.php
$app->register(\sonrac\lumenRest\Oauth2ServiceProvider::class);
Description
league/oauth2 -server implementation for lumen
Contracts
Contracts or oauth2 server implemented in sonrac\lumenRest\Oauth2ServiceProvider
Events
Events usages described in official documentation
Middleware
Use League\OAuth2\Server\Middleware\ResourceMiddleware for validate authentication request
Use League\OAuth2\Server\Middleware\AuthorizationServerMiddleware for user authenticate
Example routing
Get access token
$app->router->post('/access_token', function (\Psr\Http\Message\ServerRequestInterface $request,
\Psr\Http\Message\ResponseInterface $response) use ($app) {
/* @var \League\OAuth2\Server\AuthorizationServer $server */
$server = $app->make(\League\OAuth2\Server\AuthorizationServer::class);
try {
// Try to respond to the request
return $server->respondToAccessTokenRequest($request, $response);
} catch (\League\OAuth2\Server\Exception\OAuthServerException $exception) {
// All instances of OAuthServerException can be formatted into a HTTP response
return $exception->generateHttpResponse($response);
} catch (\Exception $exception) {
// Unknown exception
$body = new \Zend\Diactoros\Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
}
});
Authorize third-party clients (implicit & auth code grants)
$app->router->get('/authorize', function (\League\OAuth2\Server\AuthorizationServer $server,
\Psr\Http\Message\ServerRequestInterface $request,
\Psr\Http\Message\ResponseInterface $response) {
try {
// Validate the HTTP request and return an AuthorizationRequest object.
$authRequest = $server->validateAuthorizationRequest($request);
// The auth request object can be serialized and saved into a user's session.
// You will probably want to redirect the user at this point to a login endpoint.
// Once the user has logged in set the user on the AuthorizationRequest
$authRequest->setUser(app()->make(\League\OAuth2\Server\Entities\UserEntityInterface::class)); // an instance of UserEntityInterface
// At this point you should redirect the user to an authorization page.
// This form will ask the user to approve the client and the scopes requested.
// 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 (\Exception $exception) {
// Unknown exception
$body = new \Zend\Diactoros\Stream('php://temp', 'r+');
$body->write($exception->getMessage());
return $response->withStatus(500)->withBody($body);
}
});
JWT guard
For using JWT token you need define JWT guard
Example config:
'defaults' => [
'guard' => 'jwt'
],
'guards' => [
'jwt' => [
'driver' => 'jwt',
'provider' => 'clients',
],
'user' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'clients' => [
'driver' => 'eloquent',
'model' => app(\League\OAuth2\Server\Entities\ClientEntityInterface::class),
],
'users' => [
'driver' => 'eloquent',
'model' => app(\League\OAuth2\Server\Entities\UserEntityInterface::class),
],
],
Generate keys
For using SSL encryption generate keys first:
php artisan generate:keys
Related Packages
doctrine/dbal
Powerful PHP database abstraction layer (DBAL) with many features for database s...
641,106,524
9,700
laravel/serializable-closure
Laravel Serializable Closure provides an easy and secure way to serialize closur...
418,291,778
609
nunomaduro/collision
Cli error handling for console/command-line PHP applications.
394,964,158
4,660
Version History
| Version | Released | PHP | License |
|---|---|---|---|
| 1.3.1 | >=7.0 | MIT | |
| 1.3 | >=7.0 | MIT | |
| 1.2 | >=7.0 | MIT | |
| 1.1 | >=7.0 | MIT | |
| 1.0.1 | >=7.0 | MIT | |
| 1.0 | >=7.0 | MIT | |
| 0.3.3 | MIT | ||
| 0.3.2 | MIT | ||
| 0.3.1 | MIT | ||
| 0.3.0 | MIT | ||
| 0.2.9 | MIT | ||
| 0.2.8 | MIT | ||
| 0.2.7 | MIT | ||
| 0.2.6 | MIT | ||
| 0.2.5 | MIT | ||
| 0.2.4 | MIT | ||
| 0.2.3 | MIT | ||
| 0.2.2 | MIT | ||
| 0.2.1 | MIT | ||
| 0.2 | MIT | ||
| 0.1.2 | MIT | ||
| 0.1.1 | MIT |
Pre-releases (1)
| Version | Released | License |
|---|---|---|
| 0.2.3-rc1 | MIT |