From 6473f6e40a3dd46d12b190d0378ccbcb39f9fd93 Mon Sep 17 00:00:00 2001
From: Markin Igor <markin.io210@gmail.com>
Date: Fri, 8 Feb 2019 12:05:16 +0300
Subject: [PATCH] Refactor window.authenticationPublicKey

---
 javascript/src/iframe/viamapi-iframe.js | 55 +++++++++++--------------
 1 file changed, 24 insertions(+), 31 deletions(-)

diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js
index 5e19250..d148247 100644
--- a/javascript/src/iframe/viamapi-iframe.js
+++ b/javascript/src/iframe/viamapi-iframe.js
@@ -1773,9 +1773,9 @@ const connection = Penpal.connectToParent({
     },
     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"
@@ -1814,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"
@@ -1843,8 +1843,8 @@ const connection = Penpal.connectToParent({
       });
     },
     logout() {
-      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"
@@ -1863,7 +1863,6 @@ const connection = Penpal.connectToParent({
           viamApi.setSessionData("", "");
           clearPinCodeTtl(authenticationPublicKey);
 
-          window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
           localStorage.removeItem("uuid");
           localStorage.removeItem("token");
           localStorage.removeItem("authenticatedIdentity");
@@ -1887,8 +1886,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"
@@ -1924,8 +1923,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"
@@ -1954,8 +1953,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"
@@ -2014,8 +2013,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"
@@ -2089,8 +2088,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"
@@ -2255,29 +2254,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);
         }
@@ -2312,13 +2309,9 @@ connection.promise.then(parent => {
           }
         })
       } else {
-        window.authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
+        const 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]);
@@ -2404,7 +2397,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");
-- 
GitLab