Skip to content

Commit

Permalink
feat: Configurable Route Paths (#436)
Browse files Browse the repository at this point in the history
* Update composer.json

* Update auth0.php

* Update rector.php

* Update Configuration.php

* Update CallbackControllerAbstract.php

* Update LoginControllerAbstract.php

* Update LogoutControllerAbstract.php

* Update CallbackControllerAbstract.php

* Update rector.php

* Update Configuration.php

* Updates to router configuration

* Router configuration updates

* Update router configuration

* Update auth0.php
  • Loading branch information
evansims authored Dec 9, 2023
1 parent 4482d34 commit ac12090
Show file tree
Hide file tree
Showing 9 changed files with 229 additions and 101 deletions.
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');
});

0 comments on commit ac12090

Please sign in to comment.