Skip to content

Feature/add support to passport 11 #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
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
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ Installation
Installation using composer:

```sh
composer require designmynight/laravel-mongodb-passport
composer require sysvale/laravel-mongodb-passport
```

You need to have your `App\User` class extend `DesignMyNight\Mongodb\Auth\User.php` instead of the default `Illuminate\Foundation\Auth\User`. This user class extends larvel-mongodb eloquent user as well as adding all the standard and required authentication and laravel passport traits.
You need to have your `App\User` class extend `Sysvale\Mongodb\Auth\User.php` instead of the default `Illuminate\Foundation\Auth\User`. This user class extends larvel-mongodb eloquent user as well as adding all the standard and required authentication and laravel passport traits.

```php
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use DesignMyNight\Mongodb\Auth\User as Authenticatable;
use Sysvale\Mongodb\Auth\User as Authenticatable;

class User extends Authenticatable
{
Expand All @@ -41,17 +41,17 @@ class User extends Authenticatable
5.5.x | 4.0.x, 5.0.x, 6.0.x, 7.0.x | 1.1.x
5.6.x | 4.0.x, 5.0.x, 6.0.x, 7.0.x | 1.1.x
6.x | 4.0.x, 5.0.x, 6.0.x, 7.x, 8.x| 1.2.x

And add the service provider in `config/app.php`:

```php
DesignMyNight\Mongodb\MongodbPassportServiceProvider::class,
Sysvale\Mongodb\MongodbPassportServiceProvider::class,
```

For usage with [Lumen](http://lumen.laravel.com), add the service provider in `bootstrap/app.php`.

```php
$app->register(DesignMyNight\Mongodb\MongodbPassportServiceProvider::class);
$app->register(Sysvale\Mongodb\MongodbPassportServiceProvider::class);
```

The service provider will overide the default laravel passport models in order to use mongodb's implementation of eloquent. There is no need to register any additional classes or add any additional configuration other than those outlined in [Laravel Passport](https://github.com/laravel/passport) and [MongoDB](https://github.com/jenssegers/laravel-mongodb).
23 changes: 13 additions & 10 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "designmynight/laravel-mongodb-passport",
"name": "sysvale/laravel-mongodb-passport",
"description": "A package to allow laravel/passport use with jenssegers/laravel-mongodb",
"homepage": "https://github.com/designmynight/laravel-mongodb-passport",
"homepage": "https://github.com/Sysvale/laravel-mongodb-passport",
"license": "MIT",
"keywords": [
"laravel",
Expand All @@ -10,30 +10,33 @@
"laravel-passport",
"mongodb",
"passport",
"designmynight"
"sysvale"
],
"require": {
"php": ">=7.1",
"illuminate/support": "^5.5 || ^6.0",
"jenssegers/mongodb": "3.3.* || 3.4.* || 3.5.* || 3.6.*",
"laravel/passport": "6.0.* || 7.0.* || 7.4.* || 7.5.* || ^8.0 || ^9.0"
"illuminate/support": "^5.5 || ^6.0 || ^7.0 || ^8.0 || ^9.0",
"jenssegers/mongodb": "3.3.* || 3.4.* || 3.5.* || 3.6.* || 3.8.* || 3.9.*",
"laravel/passport": "6.0.* || 7.0.* || 7.4.* || 7.5.* || ^8.0 || ^9.0 || ^10 || ^11"
},
"autoload": {
"psr-4": {
"DesignMyNight\\Mongodb\\": "src"
"Sysvale\\Mongodb\\": "src"
}
},
"authors": [
{
"name": "DesignMyNight",
"email": "[email protected]",
"role": "Support"
"email": "[email protected]"
},
{
"name": "Geidson",
"email": "[email protected]"
}
],
"extra": {
"laravel": {
"providers": [
"DesignMyNight\\Mongodb\\MongodbPassportServiceProvider"
"Sysvale\\Mongodb\\MongodbPassportServiceProvider"
]
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Auth/User.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DesignMyNight\Mongodb\Auth;
namespace Sysvale\Mongodb\Auth;

use Illuminate\Auth\Authenticatable;
use Illuminate\Auth\Passwords\CanResetPassword;
Expand Down
118 changes: 118 additions & 0 deletions src/Console/ClientCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Sysvale\Mongodb\Console;

use Sysvale\Mongodb\Passport\Client;
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Console\ClientCommand as PassportClientCommand;

class ClientCommand extends PassportClientCommand
{
/**
* Create a new personal access client.
*
* @param \Laravel\Passport\ClientRepository $clients
* @return void
*/
protected function createPersonalClient(ClientRepository $clients)
{
$name = $this->option('name') ?: $this->ask(
'What should we name the personal access client?',
config('app.name').' Personal Access Client'
);

$client = $clients->createPersonalAccessClient(
null, $name, 'http://localhost'
);

$this->info('Personal access client created successfully.');

$this->patchedOutputClientDetails($client);
}

/**
* Create a new password grant client.
*
* @param \Laravel\Passport\ClientRepository $clients
* @return void
*/
protected function createPasswordClient(ClientRepository $clients)
{
$name = $this->option('name') ?: $this->ask(
'What should we name the password grant client?',
config('app.name').' Password Grant Client'
);

$client = $clients->createPasswordGrantClient(
null, $name, 'http://localhost'
);

$this->info('Password grant client created successfully.');

$this->patchedOutputClientDetails($client);
}

/**
* Create a client credentials grant client.
*
* @param \Laravel\Passport\ClientRepository $clients
* @return void
*/
protected function createClientCredentialsClient(ClientRepository $clients)
{
$name = $this->option('name') ?: $this->ask(
'What should we name the client?',
config('app.name').' ClientCredentials Grant Client'
);

$client = $clients->create(
null, $name, ''
);

$this->info('New client created successfully.');

$this->patchedOutputClientDetails($client);
}

/**
* Create a authorization code client.
*
* @param \Laravel\Passport\ClientRepository $clients
* @return void
*/
protected function createAuthCodeClient(ClientRepository $clients)
{
$userId = $this->option('user_id') ?: $this->ask(
'Which user ID should the client be assigned to?'
);

$name = $this->option('name') ?: $this->ask(
'What should we name the client?'
);

$redirect = $this->option('redirect_uri') ?: $this->ask(
'Where should we redirect the request after authorization?',
url('/auth/callback')
);

$client = $clients->create(
$userId, $name, $redirect, false, false, ! $this->option('public')
);

$this->info('New client created successfully.');

$this->patchedOutputClientDetails($client);
}

/**
* Output the client's ID and secret key.
*
* @param \Laravel\Passport\Client $client
* @return void
*/
protected function patchedOutputClientDetails(Client $client)
{
$this->line('<comment>Client ID:</comment> '.$client->id);
$this->line('<comment>Client secret:</comment> '.$client->secret);
}
}
37 changes: 37 additions & 0 deletions src/Console/PurgeCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Sysvale\Mongodb\Console;

use Illuminate\Support\Carbon;
use Laravel\Passport\Console\PurgeCommand as PassportPurgeCommand;
use Laravel\Passport\Passport;

class PurgeCommand extends PassportPurgeCommand
{
public function handle()
{
$expired = Carbon::now()->subDays(7);

if (($this->option('revoked') && $this->option('expired')) ||
(!$this->option('revoked') && !$this->option('expired'))
) {
Passport::token()->where('revoked', true)->orWhere('expires_at', '<', $expired)->delete();
Passport::authCode()->where('revoked', true)->orWhere('expires_at', '<', $expired)->delete();
Passport::refreshToken()->where('revoked', true)->orWhere('expires_at', '<', $expired)->delete();

$this->info('Purged revoked items and items expired for more than seven days.');
} elseif ($this->option('revoked')) {
Passport::token()->where('revoked', true)->delete();
Passport::authCode()->where('revoked', true)->delete();
Passport::refreshToken()->where('revoked', true)->delete();

$this->info('Purged revoked items.');
} elseif ($this->option('expired')) {
Passport::token()->where('expires_at', '<', $expired)->delete();
Passport::authCode()->where('expires_at', '<', $expired)->delete();
Passport::refreshToken()->where('expires_at', '<', $expired)->delete();

$this->info('Purged items expired for more than seven days.');
}
}
}
37 changes: 28 additions & 9 deletions src/MongodbPassportServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,31 +1,50 @@
<?php

namespace DesignMyNight\Mongodb;
namespace Sysvale\Mongodb;

use Illuminate\Support\ServiceProvider;
use DesignMyNight\Mongodb\Passport\AuthCode;
use DesignMyNight\Mongodb\Passport\Bridge\RefreshTokenRepository;
use DesignMyNight\Mongodb\Passport\Client;
use DesignMyNight\Mongodb\Passport\PersonalAccessClient;
use DesignMyNight\Mongodb\Passport\RefreshToken;
use DesignMyNight\Mongodb\Passport\Token;
use Sysvale\Mongodb\Passport\AuthCode;
use Sysvale\Mongodb\Console\ClientCommand;
use Sysvale\Mongodb\Passport\Bridge\RefreshTokenRepository;
use Sysvale\Mongodb\Passport\Client;
use Sysvale\Mongodb\Passport\PersonalAccessClient;
use Sysvale\Mongodb\Passport\Token;
use Laravel\Passport\Bridge\RefreshTokenRepository as PassportRefreshTokenRepository;
use Laravel\Passport\Console\ClientCommand as PassportClientCommand;
use Laravel\Passport\Console\PurgeCommand as PassportPurgeCommand;
use Laravel\Passport\Passport;
use Laravel\Passport\TokenRepository as PassportTokenRepository;
use Sysvale\Mongodb\Console\PurgeCommand;
use Sysvale\Mongodb\Passport\RefreshToken;
use Sysvale\Mongodb\Passport\TokenRepository;

class MongodbPassportServiceProvider extends ServiceProvider
{
/**
* @return void
*/
* @return void
*/
public function register()
{
Passport::useAuthCodeModel(AuthCode::class);
Passport::useClientModel(Client::class);
Passport::usePersonalAccessClientModel(PersonalAccessClient::class);
Passport::useTokenModel(Token::class);
Passport::useRefreshTokenModel(RefreshToken::class);

$this->app->bind(PassportRefreshTokenRepository::class, function () {
return $this->app->make(RefreshTokenRepository::class);
});

$this->app->extend(PassportClientCommand::class, function () {
return new ClientCommand();
});

$this->app->extend(PassportPurgeCommand::class, function () {
return new PurgeCommand();
});

$this->app->bind(PassportTokenRepository::class, function () {
return $this->app->make(TokenRepository::class);
});
}
}
2 changes: 1 addition & 1 deletion src/Passport/AuthCode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DesignMyNight\Mongodb\Passport;
namespace Sysvale\Mongodb\Passport;

use Jenssegers\Mongodb\Eloquent\Model;

Expand Down
4 changes: 2 additions & 2 deletions src/Passport/Bridge/RefreshToken.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DesignMyNight\Mongodb\Passport\Bridge;
namespace Sysvale\Mongodb\Passport\Bridge;

use Jenssegers\Mongodb\Eloquent\Model;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
Expand All @@ -9,7 +9,7 @@

/**
* Class RefreshToken
* @package DesignMyNight\Mongodb\Passport\Bridge
* @package Sysvale\Mongodb\Passport\Bridge
*/
class RefreshToken extends Model implements RefreshTokenEntityInterface
{
Expand Down
5 changes: 3 additions & 2 deletions src/Passport/Bridge/RefreshTokenRepository.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace DesignMyNight\Mongodb\Passport\Bridge;
namespace Sysvale\Mongodb\Passport\Bridge;

use Laravel\Passport\Bridge\RefreshTokenRepository as BaseRefreshTokenRepository;
use Laravel\Passport\Events\RefreshTokenCreated;
use Laravel\Passport\Passport;
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;

/**
Expand All @@ -17,7 +18,7 @@ class RefreshTokenRepository extends BaseRefreshTokenRepository
*/
public function getNewRefreshToken()
{
return new RefreshToken();
return Passport::refreshToken();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Passport/Client.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DesignMyNight\Mongodb\Passport;
namespace Sysvale\Mongodb\Passport;

use Jenssegers\Mongodb\Eloquent\Model;

Expand Down Expand Up @@ -79,7 +79,7 @@ public function skipsAuthorization()
{
return false;
}

/**
* Determine if the client is a confidential client.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Passport/PersonalAccessClient.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DesignMyNight\Mongodb\Passport;
namespace Sysvale\Mongodb\Passport;

use Jenssegers\Mongodb\Eloquent\Model;

Expand Down
2 changes: 1 addition & 1 deletion src/Passport/PersonalAccessTokenFactory.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DesignMyNight\Mongodb\Passport;
namespace Sysvale\Mongodb\Passport;

use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
Expand Down
Loading