密碼是帳戶安全中最薄弱的環節。它們可能被釣魚、洩露、重複使用或遺忘。這就是我們新增 WebAuthn Passkeys 支援的原因 - 一種防釣魚、無密碼的認證方法,既更安全又更便捷。

什麼是 Passkeys?

Passkeys 是儲存在您裝置(手機、筆記型電腦、安全金鑰)上的加密憑證,用於取代密碼進行認證。它們使用 WebAuthn 標準(FIDO2 的一部分),提供多項優勢:

  • 防釣魚 - Passkeys 綁定到網站網域,無法在假冒網站上使用
  • 沒有可洩露的秘密 - 伺服器上只儲存公鑰
  • 綁定裝置 - 私鑰永遠不會離開您的裝置
  • 生物辨識保護 - 通常由 Face ID、Touch ID 或 Windows Hello 保護
  • 跨裝置同步 - 現代平台透過 iCloud、Google 或 Microsoft 帳戶在裝置間同步 Passkeys

我們的實現

我們將 WebAuthn Passkeys 支援整合到現有的 django-allauth 認證堆疊中,提供網頁和行動端 API 端點。

註冊流程

當使用者註冊新的 Passkey 時,流程如下:

  1. 用戶端向伺服器請求註冊選項
  2. 伺服器產生挑戰,包含使用者和依賴方資訊
  3. 瀏覽器的 WebAuthn API 提示使用者(生物辨識或安全金鑰)
  4. 用戶端將憑證傳送回伺服器
  5. 伺服器驗證並儲存公鑰
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
# API endpoint to begin registration
class WebAuthnRegistrationOptionsView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        from allauth.mfa.webauthn.internal import auth as webauthn_auth

        # Generate credential creation options
        creation_options = webauthn_auth.begin_registration(
            request.user,
            passwordless=False  # Use as 2FA, not passwordless
        )

        return Response({
            "success": True,
            "creation_options": creation_options,
        })

認證流程

使用 Passkey 進行雙因素認證:

  1. 使用者完成主要認證(使用者名稱/密碼或社交登入)
  2. 伺服器回傳挑戰用於 WebAuthn 驗證
  3. 瀏覽器請求 Passkey(生物辨識觸摸)
  4. 憑證與儲存的公鑰進行驗證
  5. 使用者完全通過認證
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class WebAuthnVerifyView(APIView):
    def post(self, request):
        credential = request.data.get("credential")

        # Complete authentication and verify credential
        authenticator = webauthn_auth.complete_authentication(
            request.user,
            credential
        )

        # Record usage for security monitoring
        authenticator.last_used_at = timezone.now()
        authenticator.save()

        return Response({"success": True})

Passkey 管理

使用者可以管理已註冊的 Passkeys:

  • 列出所有 Passkeys - 包含註冊日期和最後使用時間
  • 重新命名 Passkeys - 便於識別(例如,“iPhone”、“MacBook”)
  • 刪除 Passkeys - 包含防止刪除最後一個認證器的保護
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
class WebAuthnListView(APIView):
    def get(self, request):
        authenticators = Authenticator.objects.filter(
            user=request.user,
            type=Authenticator.Type.WEBAUTHN
        ).order_by("-created_at")

        return Response({
            "passkeys": [
                {
                    "id": auth.pk,
                    "name": auth.wrap().name,
                    "created_at": auth.created_at.isoformat(),
                    "last_used_at": auth.last_used_at.isoformat() if auth.last_used_at else None,
                }
                for auth in authenticators
            ]
        })

API 端點

我們的 REST API 為行動應用程式提供完整的 WebAuthn 支援:

端點方法描述
/api/webauthn/register/options/GET取得註冊挑戰
/api/webauthn/register/complete/POST完成註冊
/api/webauthn/authenticate/options/GET取得認證挑戰
/api/webauthn/verify/POST驗證 2FA 憑證
/api/webauthn/GET列出已註冊的 Passkeys
/api/webauthn/<id>/PATCH重新命名 Passkey
/api/webauthn/<id>/DELETE刪除 Passkey

安全考量

復原碼

當使用者註冊第一個認證器(Passkey 或 TOTP)時,我們會自動產生復原碼。這些一次性代碼可在所有其他認證方法不可用時使用:

1
2
if Authenticator.objects.filter(user=request.user).count() == 1:
    auto_generate_recovery_codes(request._request)

防止鎖定

如果刪除最後一個 Passkey 會導致使用者沒有任何雙因素認證方法,則不允許刪除:

1
2
3
4
if not adapter.can_delete_authenticator(authenticator):
    return Response({
        "message": "Cannot delete - you must have at least one authentication method"
    }, status=400)

監控

我們透過 Datadog 指標追蹤 Passkey 使用情況以進行安全監控:

1
2
3
increment("webauthn.registration.success")
increment("webauthn.verify.success")
increment("webauthn.verify.failed")

瀏覽器支援

所有現代瀏覽器都支援 WebAuthn:

  • Chrome 67+
  • Firefox 60+
  • Safari 13+
  • Edge 79+
  • 行動版 Chrome、Safari、Firefox

對於舊版瀏覽器,使用者仍可使用 TOTP(認證應用程式)或復原碼。

使用者體驗

新增 Passkey 只需幾秒鐘:

  1. 前往設定 → 安全 → 雙因素認證
  2. 點擊新增 Passkey
  3. 為您的裝置命名(例如,“iPhone 15”)
  4. 使用 Face ID / Touch ID / Windows Hello 認證
  5. 完成!您的 Passkey 已註冊

今後登入只需生物辨識確認 - 無需輸入密碼或從認證應用程式複製代碼。

為什麼我們選擇 Passkeys

方法釣魚風險使用者體驗復原方式
密碼差(易忘、弱密碼)郵件重設
SMS 2FA中(SIM 卡交換)一般手機號
TOTP 應用程式手動輸入代碼備份金鑰
Passkeys非常低優秀復原碼

Passkeys 代表認證的未來。它們已被 Apple、Google 和 Microsoft 平台支援,幾乎所有使用者都可以使用。


準備好告別密碼了嗎?在您的安全設定中啟用 Passkeys!