diff --git a/javascript/src/constants.js b/javascript/src/constants.js new file mode 100644 index 0000000000000000000000000000000000000000..e00c27fa1dd9acd046795061a019a054a7e56af8 --- /dev/null +++ b/javascript/src/constants.js @@ -0,0 +1,5 @@ +export const LOGIN_MODES = { + SMS: 'sms', + PREVIOUSLY_ADDED_DEVICE: 'previousaddeddevice', + NEW_DEVICE: 'newdevice' +}; diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js index 6322955c6e84afc62d48234553c9e44e429f0326..981fa97de178948ebd5174d6d4792b7de911955a 100644 --- a/javascript/src/iframe/viamapi-iframe.js +++ b/javascript/src/iframe/viamapi-iframe.js @@ -1,4 +1,5 @@ import {createDeviceHash} from '../utilities/appUtility'; +import {LOGIN_MODES} from '../constants'; const libmime = require('libmime'); const QRCode = require('qrcode'); @@ -1382,24 +1383,17 @@ window.currentlyAuthenticatedIdentity = null; window.currentlyLoadedIdentity = null; window.lastTimeGetProfile = 0; -const handleIdentityLogin = async (identity, uuid, token) => { - try { - const { loadedIdentities, viamApi } = window; - const { publicKey } = identity.authentication; - - const deviceHash = await createDeviceHash(publicKey); - - viamApi.setSessionData(uuid, token, deviceHash); - localStorage.setItem("uuid", uuid); - localStorage.setItem("token", token); - localStorage.setItem("authenticatedIdentity", publicKey); - window.currentlyAuthenticatedIdentity = loadedIdentities[publicKey]; - window.lastTimeGetProfile = 0; - setKeyForUUID(uuid, publicKey); - } catch (error) { - console.warn(error); - } - +const handleIdentityLogin = (identity, uuid, token) => { + const { loadedIdentities, viamApi } = window; + const { publicKey } = identity.authentication; + + viamApi.setSessionData(uuid, token); + localStorage.setItem("uuid", uuid); + localStorage.setItem("token", token); + localStorage.setItem("authenticatedIdentity", publicKey); + window.currentlyAuthenticatedIdentity = loadedIdentities[publicKey]; + window.lastTimeGetProfile = 0; + setKeyForUUID(uuid, publicKey); }; function executeRestfulFunction(type, that, fn, ...args) { @@ -1411,11 +1405,11 @@ function executeRestfulFunction(type, that, fn, ...args) { if (type === "private" && identity && response.data.code === "400" && response.data.status === "Bad session") { viamApi.identityLogin("previousaddeddevice") - .then(async (response) => { + .then((response) => { if (response.data.code === "200") { const uuid = response.data.data["Uuid"]; const token = response.data.data["Session"]; - await handleIdentityLogin(identity, uuid, token); + handleIdentityLogin(identity, uuid, token); // TODO: Previously there was fn.apply(null, args) where null is probably wrong context for fn.apply() fn.apply(that, args).then(({data}) => executeResult(data)); } else { @@ -1448,11 +1442,12 @@ function loadIdentityInternal(identityKey, pinCode) { if (identityKey === localStorage.getItem("authenticatedIdentity")) { window.currentlyAuthenticatedIdentity = copiedIdentity; - window.viamApi.setIdentity(identityKey); const uuid = localStorage.getItem("uuid"); const token = localStorage.getItem("token"); const deviceHash = await createDeviceHash(identityKey); - window.viamApi.setSessionData(uuid, token, deviceHash); + window.viamApi.setIdentity(identityKey); + window.viamApi.setDeviceHash(deviceHash); + window.viamApi.setSessionData(uuid, token); } window.currentlyLoadedIdentity = copiedIdentity; @@ -1735,83 +1730,52 @@ const connection = Penpal.connectToParent({ }); }); }, - login(loginIdentity, mode, code, actionID) { - return new Penpal.Promise(async setIdentityResult => { - if (window.loadedIdentities[loginIdentity.authentication.publicKey] === null) { - setIdentityResult({"data" : "", - "code" : "400", - "status" : "Identity not loaded" - }) - } - - const deviceHash = await createDeviceHash(loginIdentity.authentication.publicKey); - window.viamApi.setSessionData('', '', deviceHash); - window.viamApi.setIdentity(loginIdentity.authentication.publicKey); - - executeRestfulFunction("public", viamApi, viamApi.identityLogin, mode, code, actionID).then(async executeResult => { - switch (mode) { - case "sms" : { - if (executeResult.code === "200") { - const uuid = executeResult.data["Uuid"]; - const token = executeResult.data["Session"]; - await handleIdentityLogin(loginIdentity, uuid, token); - delete executeResult.data["Uuid"]; - delete executeResult.data["Session"]; - getProfileData(loginIdentity).then(executeResult1 => { - setIdentityInLocalStorage(loginIdentity).then(() => { - setIdentityResult(executeResult); - }); - }); - } else { - setIdentityResult(executeResult); - } - - break; - } - case "previousaddeddevice" : { - if (executeResult.code === "200") { - const uuid = executeResult.data["Uuid"]; - const token = executeResult.data["Session"]; - await handleIdentityLogin(loginIdentity, uuid, token); - delete executeResult.data["Uuid"]; - delete executeResult.data["Session"]; - getProfileData(loginIdentity).then(executeResult1 => { - setIdentityResult(executeResult); - }); - } else { - setIdentityResult(executeResult); - } - - break; - } - - case "newdevice" : { - if (executeResult.code === "200") { - var actionID = executeResult.data["ActionID"]; - var QrCode = executeResult.data["QrCode"]; - QRCode.toDataURL(actionID + "," + QrCode, function (err, url) { - executeResult.data["image"] = url; - setIdentityResult(executeResult); - }) - } else { - setIdentityResult(executeResult); - } - break; - } + login: async (loginIdentity, mode, requestCode, requestActionID) => { + if (!window.loadedIdentities[loginIdentity.authentication.publicKey]) { + return { + data: "", + code: "400", + status: "Identity not loaded" + }; + } - default : { - setIdentityResult(executeResult); - break; - } + const deviceHash = await createDeviceHash(loginIdentity.authentication.publicKey); + window.viamApi.setDeviceHash(deviceHash); + window.viamApi.setIdentity(loginIdentity.authentication.publicKey); + + const identityLoginResponse = + await executeRestfulFunction( + "public", + window.viamApi, + window.viamApi.identityLogin, + mode, requestCode, + requestActionID + ); + + const { code, data } = identityLoginResponse; + const responseToClient = Object.assign({}, identityLoginResponse); + + if (code === "200") { + if (mode === LOGIN_MODES.SMS || mode === LOGIN_MODES.PREVIOUSLY_ADDED_DEVICE) { + handleIdentityLogin(loginIdentity, data.Uuid, data.Session); + await getProfileData(loginIdentity); + + if (mode === LOGIN_MODES.SMS) { + await setIdentityInLocalStorage(loginIdentity); } - }); - }); + } else if (mode === LOGIN_MODES.NEW_DEVICE) { + const dataUrl = await QRCode.toDataURL(`${data.ActionID},${data.QrCode}`); + Object.assign(responseToClient.data, { image: dataUrl }); + } + } + + return responseToClient; }, identityAddNewDevice() { return new Penpal.Promise(result => { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (window.authenticationPublicKey === null) { + if (authenticationPublicKey === null) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -1850,8 +1814,8 @@ const connection = Penpal.connectToParent({ }, identityDestroyKeysForDevice(authenticationPublicKeyArg) { return new Penpal.Promise(result => { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (window.authenticationPublicKey === null) { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + if (authenticationPublicKey === null) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -1878,39 +1842,35 @@ const connection = Penpal.connectToParent({ }); }); }, - logout() { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (window.authenticationPublicKey === null) { - return {"data" : "", - "code" : "400", - "status" : "Identity not loaded" - } - } - if (window.loadedIdentities[authenticationPublicKey] === null) { - return {"data" : "", - "code" : "400", - "status" : "Identity not loaded" - } + logout: async () => { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + if (!authenticationPublicKey || !window.loadedIdentities[authenticationPublicKey]) { + return { + data: "", + code: "400", + status: "Identity not loaded" + }; } - return new Penpal.Promise(result => { - executeRestfulFunction("private", viamApi, viamApi.identityLogout).then(executeResult => { - viamApi.setIdentity(""); - viamApi.setSessionData("", "", "", ""); - clearPinCodeTtl(authenticationPublicKey); - - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - localStorage.removeItem("uuid"); - localStorage.removeItem("token"); - localStorage.removeItem("authenticatedIdentity"); - delete window.loadedIdentities[authenticationPublicKey]; - window.currentlyLoadedIdentity = null; - window.currentlyAuthenticatedIdentity = null; - window.lastTimeGetProfile = 0; + const identityLogoutResponse = await executeRestfulFunction( + "private", + window.viamApi, + window.viamApi.identityLogout + ); - result(executeResult); - }); - }); + window.viamApi.setIdentity(""); + window.viamApi.setSessionData("", ""); + clearPinCodeTtl(authenticationPublicKey); + + localStorage.removeItem("uuid"); + localStorage.removeItem("token"); + localStorage.removeItem("authenticatedIdentity"); + delete window.loadedIdentities[authenticationPublicKey]; + window.currentlyLoadedIdentity = null; + window.currentlyAuthenticatedIdentity = null; + window.lastTimeGetProfile = 0; + + return identityLogoutResponse; }, identityRestoreAccess(restoreAccessIdentity, identificator) { return new Penpal.Promise(result => { @@ -1923,8 +1883,8 @@ const connection = Penpal.connectToParent({ }, getCurrentlyLoggedInUUID() { return new Penpal.Promise(result => { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (window.authenticationPublicKey === null) { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + if (authenticationPublicKey === null) { return {"data" : "", "code" : "400", "status" : "Identity not loaded" @@ -1960,8 +1920,8 @@ const connection = Penpal.connectToParent({ }, getCertificateByPassport(passportUUID) { return new Penpal.Promise(result => { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (window.authenticationPublicKey === null) { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + if (authenticationPublicKey === null) { return {"data" : "", "code" : "400", "status" : "Identity not loaded" @@ -1990,8 +1950,8 @@ const connection = Penpal.connectToParent({ }, getOneTimeCertificateByPassport(passportUUID, emailArg) { return new Penpal.Promise(result => { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (window.authenticationPublicKey === null) { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + if (authenticationPublicKey === null) { return {"data" : "", "code" : "400", "status" : "Identity not loaded" @@ -2050,8 +2010,8 @@ const connection = Penpal.connectToParent({ }, signEmail(passportUUID, emailArg, emailMessage) { return new Penpal.Promise(result => { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (window.authenticationPublicKey === null) { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + if (authenticationPublicKey === null) { return {"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -2125,8 +2085,8 @@ const connection = Penpal.connectToParent({ }, hasSession() { return new Penpal.Promise(result => { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - if (window.authenticationPublicKey === null) { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + if (authenticationPublicKey === null) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -2159,7 +2119,7 @@ const connection = Penpal.connectToParent({ executeRestfulFunction("public", viamApi, viamApi.marketingSignUpIdentificator, identificator, reference).then(executeResult => { viamApi.setIdentity(""); - viamApi.setSessionData("", "", "", ""); + viamApi.setSessionData("", ""); result(executeResult); }); }); @@ -2170,7 +2130,7 @@ const connection = Penpal.connectToParent({ executeRestfulFunction("public", viamApi, viamApi.marketingGetIdentificatorProfile, identificator, pincode).then(executeResult => { viamApi.setIdentity(""); - viamApi.setSessionData("", "", "", ""); + viamApi.setSessionData("", ""); result(executeResult); }); }); @@ -2181,7 +2141,7 @@ const connection = Penpal.connectToParent({ executeRestfulFunction("public", viamApi, viamApi.marketingExecuteEventForIdentificator, identificator, pincode, event).then(executeResult => { viamApi.setIdentity(""); - viamApi.setSessionData("", "", "", ""); + viamApi.setSessionData("", ""); result(executeResult); }); }); @@ -2291,29 +2251,27 @@ connection.promise.then(parent => { localStorage.removeItem("token"); localStorage.removeItem("authenticatedIdentity"); } else { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - const pinCode = getPincode(window.authenticationPublicKey); + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + const pinCode = getPincode(authenticationPublicKey); if (pinCode === "" || pinCode === null) { - loadIdentityInternal(window.authenticationPublicKey, "00000000").then(result => { + loadIdentityInternal(authenticationPublicKey, "00000000").then(result => { if (result.code !== "200") { - //var event = createEvent("CanNotLoadIdentity", "ErrorDuringLoadingIdentity", [authenticationPublicKey]) - //parent.onEvent(event) const event = createEvent( "CanNotGetPincodeForAuthenticatedIdentity", "IdentityNotLoaded", - [window.authenticationPublicKey] + [authenticationPublicKey] ); parent.onEvent(event); } }); } else { - loadIdentityInternal(window.authenticationPublicKey, pinCode).then(result => { + loadIdentityInternal(authenticationPublicKey, pinCode).then(result => { if (result.code !== "200") { const event = createEvent( "CanNotLoadIdentity", "ErrorDuringLoadingIdentity", - [window.authenticationPublicKey] + [authenticationPublicKey] ); parent.onEvent(event); } @@ -2333,36 +2291,31 @@ connection.promise.then(parent => { var identityLoadedEvent = false; var identityAuthenticatedEvent = false; - setInterval(function() { - if(window.currentlyAuthenticatedIdentity != null) { - var pinCode = getPincode(window.currentlyAuthenticatedIdentity.authentication.publicKey); - if(pinCode != null && pinCode !== "") { - getIdentityFromLocalStorage(window.currentlyAuthenticatedIdentity.authentication.publicKey, - pinCode, false).then((gotIdentity) => { - window.currentlyAuthenticatedIdentity = gotIdentity; - window.currentlyLoadedIdentity = gotIdentity; - if(identityAuthenticatedEvent === false && gotIdentity != null) { - var event = createEvent("IdentityAuthenticated", "Authenticated", [gotIdentity.authentication.publicKey]); + setInterval(async function () { + if (window.currentlyAuthenticatedIdentity) { + const { authentication } = window.currentlyAuthenticatedIdentity; + const pinCode = getPincode(authentication.publicKey); + if (pinCode) { + const identity = await getIdentityFromLocalStorage(authentication.publicKey, pinCode, false); + + window.currentlyLoadedIdentity = identity; + + if (!identityAuthenticatedEvent && identity) { + const event = createEvent("IdentityAuthenticated", "Authenticated", [identity.authentication.publicKey]); + parent.onEvent(event); + identityAuthenticatedEvent = true; + } + } else { + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + + if (authenticationPublicKey) { + const result = await loadIdentityInternal(authenticationPublicKey, "00000000"); + if (result.code !== "200") { + const event = createEvent("CanNotGetPincodeForAuthenticatedIdentity", "IdentityNotLoaded", [authenticationPublicKey]); parent.onEvent(event); - identityAuthenticatedEvent = true + clearPinCodeTtl(authenticationPublicKey); + window.currentlyAuthenticatedIdentity = null; } - }) - } else { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); - - if(authenticationPublicKey != null && authenticationPublicKey !== "") { - /*var event = createEvent("CanNotLoadPincodeForAuthenticatedIdentity", "IdentityNotLoaded", [window.currentlyAuthenticatedIdentity.authentication.publicKey]) - parent.onEvent(event) - clearPinCodeTtl(authenticationPublicKey) - window.currentlyAuthenticatedIdentity = null*/ - loadIdentityInternal(authenticationPublicKey, "00000000").then(result => { - if(result.code !== "200") { - var event = createEvent("CanNotGetPincodeForAuthenticatedIdentity", "IdentityNotLoaded", [authenticationPublicKey]); - parent.onEvent(event); - clearPinCodeTtl(authenticationPublicKey); - window.currentlyAuthenticatedIdentity = null; - } - }); } identityAuthenticatedEvent = false; @@ -2370,40 +2323,33 @@ connection.promise.then(parent => { } } - if(window.currentlyLoadedIdentity != null) { - var pinCode = getPincode(window.currentlyLoadedIdentity.authentication.publicKey); - if(pinCode === "" || pinCode == null) { - if(identityLoadedEvent === false) { - /*var event = createEvent("CanNotLoadPincodeForLoadedIdentity", "IdentityNotLoaded", [window.currentlyLoadedIdentity.authentication.publicKey]) - parent.onEvent(event) - identityLoadedEvent = true*/ - loadIdentityInternal(window.currentlyLoadedIdentity.authentication.publicKey, "00000000").then(result => { - if(result.code !== "200") { - var event = createEvent("CanNotLoadPincodeForLoadedIdentity", "IdentityNotLoaded", [window.currentlyLoadedIdentity.authentication.publicKey]); - parent.onEvent(event); - identityLoadedEvent = true - } - }); + if (window.currentlyLoadedIdentity) { + const pinCode = getPincode(window.currentlyLoadedIdentity.authentication.publicKey); + if (!pinCode) { + if (!identityLoadedEvent) { + const result = await loadIdentityInternal(window.currentlyLoadedIdentity.authentication.publicKey, "00000000"); + if (result.code !== "200") { + const event = createEvent("CanNotLoadPincodeForLoadedIdentity", "IdentityNotLoaded", [window.currentlyLoadedIdentity.authentication.publicKey]); + parent.onEvent(event); + identityLoadedEvent = true; + } } } else { - identityLoadedEvent = false + identityLoadedEvent = false; } } - - if (window.currentlyAuthenticatedIdentity != null) { - var now = new Date().getTime(); - if(now - window.lastTimeGetProfile > 30000) { - var identityToStore = window.currentlyAuthenticatedIdentity; - getProfileData(identityToStore); + if (window.currentlyAuthenticatedIdentity) { + const now = new Date().getTime(); + if (now - window.lastTimeGetProfile > 30000) { + getProfileData(window.currentlyAuthenticatedIdentity); window.lastTimeGetProfile = now; } } }, 50); setInterval(function() { - - if (window.currentlyLoadedIdentity != null && anynomousDeviceKeyEventsProcessing === false) { + if (window.currentlyLoadedIdentity && !anynomousDeviceKeyEventsProcessing && !window.currentlyAuthenticatedIdentity) { anynomousDeviceKeyEventsProcessing = true; executeRestfulFunction("public", viamAnonymousApi, viamAnonymousApi.eventGetNewEventsWithoutSession, "devicekey").then(async executeResult => { if(executeResult.code === "200") { @@ -2415,7 +2361,7 @@ connection.promise.then(parent => { case "Authenticated" : { const uuid = event.payloads[0]; const token = event.payloads[1]; - await handleIdentityLogin(window.currentlyLoadedIdentity, uuid, token); + handleIdentityLogin(window.currentlyLoadedIdentity, uuid, token); const identityToStore = window.currentlyAuthenticatedIdentity; event.payloads = [{fromQRCode: true}]; setIdentityInLocalStorage(identityToStore).then(() => { @@ -2440,7 +2386,7 @@ connection.promise.then(parent => { } case "KeyDeleted" : { - window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); + const authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); clearPinCodeTtl(authenticationPublicKey); localStorage.removeItem("uuid"); localStorage.removeItem("token"); diff --git a/main.go b/main.go index 09206ad80e6d91fa5f1896ff9009a3a17ad4bb7a..f688ea9944a2a2475ec20e9ca8bb7fe7334b6ff5 100644 --- a/main.go +++ b/main.go @@ -79,9 +79,12 @@ func buildViamAPI() string { "}\n\n" - result += "ViamAPI.prototype.setSessionData = function(uuid, token, deviceHash) {\n" + + result += "ViamAPI.prototype.setSessionData = function(uuid, token) {\n" + " this.config.headers.uuid = uuid;\n" + " this.config.headers.token = token;\n" + + "};\n\n" + + result += "ViamAPI.prototype.setDeviceHash = function(deviceHash) {\n" + " this.config.headers.deviceHash = deviceHash;\n" + "};\n\n"