Skip to content
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

[Feature] Allow configuration of login redirect route via config #1708

Closed
wants to merge 1 commit into from
Closed
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
Allow configuration of login route via config
robertmarney committed Jan 16, 2024

Verified

This commit was signed with the committer’s verified signature.
nstarman Nathaniel Starkman
commit 3566b17befc0c14632fb16509a1c1bfe611d93ac
12 changes: 12 additions & 0 deletions config/passport.php
Original file line number Diff line number Diff line change
@@ -15,6 +15,18 @@

'guard' => 'web',

/*
|--------------------------------------------------------------------------
| Login Route
|--------------------------------------------------------------------------
|
| Here you may specify which route Passport will redirect Unauthenticated
| users to.
|
*/

'login_route' => route('login'),

/*
|--------------------------------------------------------------------------
| Encryption Keys
4 changes: 4 additions & 0 deletions src/Exceptions/AuthenticationException.php
Original file line number Diff line number Diff line change
@@ -6,4 +6,8 @@

class AuthenticationException extends Exception
{
static function make(): self
{
return new self(redirectTo: config('passport.login_route', route('login')));
}
}
2 changes: 1 addition & 1 deletion src/Http/Controllers/AuthorizationController.php
Original file line number Diff line number Diff line change
@@ -215,6 +215,6 @@ protected function promptForLogin($request)
{
$request->session()->put('promptedForLogin', true);

throw new AuthenticationException;
throw AuthenticationException::make();
}
}
2 changes: 1 addition & 1 deletion src/Http/Middleware/CheckClientCredentials.php
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@ class CheckClientCredentials extends CheckCredentials
protected function validateCredentials($token)
{
if (! $token) {
throw new AuthenticationException;
throw AuthenticationException::make();
}
}

3 changes: 2 additions & 1 deletion src/Http/Middleware/CheckClientCredentialsForAnyScope.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

namespace Laravel\Passport\Http\Middleware;

use Illuminate\Support\Facades\Auth;
use Laravel\Passport\Exceptions\AuthenticationException;
use Laravel\Passport\Exceptions\MissingScopeException;

@@ -18,7 +19,7 @@ class CheckClientCredentialsForAnyScope extends CheckCredentials
protected function validateCredentials($token)
{
if (! $token) {
throw new AuthenticationException;
throw AuthenticationException::make();
}
}

2 changes: 1 addition & 1 deletion src/Http/Middleware/CheckCredentials.php
Original file line number Diff line number Diff line change
@@ -76,7 +76,7 @@ public function handle($request, Closure $next, ...$scopes)
try {
$psr = $this->server->validateAuthenticatedRequest($psr);
} catch (OAuthServerException $e) {
throw new AuthenticationException;
throw AuthenticationException::make();
}

$this->validate($psr, $scopes);
2 changes: 1 addition & 1 deletion src/Http/Middleware/CheckForAnyScope.php
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public static function using(...$scopes)
public function handle($request, $next, ...$scopes)
{
if (! $request->user() || ! $request->user()->token()) {
throw new AuthenticationException;
throw AuthenticationException::make();
}

foreach ($scopes as $scope) {
2 changes: 1 addition & 1 deletion src/Http/Middleware/CheckScopes.php
Original file line number Diff line number Diff line change
@@ -35,7 +35,7 @@ public static function using(...$scopes)
public function handle($request, $next, ...$scopes)
{
if (! $request->user() || ! $request->user()->token()) {
throw new AuthenticationException;
throw AuthenticationException::make();
}

foreach ($scopes as $scope) {
62 changes: 62 additions & 0 deletions tests/Unit/AuthorizationControllerTest.php
Original file line number Diff line number Diff line change
@@ -343,6 +343,16 @@ public function test_logout_and_prompt_login_if_request_has_prompt_equals_to_log
$response = m::mock(AuthorizationViewResponse::class);
$guard = m::mock(StatefulGuard::class);

// Mock the URL generator...
$urlGenerator = m::mock(\Illuminate\Contracts\Routing\UrlGenerator::class);
app()->instance('url', $urlGenerator);
$urlGenerator->shouldReceive('route')->with('login', [], true)->andReturn('login');

// Mock the config class...
$config = m::mock('alias:config');
$config->shouldReceive('get')->with('passport.login_route', 'login')->andReturn('login');
app()->instance('config', $config);

$controller = new AuthorizationController($server, $guard, $response);

$guard->shouldReceive('guest')->andReturn(false);
@@ -370,6 +380,16 @@ public function test_user_should_be_authenticated()
{
$this->expectException(AuthenticationException::class);

// Mock the URL generator...
$urlGenerator = m::mock(\Illuminate\Contracts\Routing\UrlGenerator::class);
app()->instance('url', $urlGenerator);
$urlGenerator->shouldReceive('route')->with('login', [], true)->andReturn('login');

// Mock the config class...
$config = m::mock('alias:config');
$config->shouldReceive('get')->with('passport.login_route', 'login')->andReturn('login');
app()->instance('config', $config);

$server = m::mock(AuthorizationServer::class);
$response = m::mock(AuthorizationViewResponse::class);
$guard = m::mock(StatefulGuard::class);
@@ -393,4 +413,46 @@ public function test_user_should_be_authenticated()
m::mock(ServerRequestInterface::class), $request, $clients, $tokens
);
}

public function test_user_redirected_to_configured_login_url_when_unauthenticated()
{
$this->expectException(AuthenticationException::class);

// Mock the URL generator...
$urlGenerator = m::mock(\Illuminate\Contracts\Routing\UrlGenerator::class);
app()->instance('url', $urlGenerator);
$urlGenerator->shouldReceive('route')->with('login', [], true)->andReturn('custom_login');

// Mock the config class
$config = m::mock('alias:config');
$config->shouldReceive('get')->with('passport.login_route', 'custom_login')->andReturn('custom_login_uri');
app()->instance('config', $config);

$server = m::mock(AuthorizationServer::class);
$response = m::mock(AuthorizationViewResponse::class);
$guard = m::mock(StatefulGuard::class);

$controller = new AuthorizationController($server, $guard, $response);

$guard->shouldReceive('guest')->andReturn(true);
$server->shouldReceive('validateAuthorizationRequest')->once();

$request = m::mock(Request::class);
$request->shouldReceive('session')->andReturn($session = m::mock());
$session->shouldReceive('put')->with('promptedForLogin', true)->once();
$request->shouldReceive('get')->with('prompt')->andReturn(null);

$clients = m::mock(ClientRepository::class);
$tokens = m::mock(TokenRepository::class);

try {
$controller->authorize(
m::mock(ServerRequestInterface::class), $request, $clients, $tokens
);
} catch (AuthenticationException $exception) {
$this->assertEquals('custom_login_uri', $exception->redirectTo());

throw $exception;
}
}
}