Skip to content
Snippets Groups Projects
viamapi-iframe.js 72 KiB
Newer Older
Sasha Ilieva's avatar
Sasha Ilieva committed
import "../lib/textencoder.polyfill";
import { parseSMIME, prepareVCardParts } from "../utilities/emailUtilities";
import {
  stringToUtf8ByteArray,
  utf8ByteArrayToString,
  stringToUtf8Base64,
  utf8Base64ToString,
  base64ToByteArray,
  byteArrayToBase64
Alexey Lunin's avatar
Alexey Lunin committed
} from "../utilities/stringUtilities";
import { extractMessageID } from "../helpers/mailparser";
Alexey Lunin's avatar
Alexey Lunin committed
const Penpal = require("penpal").default;
import {
  createDeviceHash,
  destroyIdentityFromLocalStorage,
  encodeResponse,
Alexey Lunin's avatar
Alexey Lunin committed
  listIdentitiesFromLocalStorage,
  makeid
} from "../utilities/appUtility";
import { LOGIN_MODES } from "../constants/authentication";
Damyan Mitev's avatar
Damyan Mitev committed
  ImageData,
  createOneTimePassportCertificate,
  createPassportCertificate,
  decryptMessage,
  encryptMessage,
  parseCertificate,
  signEmail,
  verifySMIME
Alexey Lunin's avatar
Alexey Lunin committed
} from "../utilities/signingUtilities";
import { signPdf } from "../utilities/pdfUtilities";
import CryptoData from "../CryptoData";
import Identity from "../Identity";
import {
  STATUS_DEVICE_REVOKED,
Sasha Ilieva's avatar
Sasha Ilieva committed
  STATUS_USER_NOT_ACTIVATED,
  STATUS_USER_BLOCKED
} from "../constants/statuses";
import generateQrCode from "../utilities/generateQrCode";
import {
  generateRecoveryKey,
  divideSecretToShares,
  combineSecret,
  encryptShare
} from "../utilities/secrets";
Alexey Lunin's avatar
Alexey Lunin committed

const penpalMethods = require("../../temp/penpal-methods").default;
const WopiAPI = require("./wopiapi-iframe");
const CollaboraAPI = require("./collaboraapi-iframe");
const ViamAPI = require("../../temp/viamapi");
Zdravko Iliev's avatar
Zdravko Iliev committed
const identityColors = ["#994392", "#cb0767", "#e51d31", "#ec671b", "#fab610"];

function getNextColor() {
Alexey Lunin's avatar
Alexey Lunin committed
  let colorIndex = localStorage.getItem("colorIndex");
Markin Igor's avatar
Markin Igor committed
  if (colorIndex == null || colorIndex === "") {
Alexey Lunin's avatar
Alexey Lunin committed
    colorIndex = 0;
Zdravko Iliev's avatar
Zdravko Iliev committed
  const 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);
Alexey Lunin's avatar
Alexey Lunin committed
  return color;
}

