Newer
Older
executeRestfulFunction("public", viamApi, viamApi.marketingExecuteEventForIdentificator, null, 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)
})
},
return collaboraApi.discovery().then(apps => apps);
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
// WOPI APIs
// TODO rewrite with async await
getPassports: function(fileID) {
return new Penpal.Promise(function(result) {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (
!authenticationPublicKey ||
!window.loadedIdentities[authenticationPublicKey] ||
!extendPinCodeTtl(authenticationPublicKey)
) {
return encodeResponse("400", "", "Identity not authenticated");
}
wopiAPI.getPassports(fileID).then(function(response) {
result(response.data);
});
});
},
// TODO rewrite with async await
wopiPutFile: function (fileId, accessToken, file) {
return new Penpal.Promise(function(result) {
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (
!authenticationPublicKey ||
!window.loadedIdentities[authenticationPublicKey] ||
!extendPinCodeTtl(authenticationPublicKey)
) {
return encodeResponse("400", "", "Identity not authenticated");
}
wopiAPI.putDocument(fileId, accessToken, file).then(function(response) {
result(response.data);
});
});
},
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;
setInterval(async function () {
if (window.currentlyAuthenticatedIdentity) {
const { authentication } = window.currentlyAuthenticatedIdentity;
const pinCode = getPincode(authentication.publicKey);
if (pinCode) {
const identity = await getIdentityFromLocalStorage(authentication.publicKey, pinCode, false);
window.currentlyLoadedIdentity = identity;
if (!identityAuthenticatedEvent && identity) {
const event = createEvent("IdentityAuthenticated", "Authenticated", [identity.authentication.publicKey]);
parent.onEvent(event);
identityAuthenticatedEvent = true;
}
const authenticationPublicKey = localStorage.getItem("authenticatedIdentity");
if (authenticationPublicKey) {
const result = await loadIdentityInternal(authenticationPublicKey, "00000000");
if (result.code !== "200") {
const event = createEvent("CanNotGetPincodeForAuthenticatedIdentity", "IdentityNotLoaded", [authenticationPublicKey]);
parent.onEvent(event);
clearPinCodeTtl(authenticationPublicKey);
window.currentlyAuthenticatedIdentity = null;
}
if (window.currentlyLoadedIdentity) {
const pinCode = getPincode(window.currentlyLoadedIdentity.authentication.publicKey);
if (!pinCode) {
if (!identityLoadedEvent) {
const result = await loadIdentityInternal(window.currentlyLoadedIdentity.authentication.publicKey, "00000000");
if (result.code !== "200") {
const event = createEvent("CanNotLoadPincodeForLoadedIdentity", "IdentityNotLoaded", [window.currentlyLoadedIdentity.authentication.publicKey]);
parent.onEvent(event);
identityLoadedEvent = true;
}
if (window.currentlyAuthenticatedIdentity) {
const now = new Date().getTime();
if (now - window.lastTimeGetProfile > 30000) {
getProfileData(window.currentlyAuthenticatedIdentity);
if (window.currentlyLoadedIdentity && !anynomousDeviceKeyEventsProcessing && !window.currentlyAuthenticatedIdentity) {
executeRestfulFunction("public", viamAnonymousApi, viamAnonymousApi.eventGetNewEventsWithoutSession, null, "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,
null, "devicekey", maxDeviceKeyAnonymousEventTime.toString()).then(() => {
anynomousDeviceKeyEventsProcessing = false;
});
if (window.currentlyAuthenticatedIdentity != null && eventsDeviceEventsProcessing === false) {
executeRestfulFunction("private", viamApi, viamApi.eventGetNewEvents, null, "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) {
parent.onEvent(eventCopy)
})
} else {
parent.onEvent(event)
}
maxDeviceKeyEventTime = Math.max(maxDeviceKeyEventTime, event.stamp)
}
if(changedMaxDeviceKeyEventTime) {
executeRestfulFunction("private", viamApi, viamApi.eventUpdateLastViewed, null, "devicekey",
maxDeviceKeyEventTime.toString()).then(executeResult1 => {
eventsDeviceEventsProcessing = false
})
} else {
eventsDeviceEventsProcessing = false
}
} else {
eventsDeviceEventsProcessing = false
}
});
}
if (window.currentlyAuthenticatedIdentity != null && eventsEntityEventsProcessing === false) {
executeRestfulFunction("private", viamApi, viamApi.eventGetNewEvents, null, "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, null, "entity",
maxEntityEventTime.toString()).then(executeResult1 => {
eventsEntityEventsProcessing = false
})
} else {
eventsEntityEventsProcessing = false
}
} else {
eventsEntityEventsProcessing = false
}
});
}
}, 1000);
});