Skip to content
Snippets Groups Projects
appUtility.js 3.62 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alexey Lunin's avatar
    Alexey Lunin committed
    import { getCrypto } from "pkijs";
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    export const createDeviceHash = async publicKey => {
    
      try {
        const stringToEncode = publicKey + navigator.userAgent;
        const crypto = getCrypto();
    
        const buffer = new window.TextEncoder().encode(stringToEncode);
        const hash = await crypto.digest({ name: "SHA-1" }, buffer);
        return window.btoa(String.fromCharCode(...new Uint8Array(hash)));
    
      } catch (error) {
        console.warn(error); // eslint-disable-line no-console
      }
    };
    
    
    export const encodeResponse = (code, data, status) => {
      return {
        code,
        data,
        status
      };
    
    };
    
    export function makeid(len) {
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      if (typeof len === "undefined") {
        len = 10;
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      var possible =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    
    
      for (var i = 0; i < len; i++)
        text += possible.charAt(Math.floor(Math.random() * possible.length));
    
      return text;
    }
    
    //*********************************************************************************
    function getBlockFinishTimeInLocalStorage() {
      return localStorage.getItem("blockFinishTime") || getCurrentTime();
    }
    
    function getCurrentTime() {
      return Math.floor(new Date().getTime() / 1000);
    }
    
    export function getTimeLeftInLocalStorage() {
      const blockFinishTime = getBlockFinishTimeInLocalStorage();
      const timeNow = getCurrentTime();
      const seconds = (blockFinishTime - timeNow) % 60;
      let minutes = Math.floor((blockFinishTime - timeNow) / 60);
      minutes %= 60;
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      const left =
        "Your identity has been locked. Try again in " +
        minutes +
        " minutes and " +
        seconds +
        " seconds.";
    
      return left;
    }
    
    export function listIdentitiesFromLocalStorage() {
      var serializedIdentitiesList = localStorage.getItem("identities");
      var identities = JSON.parse(serializedIdentitiesList);
      var identitiesResult = {};
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      for (var key in identities) {
        var profile = JSON.parse(
          JSON.stringify(localStorage.getItem("profiles/" + key))
        );
        if (profile != null && profile !== "") {
          identitiesResult[key] = JSON.parse(profile);
    
    Alexey Lunin's avatar
    Alexey Lunin committed
          identitiesResult[key] = {};
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      return identitiesResult;
    
    }
    
    export function destroyIdentityFromLocalStorage(key) {
      localStorage.removeItem(key);
      localStorage.removeItem("profiles/" + key);
      localStorage.removeItem("colors/" + key);
    
      var serializedIdentitiesList = localStorage.getItem("identities");
    
      var identities = JSON.parse(serializedIdentitiesList);
    
      identities[key] = null;
    
      delete identities[key];
    
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      localStorage.setItem("identities", JSON.stringify(identities));
    
    }
    
    export function failPincodeAttempt(password) {
      let message = "Wrong pincode";
    
    Alexey Lunin's avatar
    Alexey Lunin committed
      if (password !== "00000000") {
    
        let attempt = localStorage.getItem("attempt") || 1;
        attempt = parseInt(attempt);
        if (attempt === 9) {
          const identitiesTemp = listIdentitiesFromLocalStorage();
          for (let i in identitiesTemp) {
            destroyIdentityFromLocalStorage(i);
          }
          message = "9 failed attempts. Identity is revoked!";
          localStorage.removeItem("attempt");
        } else if (attempt % 3 === 0) {
          const timeNow = getCurrentTime();
          const blockFinishTime = timeNow + 300;
          localStorage.setItem("blockFinishTime", blockFinishTime);
          localStorage.setItem("attempt", attempt + 1);
          message = "3 failed attempts. Identity is locked!";
        } else {
          localStorage.setItem("attempt", attempt + 1);
        }
      }
      return message;
    }
    
    export function canTryPincode() {
      const timeNow = getCurrentTime();
      const blockFinishTime = getBlockFinishTimeInLocalStorage();
      if (blockFinishTime <= timeNow) {
        localStorage.removeItem("blockFinishTime");
        return true;
      } else {
        return false;
      }
    
    Alexey Lunin's avatar
    Alexey Lunin committed
    }