如何在插件层拦截 WebAuthn 登录,强制要求二次 PIN 码验证?

解读

面试官抛出这道题,核心想验证三件事:

  1. 是否真正理解 Joomla 插件体系,尤其是「用户—身份验证」这一钩子链条;
  2. 是否熟悉 WebAuthn 在 Joomla 4 中的事件流(从 onUserLogin 到 onUserAfterLogin 再到 onUserLoginFailure);
  3. 能否在不侵入核心、不破坏升级路径的前提下,用「插件」而非「改核心」完成二次鉴权,并兼顾国内等保 2.0 对「多因素认证」的审计要求。

国内政企项目普遍把「WebAuthn 硬件密钥」算作「第一种因子」,把「静态 PIN」算作「第二种因子」。插件必须做到:

  • 只在 WebAuthn 方式登录时触发,密码登录不受影响;
  • PIN 校验失败要能让 Joomla 识别为「登录失败」并记录审计日志;
  • 前后端分离场景下,REST API 也要被同一条插件拦截,不能留 bypass。

知识点

  1. 插件组与事件优先级
    身份验证相关事件集中在「user」组与「authentication」组;onUserLogin 在「user」组触发,早于 onUserAfterLogin。通过 $priority = 1 可把自己插件插到链首。

  2. WebAuthn 内部标识
    Joomla\Plugin\Multifactorauth\WebAuthn\Extension\WebAuthn 把认证方式写进 credentials['secretkey'] = 'webauthn';插件里可用 $credentials['secretkey'] === 'webauthn' 判断是否走 WebAuthn。

  3. 会话分段存储
    国内等保要求「二次验证失败 5 次即锁定」。需把「已验 WebAuthn、待验 PIN」的中间状态写入 $session->set('plg_user_pinauth.phase', 'webauthn_ok'),避免重复弹窗。

  4. 异常与事件终止
    插件里抛出 Joomla\CMS\Event\User\LoginFailureEvent 并 $event->stopPropagation(),可让 Joomla 直接记一次失败登录,无需手动写 #__users 表。

  5. 前端无刷新二次页
    国内用户习惯「弹层输 PIN」而非「跳新页」。用 plg_user_pinauth 输出一段 JSON+HTML 混合响应,前端 fetch() 拿到后渲染层,验证完再 POST /index.php?option=com_users&task=user.login 带上 pin_hash。

  6. REST API 统一拦截
    Joomla 4 的 com_users 登录接口 /api/v1/users/login 同样触发 onUserLogin;插件里判断 $app->isClient('api') 直接返回 406 并写 X-Secondary-PIN-Required: true,方便小程序端做适配。

答案

【步骤级回答,可直接在面试白板展开】

  1. 新建「user」类型插件 plg_user_pinauth,在 pinauth.xml 声明 <folder>src</folder> 走 PSR-4 自动加载,适配 Joomla 4/5。

  2. 注册事件
    public function subscribe(DispatcherInterface $dispatcher)
    {
    $dispatcher->addListener('onUserLogin', [$this, 'onUserLogin'], 1);
    $dispatcher->addListener('onUserAfterLogin', [$this, 'onUserAfterLogin'], 1);
    }

  3. 拦截 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();
    }

  4. 返回二次验证 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();
    }

  5. 校验 PIN
    前端把 PIN 做一次本地 SHA256,带 csrf_token POST 到 index.php?option=com_ajax&plugin=pinauth&task=verify。
    插件再注册 onAjaxPinauth:
    public function onAjaxPinauth()`` { pin=pin = this->getInput()->getString('pin_hash'); stored=stored = this->params->get('pinHash'); // 后台预存 if (!hash_equals(stored,stored, pin)) { this−>getApplication()−>enqueueMessage(′PIN错误′,′error′);‘‘this->getApplication()->enqueueMessage('PIN 错误', 'error');` ` this->raiseLoginFailure(); // 写审计日志 return; } session−>set(′plguserpinauth.phase′,′passed′);‘‘//重新触发登录‘‘session->set('plg_user_pinauth.phase', 'passed');` ` // 重新触发登录` ` this->getApplication()->login(['username' => $session->get('plg_user_pinauth.uid')]); }`

  6. 失败计数与锁定
    用 plg_user_pinauth.attempts.{uid} 做 session 计数,≥5 次写入 #__user_notes 并抛 LoginFailureEvent,配合系统「自动封锁」插件即可。

  7. 打包与交付
    插件根目录加 language/zh-CN/plg_user_pinauth.ini,把「二次验证说明」「PIN 输入提示」全部中文化;script.php 里写 postflight 把默认 PIN 哈希写入 #__extensions.params,避免首次安装后管理员进不去。

拓展思考

  1. 国密改造
    若客户要求 SM2/SM3,可把前端 SHA256 换成 SM3,后端用 php-gmssl 扩展校验;同时把 WebAuthn 的 ECDSA 签名算法限制为 alg: -7 (ES256) 或国密双证,满足《GMT 0009-2012》。

  2. 零信任续期
    二次 PIN 验证通过后,把「令牌有效期」缩短到 30 分钟,结合 onAfterRespond 事件每分钟做静默重验,实现「持续自适应信任」。

  3. 小程序端适配
    国内很多门户用微信小程序做统一入口。可在插件里识别 X-Client-Type: wechat-miniprogram,把 PIN 输入页做成 skyline 渲染的 webview,验证通过后回跳 navigateBackMiniProgram,实现同源票据复用。

  4. 与等保测评对接
    测评机构会抽查「多因素日志」。插件在 onUserLoginFailure 里写 JSON 格式审计到 syslog,字段包含「时间、IP、硬件密钥ID、PIN 错误码、session_id」,方便 SIEM 直接采集。