Skip to content

Commit

Permalink
Update router configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
evansims committed Dec 8, 2023
1 parent 74d1664 commit f21e9d6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 26 deletions.
31 changes: 21 additions & 10 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,6 @@ final class Configuration implements ConfigurationContract
self::CONFIG_BACKCHANNEL_LOGOUT_EXPIRES,
];

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

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

/**
* @var string
*/
Expand Down Expand Up @@ -158,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 Down Expand Up @@ -480,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
3 changes: 1 addition & 2 deletions src/Controllers/CallbackControllerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace Auth0\Laravel\Controllers;

use Auth0\Laravel\Auth\Guard;
use Auth0\Laravel\Configuration;
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
15 changes: 8 additions & 7 deletions src/Controllers/LoginControllerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@
namespace Auth0\Laravel\Controllers;

use Auth0\Laravel\Auth\Guard;
use Auth0\Laravel\Configuration;
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 @@ -42,11 +41,13 @@ public function __invoke(

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

Expand Down
5 changes: 4 additions & 1 deletion src/Controllers/LogoutControllerAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public function __invoke(

$loggedIn = $guard->check() ? true : null;
$loggedIn ??= (($guard instanceof Guard) ? $guard->find(Guard::SOURCE_SESSION) : $guard->find()) instanceof CredentialEntityContract;
$landing = config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_AFTER_LOGOUT, config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_INDEX, '/'));

$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();
Expand Down
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(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGIN, '/login'), LoginController::class)->name('login');
Route::get(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGOUT, '/logout'), LogoutController::class)->name('logout');
Route::get(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_CALLBACK, '/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(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGIN, '/login'), LoginController::class)->name('login');
Route::get(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_LOGOUT, '/logout'), LogoutController::class)->name('logout');
Route::get(config(Configuration::CONFIG_NAMESPACE_ROUTES . Configuration::CONFIG_ROUTE_CALLBACK, '/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 f21e9d6

Please sign in to comment.