diff --git a/javascript/src/Identity.js b/javascript/src/Identity.js index 43a52eafc057f67cf2feb961db87e5392f015f4d..22b112ca9e833dd25ce67e38def5a22cf32bd2ef 100644 --- a/javascript/src/Identity.js +++ b/javascript/src/Identity.js @@ -1,51 +1,39 @@ -function Identity() { -} - -Identity.prototype.set = function(obj) { - for(var member in obj) { - this[member] = JSON.parse(JSON.stringify(obj[member])) +class Identity { + constructor(source) { + let identityData = {}; + + if (typeof source === "string") { + try { + identityData = JSON.parse(source); + } catch (e) { + console.warn(e); + } + } else if (typeof source === "object") { + identityData = source; + } + + const { authentication, pinCode, passports } = identityData; + + this.authentication = authentication; + this.pinCode = pinCode; + this.passports = passports || {}; } -}; - -Identity.prototype.serialize = function() { - return JSON.stringify(this) -}; - -Identity.prototype.deserialize = function(serialized) { - var obj = JSON.parse(serialized); - this.set(obj) -}; - -Identity.prototype.setAuthentication = function(cryptoData) { - this["authentication"] = cryptoData -}; - -Identity.prototype.getAuthentication = function() { - return this["authentication"] -}; -Identity.prototype.setPinCode = function(pinCode) { - this["pinCode"] = pinCode -}; - -Identity.prototype.getPinCode = function() { - return this["pinCode"] -}; - -Identity.prototype.setPassport = function(passportUUID, cryptoData) { - if(this["passports"] === undefined || this["passports"] === null) { - this["passports"] = {} + setAuthentication(cryptoData) { + this.authentication = cryptoData; } - this["passports"][passportUUID] = cryptoData -}; + setPinCode(pinCode) { + this.pinCode = pinCode; + } -Identity.prototype.getPassport = function(passportUUID) { - if(this["passports"] === undefined || this["passports"] === null) { - this["passports"] = {} + setPassport(passportUUID, cryptoData) { + this.passports[passportUUID] = cryptoData; } - return this["passports"][passportUUID] -}; + getPassport(passportUUID) { + return this.passports[passportUUID]; + } +} -export default Identity; \ No newline at end of file +export default Identity; diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js index cf62bf4b9dcc0be361911a1c03f7aad5c417c0c1..adcf52c7c27183dd35f50a4ff7edd241575a51c6 100644 --- a/javascript/src/iframe/viamapi-iframe.js +++ b/javascript/src/iframe/viamapi-iframe.js @@ -116,32 +116,28 @@ function getProfileData(identity) { }); } -function getIdentityFromLocalStorage(key, pinCode, extendTtl = true) { +async function getIdentityFromLocalStorage(key, pinCode, extendTtl = true) { const encryptedIdentity = localStorage.getItem(key); - if (encryptedIdentity == null) { + + if (!encryptedIdentity) { console.log("No such identity for public key"); - return Promise.resolve(null) + return null; } - return decryptMessage(encryptedIdentity, pinCode).then((serializedIdentity) => { - var parsedIdentity = JSON.parse(serializedIdentity); - parsedIdentity["pinCode"] = ""; - - if(extendTtl === true) { - var success = extendPinCodeTtl(key, pinCode); - if (success === true) { - return parsedIdentity - } else { - console.log("Can not extend pincode ttl"); - return null - } - } else { - return parsedIdentity - } - }); -} + 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; + } + } + + return identity; +} function extendPinCodeTtl(key, pinCode) { if(pinCode == null || pinCode === "") { @@ -260,11 +256,12 @@ function loadIdentityInternal(identityKey, pinCode) { }); } localStorage.removeItem("attempt"); - const copiedIdentity = JSON.parse(JSON.stringify(loadedIdentity)); + window.loadedIdentities[identityKey] = loadedIdentity; + window.currentlyLoadedIdentity = loadedIdentity; if (identityKey === localStorage.getItem("authenticatedIdentity")) { - window.currentlyAuthenticatedIdentity = copiedIdentity; + window.currentlyAuthenticatedIdentity = loadedIdentity; const uuid = localStorage.getItem("uuid"); const token = localStorage.getItem("token"); const deviceHash = await createDeviceHash(identityKey); @@ -273,14 +270,17 @@ function loadIdentityInternal(identityKey, pinCode) { window.viamApi.setSessionData(uuid, token); } - window.currentlyLoadedIdentity = copiedIdentity; window.viamAnonymousApi.setIdentity(window.currentlyLoadedIdentity.authentication.publicKey); - copiedIdentity.pinCode = ""; - copiedIdentity.authentication.privateKey = ""; + const { publicKey, x509Certificate } = loadedIdentity.authentication; result({ - "data": copiedIdentity, + "data": { + authentication: { + publicKey, + x509Certificate + } + }, "code": "200", "status": "Identity loaded" }); @@ -334,8 +334,7 @@ function getCertificateForPassport(passportUUID, internal) { } } - var passportIdentity = new Identity(); - passportIdentity.set(window.currentlyAuthenticatedIdentity); + const passportIdentity = window.currentlyAuthenticatedIdentity; var passport = passportIdentity.getPassport(passportUUID); if(passport === undefined || passport === null) { createPassportCertificate(passportUUID).then(function(keys){ @@ -961,12 +960,18 @@ const connection = Penpal.connectToParent({ }); }, getCurrentlyAuthenticatedIdentity() { - return new Penpal.Promise(result => { - result({"data" : window.currentlyAuthenticatedIdentity, - "code" : "200", - "status" : "Currently authenticated identity" - }) - }); + const { publicKey, x509Certificate } = window.currentlyAuthenticatedIdentity.authentication; + + return encodeResponse( + "200", + { + authentication: { + publicKey, + x509Certificate + } + }, + "Currently authenticated identity" + ); }, stringToUtf8ByteArray(str) { if (typeof str !== 'string') {