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