如何在插件层拦截 WebAuthn 登录,强制要求二次 PIN 码验证?
解读
面试官抛出这道题,核心想验证三件事:
- 是否真正理解 Joomla 插件体系,尤其是「用户—身份验证」这一钩子链条;
- 是否熟悉 WebAuthn 在 Joomla 4 中的事件流(从
onUserLogin到onUserAfterLogin再到onUserLoginFailure); - 能否在不侵入核心、不破坏升级路径的前提下,用「插件」而非「改核心」完成二次鉴权,并兼顾国内等保 2.0 对「多因素认证」的审计要求。
国内政企项目普遍把「WebAuthn 硬件密钥」算作「第一种因子」,把「静态 PIN」算作「第二种因子」。插件必须做到:
- 只在 WebAuthn 方式登录时触发,密码登录不受影响;
- PIN 校验失败要能让 Joomla 识别为「登录失败」并记录审计日志;
- 前后端分离场景下,REST API 也要被同一条插件拦截,不能留 bypass。
知识点
-
插件组与事件优先级
身份验证相关事件集中在「user」组与「authentication」组;onUserLogin在「user」组触发,早于onUserAfterLogin。通过$priority = 1可把自己插件插到链首。 -
WebAuthn 内部标识
Joomla\Plugin\Multifactorauth\WebAuthn\Extension\WebAuthn 把认证方式写进credentials['secretkey'] = 'webauthn';插件里可用$credentials['secretkey'] === 'webauthn'判断是否走 WebAuthn。 -
会话分段存储
国内等保要求「二次验证失败 5 次即锁定」。需把「已验 WebAuthn、待验 PIN」的中间状态写入$session->set('plg_user_pinauth.phase', 'webauthn_ok'),避免重复弹窗。 -
异常与事件终止
插件里抛出Joomla\CMS\Event\User\LoginFailureEvent并$event->stopPropagation(),可让 Joomla 直接记一次失败登录,无需手动写#__users表。 -
前端无刷新二次页
国内用户习惯「弹层输 PIN」而非「跳新页」。用plg_user_pinauth输出一段 JSON+HTML 混合响应,前端fetch()拿到后渲染层,验证完再POST /index.php?option=com_users&task=user.login带上pin_hash。 -
REST API 统一拦截
Joomla 4 的com_users登录接口/api/v1/users/login同样触发onUserLogin;插件里判断$app->isClient('api')直接返回406并写X-Secondary-PIN-Required: true,方便小程序端做适配。
答案
【步骤级回答,可直接在面试白板展开】
-
新建「user」类型插件
plg_user_pinauth,在pinauth.xml声明<folder>src</folder>走 PSR-4 自动加载,适配 Joomla 4/5。 -
注册事件
public function subscribe(DispatcherInterface $dispatcher)
{
$dispatcher->addListener('onUserLogin', [$this, 'onUserLogin'], 1);
$dispatcher->addListener('onUserAfterLogin', [$this, 'onUserAfterLogin'], 1);
} -
拦截 WebAuthn
public function onUserLogin(Event $event)
{
$credentials = $event->getArgument('credentials');
if (($credentials['secretkey'] ?? null) !== 'webauthn') return;
$session = $this->getApplication()->getSession();
$session->set('plg_user_pinauth.uid', $credentials['username']);
$session->set('plg_user_pinauth.phase', 'webauthn_ok');
// 不让登录继续
$event->stopPropagation();
} -
返回二次验证 payload
public function onUserAfterLogin(Event $event)
{
$session = $this->getApplication()->getSession();
if ($session->get('plg_user_pinauth.phase') !== 'webauthn_ok') return;
// 已验 WebAuthn,未验 PIN
$pinForm = $this->renderPinLayer(); // 返回 JSON/HTML
$event['subject']->setResponse(new JsonResponse(['pin_required' => true, 'html' => $pinForm]));
$event->stopPropagation();
} -
校验 PIN
前端把 PIN 做一次本地 SHA256,带csrf_tokenPOST 到index.php?option=com_ajax&plugin=pinauth&task=verify。
插件再注册onAjaxPinauth:
public function onAjaxPinauth()``{this->getInput()->getString('pin_hash');this->params->get('pinHash'); // 后台预存if (!hash_equals(pin)){this->raiseLoginFailure(); // 写审计日志return;}this->getApplication()->login(['username' => $session->get('plg_user_pinauth.uid')]);}` -
失败计数与锁定
用plg_user_pinauth.attempts.{uid}做 session 计数,≥5 次写入#__user_notes并抛LoginFailureEvent,配合系统「自动封锁」插件即可。 -
打包与交付
插件根目录加language/zh-CN/plg_user_pinauth.ini,把「二次验证说明」「PIN 输入提示」全部中文化;script.php里写postflight把默认 PIN 哈希写入#__extensions.params,避免首次安装后管理员进不去。
拓展思考
-
国密改造
若客户要求 SM2/SM3,可把前端 SHA256 换成 SM3,后端用php-gmssl扩展校验;同时把 WebAuthn 的 ECDSA 签名算法限制为alg: -7(ES256) 或国密双证,满足《GMT 0009-2012》。 -
零信任续期
二次 PIN 验证通过后,把「令牌有效期」缩短到 30 分钟,结合onAfterRespond事件每分钟做静默重验,实现「持续自适应信任」。 -
小程序端适配
国内很多门户用微信小程序做统一入口。可在插件里识别X-Client-Type: wechat-miniprogram,把 PIN 输入页做成skyline渲染的webview,验证通过后回跳navigateBackMiniProgram,实现同源票据复用。 -
与等保测评对接
测评机构会抽查「多因素日志」。插件在onUserLoginFailure里写 JSON 格式审计到syslog,字段包含「时间、IP、硬件密钥ID、PIN 错误码、session_id」,方便 SIEM 直接采集。