function setKeyForUUID(uuid, key) {
Zdravko Iliev's avatar
Zdravko Iliev committed
  const storedIdentityForUuid = localStorage.getItem("keyperuuid/" + uuid);
Alexey Lunin's avatar
Alexey Lunin committed
  if (
    storedIdentityForUuid !== key &&
    storedIdentityForUuid != null &&
    storedIdentityForUuid !== ""
  ) {
    destroyIdentityFromLocalStorage(storedIdentityForUuid);
Alexey Lunin's avatar
Alexey Lunin committed
  localStorage.setItem("keyperuuid/" + uuid, key);
}

function getColorForIdentity(key) {
Alexey Lunin's avatar
Alexey Lunin committed
  let storedColor = localStorage.getItem("colors/" + key);
Alexey Lunin's avatar
Alexey Lunin committed
  if (storedColor == null || storedColor === "") {
Markin Igor's avatar
Markin Igor committed
    storedColor = getNextColor();
Alexey Lunin's avatar
Alexey Lunin committed
    localStorage.setItem("colors/" + key, storedColor);
Alexey Lunin's avatar
Alexey Lunin committed
  return storedColor;
}

function setIdentityInLocalStorage(identityToStore, extendKey = true) {
Alexey Lunin's avatar
Alexey Lunin committed
  let pinCode = identityToStore.pinCode;
  const serializedIdentity = JSON.stringify(identityToStore);
  const key = identityToStore.authentication.publicKey;

Alexey Lunin's avatar
Alexey Lunin committed
  if (pinCode == null || pinCode === "") {
    pinCode = getPincode(key);
Alexey Lunin's avatar
Alexey Lunin committed
  if (pinCode == null || pinCode === "") {
Alexey Lunin's avatar
Alexey Lunin committed
  return encryptMessage(serializedIdentity, pinCode, "identity").then(
    encryptedIdentity => {
Alexey Lunin's avatar
Alexey Lunin committed
      let success = true;
Alexey Lunin's avatar
Alexey Lunin committed
      if (extendKey === true) {
        success = extendPinCodeTtl(key, pinCode);
      }
      if (success === true) {
        localStorage.setItem(key, encryptedIdentity);
Zdravko Iliev's avatar
Zdravko Iliev committed
        const serializedIdentitiesList = localStorage.getItem("identities");
        const identities = JSON.parse(serializedIdentitiesList);
Alexey Lunin's avatar
Alexey Lunin committed
        identities[key] = true;
Alexey Lunin's avatar
Alexey Lunin committed
        localStorage.setItem("identities", JSON.stringify(identities));
      } else {
        console.log("Can not extend pincode ttl");
      }
Alexey Lunin's avatar
Alexey Lunin committed
  );
}

function getProfileData(identity) {
  return new Penpal.Promise(executeResultUpper => {
Alexey Lunin's avatar
Alexey Lunin committed
    executeRestfulFunction(
      "private",
      viamApi,
      viamApi.identityGetIdentityProfileData,
      null
    ).then(executeResult => {
      if (executeResult.code === "200") {
Zdravko Iliev's avatar
Zdravko Iliev committed
        const listItem = {};
Alexey Lunin's avatar
Alexey Lunin committed
        listItem.identityColor = getColorForIdentity(
          identity.authentication.publicKey
        );
Markin Igor's avatar
Markin Igor committed
        listItem.initials = executeResult.data.initials;
Alexey Lunin's avatar
Alexey Lunin committed
        if (listItem.initials === null || listItem.initials === "") {
          listItem.initials = "JD";
        }
Alexey Lunin's avatar
Alexey Lunin committed
        localStorage.setItem(
          "profiles/" + identity.authentication.publicKey,
          JSON.stringify(listItem)
        );
        executeResultUpper(listItem);
      } else {
Alexey Lunin's avatar
Alexey Lunin committed
        executeResultUpper({});
async function getIdentityFromLocalStorage(key, pinCode, extendTtl = true) {
  const encryptedIdentity = localStorage.getItem(key);
Markin Igor's avatar
Markin Igor committed
    console.log("No such identity for public key");
  const serializedIdentity = await decryptMessage(encryptedIdentity, pinCode);

  const identity = new Identity(serializedIdentity);
  if (extendTtl) {
    const success = extendPinCodeTtl(key, pinCode);
    if (!success) {
      console.log("Can not extend pincode ttl");
      return null;
    }
  }
function extendPinCodeTtl(key, pinCode) {
Alexey Lunin's avatar
Alexey Lunin committed
  if (pinCode == null || pinCode === "") {
Zdravko Iliev's avatar
Zdravko Iliev committed
    const now = new Date();
    const nowMillis = now.getTime();
    const ttl = window.sessionStorage.getItem("pincodettls/" + key);
    if (ttl == null || ttl === "" || nowMillis >= parseInt(ttl)) {
Markin Igor's avatar
Markin Igor committed
      clearPinCodeTtl(key);
Alexey Lunin's avatar
Alexey Lunin committed
      return false;
Zdravko Iliev's avatar
Zdravko Iliev committed
      const ttl = now.getTime() + 4 * 60 * 60 * 1000;
      window.sessionStorage.setItem("pincodettls/" + key, ttl);
    }
  } else {
Zdravko Iliev's avatar
Zdravko Iliev committed
    const now = new Date();
    const ttl = now.getTime() + 4 * 60 * 60 * 1000;
    window.sessionStorage.setItem("pincodettls/" + key, ttl);
    window.sessionStorage.setItem("pincodes/" + key, pinCode);
  }

  return true;
}

window.extendPinCodeTtl = extendPinCodeTtl;

function clearPinCodeTtl(key) {
Markin Igor's avatar
Markin Igor committed
  window.sessionStorage.removeItem("pincodettls/" + key);
Alexey Lunin's avatar
Alexey Lunin committed
  window.sessionStorage.removeItem("pincodes/" + key);
}

function getPincode(key) {
Zdravko Iliev's avatar
Zdravko Iliev committed
  const now = new Date();
  const nowMillis = now.getTime();
  const ttl = window.sessionStorage.getItem("pincodettls/" + key);
Markin Igor's avatar
Markin Igor committed
  if (ttl == null || ttl === "") {
Alexey Lunin's avatar
Alexey Lunin committed
    return null;
  } else {
Alexey Lunin's avatar
Alexey Lunin committed
    if (nowMillis >= parseInt(ttl)) {
Markin Igor's avatar
Markin Igor committed
      clearPinCodeTtl(key);
Alexey Lunin's avatar
Alexey Lunin committed
      return null;
    } else {
      return window.sessionStorage.getItem("pincodes/" + key);
    }
  }
}

function createEvent(actionId, type, payloads) {
  return {
Alexey Lunin's avatar
Alexey Lunin committed
    actionID: actionId,
Zdravko Iliev's avatar
Zdravko Iliev committed
    type,
Alexey Lunin's avatar
Alexey Lunin committed
    stamp: new Date().getTime(),
Zdravko Iliev's avatar
Zdravko Iliev committed
    payloads
Alexey Lunin's avatar
Alexey Lunin committed
  };
const destroyAuthentication = () => {
  const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");

  window.viamApi.setIdentity("");
  window.viamApi.setSessionData("", "");

  clearPinCodeTtl(authenticationPublicKey);

  localStorage.removeItem("uuid");
  localStorage.removeItem("token");
  localStorage.removeItem("authenticatedIdentity");

  window.currentlyAuthenticatedIdentity = null;
  window.lastTimeGetProfile = 0;
};

const destroyIdentity = () => {
  destroyAuthentication();

  if (window.currentlyLoadedIdentity) {
    const { publicKey } = window.currentlyLoadedIdentity.authentication;

    delete window.loadedIdentities[publicKey];
    window.currentlyLoadedIdentity = null;
    destroyIdentityFromLocalStorage(publicKey);
  }
};

Markin Igor's avatar
Markin Igor committed
window.loadedIdentities = {};
window.wopiAPI = new WopiAPI();
window.collaboraApi = new CollaboraAPI();
window.viamApi = new ViamAPI();
window.viamAnonymousApi = new ViamAPI();
Markin Igor's avatar
Markin Igor committed
window.currentlyAuthenticatedIdentity = null;
window.currentlyLoadedIdentity = null;
window.lastTimeGetProfile = 0;
let iframeParent = null;

Markin Igor's avatar
Markin Igor committed
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);
const getTrusteeContactsPublicKeys = async () => {
  const { viamApi } = window;
  const response = await viamApi.contactsGetTrusteeContactsPublicKeys();
  console.log({ response });
  if (response.data.code !== "200") return response.data;

Sasha Ilieva's avatar
Sasha Ilieva committed
  const responseData = response.data.data;
  console.log({ responseData });
  const contactsPublicKeys = Object.values(responseData).flat();
  console.log({ contactsPublicKeys });
  const sharesNumber = contactsPublicKeys.length;
  const getThreshold = () =>
    sharesNumber === 3 ? 2 : parseInt(sharesNumber / 2);
  const threshold = getThreshold();
  console.log({ threshold });
  const recoveryKey = generateRecoveryKey(512);
  const recoveryKeyShares = divideSecretToShares(
    recoveryKey,
    sharesNumber,
    threshold
  );
  // Sanity check
  const checkKey = combineSecret(recoveryKeyShares.slice(0, 1));
  console.log("first sanity check", checkKey === recoveryKey);
};

async function executeRestfulFunction(type, that, fn, config, ...args) {
Alexey Lunin's avatar
Alexey Lunin committed
  const {
    currentlyAuthenticatedIdentity,
    viamApi,
    currentlyLoadedIdentity
  } = window;
  let response;
  try {
    response = await fn.apply(that, [config, ...args]);
  } catch (error) {
    if (error.response) {
      //Resposnse with status code != 2xx still has valid response
      response = error.response;
    } else {
      //Connection error or similar
      const data = {
Alexey Lunin's avatar
Alexey Lunin committed
        data: "",
        code: "999",
        status: error.message
  const identity = currentlyAuthenticatedIdentity || currentlyLoadedIdentity;
  const { code, status } = response.data;
Sasha Ilieva's avatar
Sasha Ilieva committed

Alexey Lunin's avatar
Alexey Lunin committed
  const deviceRevoked =
    type === "private" && code === "401" && status === STATUS_DEVICE_REVOKED;
    const event = createEvent("", "DeviceRevoked");
    iframeParent.onEvent(event);
  const userNotActivated =
    type === "private" &&
    code === "400" &&
Sasha Ilieva's avatar
Sasha Ilieva committed
    (status === STATUS_USER_NOT_ACTIVATED || status === STATUS_USER_BLOCKED);

  if (userNotActivated) {
    destroyIdentity();

Sasha Ilieva's avatar
Sasha Ilieva committed
    const event = createEvent("", "UserBlocked");
    iframeParent.onEvent(event);

    return response.data;
  }

Alexey Lunin's avatar
Alexey Lunin committed
  const badSession =
    type === "private" &&
    identity &&
    code === "400" &&
    status === "Bad session";
Sasha Ilieva's avatar
Sasha Ilieva committed
  if (!badSession) {
    return response.data;
  }
Alexey Lunin's avatar
Alexey Lunin committed
  const loginResponse = await viamApi.identityLogin(
    null,
    "previousaddeddevice"
  );
Sasha Ilieva's avatar
Sasha Ilieva committed
  if (loginResponse.data.code !== "200") {
    return loginResponse.data;
  }

  const uuid = loginResponse.data.data["Uuid"];
  const token = loginResponse.data.data["Session"];
  handleIdentityLogin(identity, uuid, token);
  try {
    response = await fn.apply(that, [config, ...args]);
  } catch (error) {
    response = error.response;
  }
  return response.data;
}

window.executeRestfulFunction = executeRestfulFunction;

function loadIdentityInternal(identityKey, pinCode) {
  return new Penpal.Promise(result => {
Alexey Lunin's avatar
Alexey Lunin committed
    getIdentityFromLocalStorage(identityKey, pinCode)
      .then(async loadedIdentity => {
        if (loadedIdentity == null) {
          result({
            data: "",
            code: "400",
            status:
              "Please restore or authorize your account via another device."
          });
        }
        localStorage.removeItem("attempt");

        window.loadedIdentities[identityKey] = loadedIdentity;
        window.currentlyLoadedIdentity = loadedIdentity;

        if (identityKey === localStorage.getItem("authenticatedIdentity")) {
          window.currentlyAuthenticatedIdentity = loadedIdentity;
          const uuid = localStorage.getItem("uuid");
          const token = localStorage.getItem("token");
          const deviceHash = await createDeviceHash(identityKey);
          window.viamApi.setIdentity(identityKey);
          window.viamApi.setDeviceHash(deviceHash);
          window.viamApi.setSessionData(uuid, token);
        }
Alexey Lunin's avatar
Alexey Lunin committed
        window.viamAnonymousApi.setIdentity(
          window.currentlyLoadedIdentity.authentication.publicKey
        );
        getTrusteeContactsPublicKeys();
Alexey Lunin's avatar
Alexey Lunin committed
        const { publicKey, x509Certificate } = loadedIdentity.authentication;
Alexey Lunin's avatar
Alexey Lunin committed
        result({
          data: {
            authentication: {
              publicKey,
              x509Certificate
            }
          },
          code: "200",
          status: "Identity loaded"
        });
      })
      .catch(e => {
        result({
          data: "",
          code: "400",
          status: "" + e
        });
  });
}

function getCertificateForPassport(passportUUID, internal) {
  return new Penpal.Promise(certificateResult => {
    if (window.currentlyAuthenticatedIdentity === null) {
Sasha Ilieva's avatar
Sasha Ilieva committed
      return { data: "", code: "400", status: "Identity not authenticated" };
    const passportIdentity = window.currentlyAuthenticatedIdentity;
Zdravko Iliev's avatar
Zdravko Iliev committed
    const passport = passportIdentity.getPassport(passportUUID);
Alexey Lunin's avatar
Alexey Lunin committed
    if (passport === undefined || passport === null) {
Sasha Ilieva's avatar
Sasha Ilieva committed
      createPassportCertificate(passportUUID).then(function(keys) {
Zdravko Iliev's avatar
Zdravko Iliev committed
        const cryptoData = new CryptoData();
Markin Igor's avatar
Markin Igor committed
        cryptoData.setPublicKey(keys["publicKeyPEM"]);
        cryptoData.setPrivateKey(keys["privateKeyPEM"]);
Zdravko Iliev's avatar
Zdravko Iliev committed
        const certificate = keys["certificatePEM"];
        //download("passportCertificateBeforeSigning.crt", "text/plain", certificate)
        //cryptoData.setx509Certificate(keys["certificate"])
Alexey Lunin's avatar
Alexey Lunin committed
        executeRestfulFunction(
          "private",
          viamApi,
          viamApi.signSignCertificate,
          null,
          btoa(certificate),
          passportUUID
        ).then(executeResult => {
          if (executeResult.code === "200") {
Zdravko Iliev's avatar
Zdravko Iliev committed
            const signedCertificate = atob(
Alexey Lunin's avatar
Alexey Lunin committed
              executeResult.data["SignedCertificate"]
            );
            //download("passportCertificateAfterSigning.crt", "text/plain", signedCertificate)
Zdravko Iliev's avatar
Zdravko Iliev committed
            const keyUUID = executeResult.data["CertificateUUID"];
            const encodedChain = executeResult.data["Chain"];
            //download("rootCertificate.crt", "text/plain", atob(encodedChain[0]))

Zdravko Iliev's avatar
Zdravko Iliev committed
            const chain = [];
Alexey Lunin's avatar
Alexey Lunin committed
            for (let i = 0; i < 
Loading
Loading full blame...