Skip to content

Commit

Permalink
chore: upgraded package for laravel 8
Browse files Browse the repository at this point in the history
  • Loading branch information
radu-c-tag committed Aug 8, 2024
1 parent c897b65 commit 4253511
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 17 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
31 changes: 31 additions & 0 deletions src/Passport/AuthCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand All @@ -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;
}
}
4 changes: 2 additions & 2 deletions src/Passport/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions src/Passport/PersonalAccessClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
24 changes: 15 additions & 9 deletions src/Passport/PersonalAccessTokenFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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),
]);
Expand All @@ -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
Expand Down
17 changes: 17 additions & 0 deletions src/Passport/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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;
}
}
59 changes: 56 additions & 3 deletions src/Passport/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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());
}

/**
Expand All @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 4253511

Please sign in to comment.