Skip to content
Snippets Groups Projects
viamapi-iframe.js 45.8 KiB
Newer Older
import {
  createDeviceHash,
  destroyIdentityFromLocalStorage,
  encodeResponse,
  listIdentitiesFromLocalStorage, makeid
} from '../utilities/appUtility';
Markin Igor's avatar
Markin Igor committed
import {LOGIN_MODES} from '../constants';
import {
  createOneTimePassportCertificate,
  createPassportCertificate,
  decryptMessage,
  encryptMessage, signEmail
} from '../utilities/signingUtilities';
const QRCode = require('qrcode');
const Penpal = require('penpal').default;
const penpalMethods = require('../../temp/penpal-methods').default;
const WopiAPI = require('./wopiapi-iframe');
const ViamAPI = require('../../temp/viamapi');

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();
    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 === "") {
    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 {
Markin Igor's avatar
Markin Igor committed
      console.log("Can not extend pincode ttl");
Loading
Loading full blame...