diff --git a/composer.json b/composer.json index fb36078..824c3c7 100644 --- a/composer.json +++ b/composer.json @@ -14,9 +14,9 @@ ], "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": "^8.0", + "mongodb/laravel-mongodb": "^3.8", + "laravel/passport": "^10.0" }, "autoload": { "psr-4": { diff --git a/src/Passport/AuthCode.php b/src/Passport/AuthCode.php index c920a3d..3f67188 100644 --- a/src/Passport/AuthCode.php +++ b/src/Passport/AuthCode.php @@ -13,6 +13,13 @@ class AuthCode extends Model */ protected $table = 'oauth_auth_codes'; + /** + * Indicates if the IDs are auto-incrementing. + * + * @var bool + */ + public $incrementing = false; + /** * The guarded attributes on the model. * @@ -38,6 +45,20 @@ class AuthCode extends Model 'expires_at', ]; + /** + * Indicates if the model should be timestamped. + * + * @var bool + */ + public $timestamps = false; + + /** + * The "type" of the primary key ID. + * + * @var string + */ + protected $keyType = 'string'; + /** * Get the client that owns the authentication code. * @@ -47,4 +68,14 @@ public function client() { return $this->hasMany(Client::class); } + + /** + * Get the current connection name for the model. + * + * @return string|null + */ + public function getConnectionName() + { + return config('passport.storage.database.connection') ?? $this->connection; + } } diff --git a/src/Passport/Client.php b/src/Passport/Client.php index 3be09de..fc5b19a 100644 --- a/src/Passport/Client.php +++ b/src/Passport/Client.php @@ -2,9 +2,9 @@ namespace DesignMyNight\Mongodb\Passport; -use Jenssegers\Mongodb\Eloquent\Model; +use Laravel\Passport\Client as BaseClient; -class Client extends Model +class Client extends BaseClient { /** * The database table used by the model. diff --git a/src/Passport/PersonalAccessClient.php b/src/Passport/PersonalAccessClient.php index 904f5b3..d2783ef 100644 --- a/src/Passport/PersonalAccessClient.php +++ b/src/Passport/PersonalAccessClient.php @@ -29,4 +29,14 @@ public function client() { return $this->belongsTo(Client::class); } + + /** + * Get the current connection name for the model. + * + * @return string|null + */ + public function getConnectionName() + { + return config('passport.storage.database.connection') ?? $this->connection; + } } diff --git a/src/Passport/PersonalAccessTokenFactory.php b/src/Passport/PersonalAccessTokenFactory.php index 739e07f..3412623 100644 --- a/src/Passport/PersonalAccessTokenFactory.php +++ b/src/Passport/PersonalAccessTokenFactory.php @@ -2,13 +2,15 @@ namespace DesignMyNight\Mongodb\Passport; -use Zend\Diactoros\Response; -use Zend\Diactoros\ServerRequest; -use Lcobucci\JWT\Parser as JwtParser; -use League\OAuth2\Server\AuthorizationServer; -use \Laravel\Passport\ClientRepository; +use Laravel\Passport\ClientRepository; use Laravel\Passport\PersonalAccessTokenResult; use Laravel\Passport\TokenRepository; +use Laravel\Passport\Passport; +use Lcobucci\JWT\Parser as JwtParser; +use League\OAuth2\Server\AuthorizationServer; +use Nyholm\Psr7\Response; +use Nyholm\Psr7\ServerRequest; +use Psr\Http\Message\ServerRequestInterface; class PersonalAccessTokenFactory { @@ -37,6 +39,8 @@ class PersonalAccessTokenFactory * The JWT token parser instance. * * @var \Lcobucci\JWT\Parser + * + * @deprecated This property will be removed in a future Passport version. */ protected $jwt; @@ -92,14 +96,16 @@ public function make($userId, $name, array $scopes = []) * @param \Laravel\Passport\Client $client * @param mixed $userId * @param array $scopes - * @return \Zend\Diactoros\ServerRequest + * @return \Psr\Http\Message\ServerRequestInterface */ protected function createRequest($client, $userId, array $scopes) { + $secret = Passport::$hashesClientSecrets ? $this->clients->getPersonalAccessClientSecret() : $client->secret; + return (new ServerRequest)->withParsedBody([ 'grant_type' => 'personal_access', 'client_id' => $client->id, - 'client_secret' => $client->secret, + 'client_secret' => $secret, 'user_id' => $userId, 'scope' => implode(' ', $scopes), ]); @@ -108,10 +114,10 @@ protected function createRequest($client, $userId, array $scopes) /** * Dispatch the given request to the authorization server. * - * @param \Zend\Diactoros\ServerRequest $request + * @param Psr\Http\Message\ServerRequestInterface $request * @return array */ - protected function dispatchRequestToAuthorizationServer(ServerRequest $request) + protected function dispatchRequestToAuthorizationServer(ServerRequestInterface $request) { return json_decode($this->server->respondToAccessTokenRequest( $request, new Response diff --git a/src/Passport/RefreshToken.php b/src/Passport/RefreshToken.php index f696717..82b8c80 100644 --- a/src/Passport/RefreshToken.php +++ b/src/Passport/RefreshToken.php @@ -13,6 +13,13 @@ class RefreshToken extends Model */ protected $table = 'oauth_refresh_tokens'; + /** + * The "type" of the primary key ID. + * + * @var string + */ + protected $keyType = 'string'; + /** * Indicates if the IDs are auto-incrementing. * @@ -81,4 +88,14 @@ public function transient() { return false; } + + /** + * Get the current connection name for the model. + * + * @return string|null + */ + public function getConnectionName() + { + return config('passport.storage.database.connection') ?? $this->connection; + } } diff --git a/src/Passport/Token.php b/src/Passport/Token.php index 52b4abf..3f9329c 100644 --- a/src/Passport/Token.php +++ b/src/Passport/Token.php @@ -20,6 +20,13 @@ class Token extends Model */ protected $table = 'oauth_access_tokens'; + /** + * The "type" of the primary key ID. + * + * @var string + */ + protected $keyType = 'string'; + /** * Indicates if the IDs are auto-incrementing. * @@ -71,7 +78,9 @@ public function user() { $provider = config('auth.guards.api.provider'); - return $this->belongsTo(config('auth.providers.' . $provider . '.model')); + $model = config('auth.providers.'.$provider.'.model'); + + return $this->belongsTo($model, 'user_id', (new $model)->getKeyName()); } /** @@ -83,8 +92,42 @@ public function user() */ public function can($scope) { - return in_array('*', $this->scopes) || - array_key_exists($scope, array_flip($this->scopes)); + if (in_array('*', $this->scopes)) { + return true; + } + + $scopes = Passport::$withInheritedScopes + ? $this->resolveInheritedScopes($scope) + : [$scope]; + + foreach ($scopes as $scope) { + if (array_key_exists($scope, array_flip($this->scopes))) { + return true; + } + } + + return false; + } + + /** + * Resolve all possible scopes. + * + * @param string $scope + * @return array + */ + protected function resolveInheritedScopes($scope) + { + $parts = explode(':', $scope); + + $partsCount = count($parts); + + $scopes = []; + + for ($i = 1; $i <= $partsCount; $i++) { + $scopes[] = implode(':', array_slice($parts, 0, $i)); + } + + return $scopes; } /** @@ -117,4 +160,14 @@ public function transient() { return false; } + + /** + * Get the current connection name for the model. + * + * @return string|null + */ + public function getConnectionName() + { + return config('passport.storage.database.connection') ?? $this->connection; + } }