var enableSSO = false; class AuthContext { source; secuityToken; clientID; select; confrimed; } class AuthProvider { constructor() { } init() { } hide() { } } class Google extends AuthProvider { constructor() { super(); } onClickHandler() { console.log("Sign in with Google button clicked..."); /*showLoading("Google Account authentication in progress...");*/ } handleCredentialResponse(CredentialResponse) { var clientID = CredentialResponse.client_id; var secuityToken = CredentialResponse.credential; var select = CredentialResponse.select_by; showLoading("Google account authentication complete, signing in to CameraFTP account..."); var ctx = new AuthContext(); ctx.source = "google"; ctx.credential = secuityToken; ctx.clientID = clientID; ctx.select = select; ctx.confrimed = "false"; afterAuthenticated(ctx); } intermediate_iframe_close_callback() { //debugger; } onClickHandler() { console.log("Sign in with Google button clicked..."); /*showLoading("Google Account authentication in progress...");*/ } init() { window.onGoogleLibraryLoad = () => { if (location.host == "testftp.drivehq.com") { enableSSO = true; } google.accounts.id.initialize({ client_id: '656779963598-b7hgbegha876uf350rnmps1stv8gomkv.apps.googleusercontent.com', auto_select: false, itp_support: true, callback: this.handleCredentialResponse, intermediate_iframe_close_callback: this.intermediate_iframe_close_callback//, //native_callback: handleCredentialResponse_Native }); google.accounts.id.renderButton(document.getElementById("signinDiv"), { theme: 'outline', size: 'large', width: 220, text: "continue_with", click_listener: this.onClickHandler }); google.accounts.id.prompt(); }; } } class Apple extends AuthProvider { constructor() { super(); this.elementID = ".btn-continue-with-apple"; } init() { const scheme = window.location.protocol; const host = window.location.host; const baseUrl = `${scheme}//${host}`; const clientId = "com.cameraftp.viewer"; AppleID.auth.init({ clientId: clientId, scope: 'name email', redirectURI: baseUrl + '/api/secure/auth/applecallback.aspx', state: 'start', nonce: String(Date.now()), usePopup: true }); // Listen for authorization success. document.addEventListener('AppleIDSignInOnSuccess', (event) => { // Handle successful response. console.log(event.detail.authorization.code); console.log(event.detail.authorization.id_token); var ctx = new AuthContext(); ctx.source = "apple"; ctx.credential = event.detail.authorization.id_token; ctx.clientID = clientId; ctx.select = ""; ctx.confrimed = "false"; afterAuthenticated(ctx); }); // Listen for authorization failures. document.addEventListener('AppleIDSignInOnFailure', (event) => { // Handle error. console.log(event.detail.error); var err = event.detail.error; if (err.indexOf("popup_closed_by_user") < 0 && err.indexOf("user_trigger_new_signin_flow") < 0) { showErrorMsg(event.detail.error); } }); $(this.elementID).click(function () { AppleID.auth.signIn(); }); } hide() { $(this.elementID).hide(); } } function afterAuthenticated(ctx) { var src = ctx.source.charAt(0).toUpperCase() + ctx.source.slice(1); showLoading(src+" account authentication complete, signing in to CameraFTP account..."); var data = "source=" + ctx.source + "&credential=" + ctx.credential + "&clientID=" + ctx.clientID + "&select=" + ctx.select + "&confirm=" + ctx.confrimed; $.post("/api/secure/SSOlogonPublicProvider.aspx", data, function (data, status, xhr) { if (status === "success") { console.log("Data sent successfully:", data); var RETURN_STATUS = getAPIValue("RETURN_STATUS", data); var RETURN_STATUSDESCR = getAPIValue("RETURN_STATUSDESCR", data); var RETURN_SESSIONID = getAPIValue("RETURN_SESSIONID", data); var RETURN_USERTYPE = getAPIValue("RETURN_USERTYPE", data); hideLoading(); if (RETURN_STATUS == "0") { showLoading("Signed in to CameraFTP account successfully. Redirecting you in 5 seconds..."); location.href = "/Camera/CameraList.aspx"; } else if (RETURN_STATUS == "3") {//which need to confrim creating a new account. showConfirm(ctx); } else { if (RETURN_STATUSDESCR == "") RETURN_STATUSDESCR = "The API invocation failed. Please attempt again."; //error; if (ctx.select == "auto") { } else { showErrorMsg(RETURN_STATUSDESCR); } } } else { console.error("Error sending data:", xhr.statusText); } }).fail(function (jqXHR, textStatus, errorThrown) { showErrorMsg("The API invocation failed with status=" + textStatus + ", please try again later."); console.log("AJAX request failed: " + textStatus, errorThrown); }); } function showErrorMsg(msg) { var modaldia = $(".ssoLogonErrorBox"); modaldia.dialog({ // make the dialog one time. autoOpen: true, width: 400, modal: true, create: function (event, ui) { $(".ui-widget-header").hide();//hide header. $(".ssoLogonErrorTxt").text(msg); } }); $(".err-btn-close").click(function () { $(".ssoLogonErrorBox").dialog('close'); }); } function decodeJwtResponse(token) { var base64Url = token.split('.')[1]; var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); var jsonPayload = decodeURIComponent(window.atob(base64).split('').map(function (c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }).join('')); return JSON.parse(jsonPayload); } function showConfirm(ctx) { var secuityToken = ctx.credential; const responsePayload = decodeJwtResponse(secuityToken); console.log("ID: " + responsePayload.sub); console.log('Full Name: ' + responsePayload.name); console.log('Given Name: ' + responsePayload.given_name); console.log('Family Name: ' + responsePayload.family_name); console.log("Image URL: " + responsePayload.picture); console.log("Email: " + responsePayload.email); $(".spUser").text(responsePayload.name); $(".spEmail").text(responsePayload.email); const capitalizedString = ctx.source[0].toUpperCase() + ctx.source.slice(1); $(".srcProvider").text(capitalizedString); var modaldia = $(".ssoDialogModalConfirm");//ssoDialogModalConfirm modaldia.dialog({ // make the dialog one time. autoOpen: true, width: 400, modal: true, create: function (event, ui) { $(".ui-widget-header").hide();//hide header. //$(".ssoLogonErrorTxt").text(msg); } }); $(".ssoConfirmBtns_cancel").click(function () { $(".ssoDialogModalConfirm").dialog('close'); // handleCredentialResponse(CredentialResponse, "true"); }); $(".ssoConfirmBtns_agree").click(function () { $(".ssoDialogModalConfirm").dialog('close'); ctx.confrimed = "true"; afterAuthenticated(ctx); //handleCredentialResponse(CredentialResponse, "true"); }); } function getAPIValue(key, apiResult) { //const regex = "/" + key + "=([^<]*)/g"; const regex = new RegExp('' + key + '=([^<]*)'); // Match the string const match = apiResult.match(regex); if (match) { // Access the captured group (the value after "=") const value = match[1]; return value; } else { console.log("No match found"); } return ""; } function showLoading(msg) { updateLoading(msg); var modaldia = $("#ssoDialogModalCreate"); modaldia.dialog({ // make the dialog one time. autoOpen: true, width: 320, modal: true, create: function (event, ui) { $(".ui-widget-header").hide();//hide header. } }); } function updateLoading(msg) { $(".ssoDialogModalText").text(msg); } function hideLoading() { try { $("#ssoDialogModalCreate").dialog('close'); }catch{} } window.onload = function () { //alert("Window Loaded."); } $(function () { var googe = new Google(); var apple = new Apple(); googe.init(); apple.init(); if (location.host == "www.cameraftp.com") { //apple.hide(); } if ($(".ssoLogonContainer").length > 0) { return; } var ssoContainer = ""; ssoContainer += "
"; ssoContainer += ''; ssoContainer += ''; ssoContainer += ''; ssoContainer += ''; ssoContainer += ''; ssoContainer += ''; ssoContainer += "
"; $('body').append(ssoContainer); });