Newer
Older
});
} else {
result({"data" : "",
"code" : "400",
"status" : "Can not generate one time certificate"
})
}
})
});
},
signEmail(passportUUID, emailArg, emailMessage) {
return new Penpal.Promise(result => {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (authenticationPublicKey === null) {
return {"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
}
}
if (window.loadedIdentities[authenticationPublicKey] === null) {
return {"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
}
}
var success = extendPinCodeTtl(authenticationPublicKey);
result({"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
})
}
getCertificateForPassport(passportUUID, true).then(certificateResult => {
var passportCertificate = certificateResult.data["x509Certificate"];
var passportPrivateKey = certificateResult.data["privateKey"];
var passportChain = certificateResult.data["chain"];
createOneTimePassportCertificate(makeid() + "-" + passportUUID, emailArg, passportPrivateKey, passportCertificate).then(function(keys){
var publicKeyOneTime = keys["publicKeyPEM"];
var privateKeyOneTime = keys["privateKeyPEM"];
var certificateOneTime = keys["certificatePEM"];
//download("certificateOneTime.crt", "text/plain", certificateOneTime)
executeRestfulFunction("private", viamApi, viamApi.passportGetEmailWithHeaderByPassport,
passportUUID, emailMessage).then(executeResult2 => {
//download("withheader.eml", "message/rfc822", emailWithHeader)
var signedEmailPromise = signEmail(emailWithHeader,
certificateOneTime,
passportChain,
signedEmailPromise.then(signedEmail => {
executeRestfulFunction("private", viamApi, viamApi.signResignEmail,
passportUUID, signedEmail).then(executeResult => {
result({"data" : executeResult.data,
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
"code" : "200",
"status" : "Email signed"
})
});
/*result({"data" : signedEmail,
"code" : "200",
"status" : "Email signed"
})*/
});
});
// Prints PEM formatted signed certificate
// -----BEGIN CERTIFICATE-----MIID....7Hyg==-----END CERTIFICATE-----
});
} else {
result({"data" : "",
"code" : "400",
"status" : "Can not sign email"
})
}
})
});
},
hasSession() {
return new Penpal.Promise(result => {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (authenticationPublicKey === null) {
result({"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
if (window.loadedIdentities[authenticationPublicKey] === null) {
result({"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
});
}
var success = extendPinCodeTtl(authenticationPublicKey);
result({"data" : "",
"code" : "400",
"status" : "Identity not authenticated"
})
}
executeRestfulFunction("private", viamApi, viamApi.identityHasSession).then(executeResult => {
result(executeResult);
});
});
},
marketingSignUpIdentificator(identificator, reference) {
return new Penpal.Promise(result => {
executeRestfulFunction("public", viamApi, viamApi.marketingSignUpIdentificator, identificator, reference).then(executeResult => {
result(executeResult);
});
});
},
marketingGetIdentificatorProfile(identificator, pincode) {
return new Penpal.Promise(result => {
executeRestfulFunction("public", viamApi, viamApi.marketingGetIdentificatorProfile, identificator, pincode).then(executeResult => {
result(executeResult);
});
});
},
marketingExecuteEventForIdentificator(identificator, pincode, event) {
executeRestfulFunction("public", viamApi, viamApi.marketingExecuteEventForIdentificator, identificator, pincode, event).then(executeResult => {
result(executeResult);
});
});
},
getCurrentlyAuthenticatedIdentity() {
return new Penpal.Promise(result => {
result({"data" : window.currentlyAuthenticatedIdentity,
"code" : "200",
"status" : "Currently authenticated identity"
})
});
},
stringToUtf8ByteArray(str) {
if (typeof str !== 'string') {
str = str.toString()
}
return new Penpal.Promise(result => {
result(res)
})
},
utf8ByteArrayToString(ba) {
if (!Buffer.isBuffer(ba)) {
ba = Buffer.from(ba)
}
return new Penpal.Promise(result => {
result(res)
})
},
stringToUtf8Base64(str) {
if (!Buffer.isBuffer(str)) {
if (typeof str !== 'string') {
str = str.toString()
}
str = Buffer.from(str, 'utf-8')
}
return new Penpal.Promise(result => {
result(res)
})
},
utf8Base64ToString(strBase64) {
if (!Buffer.isBuffer(strBase64)) {
if (typeof strBase64 !== 'string') {
strBase64 = strBase64.toString()
}
strBase64 = Buffer.from(strBase64, 'base64')
}
return new Penpal.Promise(result => {
result(res)
})
},
base64ToByteArray(strBase64) {
if (typeof strBase64 !== 'string') {
strBase64 = strBase64.toString()
}
return new Penpal.Promise(result => {
result(res)
})
},
byteArrayToBase64(ba) {
if (!Buffer.isBuffer(ba)) {
ba = Buffer.from(ba)
}
return new Penpal.Promise(result => {
result(res)
})
},
connection.promise.then(parent => {
if (!navigator.cookieEnabled) {
console.warn("Cookie disabled. Can't start library.");
return;
}
window.addEventListener('storage', event => {
if (event.key === "authenticatedIdentity" && event.newValue === null) {
const publicKey = window.currentlyAuthenticatedIdentity.authentication.publicKey;
window.currentlyLoadedIdentity = null;
window.currentlyAuthenticatedIdentity = null;
const event = createEvent("LogoutFromAnotherTab", "Logout", [publicKey]);
parent.onEvent(event);
}
});
const identities = localStorage.getItem("identities");
console.log("Library loaded at: " + new Date().toISOString());
if (identities === "" || identities === null) {
localStorage.setItem("identities", JSON.stringify({}));
if (
localStorage.getItem("uuid") === null ||
localStorage.getItem("token") === null ||
localStorage.getItem("authenticatedIdentity") === null
) {
localStorage.removeItem("uuid");
localStorage.removeItem("token");
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
const pinCode = getPincode(authenticationPublicKey);
loadIdentityInternal(authenticationPublicKey, "00000000").then(result => {
if (result.code !== "200") {
const event = createEvent(
"CanNotGetPincodeForAuthenticatedIdentity",
"IdentityNotLoaded",
loadIdentityInternal(authenticationPublicKey, pinCode).then(result => {
if (result.code !== "200") {
const event = createEvent(
"CanNotLoadIdentity",
"ErrorDuringLoadingIdentity",
var anynomousDeviceKeyEventsProcessing = false;
var maxDeviceKeyAnonymousEventTime = 0;
var eventsDeviceEventsProcessing = false;
var maxDeviceKeyEventTime = 0;
var eventsEntityEventsProcessing = false;
var maxEntityEventTime = 0;
var identityLoadedEvent = false;
var identityAuthenticatedEvent = false;
if(window.currentlyAuthenticatedIdentity != null) {
var pinCode = getPincode(window.currentlyAuthenticatedIdentity.authentication.publicKey);
if(pinCode != null && pinCode !== "") {
getIdentityFromLocalStorage(window.currentlyAuthenticatedIdentity.authentication.publicKey,
pinCode, false).then((gotIdentity) => {
window.currentlyAuthenticatedIdentity = gotIdentity;
window.currentlyLoadedIdentity = gotIdentity;
if(identityAuthenticatedEvent === false && gotIdentity != null) {
var event = createEvent("IdentityAuthenticated", "Authenticated", [gotIdentity.authentication.publicKey]);
parent.onEvent(event);
identityAuthenticatedEvent = true
}
})
} else {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if(authenticationPublicKey != null && authenticationPublicKey !== "") {
loadIdentityInternal(authenticationPublicKey, "00000000").then(result => {
if(result.code !== "200") {
var event = createEvent("CanNotGetPincodeForAuthenticatedIdentity", "IdentityNotLoaded", [authenticationPublicKey]);
parent.onEvent(event);
clearPinCodeTtl(authenticationPublicKey);
if(window.currentlyLoadedIdentity != null) {
var pinCode = getPincode(window.currentlyLoadedIdentity.authentication.publicKey);
if(pinCode === "" || pinCode == null) {
/*var event = createEvent("CanNotLoadPincodeForLoadedIdentity", "IdentityNotLoaded", [window.currentlyLoadedIdentity.authentication.publicKey])
parent.onEvent(event)
identityLoadedEvent = true*/
loadIdentityInternal(window.currentlyLoadedIdentity.authentication.publicKey, "00000000").then(result => {
if(result.code !== "200") {
var event = createEvent("CanNotLoadPincodeForLoadedIdentity", "IdentityNotLoaded", [window.currentlyLoadedIdentity.authentication.publicKey]);
identityLoadedEvent = true
}
});
}
} else {
identityLoadedEvent = false
}
}
if (window.currentlyAuthenticatedIdentity != null) {
if(now - window.lastTimeGetProfile > 30000) {
var identityToStore = window.currentlyAuthenticatedIdentity;
getProfileData(identityToStore);
window.lastTimeGetProfile = now;
if (window.currentlyLoadedIdentity != null && anynomousDeviceKeyEventsProcessing === false) {
executeRestfulFunction("public", viamAnonymousApi, viamAnonymousApi.eventGetNewEventsWithoutSession, "devicekey").then(async executeResult => {
let changedMaxDeviceKeyAnonymousEventTime = false;
for (var i = 0; i < eventsLen; i++) {
const uuid = event.payloads[0];
const token = event.payloads[1];
handleIdentityLogin(window.currentlyLoadedIdentity, uuid, token);
const identityToStore = window.currentlyAuthenticatedIdentity;
event.payloads = [{fromQRCode: true}];
setIdentityInLocalStorage(identityToStore).then(() => {
getProfileData(identityToStore).then(() => {
parent.onEvent(event);
});
});
break;
}
case "QRCodeUpdated" : {
var actionID = event["actionID"];
var QrCode = event["payloads"][1];
var eventCopy = JSON.parse(JSON.stringify(event));
QRCode.toDataURL(actionID + "," + QrCode, function (err, url) {
break
}
case "KeyDeleted" : {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
clearPinCodeTtl(authenticationPublicKey);
localStorage.removeItem("uuid");
localStorage.removeItem("token");
localStorage.removeItem("authenticatedIdentity");
delete window.loadedIdentities[authenticationPublicKey];
window.currentlyLoadedIdentity = null;
window.currentlyAuthenticatedIdentity = null;
window.lastTimeGetProfile = 0;
destroyIdentityFromLocalStorage(authenticationPublicKey);
break
}
default : {
parent.onEvent(event)
}
}
maxDeviceKeyAnonymousEventTime = Math.max(maxDeviceKeyAnonymousEventTime, event.stamp)
}
if(changedMaxDeviceKeyAnonymousEventTime) {
executeRestfulFunction("public", viamAnonymousApi, viamAnonymousApi.eventUpdateLastViewedWithoutSession,
"devicekey", maxDeviceKeyAnonymousEventTime.toString()).then(() => {
anynomousDeviceKeyEventsProcessing = false;
});
if (window.currentlyAuthenticatedIdentity != null && eventsDeviceEventsProcessing === false) {
executeRestfulFunction("private", viamApi, viamApi.eventGetNewEvents, "devicekey").then(executeResult => {
const changedMaxDeviceKeyEventTime = false;
var actionID = event["actionID"];
var QrCode = event["payloads"][1];
var eventCopy = JSON.parse(JSON.stringify(event));
QRCode.toDataURL(actionID + "," + QrCode, function (err, url) {
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
parent.onEvent(eventCopy)
})
} else {
parent.onEvent(event)
}
maxDeviceKeyEventTime = Math.max(maxDeviceKeyEventTime, event.stamp)
}
if(changedMaxDeviceKeyEventTime) {
executeRestfulFunction("private", viamApi, viamApi.eventUpdateLastViewed, "devicekey",
maxDeviceKeyEventTime.toString()).then(executeResult1 => {
eventsDeviceEventsProcessing = false
})
} else {
eventsDeviceEventsProcessing = false
}
} else {
eventsDeviceEventsProcessing = false
}
});
}
if (window.currentlyAuthenticatedIdentity != null && eventsEntityEventsProcessing === false) {
executeRestfulFunction("private", viamApi, viamApi.eventGetNewEvents, "entity").then(executeResult => {
let changedMaxEntityEventTime = false;
var actionID = event["actionID"];
var QrCode = event["payloads"][1];
var eventCopy = JSON.parse(JSON.stringify(event));
QRCode.toDataURL(actionID + "," + QrCode, function (err, url) {
parent.onEvent(event);
changedMaxEntityEventTime = true;
maxEntityEventTime = Math.max(maxEntityEventTime, event.stamp)
}
if(changedMaxEntityEventTime) {
executeRestfulFunction("private", viamApi, viamApi.eventUpdateLastViewed, "entity",
maxEntityEventTime.toString()).then(executeResult1 => {
eventsEntityEventsProcessing = false
})
} else {
eventsEntityEventsProcessing = false
}
} else {
eventsEntityEventsProcessing = false
}
});
}
}, 1000);
});