4 Commits
Author SHA1 Message Date
Romulus21 116129a098 fix tests 2026-05-27 14:12:47 +02:00
Romulus21 470596e0b4 change token method & remove error page 2026-05-27 12:07:05 +02:00
Romulus21 d0b7c9872b add error page 2026-05-27 11:11:51 +02:00
Romulus21 ccd7934bb8 pint 2026-05-27 10:16:44 +02:00
4 changed files with 39 additions and 34 deletions
+12 -8
View File
@@ -9,22 +9,26 @@ class DelegateAuthServiceProvider extends ServiceProvider
public function register(): void
{
$this->mergeConfigFrom(
__DIR__.'/../config/delegate-auth.php', 'delegate-auth'
__DIR__ . "/../config/delegate-auth.php",
"delegate-auth",
);
}
public function boot(): void
{
$this->publishes([
__DIR__.'/../config/delegate-auth.php' => config_path('delegate-auth.php'),
], 'delegate-auth-config');
$this->publishes(
[
__DIR__ . "/../config/delegate-auth.php" => config_path(
"delegate-auth.php",
),
],
"delegate-auth-config",
);
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
$this->loadRoutesFrom(__DIR__ . "/../routes/web.php");
if ($this->app->runningInConsole()) {
$this->commands([
Console\InstallCommand::class,
]);
$this->commands([Console\InstallCommand::class]);
}
}
}
+23 -22
View File
@@ -10,60 +10,61 @@ class DelegateAuthController extends Controller
{
public function login(Request $request)
{
$request->validate(['token' => 'required|string']);
$request->validate(["token" => "required|string"]);
$payload = $this->decryptToken($request->input('token'));
$payload = $this->decryptToken($request->input("token"));
abort_if(! $payload, 401, 'Token invalide.');
abort_if(!$payload, 401, "Token invalide.");
$field = config('delegate-auth.user_field');
$field = config("delegate-auth.user_field");
abort_if(empty($payload[$field]), 401, 'Token invalide : champ '.$field.' manquant.');
abort_if(
empty($payload[$field]),
401,
"Token invalide : champ " . $field . " manquant.",
);
$guard = Auth::guard(config('delegate-auth.guard'));
$user = $guard->getProvider()->retrieveByCredentials([$field => $payload[$field]]);
$guard = Auth::guard(config("delegate-auth.guard"));
$user = $guard
->getProvider()
->retrieveByCredentials([$field => $payload[$field]]);
abort_if(! $user, 401, 'Utilisateur introuvable.');
abort_if(!$user, 401, "Utilisateur introuvable.");
$guard->login($user);
return redirect(config('delegate-auth.redirect_after_login'));
return redirect(config("delegate-auth.redirect_after_login"));
}
public function logout(Request $request)
{
$guard = Auth::guard(config('delegate-auth.guard'));
$guard = Auth::guard(config("delegate-auth.guard"));
$guard->logout();
$request->session()->invalidate();
$request->session()->regenerateToken();
return redirect(config('delegate-auth.redirect_after_logout'));
return redirect(config("delegate-auth.redirect_after_logout"));
}
private function decryptToken(string $token): ?array
{
if (!str_contains($token, '.')) {
$data = base64_decode($token, strict: true);
if ($data === false || strlen($data) <= 16) {
return null;
}
[$ivB64, $encrypted] = explode('.', $token, 2);
$iv = base64_decode($ivB64, strict: true);
$key = substr(hash('sha256', config('delegate-auth.encrypt_key'), binary: true), 0, 32);
if ($iv === false || strlen($iv) !== 16) {
return null;
}
$payload = openssl_decrypt($encrypted, 'AES-256-CBC', $key, 0, $iv);
$payload = openssl_decrypt(substr($data, 16), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, substr($data, 0, 16));
if ($payload === false) {
return null;
}
$data = json_decode($payload, associative: true);
$decoded = json_decode($payload, associative: true);
return is_array($data) ? $data : null;
return is_array($decoded) ? $decoded : null;
}
}
+2 -2
View File
@@ -37,8 +37,8 @@ class LoginTest extends TestCase
{
$wrongKey = substr(hash('sha256', 'wrong-key', binary: true), 0, 32);
$iv = random_bytes(16);
$encrypted = openssl_encrypt(json_encode(['email' => 'user@example.com']), 'AES-256-CBC', $wrongKey, 0, $iv);
$token = base64_encode($iv).'.'.$encrypted;
$encrypted = openssl_encrypt(json_encode(['email' => 'user@example.com']), 'AES-256-CBC', $wrongKey, OPENSSL_RAW_DATA, $iv);
$token = base64_encode($iv.$encrypted);
User::create(['email' => 'user@example.com']);
+2 -2
View File
@@ -39,8 +39,8 @@ abstract class TestCase extends OrchestraTestCase
{
$key = substr(hash('sha256', config('delegate-auth.encrypt_key'), binary: true), 0, 32);
$iv = random_bytes(16);
$encrypted = openssl_encrypt(json_encode($payload), 'AES-256-CBC', $key, 0, $iv);
$encrypted = openssl_encrypt(json_encode($payload), 'AES-256-CBC', $key, OPENSSL_RAW_DATA, $iv);
return base64_encode($iv).'.'.$encrypted;
return base64_encode($iv.$encrypted);
}
}