Skip to content

[13.x] Cleanup #1758

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

Merged
merged 3 commits into from
Jun 24, 2024
Merged
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
2 changes: 2 additions & 0 deletions src/AuthCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ class AuthCode extends Model
/**
* Get the client that owns the authentication code.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function client()
Expand Down
2 changes: 1 addition & 1 deletion src/Bridge/PersonalAccessGrant.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function respondToAccessTokenRequest(
): ResponseTypeInterface {
// Validate request
$client = $this->validateClient($request);
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request));
$scopes = $this->validateScopes($this->getRequestParameter('scope', $request, $this->defaultScope));
$userIdentifier = $this->getRequestParameter('user_id', $request);

// Finalize the requested scopes
Expand Down
2 changes: 2 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public function user()
/**
* Get all of the authentication codes for the client.
*
* @deprecated Will be removed in a future Laravel version.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function authCodes()
Expand Down
5 changes: 1 addition & 4 deletions src/Console/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,8 @@ public function handle()
if ($this->confirm('Would you like to run all pending database migrations?', true)) {
$this->call('migrate');

if ($this->confirm('Would you like to create the "personal access" and "password grant" clients?', true)) {
$provider = in_array('users', array_keys(config('auth.providers'))) ? 'users' : null;

if ($this->confirm('Would you like to create the "personal access" grant client?', true)) {
$this->call('passport:client', ['--personal' => true, '--name' => config('app.name').' Personal Access Client']);
$this->call('passport:client', ['--password' => true, '--name' => config('app.name').' Password Grant Client', '--provider' => $provider]);
}
}
}
Expand Down
24 changes: 7 additions & 17 deletions src/Guards/TokenGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Laravel\Passport\ClientRepository;
use Laravel\Passport\Passport;
use Laravel\Passport\PassportUserProvider;
use Laravel\Passport\TokenRepository;
use Laravel\Passport\TransientToken;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\ResourceServer;
Expand All @@ -44,13 +43,6 @@ class TokenGuard implements Guard
*/
protected $provider;

/**
* The token repository instance.
*
* @var \Laravel\Passport\TokenRepository
*/
protected $tokens;

