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

feat: Configurable Route Paths #436

Merged
merged 17 commits into from
Dec 9, 2023
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
9 changes: 9 additions & 0 deletions config/auth0.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,13 @@
Configuration::CONFIG_TRANSIENT_STORAGE_ID => Configuration::get(Configuration::CONFIG_TRANSIENT_STORAGE_ID),
],
],

'routes' => [
Configuration::CONFIG_ROUTE_INDEX => Configuration::get(Configuration::CONFIG_ROUTE_INDEX, '/'),
Configuration::CONFIG_ROUTE_CALLBACK => Configuration::get(Configuration::CONFIG_ROUTE_CALLBACK, '/callback'),
Configuration::CONFIG_ROUTE_LOGIN => Configuration::get(Configuration::CONFIG_ROUTE_LOGIN, '/login'),
Configuration::CONFIG_ROUTE_AFTER_LOGIN => Configuration::get(Configuration::CONFIG_ROUTE_AFTER_LOGIN, '/'),
Configuration::CONFIG_ROUTE_LOGOUT => Configuration::get(Configuration::CONFIG_ROUTE_LOGOUT, '/logout'),
Configuration::CONFIG_ROUTE_AFTER_LOGOUT => Configuration::get(Configuration::CONFIG_ROUTE_AFTER_LOGOUT, '/'),
],
];
210 changes: 122 additions & 88 deletions rector.php

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,16 @@ final class Configuration implements ConfigurationContract
*/
public const CONFIG_MANAGEMENT_TOKEN_CACHE = 'managementTokenCache';

/**
* @var string
*/
public const CONFIG_NAMESPACE = 'auth0.';

/**
* @var string
*/
public const CONFIG_NAMESPACE_ROUTES = 'auth0.routes.';

/**
* @var string
*/
Expand All @@ -173,6 +183,41 @@ final class Configuration implements ConfigurationContract
*/
public const CONFIG_RESPONSE_TYPE = 'responseType';

/**
* @var string
*/
public const CONFIG_ROUTE_AFTER_LOGIN = 'afterLogin';

/**
* @var string
*/
public const CONFIG_ROUTE_AFTER_LOGOUT = 'afterLogout';

/**
* @var string
*/
public const CONFIG_ROUTE_BACKCHANNEL = 'backchannel';

/**
* @var string
*/
public const CONFIG_ROUTE_CALLBACK = 'callback';

/**
* @var string
*/
public const CONFIG_ROUTE_INDEX = 'index';

/**
* @var string
*/
public const CONFIG_ROUTE_LOGIN = 'login';

/**
* @var string
*/
public const CONFIG_ROUTE_LOGOUT = 'logout';

/**
* @var string
*/
Expand Down Expand Up @@ -435,6 +480,17 @@ public static function getPath(): string
return self::$path;
}

public static function string(string $key, ?string $default = null): ?string
{
$value = config($key, $default);

if (is_string($value)) {
return $value;
}

return null;
}

public static function stringOrIntToIntOrNull(
int | string $value,
int | null $default = null,
Expand Down
6 changes: 3 additions & 3 deletions src/Controllers/CallbackControllerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use Auth0\Laravel\Auth\Guard;
use Auth0\Laravel\Entities\CredentialEntityContract;
use Auth0\Laravel\Events;
use Auth0\Laravel\Events\{AuthenticationFailed, AuthenticationSucceeded};
use Auth0\Laravel\Exceptions\ControllerException;
use Auth0\Laravel\Exceptions\Controllers\CallbackControllerException;
use Auth0\Laravel\Guards\GuardAbstract;
use Auth0\Laravel\{Configuration, Events};
use Illuminate\Auth\Events\{Attempting, Authenticated, Failed, Validated};
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
Expand Down Expand Up @@ -115,7 +115,7 @@ public function __invoke(
}

if (! $success) {
return redirect()->intended('/login');
return redirect()->intended(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGIN, '/login'));
}

$credential = ($guard instanceof Guard) ? $guard->find(Guard::SOURCE_SESSION) : $guard->find();
Expand All @@ -140,6 +140,6 @@ public function __invoke(
}
}

