Skip to content
Snippets Groups Projects
Commit 249214fc authored by Markin Igor's avatar Markin Igor
Browse files

Dispatch DeviceRevoked event to clients.

parent 6b1beabe
Branches 390-device-management-ability-to-remove-device
No related tags found
1 merge request!28Dispatch DeviceRevoked event to clients.
......@@ -25,7 +25,7 @@
# unused-packages = true
[[constraint]]
branch = "master"
branch = "390-device-management-ability-to-remove-device"
name = "code.vereign.com/code/restful-api"
[prune]
......
export const STATUS_DEVICE_REVOKED = "Device revoked";
......@@ -18,6 +18,7 @@ import {
} from '../utilities/signingUtilities';
import CryptoData from '../CryptoData';
import Identity from '../Identity';
import {STATUS_DEVICE_REVOKED} from '../constants/statuses';
const penpalMethods = require('../../temp/penpal-methods').default;
const WopiAPI = require('./wopiapi-iframe');
......@@ -193,6 +194,34 @@ function createEvent(actionId, type, payloads) {
}
}
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);
}
};
window.loadedIdentities = {};
window.wopiAPI = new WopiAPI();
window.collaboraApi = new CollaboraAPI();
......@@ -202,6 +231,8 @@ window.currentlyAuthenticatedIdentity = null;
window.currentlyLoadedIdentity = null;
window.lastTimeGetProfile = 0;
let iframeParent = null;
const handleIdentityLogin = (identity, uuid, token) => {
const { loadedIdentities, viamApi } = window;
const { publicKey } = identity.authentication;
......@@ -219,10 +250,22 @@ function executeRestfulFunction(type, that, fn, ...args) {
const { currentlyAuthenticatedIdentity, viamApi, currentlyLoadedIdentity } = window;
return new Penpal.Promise(executeResult => {
fn.apply(that, args).then((response) => {
fn.apply(that, args).then(async (response) => {
const identity = currentlyAuthenticatedIdentity || currentlyLoadedIdentity;
if (type === "private" && identity && response.data.code === "400" && response.data.status === "Bad session") {
const { code, status } = response.data;
// Destroy local storage in case device was revoked
if (type === "private" && code === "401" && status === STATUS_DEVICE_REVOKED) {
destroyIdentity();
const event = createEvent("", "DeviceRevoked");
iframeParent.onEvent(event);
return executeResult(response.data);
}
if (type === "private" && identity && code === "400" && status === "Bad session") {
viamApi.identityLogin("previousaddeddevice")
.then((response) => {
if (response.data.code === "200") {
......@@ -696,17 +739,7 @@ const connection = Penpal.connectToParent({
window.viamApi.identityLogout
);
window.viamApi.setIdentity("");
window.viamApi.setSessionData("", "");
clearPinCodeTtl(authenticationPublicKey);
localStorage.removeItem("uuid");
localStorage.removeItem("token");
localStorage.removeItem("authenticatedIdentity");
delete window.loadedIdentities[authenticationPublicKey];
window.currentlyLoadedIdentity = null;
window.currentlyAuthenticatedIdentity = null;
window.lastTimeGetProfile = 0;
destroyAuthentication();
return identityLogoutResponse;
},
......@@ -1041,6 +1074,8 @@ const connection = Penpal.connectToParent({
});
connection.promise.then(parent => {
iframeParent = parent;
if (!navigator.cookieEnabled) {
console.warn("Cookie disabled. Can't start library.");
return;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment