Protect administrator password and quick logins independently for DPL HN and FUC
This commit is contained in:
1 parent
3a0f499769
commit
bf145846a6
3 files changed
+85
-10
No files matched your search
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use TeleHub\WebAdmin\GuardUnavailable;
|
||||
use TeleHub\WebAdmin\LoginBlocked;
|
||||
use TeleHub\WebAdmin\Ticket;
|
||||
|
||||
final class XbAdminLogin
|
||||
{
|
||||
private static function begin(Request $request, $user)
|
||||
{
|
||||
if (!$user || !$user->is_admin) {
|
||||
return null;
|
||||
}
|
||||
$profile = $request->server->get('XB_INSTANCE') ?? config('site.profile');
|
||||
$instances = ['dpl' => 'xb-dpl', 'hn' => 'xb-hn', 'fuc-xb-test' => 'xb-fuc'];
|
||||
$instance = $instances[$profile] ?? null;
|
||||
if (!$instance || (!app()->environment('production') && !is_dir('/run/web-admin-login-'.$instance))) {
|
||||
return null;
|
||||
}
|
||||
require_once '/opt/web-admin-login/current/Ticket.php';
|
||||
return Ticket::begin($instance, $request->server->all());
|
||||
}
|
||||
|
||||
public static function password(Request $request, string $email, callable $verify, callable $issue): array
|
||||
{
|
||||
// Match the account lookup used by this backend, including older releases.
|
||||
$query = method_exists(User::class, 'scopeByEmail') || method_exists(User::class, 'byEmail')
|
||||
? User::byEmail($email) : User::where('email', $email);
|
||||
$candidate = $query->first(['id','is_admin']);
|
||||
return self::handle($request, $candidate, $verify, $issue);
|
||||
}
|
||||
|
||||
public static function verified(Request $request, $user, callable $issue)
|
||||
{
|
||||
[, $data] = self::handle($request, $user, fn () => [true, $user], $issue);
|
||||
return $data;
|
||||
}
|
||||
|
||||
private static function handle(Request $request, $candidate, callable $verify, callable $issue): array
|
||||
{
|
||||
try {
|
||||
$ticket = self::begin($request, $candidate);
|
||||
try {
|
||||
[$success, $result] = $verify();
|
||||
if ($success) {
|
||||
// Account privileges may have changed during authentication.
|
||||
if ($ticket === null && $result->is_admin && (!$candidate || !$candidate->is_admin)) {
|
||||
$ticket = self::begin($request, $result);
|
||||
}
|
||||
$result = $issue($result);
|
||||
}
|
||||
} catch (\Throwable $error) {
|
||||
if ($ticket !== null) {
|
||||
$ticket->finish('error');
|
||||
}
|
||||
throw $error;
|
||||
}
|
||||
if ($ticket !== null) {
|
||||
$ticket->finish($success ? 'success' : 'failure');
|
||||
}
|
||||
return [$success, $result];
|
||||
} catch (LoginBlocked $error) {
|
||||
abort(429, '该来源 IP 已触发管理员登录保护。');
|
||||
} catch (GuardUnavailable $error) {
|
||||
abort(503, '管理员登录保护暂不可用,请稍后重试。');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user