-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebmail_bridge.html
More file actions
129 lines (108 loc) · 3.65 KB
/
Copy pathwebmail_bridge.html
File metadata and controls
129 lines (108 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"></head>
<body>
<script>
const SSO_ORIGIN = 'https://sso.DOMAIN_TO_REPLACE';
const WEBMAIL_ORIGIN = 'https://web.DOMAIN_TO_REPLACE';
const DB_NAME = 'aurion-plugin-store';
const AURION_SECRET_STORE = 'aurion-secret';
const SECRET_KEY = 'aurion-secret';
const DANGEROUS_KEYS_STORE = 'dangerous-keys';
const KEY_RECORDS_STORE = 'key-records';
const MESSAGE_CACHE_STORE = 'message-cache';
async function performStorageCleanup() {
return new Promise((resolve, reject) => {
const req = indexedDB.open(DB_NAME);
req.onerror = () => reject(req.error);
req.onsuccess = (e) => {
const db = e.target.result;
const storesToClear = [
DANGEROUS_KEYS_STORE,
KEY_RECORDS_STORE,
MESSAGE_CACHE_STORE
].filter(storeName => db.objectStoreNames.contains(storeName));
if (storesToClear.length === 0) {
db.close();
return resolve();
}
const tx = db.transaction(storesToClear, 'readwrite');
storesToClear.forEach((storeName) => {
tx.objectStore(storeName).clear();
});
tx.oncomplete = () => {
db.close();
resolve();
};
tx.onerror = () => {
db.close();
reject(tx.error);
};
};
});
}
window.addEventListener('message', async (event) => {
if (event.origin !== SSO_ORIGIN && event.origin !== WEBMAIL_ORIGIN) return;
if (event.data && event.data.type === 'WRITE_SECRET' && event.data.secret) {
const req = indexedDB.open(DB_NAME);
req.onupgradeneeded = (e) => {
const db = e.target.result;
if (!db.objectStoreNames.contains(AURION_SECRET_STORE)) {
db.createObjectStore(AURION_SECRET_STORE);
}
};
req.onsuccess = (e) => {
const db = e.target.result;
const transaction = db.transaction(AURION_SECRET_STORE, "readwrite");
const store = transaction.objectStore(AURION_SECRET_STORE);
store.put(event.data.secret, SECRET_KEY);
transaction.oncomplete = () => {
event.source.postMessage({ type: 'WRITE_SUCCESS' }, event.origin);
};
};
}
if (event.data && event.data.type === 'LOGOUT') {
const channel = new BroadcastChannel('aurion-session-bus');
let responded = false;
const callerSource = event.source;
const callerOrigin = event.origin;
const requestId = Math.random().toString(36).substring(2);
const handleMessage = (msgEvent) => {
// Vérification du type ET de l'ID de requête
if (
msgEvent.data &&
msgEvent.data.type === 'RESPONSE_LOGOUT_REQUEST' &&
msgEvent.data.requestId === requestId
) {
responded = true;
cleanupAndClose();
callerSource.postMessage({ type: 'LOGOUT_SUCCESS' }, callerOrigin);
}
};
channel.addEventListener('message', handleMessage);
channel.postMessage({
type: 'LOGOUT_REQUEST',
requestId: requestId
});
const timeoutId = setTimeout(async () => {
if (!responded) {
cleanupAndClose();
try {
await performStorageCleanup();
} catch (err) {
console.error('Error when cleaning IndexedDB :', err);
} finally {
callerSource.postMessage({ type: 'LOGOUT_SUCCESS' }, callerOrigin);
}
}
}, 1500);
function cleanupAndClose() {
clearTimeout(timeoutId);
channel.removeEventListener('message', handleMessage);
channel.close();
}
}
});
</script>
</body>
</html>