Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ A simple service provider that makes Laravel Passport work with Lumen
## Dependencies

* PHP >= 5.6.3
* Lumen >= 5.3
* Lumen >= 5.5

## Installation via Composer

Expand Down Expand Up @@ -131,13 +131,26 @@ Next, you should call the LumenPassport::routes method within the boot method of
This method will register the routes necessary to issue access tokens and revoke access tokens, clients, and personal access tokens:

```php
\Dusterio\LumenPassport\LumenPassport::routes($this->app);
Dusterio\LumenPassport\LumenPassport::routes();
```

Use de `$callback` parameter to customize which endpoints will be enabled:

```php
Dusterio\LumenPassport\LumenPassport::routes(function ($router) {
// call just what you need of these functions
// all defined in Dusterio\LumenPassport\RouteRegistrar
$router->forAccessTokens();
$router->forTransientTokens();
$router->forClients();
$router->forPersonalAccessTokens();
});
```

You can add that into an existing group, or add use this route registrar independently like so;

```php
\Dusterio\LumenPassport\LumenPassport::routes($this->app, ['prefix' => 'v1/oauth']);
Dusterio\LumenPassport\LumenPassport::routes(null, ['prefix' => 'v1/oauth']);
```

## User model
Expand Down
21 changes: 8 additions & 13 deletions src/LumenPassport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Dusterio\LumenPassport;

use Laravel\Passport\Passport;
use DateTimeInterface;
use DateInterval;
use Carbon\Carbon;
use Laravel\Lumen\Application;
use Laravel\Lumen\Routing\Router;
use DateTimeInterface;
use Laravel\Passport\Passport;
use Illuminate\Support\Facades\Route;

class LumenPassport
{
Expand Down Expand Up @@ -66,14 +64,12 @@ public static function tokensExpireIn(DateTimeInterface $date = null, $clientId
/**
* Get a Passport route registrar.
*
* @param callable|Router|Application $callback
* @param array $options
* @return RouteRegistrar
* @param callable|null $callback
* @param array $options
* @return void
*/
public static function routes($callback = null, array $options = [])
{
if ($callback instanceof Application && preg_match('/5\.[5-8]\..*/', $callback->version())) $callback = $callback->router;

$callback = $callback ?: function ($router) {
$router->all();
};
Expand All @@ -85,9 +81,8 @@ public static function routes($callback = null, array $options = [])

$options = array_merge($defaultOptions, $options);

$callback->group(array_except($options, ['namespace']), function ($router) use ($callback, $options) {
$routes = new RouteRegistrar($router, $options);
$routes->all();
Route::group(array_except($options, ['namespace']), function ($router) use ($callback, $options) {
$callback(new RouteRegistrar($router, $options));
});
}
}
41 changes: 21 additions & 20 deletions src/RouteRegistrar.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
class RouteRegistrar
{
/**
* @var Application
* @var \Laravel\Lumen\Routing\Router Router
*/
private $app;
private $router;

/**
* @var array
Expand All @@ -17,12 +17,13 @@ class RouteRegistrar
/**
* Create a new route registrar instance.
*
* @param $app
* @param \Laravel\Lumen\Routing\Router $router
* @param array $options
* @return void
*/
public function __construct($app, array $options = [])
public function __construct(\Laravel\Lumen\Routing\Router $router, array $options = [])
{
$this->app = $app;
$this->router = $router;
$this->options = $options;
}

Expand Down Expand Up @@ -57,11 +58,11 @@ private function prefix($path)
*/
public function forAccessTokens()
{
$this->app->post('/token', $this->prefix('\Dusterio\LumenPassport\Http\Controllers\AccessTokenController@issueToken'));
$this->router->post('/token', $this->prefix('\Dusterio\LumenPassport\Http\Controllers\AccessTokenController@issueToken'));

$this->app->group(['middleware' => ['auth']], function () {
$this->app->get('/tokens', $this->prefix('AuthorizedAccessTokenController@forUser'));
$this->app->delete('/tokens/{token_id}', $this->prefix('AuthorizedAccessTokenController@destroy'));
$this->router->group(['middleware' => ['auth']], function () {
$this->router->get('/tokens', $this->prefix('AuthorizedAccessTokenController@forUser'));
$this->router->delete('/tokens/{token_id}', $this->prefix('AuthorizedAccessTokenController@destroy'));
});
}

Expand All @@ -72,7 +73,7 @@ public function forAccessTokens()
*/
public function forTransientTokens()
{
$this->app->post('/token/refresh', [
$this->router->post('/token/refresh', [
'middleware' => ['auth'],
'uses' => $this->prefix('TransientTokenController@refresh')
]);
Expand All @@ -85,11 +86,11 @@ public function forTransientTokens()
*/
public function forClients()
{
$this->app->group(['middleware' => ['auth']], function () {
$this->app->get('/clients', $this->prefix('ClientController@forUser'));
$this->app->post('/clients', $this->prefix('ClientController@store'));
$this->app->put('/clients/{client_id}', $this->prefix('ClientController@update'));
$this->app->delete('/clients/{client_id}', $this->prefix('ClientController@destroy'));
$this->router->group(['middleware' => ['auth']], function () {
$this->router->get('/clients', $this->prefix('ClientController@forUser'));
$this->router->post('/clients', $this->prefix('ClientController@store'));
$this->router->put('/clients/{client_id}', $this->prefix('ClientController@update'));
$this->router->delete('/clients/{client_id}', $this->prefix('ClientController@destroy'));
});
}

Expand All @@ -100,11 +101,11 @@ public function forClients()
*/
public function forPersonalAccessTokens()
{
$this->app->group(['middleware' => ['auth']], function () {
$this->app->get('/scopes', $this->prefix('ScopeController@all'));
$this->app->get('/personal-access-tokens', $this->prefix('PersonalAccessTokenController@forUser'));
$this->app->post('/personal-access-tokens', $this->prefix('PersonalAccessTokenController@store'));
$this->app->delete('/personal-access-tokens/{token_id}', $this->prefix('PersonalAccessTokenController@destroy'));
$this->router->group(['middleware' => ['auth']], function () {
$this->router->get('/scopes', $this->prefix('ScopeController@all'));
$this->router->get('/personal-access-tokens', $this->prefix('PersonalAccessTokenController@forUser'));
$this->router->post('/personal-access-tokens', $this->prefix('PersonalAccessTokenController@store'));
$this->router->delete('/personal-access-tokens/{token_id}', $this->prefix('PersonalAccessTokenController@destroy'));
});
}
}