From 8ff3a557ac5e9916451e814aa8907bb5f638078e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ou=C5=A1ek?= <brousek@ics.muni.cz>
Date: Thu, 24 Mar 2022 13:24:56 +0100
Subject: [PATCH] fix: improve behavior when preferredToken=webAuthn in
 unsupported browser

switch method to OTP only once
---
 www/js/loginform.js | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/www/js/loginform.js b/www/js/loginform.js
index 4d55780..e5e2b71 100644
--- a/www/js/loginform.js
+++ b/www/js/loginform.js
@@ -60,6 +60,14 @@ function changeMode(newMode) {
   document.forms["piLoginForm"].submit();
 }
 
+function fallbackToOTP() {
+  if (value("mode") !== "otp") {
+    setTimeout(() => {
+      changeMode("otp");
+    }, 3000);
+  }
+}
+
 function setMessage(newMessage) {
   getElement("message").innerText = newMessage;
 }
@@ -113,14 +121,14 @@ function doWebAuthn() {
     console.log(
       "Insecure context detected: Aborting Web Authn authentication!"
     );
-    changeMode("otp");
+    fallbackToOTP();
     return;
   }
 
   if (!window.pi_webauthn) {
     enable("useWebAuthnButton");
     setMessage(t("webauthn_library_unavailable"));
-    changeMode("otp");
+    fallbackToOTP();
     return;
   }
 
@@ -163,7 +171,7 @@ function doU2F() {
   if (!window.isSecureContext) {
     setMessage(t("u2f_insecure_context"));
     console.log("Insecure context detected: Aborting U2F authentication!");
-    changeMode("otp");
+    fallbackToOTP();
     return;
   }
 
@@ -171,7 +179,7 @@ function doU2F() {
 
   if (requestStr === null) {
     setMessage(t("u2f_unavailable"));
-    changeMode("otp");
+    fallbackToOTP();
     return;
   }
 
-- 
GitLab