mirror of
https://github.com/bitwarden/web
synced 2025-12-06 00:03:28 +00:00
1 line
8.5 KiB
JavaScript
1 line
8.5 KiB
JavaScript
"use strict";function init(){start(),onMessage(),info("ready")}function start(){if(sentSuccess=!1,!u2f.isSupported)return void error("U2F is not supported in this browser.");var e=getQsParam("data");if(!e)return void error("No data.");if(!(parentUrl=getQsParam("parent")))return void error("No parent.");var t=document.createElement("a");t.href=parentUrl,parentOrigin=t.origin;var r=getQsParam("v");if(!r)return void error("No version.");try{version=parseInt(r);var o=b64Decode(e),n=JSON.parse(o)}catch(e){return void error("Cannot parse data.")}if(!(n.appId&&n.challenge&&n.keys&&n.keys.length))return void error("Invalid data parameters.");stop=!1,initU2f(n)}function initU2f(e){stop||u2f.sign(e.appId,e.challenge,e.keys,function(t){if(t.errorCode)return void(5!==t.errorCode?(error("U2F Error: "+t.errorCode),setTimeout(function(){initU2f(e)},1e3)):initU2f(e));success(t)},10)}function onMessage(){window.addEventListener("message",function(e){e.origin&&""!==e.origin&&e.origin===parentOrigin&&("stop"===e.data?stop=!0:"start"===e.data&&stop&&start())},!1)}function error(e){parent.postMessage("error|"+e,parentUrl)}function success(e){if(!sentSuccess){var t=JSON.stringify(e);parent.postMessage("success|"+t,parentUrl),sentSuccess=!0}}function info(e){parent.postMessage("info|"+e,parentUrl)}function getQsParam(e){var t=window.location.href;e=e.replace(/[\[\]]/g,"\\$&");var r=new RegExp("[?&]"+e+"(=([^&#]*)|&|#|$)"),o=r.exec(t);return o?o[2]?decodeURIComponent(o[2].replace(/\+/g," ")):"":null}function b64Decode(e){return decodeURIComponent(Array.prototype.map.call(atob(e),function(e){return"%"+("00"+e.charCodeAt(0).toString(16)).slice(-2)}).join(""))}var u2f=u2f||{};u2f.isSupported=!!(void 0!==u2f&&u2f.register||"undefined"!=typeof chrome&&chrome.runtime);var js_api_version;u2f.EXTENSION_ID="kmendfapggjehodndflmmgagdbamhnfd",u2f.MessageTypes={U2F_REGISTER_REQUEST:"u2f_register_request",U2F_REGISTER_RESPONSE:"u2f_register_response",U2F_SIGN_REQUEST:"u2f_sign_request",U2F_SIGN_RESPONSE:"u2f_sign_response",U2F_GET_API_VERSION_REQUEST:"u2f_get_api_version_request",U2F_GET_API_VERSION_RESPONSE:"u2f_get_api_version_response"},u2f.ErrorCodes={OK:0,OTHER_ERROR:1,BAD_REQUEST:2,CONFIGURATION_UNSUPPORTED:3,DEVICE_INELIGIBLE:4,TIMEOUT:5},u2f.U2fRequest,u2f.U2fResponse,u2f.Error,u2f.Transport,u2f.Transports,u2f.SignRequest,u2f.SignResponse,u2f.RegisterRequest,u2f.RegisterResponse,u2f.RegisteredKey,u2f.GetJsApiVersionResponse,u2f.getMessagePort=function(e){if("undefined"!=typeof chrome&&chrome.runtime){var t={type:u2f.MessageTypes.U2F_SIGN_REQUEST,signRequests:[]};chrome.runtime.sendMessage(u2f.EXTENSION_ID,t,function(){chrome.runtime.lastError?u2f.getIframePort_(e):u2f.getChromeRuntimePort_(e)})}else u2f.isAndroidChrome_()?u2f.getAuthenticatorPort_(e):u2f.isIosChrome_()?u2f.getIosPort_(e):u2f.getIframePort_(e)},u2f.isAndroidChrome_=function(){var e=navigator.userAgent;return-1!=e.indexOf("Chrome")&&-1!=e.indexOf("Android")},u2f.isIosChrome_=function(){return["iPhone","iPad","iPod"].indexOf(navigator.platform)>-1},u2f.getChromeRuntimePort_=function(e){var t=chrome.runtime.connect(u2f.EXTENSION_ID,{includeTlsChannelId:!0});setTimeout(function(){e(new u2f.WrappedChromeRuntimePort_(t))},0)},u2f.getAuthenticatorPort_=function(e){setTimeout(function(){e(new u2f.WrappedAuthenticatorPort_)},0)},u2f.getIosPort_=function(e){setTimeout(function(){e(new u2f.WrappedIosPort_)},0)},u2f.WrappedChromeRuntimePort_=function(e){this.port_=e},u2f.formatSignRequest_=function(e,t,r,o,n){if(void 0===js_api_version||js_api_version<1.1){for(var s=[],i=0;i<r.length;i++)s[i]={version:r[i].version,challenge:t,keyHandle:r[i].keyHandle,appId:e};return{type:u2f.MessageTypes.U2F_SIGN_REQUEST,signRequests:s,timeoutSeconds:o,requestId:n}}return{type:u2f.MessageTypes.U2F_SIGN_REQUEST,appId:e,challenge:t,registeredKeys:r,timeoutSeconds:o,requestId:n}},u2f.formatRegisterRequest_=function(e,t,r,o,n){if(void 0===js_api_version||js_api_version<1.1){for(var s=0;s<r.length;s++)r[s].appId=e;for(var i=[],s=0;s<t.length;s++)i[s]={version:t[s].version,challenge:r[0],keyHandle:t[s].keyHandle,appId:e};return{type:u2f.MessageTypes.U2F_REGISTER_REQUEST,signRequests:i,registerRequests:r,timeoutSeconds:o,requestId:n}}return{type:u2f.MessageTypes.U2F_REGISTER_REQUEST,appId:e,registerRequests:r,registeredKeys:t,timeoutSeconds:o,requestId:n}},u2f.WrappedChromeRuntimePort_.prototype.postMessage=function(e){this.port_.postMessage(e)},u2f.WrappedChromeRuntimePort_.prototype.addEventListener=function(e,t){var r=e.toLowerCase();"message"==r||"onmessage"==r?this.port_.onMessage.addListener(function(e){t({data:e})}):console.error("WrappedChromeRuntimePort only supports onMessage")},u2f.WrappedAuthenticatorPort_=function(){this.requestId_=-1,this.requestObject_=null},u2f.WrappedAuthenticatorPort_.prototype.postMessage=function(e){var t=u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_+";S.request="+encodeURIComponent(JSON.stringify(e))+";end";document.location=t},u2f.WrappedAuthenticatorPort_.prototype.getPortType=function(){return"WrappedAuthenticatorPort_"},u2f.WrappedAuthenticatorPort_.prototype.addEventListener=function(e,t){if("message"==e.toLowerCase()){var r=this;window.addEventListener("message",r.onRequestUpdate_.bind(r,t),!1)}else console.error("WrappedAuthenticatorPort only supports message")},u2f.WrappedAuthenticatorPort_.prototype.onRequestUpdate_=function(e,t){var r=JSON.parse(t.data),o=(r.intentURL,r.errorCode,null);r.hasOwnProperty("data")&&(o=JSON.parse(r.data)),e({data:o})},u2f.WrappedAuthenticatorPort_.INTENT_URL_BASE_="intent:#Intent;action=com.google.android.apps.authenticator.AUTHENTICATE",u2f.WrappedIosPort_=function(){},u2f.WrappedIosPort_.prototype.postMessage=function(e){var t=JSON.stringify(e),r="u2f://auth?"+encodeURI(t);location.replace(r)},u2f.WrappedIosPort_.prototype.getPortType=function(){return"WrappedIosPort_"},u2f.WrappedIosPort_.prototype.addEventListener=function(e,t){"message"!==e.toLowerCase()&&console.error("WrappedIosPort only supports message")},u2f.getIframePort_=function(e){var t="chrome-extension://"+u2f.EXTENSION_ID,r=document.createElement("iframe");r.src=t+"/u2f-comms.html",r.setAttribute("style","display:none"),document.body.appendChild(r);var o=new MessageChannel,n=function(t){"ready"==t.data?(o.port1.removeEventListener("message",n),e(o.port1)):console.error('First event on iframe port was not "ready"')};o.port1.addEventListener("message",n),o.port1.start(),r.addEventListener("load",function(){r.contentWindow.postMessage("init",t,[o.port2])})},u2f.EXTENSION_TIMEOUT_SEC=30,u2f.port_=null,u2f.waitingForPort_=[],u2f.reqCounter_=0,u2f.callbackMap_={},u2f.getPortSingleton_=function(e){u2f.port_?e(u2f.port_):(0==u2f.waitingForPort_.length&&u2f.getMessagePort(function(e){for(u2f.port_=e,u2f.port_.addEventListener("message",u2f.responseHandler_);u2f.waitingForPort_.length;)u2f.waitingForPort_.shift()(u2f.port_)}),u2f.waitingForPort_.push(e))},u2f.responseHandler_=function(e){var t=e.data,r=t.requestId;if(!r||!u2f.callbackMap_[r])return void console.error("Unknown or missing requestId in response.");var o=u2f.callbackMap_[r];delete u2f.callbackMap_[r],o(t.responseData)},u2f.sign=function(e,t,r,o,n){void 0===js_api_version?u2f.getApiVersion(function(s){js_api_version=void 0===s.js_api_version?0:s.js_api_version,console.log("Extension JS API Version: ",js_api_version),u2f.sendSignRequest(e,t,r,o,n)}):u2f.sendSignRequest(e,t,r,o,n)},u2f.sendSignRequest=function(e,t,r,o,n){u2f.getPortSingleton_(function(s){var i=++u2f.reqCounter_;u2f.callbackMap_[i]=o;var a=void 0!==n?n:u2f.EXTENSION_TIMEOUT_SEC,u=u2f.formatSignRequest_(e,t,r,a,i);s.postMessage(u)})},u2f.register=function(e,t,r,o,n){void 0===js_api_version?u2f.getApiVersion(function(s){js_api_version=void 0===s.js_api_version?0:s.js_api_version,console.log("Extension JS API Version: ",js_api_version),u2f.sendRegisterRequest(e,t,r,o,n)}):u2f.sendRegisterRequest(e,t,r,o,n)},u2f.sendRegisterRequest=function(e,t,r,o,n){u2f.getPortSingleton_(function(s){var i=++u2f.reqCounter_;u2f.callbackMap_[i]=o;var a=void 0!==n?n:u2f.EXTENSION_TIMEOUT_SEC,u=u2f.formatRegisterRequest_(e,r,t,a,i);s.postMessage(u)})},u2f.getApiVersion=function(e,t){u2f.getPortSingleton_(function(r){if(r.getPortType){var o;switch(r.getPortType()){case"WrappedIosPort_":case"WrappedAuthenticatorPort_":o=1.1;break;default:o=0}return void e({js_api_version:o})}var n=++u2f.reqCounter_;u2f.callbackMap_[n]=e;var s={type:u2f.MessageTypes.U2F_GET_API_VERSION_REQUEST,timeoutSeconds:void 0!==t?t:u2f.EXTENSION_TIMEOUT_SEC,requestId:n};r.postMessage(s)})},document.addEventListener("DOMContentLoaded",function(e){init()});var parentUrl=null,parentOrigin=null,version=null,stop=!1,sentSuccess=!1; |