/** * @package Joomla.Plugin * @subpackage System.webauthn * * @copyright (C) 2020 Open Source Matters, Inc. * @license GNU General Public License version 2 or later; see LICENSE.txt */window.Joomla=window.Joomla||{},((a,c)=>{const u=(n,r="")=>{let t="";return Object.keys(n).forEach(e=>{if(typeof n[e]!="object"){t.length>0&&(t+="&"),r===""?t+=`${encodeURIComponent(e)}=${encodeURIComponent(n[e])}`:t+=`${encodeURIComponent(r)}[${encodeURIComponent(e)}]=${encodeURIComponent(n[e])}`;return}t+=`${u(n[e],e)}`}),t},g=(n,r)=>{const t=n.querySelectorAll(r);return t.length?t[0]:null},d=(n,r)=>{let t=null;if(!n)return t;const e=n.parentElement;if(e.nodeName==="FORM")return t=g(e,r),t;const s=e.querySelectorAll("form");if(s.length){for(let o=0;o{a.renderMessages({error:[n]})},f=n=>{const r=e=>btoa(String.fromCharCode(...e)),t=e=>{let s=e.replace(/-/g,"+").replace(/_/g,"/");const o=s.length%4;if(o){if(o===1)throw new Error("InvalidLengthError: Input base64url string is the wrong length to determine padding");s+=new Array(5-o).join("=")}return s};if(!n.challenge){l(a.Text._("PLG_SYSTEM_WEBAUTHN_ERR_INVALID_USERNAME"));return}n.challenge=Uint8Array.from(window.atob(t(n.challenge)),e=>e.charCodeAt(0)),n.allowCredentials&&(n.allowCredentials=n.allowCredentials.map(e=>(e.id=Uint8Array.from(window.atob(t(e.id)),s=>s.charCodeAt(0)),e))),navigator.credentials.get({publicKey:n}).then(e=>{const s={id:e.id,type:e.type,rawId:r(new Uint8Array(e.rawId)),response:{authenticatorData:r(new Uint8Array(e.response.authenticatorData)),clientDataJSON:r(new Uint8Array(e.response.clientDataJSON)),signature:r(new Uint8Array(e.response.signature)),userHandle:e.response.userHandle?r(new Uint8Array(e.response.userHandle)):null}},o=a.getOptions("system.paths");window.location=`${o?`${o.base}/index.php`:window.location.pathname}?${a.getOptions("csrf.token")}=1&option=com_ajax&group=system&plugin=webauthn&format=raw&akaction=login&encoding=redirect&data=${btoa(JSON.stringify(s))}`}).catch(e=>{l(e)})};a.plgSystemWebauthnLogin=n=>{const r=c.getElementById(n),t=d(r,"input[name=username]"),e=d(r,"input[name=return]");if(t===null)return a.renderMessages({error:[a.Text._("PLG_SYSTEM_WEBAUTHN_ERR_CANNOT_FIND_USERNAME")]}),!1;const s=t.value,o=e?e.value:null;if(s==="")return a.renderMessages({error:[a.Text._("PLG_SYSTEM_WEBAUTHN_ERR_EMPTY_USERNAME")]}),!1;const h={option:"com_ajax",group:"system",plugin:"webauthn",format:"raw",akaction:"challenge",encoding:"raw",username:s,returnUrl:o};h[a.getOptions("csrf.token")]=1;const p=a.getOptions("system.paths");return a.request({url:`${p?`${p.base}/index.php`:window.location.pathname}?${a.getOptions("csrf.token")}=1`,method:"POST",data:u(h),onSuccess(i){let w={};try{w=JSON.parse(i)}catch{}f(w)},onError:i=>{l(`${i.status} ${i.statusText}`)}}),!1},c.querySelectorAll(".plg_system_webauthn_login_button").forEach(n=>{n.addEventListener("click",({currentTarget:r})=>a.plgSystemWebauthnLogin(r.getAttribute("data-webauthn-form")))})})(Joomla,document);