From 3575ed12a280b256d9abff763bf241a8443e6df4 Mon Sep 17 00:00:00 2001 From: Markin Igor <markin.io210@gmail.com> Date: Mon, 10 Dec 2018 19:14:56 +0300 Subject: [PATCH] Fix equality checks. --- javascript/src/iframe/viamapi-iframe.js | 95 +++++++++++-------------- 1 file changed, 43 insertions(+), 52 deletions(-) diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js index 87609f1..ae57de9 100644 --- a/javascript/src/iframe/viamapi-iframe.js +++ b/javascript/src/iframe/viamapi-iframe.js @@ -604,7 +604,7 @@ function createPassportCertificate(commonNameArg) { function createOneTimePassportCertificate(commonNameArg, emailArg, privateKeyIssuerArg, certicateIssuerArg) { var certData = null; - if(emailArg != null && emailArg != "") { + if(emailArg != null && emailArg !== "") { certData = { algorithms: { hashAlg: "SHA-256", @@ -694,11 +694,11 @@ function arrayBufferToBase64(buffer) { const newline = /\r\n|\r|\n/g; function capitalizeFirstLetter(string) { - if(string == "id") { + if(string === "id") { return "ID" } - if(string == "mime") { + if(string === "mime") { return "MIME"; } @@ -710,7 +710,7 @@ function capitalizeHeader(string) { tokens = string.split("-"); for(var i = 0; i < tokens.length; i++) { result += capitalizeFirstLetter(tokens[i]); - if(i != tokens.length - 1) { + if(i !== tokens.length - 1) { result += "-" } } @@ -1118,7 +1118,7 @@ var identityColors = ["#994392", "#cb0767", "#e51d31", "#ec671b", "#fab610"]; function getNextColor() { var colorIndex = localStorage.getItem("colorIndex"); - if (colorIndex == null || colorIndex == "") { + if (colorIndex == null || colorIndex === "") { colorIndex = 0 } @@ -1135,7 +1135,7 @@ function getNextColor() { function setKeyForUUID(uuid, key) { var storedIdentityForUuid = localStorage.getItem("keyperuuid/" + uuid); - if(storedIdentityForUuid != key && storedIdentityForUuid != null && storedIdentityForUuid != "") { + if(storedIdentityForUuid !== key && storedIdentityForUuid != null && storedIdentityForUuid !== "") { destroyIdentityFromLocalStorage(storedIdentityForUuid) } @@ -1145,7 +1145,7 @@ function setKeyForUUID(uuid, key) { function getColorForIdentity(key) { var storedColor = localStorage.getItem("colors/" + key); - if(storedColor == null || storedColor == "") { + if(storedColor == null || storedColor === "") { storedColor = getNextColor(); console.log("Setting new color: " + storedColor); localStorage.setItem("colors/" + key, storedColor) @@ -1155,16 +1155,15 @@ function getColorForIdentity(key) { } function setIdentityInLocalStorage(identityToStore, extendKey = true) { - //console.log(getStack()) var pinCode = identityToStore.pinCode; const serializedIdentity = JSON.stringify(identityToStore); const key = identityToStore.authentication.publicKey; - if(pinCode == null || pinCode == "") { + if(pinCode == null || pinCode === "") { pinCode = getPincode(key) } - if(pinCode == null || pinCode == "") { + if(pinCode == null || pinCode === "") { console.log("Can not set identity"); return null; } @@ -1174,7 +1173,7 @@ function setIdentityInLocalStorage(identityToStore, extendKey = true) { if(extendKey === true) { success = extendPinCodeTtl(key, pinCode) } - if (success == true) { + if (success === true) { localStorage.setItem(key, encryptedIdentity); let serializedIdentitiesList = localStorage.getItem("identities"); let identities = JSON.parse(serializedIdentitiesList); @@ -1191,7 +1190,7 @@ function getProfileData(identity) { return new Penpal.Promise(executeResultUpper => { executeRestfulFunction("private", viamApi, viamApi.identityGetIdentityProfileData).then(executeResult => { - if(executeResult.code == "200") { + if(executeResult.code === "200") { console.log("In promise"); console.log(executeResult); var listItem = {}; @@ -1226,7 +1225,7 @@ function getIdentityFromLocalStorage(key, pinCode, extendTtl = true) { //console.log(parsedIdentity) if(extendTtl === true) { var success = extendPinCodeTtl(key, pinCode); - if (success == true) { + if (success === true) { return parsedIdentity } else { console.log("Can not extend pincode ttl"); @@ -1248,7 +1247,7 @@ function listIdentitiesFromLocalStorage() { var profile = JSON.parse(JSON.stringify(localStorage.getItem("profiles/" + key))); console.log("Getting profile"); console.log(profile); - if(profile != null && profile != "") { + if(profile != null && profile !== "") { console.log("Setting profile for key: " + key); //console.log(profile) identitiesResult[key] = JSON.parse(profile) @@ -1265,14 +1264,6 @@ function listIdentitiesFromLocalStorage() { return identitiesResult } -function getStack() { - try { - throw new Error(); - } catch(e) { - return e.stack; - } -} - function extendPinCodeTtl(key, pinCode) { //console.log("Extending pincode ttl") //console.log(getStack()) @@ -1311,7 +1302,7 @@ function getPincode(key) { var now = new Date(); var nowMillis = now.getTime(); var ttl = window.sessionStorage.getItem("pincodettls/" + key); - if (ttl == null || ttl == "") { + if (ttl == null || ttl === "") { return null } else { if(nowMillis >= parseInt(ttl)) { @@ -1357,14 +1348,14 @@ window.currentlyLoadedIdentity = null; window.lastTimeGetProfile = 0; function executeRestfulFunction(type, that, fn, ...args) { - if(type == "private") { + if(type === "private") { return new Penpal.Promise(executeResult => { fn.apply(that, args).then((response) => { - if (response.data.code == "400" && response.data.status == "Bad session") { + if (response.data.code === "400" && response.data.status === "Bad session") { console.log("Trying to login again"); - if(currentlyAuthenticatedIdentity != "" && currentlyAuthenticatedIdentity != null) { + if(currentlyAuthenticatedIdentity !== "" && currentlyAuthenticatedIdentity != null) { viamApi.identityLogin("previousaddeddevice").then((response1) => { - if (response1.data.code == "200") { + if (response1.data.code === "200") { //console.log(response.data.data) var uuid = response1.data.data["Uuid"]; var token = response1.data.data["Session"]; @@ -1384,9 +1375,9 @@ function executeRestfulFunction(type, that, fn, ...args) { } }); } else { - if(currentlyLoadedIdentity != "" && currentlyLoadedIdentity != null) { + if(currentlyLoadedIdentity !== "" && currentlyLoadedIdentity != null) { viamApi.identityLogin("previousaddeddevice").then((response1) => { - if (response1.data.code == "200") { + if (response1.data.code === "200") { //console.log(response.data.data) var uuid = response1.data.data["Uuid"]; var token = response1.data.data["Session"]; @@ -1527,7 +1518,7 @@ function getCertificateForPassport(passportUUID, internal) { //console.log(certificate) //cryptoData.setx509Certificate(keys["certificate"]) executeRestfulFunction("private", viamApi, viamApi.signSignCertificate, btoa(certificate), passportUUID).then(executeResult => { - if(executeResult.code == "200") { + if(executeResult.code === "200") { var signedCertificate = atob(executeResult.data["SignedCertificate"]); //download("passportCertificateAfterSigning.crt", "text/plain", signedCertificate) var keyUUID = executeResult.data["CertificateUUID"]; @@ -1650,7 +1641,7 @@ const connection = Penpal.connectToParent({ getIdentityProfile(identityKey) { return new Penpal.Promise(result => { serializedProfile = localStorage.getItem("profiles/" + identityKey); - if(serializedProfile == null || serializedProfile == "") { + if(serializedProfile == null || serializedProfile === "") { result({"data" : "", "code" : "400", "status" : "Profile is empty" @@ -1862,7 +1853,7 @@ const connection = Penpal.connectToParent({ var success = extendPinCodeTtl(authenticationPublicKey); - if(success == false) { + if(success === false) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -1870,7 +1861,7 @@ const connection = Penpal.connectToParent({ } executeRestfulFunction("private", viamApi, viamApi.identityAddNewDevice).then(executeResult => { - if (executeResult.code == "200") { + if (executeResult.code === "200") { //console.log(response.data.data) var actionID = executeResult.data["ActionID"]; var QrCode = executeResult.data["QrCode"]; @@ -1903,7 +1894,7 @@ const connection = Penpal.connectToParent({ var success = extendPinCodeTtl(authenticationPublicKey); - if(success == false) { + if(success === false) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -1982,7 +1973,7 @@ const connection = Penpal.connectToParent({ var success = extendPinCodeTtl(authenticationPublicKey); - if(success == false) { + if(success === false) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -2019,7 +2010,7 @@ const connection = Penpal.connectToParent({ var success = extendPinCodeTtl(authenticationPublicKey); - if(success == false) { + if(success === false) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -2050,7 +2041,7 @@ const connection = Penpal.connectToParent({ var success = extendPinCodeTtl(authenticationPublicKey); - if(success == false) { + if(success === false) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -2059,7 +2050,7 @@ const connection = Penpal.connectToParent({ getCertificateForPassport(passportUUID, true).then(certificateResult => { //console.log(certificateResult) - if(certificateResult.code == "200") { + if(certificateResult.code === "200") { var passportCertificate = certificateResult.data["x509Certificate"]; var passportPrivateKey = certificateResult.data["privateKey"]; var passportChain = certificateResult.data["chain"]; @@ -2111,7 +2102,7 @@ const connection = Penpal.connectToParent({ var success = extendPinCodeTtl(authenticationPublicKey); - if(success == false) { + if(success === false) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -2121,7 +2112,7 @@ const connection = Penpal.connectToParent({ getCertificateForPassport(passportUUID, true).then(certificateResult => { console.log("Certificate for passport"); console.log(certificateResult); - if(certificateResult.code == "200") { + if(certificateResult.code === "200") { var passportCertificate = certificateResult.data["x509Certificate"]; var passportPrivateKey = certificateResult.data["privateKey"]; var passportChain = certificateResult.data["chain"]; @@ -2194,7 +2185,7 @@ const connection = Penpal.connectToParent({ var success = extendPinCodeTtl(authenticationPublicKey); - if(success == false) { + if(success === false) { result({"data" : "", "code" : "400", "status" : "Identity not authenticated" @@ -2341,9 +2332,9 @@ connection.promise.then(parent => { authenticationPublicKey = localStorage.getItem("authenticatedIdentity"); var pinCode = getPincode(authenticationPublicKey); - if(pinCode == "" || pinCode == null) { + if(pinCode === "" || pinCode == null) { loadIdentityInternal(authenticationPublicKey, "00000000").then(result => { - if(result.code != "200") { + if(result.code !== "200") { console.log(result); //var event = createEvent("CanNotLoadIdentity", "ErrorDuringLoadingIdentity", [authenticationPublicKey]) //parent.onEvent(event) @@ -2355,7 +2346,7 @@ connection.promise.then(parent => { //parent.onEvent(event) } else { loadIdentityInternal(authenticationPublicKey, pinCode).then(result => { - if(result.code != "200") { + if(result.code !== "200") { var event = createEvent("CanNotLoadIdentity", "ErrorDuringLoadingIdentity", [authenticationPublicKey]); parent.onEvent(event) } @@ -2451,10 +2442,10 @@ connection.promise.then(parent => { setInterval(function() { - if (currentlyLoadedIdentity != null && anynomousDeviceKeyEventsProcessing == false) { + if (currentlyLoadedIdentity != null && anynomousDeviceKeyEventsProcessing === false) { anynomousDeviceKeyEventsProcessing = true; executeRestfulFunction("public", viamAnonymousApi, viamAnonymousApi.eventGetNewEventsWithoutSession, "devicekey").then(executeResult => { - if(executeResult.code == "200") { + if(executeResult.code === "200") { var eventsLen = executeResult.data.length; changedMaxDeviceKeyAnonymousEventTime = false; for(var i = 0; i < eventsLen; i++) { @@ -2546,16 +2537,16 @@ connection.promise.then(parent => { console.log("Currently authenticated identity is null") }*/ - if (currentlyAuthenticatedIdentity != null && eventsDeviceEventsProcessing == false) { + if (currentlyAuthenticatedIdentity != null && eventsDeviceEventsProcessing === false) { eventsDeviceEventsProcessing = true; executeRestfulFunction("private", viamApi, viamApi.eventGetNewEvents, "devicekey").then(executeResult => { - if(executeResult.code == "200") { + if(executeResult.code === "200") { var eventsLen = executeResult.data.length; changedMaxDeviceKeyEventTime = false; for(var i = 0; i < eventsLen; i++) { var event = executeResult.data[i]; //console.log("Received event device key: " + event) - if(event.type == "QRCodeUpdated") { + if(event.type === "QRCodeUpdated") { var actionID = event["actionID"]; var QrCode = event["payloads"][1]; @@ -2586,15 +2577,15 @@ connection.promise.then(parent => { }); } - if (currentlyAuthenticatedIdentity != null && eventsEntityEventsProcessing == false) { + if (currentlyAuthenticatedIdentity != null && eventsEntityEventsProcessing === false) { eventsEntityEventsProcessing = true; executeRestfulFunction("private", viamApi, viamApi.eventGetNewEvents, "entity").then(executeResult => { - if(executeResult.code == "200") { + if(executeResult.code === "200") { var eventsLen = executeResult.data.length; changedMaxEntityEventTime = false; for(var i = 0; i < eventsLen; i++) { event = executeResult.data[i]; - if(event.type == "QRCodeUpdated") { + if(event.type === "QRCodeUpdated") { var actionID = event["actionID"]; var QrCode = event["payloads"][1]; -- GitLab