/**
* The client repository instance.
*
Expand Down Expand Up @@ -84,7 +76,6 @@ class TokenGuard implements Guard
*
* @param \League\OAuth2\Server\ResourceServer $server
* @param \Laravel\Passport\PassportUserProvider $provider
* @param \Laravel\Passport\TokenRepository $tokens
* @param \Laravel\Passport\ClientRepository $clients
* @param \Illuminate\Contracts\Encryption\Encrypter $encrypter
* @param \Illuminate\Http\Request $request
Expand All @@ -93,13 +84,11 @@ class TokenGuard implements Guard
public function __construct(
ResourceServer $server,
PassportUserProvider $provider,
TokenRepository $tokens,
ClientRepository $clients,
Encrypter $encrypter,
Request $request
) {
$this->server = $server;
$this->tokens = $tokens;
$this->clients = $clients;
$this->provider = $provider;
$this->encrypter = $encrypter;
Expand All @@ -109,7 +98,7 @@ public function __construct(
/**
* Get the user for the incoming request.
*
* @return mixed
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
public function user()
{
Expand All @@ -135,7 +124,6 @@ public function validate(array $credentials = [])
return ! is_null((new static(
$this->server,
$this->provider,
$this->tokens,
$this->clients,
$this->encrypter,
$credentials['request'],
Expand Down Expand Up @@ -172,7 +160,7 @@ public function client()
* Authenticate the incoming request via the Bearer token.
*
* @param \Illuminate\Http\Request $request
* @return mixed
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
protected function authenticateViaBearerToken($request)
{
Expand All @@ -190,6 +178,8 @@ protected function authenticateViaBearerToken($request)
return;
}

$this->setClient($client);

// If the access token is valid we will retrieve the user according to the user ID
// associated with the token. We will use the provider implementation which may
// be used to retrieve users from Eloquent. Next, we'll be ready to continue.
Expand All @@ -206,7 +196,7 @@ protected function authenticateViaBearerToken($request)
// authorization such as within the developer's Laravel model policy classes.
$token = AccessToken::fromPsrRequest($psr);

return $token ? $user->withAccessToken($token) : null;
return $user->withAccessToken($token);
}

/**
Expand Down Expand Up @@ -242,7 +232,7 @@ protected function getPsrRequestViaBearerToken($request)
* Authenticate the incoming request via the token cookie.
*
* @param \Illuminate\Http\Request $request
* @return mixed
* @return \Illuminate\Contracts\Auth\Authenticatable|null
*/
protected function authenticateViaCookie($request)
{
Expand All @@ -262,7 +252,7 @@ protected function authenticateViaCookie($request)
* Get the token cookie via the incoming request.
*
* @param \Illuminate\Http\Request $request
* @return mixed
* @return array|null
*/
protected function getTokenViaCookie($request)
{
Expand Down
2 changes: 1 addition & 1 deletion src/HasApiTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function token()
*/
public function tokenCan($scope)
{
return $this->accessToken ? $this->accessToken->can($scope) : false;
return $this->accessToken && $this->accessToken->can($scope);
}

/**
Expand Down
12 changes: 1 addition & 11 deletions src/Http/Middleware/CheckCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Closure;
use Laravel\Passport\AccessToken;
use Laravel\Passport\Exceptions\AuthenticationException;
use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\ResourceServer;
use Nyholm\Psr7\Factory\Psr17Factory;
Expand All @@ -20,24 +19,15 @@ abstract class CheckCredentials
*/
protected $server;

/**
* Token Repository.
*
* @var \Laravel\Passport\TokenRepository
*/
protected $repository;

/**
* Create a new middleware instance.
*
* @param \League\OAuth2\Server\ResourceServer $server
* @param \Laravel\Passport\TokenRepository $repository
* @return void
*/
public function __construct(ResourceServer $server, TokenRepository $repository)
public function __construct(ResourceServer $server)
{
$this->server = $server;
$this->repository = $repository;
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/PassportServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,6 @@ protected function makeGuard(array $config)
return new TokenGuard(
$this->app->make(ResourceServer::class),
new PassportUserProvider(Auth::createUserProvider($config['provider']), $config['provider']),
$this->app->make(TokenRepository::class),
$this->app->make(ClientRepository::class),
$this->app->make('encrypter'),
$this->app->make('request')
Expand Down
10 changes: 0 additions & 10 deletions src/RefreshToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ public function revoke()
return $this->forceFill(['revoked' => true])->save();
}

/**
* Determine if the token is a transient JWT token.
*
* @return bool
*/
public function transient()
{
return false;
}

/**
* Get the current connection name for the model.
*
Expand Down
2 changes: 2 additions & 0 deletions src/TokenRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public function forUser($userId)
/**
* Get a valid token instance for the given user and client.
*
* @deprecated use findValidToken
*
* @param \Illuminate\Contracts\Auth\Authenticatable $user
* @param \Laravel\Passport\Client $client
* @return \Laravel\Passport\Token|null
Expand Down
16 changes: 4 additions & 12 deletions tests/Unit/CheckClientCredentialsForAnyScopeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Http\Request;
use Laravel\Passport\Exceptions\AuthenticationException;
use Laravel\Passport\Http\Middleware\CheckClientCredentialsForAnyScope;
use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\ResourceServer;
use Mockery as m;
Expand All @@ -30,9 +29,7 @@ public function test_request_is_passed_along_if_token_is_valid()
'oauth_scopes' => ['*'],
]);

$tokenRepository = m::mock(TokenRepository::class);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
$middleware = new CheckClientCredentialsForAnyScope($resourceServer);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -55,9 +52,7 @@ public function test_request_is_passed_along_if_token_has_any_required_scope()
'oauth_scopes' => ['foo', 'bar', 'baz'],
]);

$tokenRepository = m::mock(TokenRepository::class);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
$middleware = new CheckClientCredentialsForAnyScope($resourceServer);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -73,13 +68,12 @@ public function test_exception_is_thrown_when_oauth_throws_exception()
{
$this->expectException(AuthenticationException::class);

$tokenRepository = m::mock(TokenRepository::class);
$resourceServer = m::mock(ResourceServer::class);
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andThrow(
new OAuthServerException('message', 500, 'error type')
);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
$middleware = new CheckClientCredentialsForAnyScope($resourceServer);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -102,9 +96,7 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scope()
'oauth_scopes' => ['foo', 'bar'],
]);

$tokenRepository = m::mock(TokenRepository::class);

$middleware = new CheckClientCredentialsForAnyScope($resourceServer, $tokenRepository);
$middleware = new CheckClientCredentialsForAnyScope($resourceServer);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand Down
16 changes: 4 additions & 12 deletions tests/Unit/CheckClientCredentialsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Http\Request;
use Laravel\Passport\Exceptions\AuthenticationException;
use Laravel\Passport\Http\Middleware\CheckClientCredentials;
use Laravel\Passport\TokenRepository;
use League\OAuth2\Server\Exception\OAuthServerException;
use League\OAuth2\Server\ResourceServer;
use Mockery as m;
Expand All @@ -30,9 +29,7 @@ public function test_request_is_passed_along_if_token_is_valid()
'oauth_scopes' => ['*'],
]);

$tokenRepository = m::mock(TokenRepository::class);

$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
$middleware = new CheckClientCredentials($resourceServer);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -55,9 +52,7 @@ public function test_request_is_passed_along_if_token_and_scope_are_valid()
'oauth_scopes' => ['see-profile'],
]);

$tokenRepository = m::mock(TokenRepository::class);

$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
$middleware = new CheckClientCredentials($resourceServer);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -73,13 +68,12 @@ public function test_exception_is_thrown_when_oauth_throws_exception()
{
$this->expectException(AuthenticationException::class);

$tokenRepository = m::mock(TokenRepository::class);
$resourceServer = m::mock(ResourceServer::class);
$resourceServer->shouldReceive('validateAuthenticatedRequest')->andThrow(
new OAuthServerException('message', 500, 'error type')
);

$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
$middleware = new CheckClientCredentials($resourceServer);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand All @@ -102,9 +96,7 @@ public function test_exception_is_thrown_if_token_does_not_have_required_scopes(
'oauth_scopes' => ['foo', 'notbar'],
]);

$tokenRepository = m::mock(TokenRepository::class);

$middleware = new CheckClientCredentials($resourceServer, $tokenRepository);
$middleware = new CheckClientCredentials($resourceServer);

$request = Request::create('/');
$request->headers->set('Authorization', 'Bearer token');
Expand Down
Loading