(function(){"use strict";/**
 * @package     Joomla.Plugin
 * @subpackage  Multifactorauth.webauthn
 *
 * @copyright   (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */(function(s,a){var f=null,i=function(t){return btoa(String.fromCharCode.apply(String,t))},o=function(t){var e=t.replace(/-/g,"+").replace(/_/g,"/"),r=e.length%4;if(r){if(r===1)throw new Error("InvalidLengthError: Input base64url string is the wrong length to determine padding");e+=new Array(5-r).join("=")}return e},u=function(t){try{s.renderMessages({error:t})}catch{alert(t)}},c=function(t){try{a.getElementById("plg_multifactorauth_webauthn_validate_button").style.disabled="null"}catch{}u(t)},y=function(t){if(t.preventDefault(),!("credentials"in navigator))return u(s.Text._("PLG_MULTIFACTORAUTH_WEBAUTHN_ERR_NOTAVAILABLE_HEAD")),!1;var e=a.forms["com-users-method-edit"].querySelectorAll('input[name="pkRequest"]')[0].value,r=JSON.parse(atob(e));return r.challenge=Uint8Array.from(window.atob(o(r.challenge)),function(n){return n.charCodeAt(0)}),r.user.id=Uint8Array.from(window.atob(r.user.id),function(n){return n.charCodeAt(0)}),r.excludeCredentials&&(r.excludeCredentials=r.excludeCredentials.map(function(n){return n.id=Uint8Array.from(window.atob(o(n.id)),function(d){return d.charCodeAt(0)}),n})),navigator.credentials.create({publicKey:r}).then(function(n){var d={id:n.id,type:n.type,rawId:i(new Uint8Array(n.rawId)),response:{clientDataJSON:i(new Uint8Array(n.response.clientDataJSON)),attestationObject:i(new Uint8Array(n.response.attestationObject))}};a.getElementById("com-users-method-code").value=btoa(JSON.stringify(d)),a.forms["com-users-method-edit"].submit()},function(n){c(n)}),!1},p=function(){if(!("credentials"in navigator)){u(s.Text._("PLG_MULTIFACTORAUTH_WEBAUTHN_ERR_NOTAVAILABLE_HEAD"));return}var t=f;if(!t.challenge){c(s.Text._("PLG_MULTIFACTORAUTH_WEBAUTHN_ERR_NO_STORED_CREDENTIAL"));return}t.challenge=Uint8Array.from(window.atob(o(t.challenge)),function(e){return e.charCodeAt(0)}),t.allowCredentials&&(t.allowCredentials=t.allowCredentials.map(function(e){return e.id=Uint8Array.from(window.atob(o(e.id)),function(r){return r.charCodeAt(0)}),e})),navigator.credentials.get({publicKey:t}).then(function(e){var r={id:e.id,type:e.type,rawId:i(new Uint8Array(e.rawId)),response:{authenticatorData:i(new Uint8Array(e.response.authenticatorData)),clientDataJSON:i(new Uint8Array(e.response.clientDataJSON)),signature:i(new Uint8Array(e.response.signature)),userHandle:e.response.userHandle?i(new Uint8Array(e.response.userHandle)):null}};a.getElementById("users-mfa-code").value=btoa(JSON.stringify(r)),a.getElementById("users-mfa-captive-form").submit()},function(e){c(e)})},g=function(t){return t.preventDefault(),f=JSON.parse(window.atob(s.getOptions("com_users.authData"))),a.getElementById("users-mfa-captive-button-submit").style.disabled="disabled",p(),!1};a.getElementById("multifactorauth-webauthn-missing").style.display="none",typeof navigator.credentials>"u"&&(a.getElementById("multifactorauth-webauthn-missing").style.display="block",a.getElementById("multifactorauth-webauthn-controls").style.display="none"),window.addEventListener("DOMContentLoaded",function(){s.getOptions("com_users.pagetype")==="validate"?a.getElementById("users-mfa-captive-button-submit").addEventListener("click",g):a.querySelectorAll(".multifactorauth_webauthn_setup").forEach(function(l){l.addEventListener("click",y)})})})(Joomla,document)})();
