Skip to content

Commit

Permalink
Merge pull request #45 from sfelix-martins/new-feature-manage-tokens
Browse files Browse the repository at this point in the history
New feature manage tokens
  • Loading branch information
sfelix-martins authored Aug 29, 2018
2 parents 99dfe3b + 02b29fe commit e30e435
Show file tree
Hide file tree
Showing 27 changed files with 925 additions and 488 deletions.
Empty file added .env
Empty file.
13 changes: 13 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
* text=auto

/tests export-ignore
.env export-ignore
.editorconfig export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.scrutinizer.yml export-ignore
.styleci.yml export-ignore
.travis.yml export-ignore
CONTRIBUTING.md export-ignore
phpunit.xml export-ignore
UPGRADE.md export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.idea
/.phpintel
/.vscode
.composer.lock
45 changes: 23 additions & 22 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
filter:
excluded_paths: [tests/*]
excluded_paths: [tests/*]

checks:
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true
fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true
fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true

build:
tests:
override:
-
command: 'vendor/bin/phpunit --coverage-clover=some-file'
coverage:
file: 'some-file'
format: 'clover'
tests:
override:
- php-scrutinizer-run
-
command: 'vendor/bin/phpunit --coverage-clover=some-file'
coverage:
file: 'some-file'
format: 'clover'
10 changes: 10 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ matrix:
- passport=6.0.x
- testbench=3.6.x
- phpunit=7.0.x
- php: 7.1
env:
- passport=7.0.x
- testbench=3.6.x
- phpunit=7.0.x
- php: 7.2
env:
- passport=4.0.x
Expand All @@ -56,6 +61,11 @@ matrix:
- passport=6.0.x
- testbench=3.6.x
- phpunit=7.0.x
- php: 7.2
env:
- passport=7.0.x
- testbench=3.6.x
- phpunit=7.0.x

sudo: false

Expand Down
65 changes: 48 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,17 @@ Add multi-authentication support to [Laravel Passport](https://laravel.com/docs/
| ^4.0 |
| ^5.0 |
| ^6.0 |
| ^7.0 |

## Installing and configuring

- Install using composer:
Install using composer:

```sh
$ composer require smartins/passport-multiauth
```

- If you are using a Laravel version **less than 5.5** you **need to add** the provider on `config/app.php`:
If you are using a Laravel version **less than 5.5** you **need to add** the provider on `config/app.php`:

```php
'providers' => [
Expand All @@ -40,32 +41,39 @@ $ composer require smartins/passport-multiauth
],
```

- Migrate database to create `oauth_access_token_providers` table:
Migrate database to create `oauth_access_token_providers` table:

```sh
$ php artisan migrate
```

- Add new provider in `config/auth.php` using a model that extends of `Authenticatable` class and use `HasApiTokens` trait.
Instead use the `Laravel\Passport\HasApiTokens` trait from [Laravel Passport](https://laravel.com/docs/5.6/passport#installation) core, use the trait `SMartins\PassportMultiauth\HasMultiAuthApiTokens`.
Internally, this `HasMultiAuthApiTokens` uses the `HasApiTokens`, overriding the methods `tokens()` and `createToken($name, $scopes = [])`.
The behavior of the method `tokens()` was changed to join with the table `oauth_access_token_providers` getting just the tokens created
to specific model.
The method `createToken($name, $scopes = [])` was changed to create the token using the `provider` defined to model on `config/auth.php`.
Now when you create the token, this token will be related with the model that is calling.

Add new provider in `config/auth.php` using a model that extends of `Authenticatable` class and use `HasMultiAuthApiTokens` trait.

Example:

- Configure your model:
Configure your model:

```php

use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use SMartins\PassportMultiauth\HasMultiAuthApiTokens;

class Admin extends Authenticatable
{
use Notifiable, HasApiTokens;
use Notifiable, HasMultiAuthApiTokens;
}

```

- And your `config/auth.php` providers:
And your `config/auth.php` providers:

```php
// ...
Expand All @@ -86,7 +94,7 @@ class Admin extends Authenticatable
// ...
```

- Add a new `guard` in `config/auth.php` guards array using driver `passport` and the provider added above:
Add a new `guard` in `config/auth.php` guards array using driver `passport` and the provider added above:

```php
// ...
Expand All @@ -112,7 +120,7 @@ class Admin extends Authenticatable
// ...
```

- Register the middleware `AddCustomProvider` to `$routeMiddleware` attributes on `app/Http/Kernel.php` file.
Register the middleware `AddCustomProvider` to `$routeMiddleware` attributes on `app/Http/Kernel.php` file.

```php

Expand All @@ -136,7 +144,7 @@ class Kernel extends HttpKernel
}
```

- Replace the middleware `Authenticate` on `app/Http/Kernel` `$routeMiddleware` attribute.
Replace the middleware `Authenticate` on `app/Http/Kernel` `$routeMiddleware` attribute.

```php

Expand Down Expand Up @@ -166,7 +174,7 @@ class Kernel extends HttpKernel
}
```

- Encapsulate the passport routes for access token with the registered middleware in `AuthServiceProvider`. This middleware will add the capability to `Passport` route `oauth/token` use the value of `provider` param on request:
Encapsulate the passport routes for access token with the registered middleware in `AuthServiceProvider`. This middleware will add the capability to `Passport` route `oauth/token` use the value of `provider` param on request:

```php
namespace App\Providers;
Expand Down Expand Up @@ -212,7 +220,7 @@ $ php artisan vendor:publish --provider="SMartins\PassportMultiauth\Providers\Mu

## Usage

- Add the `provider` parameter in your request at `/oauth/token`:
Add the `provider` parameter in your request at `/oauth/token`:

```http
POST /oauth/token HTTP/1.1
Expand All @@ -231,7 +239,7 @@ Cache-Control: no-cache
}
```

- You can pass your guards on `auth` middleware as you wish. Example:
You can pass your guards on `auth` middleware as you wish. Example:

```php
Route::group(['middleware' => ['api', 'auth:admin']], function () {
Expand All @@ -245,7 +253,7 @@ Route::group(['middleware' => ['api', 'auth:admin']], function () {

The `api` guard use is equals the example with `admin`.

- You can pass many guards to `auth` middleware.
You can pass many guards to `auth` middleware.

```php
Route::group(['middleware' => ['api', 'auth:admin,api']], function () {
Expand All @@ -265,7 +273,7 @@ Auth::user();

### Refreshing tokens

- Add the `provider` parameter in your request at `/oauth/token`:
Add the `provider` parameter in your request at `/oauth/token`:

```http
POST /oauth/token HTTP/1.1
Expand All @@ -285,7 +293,7 @@ Cache-Control: no-cache

### Using scopes

- Just use the [`scope` and `scopes`](https://laravel.com/docs/5.5/passport#checking-scopes) middlewares from `Laravel\Passport`.
Just use the [`scope` and `scopes`](https://laravel.com/docs/5.5/passport#checking-scopes) middlewares from `Laravel\Passport`.

```php
protected $routeMiddleware = [
Expand All @@ -294,6 +302,29 @@ protected $routeMiddleware = [
];
```

### Personal Access Tokens

In your model that uses the trait `SMartins\PassportMultiauth\HasMultiAuthApiTokens` you can uses the methods `createToken($name, $scopes = [])` and `tokens()` to manage your personal access tokens. E.g.:

```php

$user = User::find(1);
$admin = Admin::find(1);

// Create token from Model instance.
$user->createToken('My Token');
$admin->createToken('My Admin Token');

// Get the tokens created to this user.
$user->tokens()->each(function ($token) {
echo $token->name; // My Token
});

$admin->tokens()->each(function ($token) {
echo $token->name; // My Admin Token
});
```

### Unit tests

Instead to use the `Laravel\Passport\Passport::actingAs()` method, use `SMartins\PassportMultiauth\PassportMultiauth::actingAs()`.
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"prefer-stable": true,
"require": {
"php": ">=5.6.4",
"laravel/passport": "^3.0|^4.0|^5.0|^6.0"
"laravel/passport": "^3.0|^4.0|^5.0|^6.0|^7.0"
},
"require-dev": {
"orchestra/testbench": "~3.5",
Expand All @@ -35,6 +35,9 @@
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
},
"laravel": {
"providers": [
"SMartins\\PassportMultiauth\\Providers\\MultiauthServiceProvider"
Expand Down
Loading

0 comments on commit e30e435

Please sign in to comment.