2018-05-11 12:31:53 +02:00
|
|
|
function Session(modules) {
|
|
|
|
var key = null;
|
|
|
|
var name = null;
|
2018-09-10 23:06:03 +02:00
|
|
|
var keys = JSON.parse(localStorage.getItem('keys')) || generateKeyPair();
|
2018-05-11 12:31:53 +02:00
|
|
|
|
|
|
|
modules.messaging.addEventListener(["Welcome"], function(o) {
|
|
|
|
key = o.key;
|
|
|
|
});
|
|
|
|
|
|
|
|
modules.messaging.addEventListener(["Relay", "LogIn"], function(o) {
|
|
|
|
if(is(o.from)) {
|
|
|
|
name = o.message.name;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
is: is,
|
2018-05-12 15:32:00 +02:00
|
|
|
getKey: getKey,
|
2018-05-11 12:31:53 +02:00
|
|
|
loggedIn: loggedIn
|
|
|
|
};
|
|
|
|
|
|
|
|
function is(sessionKey) {
|
|
|
|
return key == sessionKey;
|
|
|
|
}
|
|
|
|
|
2018-05-12 15:32:00 +02:00
|
|
|
function getKey() {
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
2018-05-11 12:31:53 +02:00
|
|
|
function loggedIn() {
|
|
|
|
return name != undefined;
|
|
|
|
}
|
2018-09-10 23:06:03 +02:00
|
|
|
|
|
|
|
function generateKeyPair() {
|
|
|
|
var crypt = new JSEncrypt({default_key_size: 1024});
|
|
|
|
crypt.getKey();
|
|
|
|
return {
|
|
|
|
priv: crypt.getPrivateKey(),
|
|
|
|
pub: crypt.getPublicKey()
|
|
|
|
};
|
|
|
|
}
|
2018-05-11 12:31:53 +02:00
|
|
|
}
|