Compare commits
2
Commits
v1.0.0
..
d0b7c9872b
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d0b7c9872b | ||
|
|
ccd7934bb8 |
@@ -0,0 +1,59 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>401 – Accès non autorisé</title>
|
||||
<style>
|
||||
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
|
||||
body {
|
||||
font-family: ui-sans-serif, system-ui, sans-serif;
|
||||
background: #f8fafc;
|
||||
color: #1e293b;
|
||||
min-height: 100vh;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: #fff;
|
||||
border: 1px solid #e2e8f0;
|
||||
border-radius: 12px;
|
||||
padding: 2.5rem 3rem;
|
||||
max-width: 420px;
|
||||
width: 100%;
|
||||
text-align: center;
|
||||
box-shadow: 0 4px 24px rgba(0,0,0,.06);
|
||||
}
|
||||
|
||||
.code {
|
||||
font-size: 4rem;
|
||||
font-weight: 700;
|
||||
color: #e11d48;
|
||||
line-height: 1;
|
||||
margin-bottom: .5rem;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 1.25rem;
|
||||
font-weight: 600;
|
||||
margin-bottom: .75rem;
|
||||
}
|
||||
|
||||
p {
|
||||
font-size: .95rem;
|
||||
color: #64748b;
|
||||
line-height: 1.6;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="card">
|
||||
<div class="code">401</div>
|
||||
<h1>Accès non autorisé</h1>
|
||||
<p>{{ $exception->getMessage() ?: 'Vous n\'êtes pas autorisé à accéder à cette ressource.' }}</p>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -19,6 +19,10 @@ class DelegateAuthServiceProvider extends ServiceProvider
|
||||
__DIR__.'/../config/delegate-auth.php' => config_path('delegate-auth.php'),
|
||||
], 'delegate-auth-config');
|
||||
|
||||
$this->publishes([
|
||||
__DIR__.'/../resources/views/errors/401.blade.php' => resource_path('views/errors/401.blade.php'),
|
||||
], 'delegate-auth-views');
|
||||
|
||||
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
|
||||
|
||||
if ($this->app->runningInConsole()) {
|
||||
|
||||
@@ -10,53 +10,63 @@ 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, '.')) {
|
||||
if (!str_contains($token, ".")) {
|
||||
return null;
|
||||
}
|
||||
|
||||
[$ivB64, $encrypted] = explode('.', $token, 2);
|
||||
[$ivB64, $encrypted] = explode(".", $token, 2);
|
||||
|
||||
$iv = base64_decode($ivB64, strict: true);
|
||||
$key = substr(hash('sha256', config('delegate-auth.encrypt_key'), binary: true), 0, 32);
|
||||
$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($encrypted, "AES-256-CBC", $key, 0, $iv);
|
||||
|
||||
if ($payload === false) {
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user