Skip to content
Snippets Groups Projects
viamapi-iframe.js 86.3 KiB
Newer Older
  • Learn to ignore specific revisions
  •   var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
      for (var i = 0; i < len; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    
      return text;
    }
    
    function CryptoData() {
    }
    
    CryptoData.prototype.set = function(obj) {
      for(var member in obj) {
        this[member] = JSON.parse(JSON.stringify(obj[member]))
      }
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.serialize = function() {
      return JSON.stringify(this)
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.deserialize = function(serialized) {
    
    Markin Igor's avatar
    Markin Igor committed
      var obj = JSON.parse(serialized);
    
      this.set(obj)
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.setPublicKey = function(publicKey) {
      this["publicKey"] = publicKey
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.getPublicKey = function() {
      return this["publicKey"]
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.setPrivateKey = function(privateKey) {
      this["privateKey"] = privateKey
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.getPrivateKey = function() {
      return this["privateKey"]
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.setx509Certificate = function(x509Certificate) {
      this["x509Certificate"] = x509Certificate
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.getx509Certificate = function() {
      return this["x509Certificate"]
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.setKeyUUID = function(keyUUID) {
      this["keyUUID"] = keyUUID
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.getKeyUUID = function() {
      return this["keyUUID"]
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.setChain = function(chain) {
      this["chain"] = chain
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    CryptoData.prototype.getChain = function() {
      return this["chain"]
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    function Identity() {
    }
    
    Identity.prototype.set = function(obj) {
      for(var member in obj) {
        this[member] = JSON.parse(JSON.stringify(obj[member]))
      }
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    Identity.prototype.serialize = function() {
      return JSON.stringify(this)
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    Identity.prototype.deserialize = function(serialized) {
    
    Markin Igor's avatar
    Markin Igor committed
      var obj = JSON.parse(serialized);
    
      this.set(obj)
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    Identity.prototype.setAuthentication = function(cryptoData) {
      this["authentication"] = cryptoData
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    Identity.prototype.getAuthentication = function() {
      return this["authentication"]
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    Identity.prototype.setPinCode = function(pinCode) {
      this["pinCode"] = pinCode
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    Identity.prototype.getPinCode = function() {
      return this["pinCode"]
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    Identity.prototype.setPassport = function(passportUUID, cryptoData) {
      if(this["passports"] === undefined || this["passports"] === null) {
        this["passports"] = {}
      }
    
      this["passports"][passportUUID] = cryptoData
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    
    Identity.prototype.getPassport = function(passportUUID) {
      if(this["passports"] === undefined || this["passports"] === null) {
        this["passports"] = {}
      }
    
      return this["passports"][passportUUID]
    
    Markin Igor's avatar
    Markin Igor committed
    };
    
    Markin Igor's avatar
    Markin Igor committed
    var identityColors = ["#994392", "#cb0767", "#e51d31", "#ec671b", "#fab610"];
    
    
    function getNextColor() {
      var colorIndex = localStorage.getItem("colorIndex");
    
    Markin Igor's avatar
    Markin Igor committed
      if (colorIndex == null || colorIndex === "") {
    
    Markin Igor's avatar
    Markin Igor committed
      var color = identityColors[colorIndex];
    
    Markin Igor's avatar
    Markin Igor committed
      colorIndex = colorIndex % identityColors.length;
    
    Markin Igor's avatar
    Markin Igor committed
      localStorage.setItem("colorIndex", colorIndex);
    
    
      return color
    }
    
    function setKeyForUUID(uuid, key) {
    
    Markin Igor's avatar
    Markin Igor committed
      var storedIdentityForUuid = localStorage.getItem("keyperuuid/" + uuid);
    
    Markin Igor's avatar
    Markin Igor committed
      if(storedIdentityForUuid !== key && storedIdentityForUuid != null && storedIdentityForUuid !== "") {
    
        destroyIdentityFromLocalStorage(storedIdentityForUuid)
      }
    
      localStorage.setItem("keyperuuid/" + uuid, key)
    }
    
    function getColorForIdentity(key) {
    
    Markin Igor's avatar
    Markin Igor committed
      var storedColor = localStorage.getItem("colors/" + key);
    
    Markin Igor's avatar
    Markin Igor committed
      if(storedColor == null || storedColor === "") {
    
    Markin Igor's avatar
    Markin Igor committed
        storedColor = getNextColor();
        console.log("Setting new color: " + storedColor);
    
        localStorage.setItem("colors/" + key, storedColor)
      }
    
      return storedColor
    }
    
    function setIdentityInLocalStorage(identityToStore, extendKey = true) {
      var pinCode = identityToStore.pinCode;
      const serializedIdentity = JSON.stringify(identityToStore);
      const key = identityToStore.authentication.publicKey;
    
    
    Markin Igor's avatar
    Markin Igor committed
      if(pinCode == null || pinCode === "") {
    
        pinCode = getPincode(key)
      }
    
    
    Markin Igor's avatar
    Markin Igor committed
      if(pinCode == null || pinCode === "") {
    
    Markin Igor's avatar
    Markin Igor committed
        console.log("Can not set identity");
    
        return null;
      }
    
      return encryptMessage(serializedIdentity, pinCode, "identity").then((encryptedIdentity) => {
    
    Markin Igor's avatar
    Markin Igor committed
        var success = true;
    
        if(extendKey === true) {
          success = extendPinCodeTtl(key, pinCode)
        }
    
    Markin Igor's avatar
    Markin Igor committed
        if (success === true) {
    
          localStorage.setItem(key, encryptedIdentity);
          let serializedIdentitiesList = localStorage.getItem("identities");
          let identities = JSON.parse(serializedIdentitiesList);
          identities[key] = true;
    
          localStorage.setItem("identities", JSON.stringify(identities))
        } else {
          console.log("Can not extend pincode ttl")
        }
      });
    }
    
    function getProfileData(identity) {
      return new Penpal.Promise(executeResultUpper => {
        executeRestfulFunction("private", viamApi,
          viamApi.identityGetIdentityProfileData).then(executeResult => {
    
    Markin Igor's avatar
    Markin Igor committed
          if(executeResult.code === "200") {
    
    Markin Igor's avatar
    Markin Igor committed
            console.log("In promise");
            console.log(executeResult);
    
            var listItem = {};
    
    
    Markin Igor's avatar
    Markin Igor committed
            console.log(identity);
            listItem.identityColor = getColorForIdentity(identity.authentication.publicKey);
            listItem.initials = executeResult.data.initials;
    
    
            if(listItem.initials === null || listItem.initials === "") {
              listItem.initials = "JD";
            }
    
    Markin Igor's avatar
    Markin Igor committed
            console.log("Item");
            console.log(listItem);
            localStorage.setItem("profiles/" + identity.authentication.publicKey, JSON.stringify(listItem));
    
            executeResultUpper(listItem)
          } else {
            executeResultUpper({})
          }
        });
      });
    }
    
    function getIdentityFromLocalStorage(key, pinCode, extendTtl = true) {
      const encryptedIdentity = localStorage.getItem(key);
      if (encryptedIdentity == null) {
    
    Markin Igor's avatar
    Markin Igor committed
        console.log("No such identity for public key");
    
        return Promise.resolve(null)
      }
      return decryptMessage(encryptedIdentity, pinCode).then((serializedIdentity) => {
        var parsedIdentity = JSON.parse(serializedIdentity);
    
    Markin Igor's avatar
    Markin Igor committed
        parsedIdentity["pinCode"] = "";
    
        //console.log(parsedIdentity)
        if(extendTtl === true) {
    
    Markin Igor's avatar
    Markin Igor committed
          var success = extendPinCodeTtl(key, pinCode);
    
    Markin Igor's avatar
    Markin Igor committed
          if (success === true) {
    
            return parsedIdentity
          } else {
    
    Markin Igor's avatar
    Markin Igor committed
            console.log("Can not extend pincode ttl");
    
            return null
          }
        } else {
          return parsedIdentity
        }
      });
    
    }
    
    function listIdentitiesFromLocalStorage() {
    
    Markin Igor's avatar
    Markin Igor committed
      var serializedIdentitiesList = localStorage.getItem("identities");
      var identities = JSON.parse(serializedIdentitiesList);
      var identitiesResult = {};
    
    
      for(var key in identities) {
    
    Markin Igor's avatar
    Markin Igor committed
        var profile = JSON.parse(JSON.stringify(localStorage.getItem("profiles/" + key)));
        console.log("Getting profile");
        console.log(profile);
    
    Markin Igor's avatar
    Markin Igor committed
        if(profile != null && profile !== "") {
    
    Markin Igor's avatar
    Markin Igor committed
          console.log("Setting profile for key: " + key);
    
          //console.log(profile)
          identitiesResult[key] = JSON.parse(profile)
          //console.log(identitiesResult)
        } else {
    
    Markin Igor's avatar
    Markin Igor committed
          console.log("Setting empty key for profile: " + key);
    
          identitiesResult[key] = {}
          //console.log(identitiesResult)
        }
      }
    
    
    Markin Igor's avatar
    Markin Igor committed
      console.log("In list identities from local storage");
      console.log(identitiesResult);
    
      return identitiesResult
    }
    
    
    function extendPinCodeTtl(key, pinCode) {
      //console.log("Extending pincode ttl")
      //console.log(getStack())
      //console.log("Extending pincode ttl for key: " + key)
      //console.log(pinCode)
      if(pinCode == null || pinCode == "") {
        var now = new Date();
        var nowMillis = now.getTime();
        var ttl = window.sessionStorage.getItem("pincodettls/" + key);
        if (ttl == null || ttl == "" || nowMillis >= parseInt(ttl)) {
    
    Markin Igor's avatar
    Markin Igor committed
          clearPinCodeTtl(key);
    
          var ttl = now.getTime() + 24 * 60 * 60 * 1000;
    
          window.sessionStorage.setItem("pincodettls/" + key, ttl);
        }
      } else {
        var now = new Date();
    
        var ttl = now.getTime() + 24 * 60 * 60 * 1000;
    
        window.sessionStorage.setItem("pincodettls/" + key, ttl);
        window.sessionStorage.setItem("pincodes/" + key, pinCode);
      }
    
      return true;
    }
    
    window.extendPinCodeTtl = extendPinCodeTtl;
    
    
    function clearPinCodeTtl(key) {
      //console.log("Clearing ttl for key: " + key)
    
    Markin Igor's avatar
    Markin Igor committed
      window.sessionStorage.removeItem("pincodettls/" + key);
    
      window.sessionStorage.removeItem("pincodes/" + key)
    }
    
    function getPincode(key) {
      var now = new Date();
      var nowMillis = now.getTime();
      var ttl = window.sessionStorage.getItem("pincodettls/" + key);
    
    Markin Igor's avatar
    Markin Igor committed
      if (ttl == null || ttl === "") {
    
        return null
      } else {
        if(nowMillis >= parseInt(ttl)) {
    
    Markin Igor's avatar
    Markin Igor committed
          clearPinCodeTtl(key);
    
          return null
        } else {
          return window.sessionStorage.getItem("pincodes/" + key);
        }
      }
    }
    
    function createEvent(actionId, type, payloads) {
      return {
        "actionID": actionId,
        "type": type,
        "stamp": new Date().getTime(),
        "payloads" : payloads
      }
    }
    
    function destroyIdentityFromLocalStorage(key) {
    
    Markin Igor's avatar
    Markin Igor committed
      localStorage.removeItem(key);
      localStorage.removeItem("profiles/" + key);
      localStorage.removeItem("colors/" + key);
    
    Markin Igor's avatar
    Markin Igor committed
      var serializedIdentitiesList = localStorage.getItem("identities");
    
    Markin Igor's avatar
    Markin Igor committed
      var identities = JSON.parse(serializedIdentitiesList);
    
    Markin Igor's avatar
    Markin Igor committed
      identities[key] = null;
    
    Markin Igor's avatar
    Markin Igor committed
      delete identities[key];
    
    
      localStorage.setItem("identities", JSON.stringify(identities))
    }
    
    
    Markin Igor's avatar
    Markin Igor committed
    window.loadedIdentities = {};
    
    window.wopiAPI = new WopiAPI();
    window.viamApi = new ViamAPI();
    window.viamAnonymousApi = new ViamAPI();
    
    Markin Igor's avatar
    Markin Igor committed
    window.currentlyAuthenticatedIdentity = null;
    window.currentlyLoadedIdentity = null;
    window.lastTimeGetProfile = 0;
    
    const handleIdentityLogin = (identity, uuid, token) => {
      const { loadedIdentities, viamApi } = window;
    
      viamApi.setSessionData(uuid, token);
      localStorage.setItem("uuid", uuid);
      localStorage.setItem("token", token);
      localStorage.setItem("authenticatedIdentity", identity.authentication.publicKey);
      window.currentlyAuthenticatedIdentity = loadedIdentities[identity.authentication.publicKey];
      window.lastTimeGetProfile = 0;
    
      setKeyForUUID(uuid, identity.authentication.publicKey);
    
    function executeRestfulFunction(type, that, fn, ...args) {
    
      const { currentlyAuthenticatedIdentity, viamApi, currentlyLoadedIdentity } = window;
    
      return new Penpal.Promise(executeResult => {
        fn.apply(that, args).then((response) => {
          const identity = currentlyAuthenticatedIdentity || currentlyLoadedIdentity;
    
          if (type === "private" && identity && response.data.code === "400" && response.data.status === "Bad session") {
            viamApi.identityLogin("previousaddeddevice")
              .then((response) => {
                if (response.data.code === "200") {
                  const uuid = response.data.data["Uuid"];
                  const token = response.data.data["Session"];
                  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 {
                  executeResult(response.data);
                }
              })
              .catch(console.warn);
          } else {
    
            executeResult(response.data);
    
    }
    
    window.executeRestfulFunction = executeRestfulFunction;
    
    
    function loadIdentityInternal(identityKey, pinCode) {
      return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
        console.log("Loading identity with pincode: " + pinCode);
    
        getIdentityFromLocalStorage(identityKey, pinCode).then((loadedIdentity) => {
          if (loadedIdentity == null) {
            result({
              "data": "",
              "code": "400",
              "status": "Can not load identity"
            })
          }
    
    Markin Igor's avatar
    Markin Igor committed
          var copiedIdentity = JSON.parse(JSON.stringify(loadedIdentity));
          loadedIdentities[identityKey] = loadedIdentity;
    
    
          if (identityKey === localStorage.getItem("authenticatedIdentity")) {
    
    Markin Igor's avatar
    Markin Igor committed
            currentlyAuthenticatedIdentity = copiedIdentity;
            viamApi.setIdentity(identityKey);
            var uuid = localStorage.getItem("uuid");
            var token = localStorage.getItem("token");
    
            //console.log("Loading " + uuid + " " + token)
            viamApi.setSessionData(uuid, token)
          }
    
          //console.log("Set loaded identity in load identity")
    
    Markin Igor's avatar
    Markin Igor committed
          currentlyLoadedIdentity = copiedIdentity;
          viamAnonymousApi.setIdentity(currentlyLoadedIdentity.authentication.publicKey);
    
    Markin Igor's avatar
    Markin Igor committed
          copiedIdentity.pinCode = "";
          copiedIdentity.authentication.privateKey = "";
    
    
          result({
            "data": copiedIdentity,
            "code": "200",
            "status": "Identity loaded"
          })
        }).catch((e) => {
          result({
            "data": "",
            "code": "400",
            "status": "Can not load entity:" + e
          })
        })
      });
    }
    
    function changeIdentityPinCodeInternal(key, oldPinCode, newPinCode) {
    
      return new Penpal.Promise(result => {
        getIdentityFromLocalStorage(key, oldPinCode, false).then((identity) => {
    
          identity.pinCode = newPinCode;
    
    
    Markin Igor's avatar
    Markin Igor committed
          console.log("Storing identity with pincode: " + identity.pinCode);
    
          setIdentityInLocalStorage(identity).then(() => {
    
            result({
              "data": "",
              "code": "200",
              "status": "Successfully changed pincode"
            });
    
          }).catch((e) => {
            result({
              "data": "",
              "code": "400",
              "status": "Cannot store identity " + e
            });
          });
    
        }).catch((e) => {
          result({
            "data": "",
            "code": "400",
            "status": "Cannot get identity " + e
          });
    
        });
      });
    }
    
    function getCertificateForPassport(passportUUID, internal) {
    
      return new Penpal.Promise(certificateResult => {
        if (currentlyAuthenticatedIdentity === null) {
          return {"data" : "",
            "code" : "400",
            "status" : "Identity not authenticated"
          }
        }
    
    
    Markin Igor's avatar
    Markin Igor committed
        var passportIdentity = new Identity();
        passportIdentity.set(currentlyAuthenticatedIdentity);
    
        //console.log("Getting: " + passportUUID)
        //console.log(identity)
    
    Markin Igor's avatar
    Markin Igor committed
        var passport = passportIdentity.getPassport(passportUUID);
    
        if(passport === undefined || passport === null) {
          createPassportCertificate(passportUUID).then(function(keys){
    
    Markin Igor's avatar
    Markin Igor committed
            var cryptoData = new CryptoData();
    
            //console.log(keys)
    
    Markin Igor's avatar
    Markin Igor committed
            cryptoData.setPublicKey(keys["publicKeyPEM"]);
            cryptoData.setPrivateKey(keys["privateKeyPEM"]);
            var certificate = keys["certificatePEM"];
    
            //download("passportCertificateBeforeSigning.crt", "text/plain", certificate)
            //console.log(certificate)
            //cryptoData.setx509Certificate(keys["certificate"])
            executeRestfulFunction("private", viamApi, viamApi.signSignCertificate, btoa(certificate), passportUUID).then(executeResult => {
    
    Markin Igor's avatar
    Markin Igor committed
              if(executeResult.code === "200") {
    
    Markin Igor's avatar
    Markin Igor committed
                var signedCertificate = atob(executeResult.data["SignedCertificate"]);
    
                //download("passportCertificateAfterSigning.crt", "text/plain", signedCertificate)
    
    Markin Igor's avatar
    Markin Igor committed
                var keyUUID = executeResult.data["CertificateUUID"];
                var encodedChain = executeResult.data["Chain"];
    
                //download("rootCertificate.crt", "text/plain", atob(encodedChain[0]))
    
    
    Markin Igor's avatar
    Markin Igor committed
                var chain = [];
    
    
                for(var i = 0; i < encodedChain.length; i++) {
                  chain.push(atob(encodedChain[i]))
                }
    
                //console.log("Chain from server")
                //console.log(chain)
                //console.log(signedCertificate)
                //console.log(certificate)
                //console.log(keyUUID)
    
    Markin Igor's avatar
    Markin Igor committed
                cryptoData.setx509Certificate(signedCertificate);
                cryptoData.setKeyUUID(keyUUID);
                cryptoData.setChain(chain);
    
    Markin Igor's avatar
    Markin Igor committed
                passportIdentity.setPassport(passportUUID, cryptoData);
    
    
                getProfileData(passportIdentity).then(executeResult1 => {
    
    Markin Igor's avatar
    Markin Igor committed
                  console.log("Profile updated in set identity");
    
                  setIdentityInLocalStorage(passportIdentity).then(() => {
    
    Markin Igor's avatar
    Markin Igor committed
                    currentlyAuthenticatedIdentity = passportIdentity;
    
                    lastTimeGetProfile = 0;
                    //console.log("Set loaded identity in passport");
    
    Markin Igor's avatar
    Markin Igor committed
                    currentlyLoadedIdentity = passportIdentity;
                    var copyOfCryptoData = JSON.parse(JSON.stringify(cryptoData));
    
    
                    if (internal === false) {
                      copyOfCryptoData["privateKey"] = ""
                    }
    
                    certificateResult({
                      "data": copyOfCryptoData,
                      "code": "200",
                      "status": "Certificate got"
                    });
                  }).catch((e) => {
                    certificateResult({
                      "data": "",
                      "code": "400",
                      "status": "Can not store certificate " + e
                    });
                  });
                });
              } else {
                certificateResult(executeResult)
              }
            });
          });
        } else {
          //console.log(passport)
    
    Markin Igor's avatar
    Markin Igor committed
          var copyOfCryptoData = JSON.parse(JSON.stringify(passport));
    
    
          if(internal === false) {
            copyOfCryptoData["privateKey"] = ""
          }
    
          certificateResult({"data" : copyOfCryptoData,
            "code" : "200",
            "status" : "Certificate got"
          });
        }
      });
    }
    
    const connection = Penpal.connectToParent({
      // Methods child is exposing to parent
      methods: {
    
        initializeApiHost: (apiUrl) => {
          window.API_HOST = apiUrl.charAt(apiUrl.length - 1) === "/" ? apiUrl : apiUrl + "/";
        },
    
        createIdentity(pinCode) {
          return new Penpal.Promise(result => {
            createPassportCertificate(makeid()).then(function(keys){
    
    Markin Igor's avatar
    Markin Igor committed
              var newIdentity = new Identity();
              var cryptoData = new CryptoData();
              cryptoData.setPublicKey(keys["publicKeyPEM"]);
              cryptoData.setPrivateKey(keys["privateKeyPEM"]);
              cryptoData.setx509Certificate(keys["certificatePEM"]);
              newIdentity.setAuthentication(cryptoData);
              newIdentity.setPinCode(pinCode);
    
    
              //console.log("Set loaded identity in createIdentity")
    
    Markin Igor's avatar
    Markin Igor committed
              currentlyLoadedIdentity = newIdentity;
              loadedIdentities[newIdentity.authentication.publicKey] = newIdentity;
              extendPinCodeTtl(newIdentity.authentication.publicKey, pinCode);
    
    Markin Igor's avatar
    Markin Igor committed
              viamAnonymousApi.setIdentity(newIdentity.authentication.publicKey);
    
    
              result({"data" : newIdentity,
                "code" : "200",
                "status" : "Identity created"
              })
    
          })
        },
        listIdentities() {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            var identities = listIdentitiesFromLocalStorage();
            console.log("Before return of identities");
            console.log(identities);
    
            result({"data" : identities,
              "code" : "200",
              "status" : "Identities listed"
            })
          });
        },
        loadIdentity(identityKey, pinCode) {
          return loadIdentityInternal(identityKey, pinCode)
        },
        changeIdentityPinCode(key, oldPinCode, newPinCode) {
          return changeIdentityPinCodeInternal(key, oldPinCode, newPinCode)
        },
        getIdentityProfile(identityKey) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            serializedProfile = localStorage.getItem("profiles/" + identityKey);
    
    Markin Igor's avatar
    Markin Igor committed
            if(serializedProfile == null || serializedProfile === "") {
    
              result({"data" : "",
                "code" : "400",
                "status" : "Profile is empty"
              });
            } else {
              result({"data" : JSON.parse(serializedProfile),
                "code" : "200",
                "status" : "Identities cleared"
              })
            }
          });
        },
        clearIdentities() {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            var identitiesTemp = listIdentitiesFromLocalStorage();
    
            //console.log(identitiesTemp.length)
            for(var i in identitiesTemp) {
              destroyIdentityFromLocalStorage(i)
            }
            result({"data" : "",
              "code" : "200",
              "status" : "Identities cleared"
            })
          });
        },
    
        confirmIdentificator(identity, confirmationCodeArg) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            viamApi.setIdentity(identity.authentication.publicKey);
    
    
            executeRestfulFunction("public", viamApi, viamApi.identityConfirmIdentificator,confirmationCodeArg).then(executeResult => {
              result(executeResult);
            });
          });
        },
        identityGetIdentificatorByRegisterToken(identity, tokenArg) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            viamApi.setIdentity(identity.authentication.publicKey);
    
    
            executeRestfulFunction("public", viamApi, viamApi.identityGetIdentificatorByRegisterToken,tokenArg).then(executeResult => {
              result(executeResult);
            });
          });
        },
        submitIdentificator(identity, identificatorArg, registerToken) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            viamApi.setIdentity(identity.authentication.publicKey);
    
    
            executeRestfulFunction("public", viamApi, viamApi.identitySubmitIdentificator,identificatorArg, registerToken).then(executeResult => {
              result(executeResult);
            });
          });
        },
        submitRegisterClaims(identity, givennameArg,familynameArg,emailArg,phonenumberArg) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            viamApi.setIdentity(identity.authentication.publicKey);
    
    
            executeRestfulFunction("public", viamApi, viamApi.identitySubmitRegisterClaims,givennameArg,familynameArg,emailArg,phonenumberArg).then(executeResult => {
              result(executeResult);
            });
          });
        },
        agreeOnRegistration(registerIdentity) {
    
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            viamApi.setIdentity(registerIdentity.authentication.publicKey);
    
            executeRestfulFunction("public", viamApi, viamApi.identityAgreeOnRegistration).then(executeResult => {
    
    Markin Igor's avatar
    Markin Igor committed
              console.log("Profile updated in set identity");
    
    Markin Igor's avatar
    Markin Igor committed
              let sequence = Promise.resolve();
    
              if (executeResult.code === "200") {
                sequence = sequence.then(() => {
                    setIdentityInLocalStorage(registerIdentity)
                  }
                )
              }
              sequence.then(() => {
                result(executeResult);
              }).catch((e) => {
                result({
                  "data": "",
                  "code": "400",
                  "status": "Can not store identity: " + e
                })
              })
            });
          });
        },
        resendConfirmationCode(identity, identificatorArg) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            viamApi.setIdentity(identity.authentication.publicKey);
    
    
            executeRestfulFunction("public", viamApi, viamApi.identityResendConfirmationCode,identificatorArg).then(executeResult => {
              result(executeResult);
            });
          });
        },
        login(loginIdentity, mode, code, actionID) {
          return new Penpal.Promise(result => {
            if (loadedIdentities[loginIdentity.authentication.publicKey] === null) {
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not loaded"
              })
            }
    
            //console.log("After loaded check")
    
    
    Markin Igor's avatar
    Markin Igor committed
            viamApi.setIdentity(loginIdentity.authentication.publicKey);
    
    
            executeRestfulFunction("public", viamApi, viamApi.identityLogin, mode, code, actionID).then(executeResult => {
              // console.log(executeResult)
              //console.log(mode)
    
                case "sms" : {
                  if (executeResult.code === "200") {
    
                    const uuid = executeResult.data["Uuid"];
                    const token = executeResult.data["Session"];
                    handleIdentityLogin(loginIdentity, uuid, token);
    
    Markin Igor's avatar
    Markin Igor committed
                    delete executeResult.data["Uuid"];
                    delete executeResult.data["Session"];
    
                    getProfileData(loginIdentity).then(executeResult1 => {
                      result(executeResult);
                    });
                  } else {
                    result(executeResult);
                  }
    
                  break;
                }
                case "previousaddeddevice" : {
                  if (executeResult.code === "200") {
    
                    const uuid = executeResult.data["Uuid"];
                    const token = executeResult.data["Session"];
                    handleIdentityLogin(loginIdentity, uuid, token);
    
    Markin Igor's avatar
    Markin Igor committed
                    delete executeResult.data["Uuid"];
                    delete executeResult.data["Session"];
    
                    getProfileData(loginIdentity).then(executeResult1 => {
                      result(executeResult);
                    });
                  } else {
                    result(executeResult);
                  }
    
                  break;
                }
    
                case "newdevice" : {
                  if (executeResult.code === "200") {
                    //console.log(executeResult)
    
    Markin Igor's avatar
    Markin Igor committed
                    var actionID = executeResult.data["ActionID"];
                    var QrCode = executeResult.data["QrCode"];
    
                    //console.log(uuid + " " + token)
                    QRCode.toDataURL(actionID + "," + QrCode, function (err, url) {
    
    Markin Igor's avatar
    Markin Igor committed
                      executeResult.data["image"] = url;
    
                      //console.log(executeResult)
                      result(executeResult);
                    })
                  } else {
                    //console.log(executeResult)
                    result(executeResult);
                  }
                  break;
                }
    
                default : {
                  result(executeResult);
                  break;
                }
              }
            });
          });
        },
        identityAddNewDevice() {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
    
    
            if (authenticationPublicKey === null) {
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not authenticated"
              })
            }
    
            if (loadedIdentities[authenticationPublicKey] === null) {
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not authenticated"
              })
            }
    
    
    Markin Igor's avatar
    Markin Igor committed
            var success = extendPinCodeTtl(authenticationPublicKey);
    
    Markin Igor's avatar
    Markin Igor committed
            if(success === false) {
    
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not authenticated"
              })
            }
    
            executeRestfulFunction("private", viamApi, viamApi.identityAddNewDevice).then(executeResult => {
    
    Markin Igor's avatar
    Markin Igor committed
              if (executeResult.code === "200") {
    
                //console.log(response.data.data)
    
    Markin Igor's avatar
    Markin Igor committed
                var actionID = executeResult.data["ActionID"];
                var QrCode = executeResult.data["QrCode"];
    
                //console.log(uuid + " " + token)
                QRCode.toDataURL(actionID + "," + QrCode, function (err, url) {
    
    Markin Igor's avatar
    Markin Igor committed
                  executeResult.data["image"] = url;
    
                  result(executeResult);
                })
              } else {
                result(executeResult);
              }
            });
          });
        },
        identityDestroyKeysForDevice(authenticationPublicKeyArg) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
    
            if (authenticationPublicKey === null) {
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not authenticated"
              })
            }
            if (loadedIdentities[authenticationPublicKey] === null) {
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not authenticated"
              })
            }
    
    
    Markin Igor's avatar
    Markin Igor committed
            var success = extendPinCodeTtl(authenticationPublicKey);
    
    Markin Igor's avatar
    Markin Igor committed
            if(success === false) {
    
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not authenticated"
              })
            }
    
            executeRestfulFunction("private", viamApi, viamApi.identityDestroyKeysForDevice, btoa(authenticationPublicKeyArg)).then(executeResult => {
              result(executeResult);
            });
          });
        },
        logout() {
    
    Markin Igor's avatar
    Markin Igor committed
          authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
    
          if (authenticationPublicKey === null) {
            return {"data" : "",
              "code" : "400",
              "status" : "Identity not loaded"
            }
          }
          if (loadedIdentities[authenticationPublicKey] === null) {
            return {"data" : "",
              "code" : "400",
              "status" : "Identity not loaded"
            }
          }
    
          return new Penpal.Promise(result => {
            executeRestfulFunction("private", viamApi, viamApi.identityLogout).then(executeResult => {
    
    Markin Igor's avatar
    Markin Igor committed
              viamApi.setIdentity("");
              viamApi.setSessionData("", "");
              clearPinCodeTtl(authenticationPublicKey);
    
              authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
              localStorage.removeItem("uuid");
              localStorage.removeItem("token");
              localStorage.removeItem("authenticatedIdentity");
              delete loadedIdentities[authenticationPublicKey];
    
              //console.log("Set loaded identity in logout")
    
    Markin Igor's avatar
    Markin Igor committed
              currentlyLoadedIdentity = null;
              currentlyAuthenticatedIdentity = null;
    
              lastTimeGetProfile = 0;
    
              result(executeResult);
            });
          });
        },
        identityRestoreAccess(restoreAccessIdentity, identificator) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            viamApi.setIdentity(restoreAccessIdentity.authentication.publicKey);
    
    
            executeRestfulFunction("public", viamApi, viamApi.identityRestoreAccess, identificator).then(executeResult => {
              if (executeResult.code === "200") {
    
    Markin Igor's avatar
    Markin Igor committed
                setIdentityInLocalStorage(restoreAccessIdentity);
    
                result(executeResult);
              } else {
                result(executeResult);
              }
            });
          });
        },
        getCurrentlyLoggedInUUID() {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
    
            if (authenticationPublicKey === null) {
              return {"data" : "",
                "code" : "400",
                "status" : "Identity not loaded"
              }
            }
            if (loadedIdentities[authenticationPublicKey] === null) {
              return {"data" : "",
                "code" : "400",
                "status" : "Identity not loaded"
              }
            }
    
    
    Markin Igor's avatar
    Markin Igor committed
            var success = extendPinCodeTtl(authenticationPublicKey);
    
    Markin Igor's avatar
    Markin Igor committed
            if(success === false) {
    
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not authenticated"
              })
            }
    
            if(localStorage.getItem("uuid") === null) {
              result({"data" : "",
                "code" : "400",
                "status" : "Not logged in UUID"
              })
            }
            result({"data" : localStorage.getItem("uuid"),
              "code" : "200",
              "status" : "UUID loaded"
            })
          });
        },
        getCertificateByPassport(passportUUID) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
    
            if (authenticationPublicKey === null) {
              return {"data" : "",
                "code" : "400",
                "status" : "Identity not loaded"
              }
            }
            if (loadedIdentities[authenticationPublicKey] === null) {
              return {"data" : "",
                "code" : "400",
                "status" : "Identity not loaded"
              }
            }
    
    
    Markin Igor's avatar
    Markin Igor committed
            var success = extendPinCodeTtl(authenticationPublicKey);
    
    Markin Igor's avatar
    Markin Igor committed
            if(success === false) {
    
              result({"data" : "",
                "code" : "400",
                "status" : "Identity not authenticated"
              })
            }
    
            getCertificateForPassport(passportUUID, false).then(certificateResult => {
              //console.log(certificateResult)
              result(certificateResult)
            })
          });
        },
        getOneTimeCertificateByPassport(passportUUID, emailArg) {
          return new Penpal.Promise(result => {
    
    Markin Igor's avatar
    Markin Igor committed
            authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
    
            if (authenticationPublicKey === null) {
              return {"data" : "",
                "code" : "400",
                "status" : "Identity not loaded"
              }
            }
            if (loadedIdentities[authenticationPublicKey] === null) {
              return {"data" : "",
                "code" : "400",
                "status" : "Identity not loaded"
              }
            }
    
    
    Markin Igor's avatar
    Markin Igor committed
            var success = extendPinCodeTtl(authenticationPublicKey);
    
    Markin Igor's avatar
    Markin Igor committed
            if(success === false) {
    
              result({"data" : "",
                "code" : "400",