From e7ed18fdfcf9a4bbaafa683ed332c3b648c3869c Mon Sep 17 00:00:00 2001 From: Penar Musaraj Date: Fri, 27 Oct 2023 11:55:55 -0400 Subject: [PATCH] FIX: Only check for conditional mediation when needed (#24142) Some browsers still don't support conditional mediation. This PR fixes issues with: - TOR browser (it doesn't have `PublicKeyCredential` at all) - Firefox 119 (doesn't support conditional mediation) We also need to make sure not to call `isConditionalMediationAvailable` on browsers that don't support the method but support the feature (like Safari on iOS). --- .../discourse/app/components/modal/login.js | 17 +++++++++++++++++ .../components/modal/login/local-login-form.js | 10 ++-------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/modal/login.js b/app/assets/javascripts/discourse/app/components/modal/login.js index a8685290cb5..7b02ea0b2b4 100644 --- a/app/assets/javascripts/discourse/app/components/modal/login.js +++ b/app/assets/javascripts/discourse/app/components/modal/login.js @@ -19,6 +19,7 @@ import escape from "discourse-common/lib/escape"; import I18n from "discourse-i18n"; export default class Login extends Component { + @service capabilities; @service dialog; @service siteSettings; @service site; @@ -116,6 +117,22 @@ export default class Login extends Component { @action async passkeyLogin(mediation = "optional") { try { + // we need to check isConditionalMediationAvailable for Firefox + // without it, Firefox will throw console errors + // We cannot do a general check because iOS Safari and Chrome in Selenium quietly support the feature + // but they do not support the PublicKeyCredential.isConditionalMediationAvailable() method + if ( + mediation === "conditional" && + this.capabilities.isFirefox && + window.PublicKeyCredential + ) { + const isCMA = + // eslint-disable-next-line no-undef + await PublicKeyCredential.isConditionalMediationAvailable(); + if (!isCMA) { + return; + } + } const response = await ajax("/session/passkey/challenge.json"); const publicKeyCredential = await getPasskeyCredential( diff --git a/app/assets/javascripts/discourse/app/components/modal/login/local-login-form.js b/app/assets/javascripts/discourse/app/components/modal/login/local-login-form.js index 728599ec43e..0fd555314ed 100644 --- a/app/assets/javascripts/discourse/app/components/modal/login/local-login-form.js +++ b/app/assets/javascripts/discourse/app/components/modal/login/local-login-form.js @@ -36,15 +36,9 @@ export default class LocalLoginBody extends Component { @action passkeyConditionalLogin() { - if ( - // eslint-disable-next-line no-undef - !PublicKeyCredential.isConditionalMediationAvailable || - !this.args.canUsePasskeys - ) { - return; + if (this.args.canUsePasskeys) { + this.args.passkeyLogin("conditional"); } - - this.args.passkeyLogin("conditional"); } @action