server/www/session.js

42 lines
802 B
JavaScript

function Session(modules) {
var key = null;
var name = null;
var keys = JSON.parse(localStorage.getItem('keys')) || generateKeyPair();
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,
getKey: getKey,
loggedIn: loggedIn
};
function is(sessionKey) {
return key == sessionKey;
}
function getKey() {
return key;
}
function loggedIn() {
return name != undefined;
}
function generateKeyPair() {
var crypt = new JSEncrypt({default_key_size: 1024});
crypt.getKey();
return {
priv: crypt.getPrivateKey(),
pub: crypt.getPublicKey()
};
}
}