return redirect()->intended('/');
return redirect()->intended(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_AFTER_LOGIN, config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_INDEX, '/')));
}
}
12 changes: 10 additions & 2 deletions src/Controllers/LoginControllerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Auth0\Laravel\Auth\Guard;
use Auth0\Laravel\Entities\CredentialEntityContract;
use Auth0\Laravel\Events;
use Auth0\Laravel\Events\LoginAttempting;
use Auth0\Laravel\Exceptions\ControllerException;
use Auth0\Laravel\Guards\GuardAbstract;
use Auth0\Laravel\{Configuration, Events};
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -40,7 +40,15 @@ public function __invoke(
$loggedIn ??= (($guard instanceof Guard) ? $guard->find(Guard::SOURCE_SESSION) : $guard->find()) instanceof CredentialEntityContract;

if ($loggedIn) {
return redirect()->intended('/');
return redirect()->intended(
config(
Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_AFTER_LOGIN,
config(
Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_INDEX,
'/',
),
),
);
}

session()->regenerate(true);
Expand Down
9 changes: 7 additions & 2 deletions src/Controllers/LogoutControllerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Auth0\Laravel\Controllers;

use Auth0\Laravel\Auth\Guard;
use Auth0\Laravel\Configuration;
use Auth0\Laravel\Entities\CredentialEntityContract;
use Auth0\Laravel\Exceptions\ControllerException;
use Auth0\Laravel\Guards\GuardAbstract;
Expand Down Expand Up @@ -37,16 +38,20 @@ public function __invoke(
$loggedIn = $guard->check() ? true : null;
$loggedIn ??= (($guard instanceof Guard) ? $guard->find(Guard::SOURCE_SESSION) : $guard->find()) instanceof CredentialEntityContract;

$landing = Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_AFTER_LOGOUT);
$landing ??= Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_INDEX);
$landing ??= '/';

if ($loggedIn) {
session()->invalidate();

$guard->logout(); /** @phpstan-ignore-line */
$route = (string) url('/'); /** @phpstan-ignore-line */
$route = (string) url($landing); /** @phpstan-ignore-line */
$url = $guard->sdk()->authentication()->getLogoutLink($route);

return redirect()->away($url);
}

return redirect()->intended('/');
return redirect()->intended($landing);
}
}
6 changes: 3 additions & 3 deletions src/ServiceAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ final public static function routes(
string $authenticationGuard = 'auth0-session',
): void {
Route::group(['middleware' => ['web', 'guard:' . $authenticationGuard]], static function (): void {
Route::get('/login', LoginController::class)->name('login');
Route::get('/logout', LogoutController::class)->name('logout');
Route::get('/callback', CallbackController::class)->name('callback');
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGIN) ?? '/login', LoginController::class)->name('login');
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGOUT) ?? '/logout', LogoutController::class)->name('logout');
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_CALLBACK) ?? '/callback', CallbackController::class)->name('callback');
});
}
}
6 changes: 3 additions & 3 deletions src/ServiceProviderAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ final public function registerRoutes(): void
{
if (true === config('auth0.registerAuthenticationRoutes')) {
Route::group(['middleware' => 'web'], static function (): void {
Route::get('/login', LoginController::class)->name('login');
Route::get('/logout', LogoutController::class)->name('logout');
Route::get('/callback', CallbackController::class)->name('callback');
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGIN) ?? '/login', LoginController::class)->name('login');
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGOUT) ?? '/logout', LogoutController::class)->name('logout');
Route::get(Configuration::string(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_CALLBACK) ?? '/callback', CallbackController::class)->name('callback');
});
}
}
Expand Down
16 changes: 16 additions & 0 deletions tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,19 @@
->toBeInt()
->toEqual(123);
});

test('string() behaves as expected', function (): void {
config(['test2' => [
'testInteger' => 123,
'testString' => '123',
]]);

define('AUTH0_OVERRIDE_CONFIGURATION_STRING_METHOD', 'test2');

expect(Configuration::string('test2.testInteger'))
->toBeNull();

expect(Configuration::string('test2.testString'))
->toBeString()
->toEqual('123');
});
Loading