From 2a8202e74846d191a321cca1202175af9db6107d Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 5 Nov 2020 01:45:06 +0100 Subject: Convert to sibs project --- javascript/demo/demo.css | 8 - javascript/demo/group_demo.html | 112 -------- javascript/demo/group_demo.js | 506 ----------------------------------- javascript/demo/one_to_one_demo.html | 147 ---------- 4 files changed, 773 deletions(-) delete mode 100644 javascript/demo/demo.css delete mode 100644 javascript/demo/group_demo.html delete mode 100644 javascript/demo/group_demo.js delete mode 100644 javascript/demo/one_to_one_demo.html (limited to 'javascript/demo') diff --git a/javascript/demo/demo.css b/javascript/demo/demo.css deleted file mode 100644 index bf07df1..0000000 --- a/javascript/demo/demo.css +++ /dev/null @@ -1,8 +0,0 @@ -div.user { - width: 500px; - float: left; - overflow: scroll; - margin: 0px 20px 0px 0px; - border: 1px solid black; - padding: 5px; -} \ No newline at end of file diff --git a/javascript/demo/group_demo.html b/javascript/demo/group_demo.html deleted file mode 100644 index d93a7cd..0000000 --- a/javascript/demo/group_demo.html +++ /dev/null @@ -1,112 +0,0 @@ - - - - - - - -
-

User1

- - - - -

Outgoing

- -

One-to-one output

-
- -

Group output

-
- -

Incoming

- -

One-to-one Received

-
- -

Group received

-
- -

Tasks

-
-
- -
-

User 2

- - - - -

Outgoing

- -

One-to-one output

-
- -

Group output

-
- -

Incoming

- -

One-to-one Received

-
- -

Group received

-
- -

Tasks

-
-
- -
-

User 3

- - - - -

Outgoing

- -

One-to-one output

-
- -

Group output

-
- -

Incoming

- -

One-to-one Received

-
- -

Group received

-
- -

Tasks

-
-
- -
-

User 4

- - - - -

Outgoing

- -

One-to-one output

-
- -

Group output

-
- -

Incoming

- -

One-to-one Received

-
- -

Group received

-
- -

Tasks

-
-
- - diff --git a/javascript/demo/group_demo.js b/javascript/demo/group_demo.js deleted file mode 100644 index 44b8166..0000000 --- a/javascript/demo/group_demo.js +++ /dev/null @@ -1,506 +0,0 @@ -/* Javascript parts of the group demo. To use, load group_demo.html in your - * browser. - */ - -function buttonElement(buttonLabel, clickHandler) { - var button = document.createElement("button"); - button.appendChild(document.createTextNode(buttonLabel)); - button.addEventListener("click", clickHandler, false); - return button; -} - -function buttonsAndText(textContent, buttonLabelToHandlerMap) { - var el = document.createElement("div"); - for (var label in buttonLabelToHandlerMap) { - if (!buttonLabelToHandlerMap.hasOwnProperty(label)) { - continue; - } - var handler = buttonLabelToHandlerMap[label]; - var button = buttonElement(label, handler); - el.appendChild(button); - } - - var message_element = document.createElement("tt"); - el.appendChild(message_element); - - var content = document.createTextNode(textContent); - message_element.appendChild(content); - - return el; -} - -function buttonAndTextElement(buttonLabel, textContent, clickHandler) { - var buttonMap = {}; - buttonMap[buttonLabel] = clickHandler; - return buttonsAndText(textContent, buttonMap); -} - -function DemoUser(name) { - this.name = name; - this.olmAccount = new Olm.Account(); - this.olmAccount.create(); - - this.idKey = this.getIdKeys()["curve25519"]; - - /* the people in our chat, indexed by their Curve25519 identity key. - */ - this.peers = {}; - - /* the Ed25519 signing key for each peer, indexed by their Curve25519 id key - */ - this.peerSigningKeys = {}; - - /* for each peer, a one-to-one session - indexed by id key and created on - * demand */ - this.peerSessions = {}; - - /* for each peer, info on their sender session - indexed by id key and - * session id */ - this.peerGroupSessions = {}; - - /* our outbound group session */ - this.groupSession = undefined; - - /* a list of pending tasks */ - this.tasks = []; - this.taskWorker = undefined; - - /* the operations our peers are allowed to do on us */ - var publicOps = [ - "getIdKeys", "getOneTimeKey", - "receiveOneToOne", "receiveGroup", - ]; - - this.remoteOps = {}; - for (var i=0; i "+result); - el2.appendChild(content); - - var body = JSON.parse(result); - - // create a new inbound session if we don't yet have one - if (!self.peerGroupSessions[sender] || - !self.peerGroupSessions[sender][body.session_id]) { - self.createInboundSession( - sender, body.session_id, body.session_key - ); - } - }); - }); - this.cipherInputDiv.appendChild(el); -}; - -/** - * add a task to decrypt a one-to-one message. Calls the callback with the - * decrypted plaintext - */ -DemoUser.prototype.decryptOneToOne = function(jsonpacket, callback) { - var self = this; - self.addTask("decrypt one-to-one message", function(done) { - var packet = JSON.parse(jsonpacket); - var peerId = packet.sender_key; - - var session = self.peerSessions[peerId]; - var plaintext; - if (session) { - plaintext = session.decrypt(packet.ciphertext.type, packet.ciphertext.body); - done(plaintext); - return; - } - - if (packet.ciphertext.type != 0) { - throw new Error("Unknown one-to-one session"); - } - - session = new Olm.Session(); - session.create_inbound(self.olmAccount, packet.ciphertext.body); - self.peerSessions[peerId] = session; - plaintext = session.decrypt(packet.ciphertext.type, packet.ciphertext.body); - done(plaintext); - }, callback) -}; - -/* ************************************************************************ - * - * group messaging - */ - - -/** - * retrieve, or initiate, an outbound group session - */ -DemoUser.prototype.getGroupSession = function() { - if (this.groupSession) { - return this.groupSession; - } - - this.groupSession = new Olm.OutboundGroupSession(); - this.groupSession.create(); - - var keymsg = { - "session_id": this.groupSession.session_id(), - "session_key": this.groupSession.session_key(), - "message_index": this.groupSession.message_index(), - }; - var jsonmsg = JSON.stringify(keymsg); - - for (var peer in this.peers) { - if (!this.peers.hasOwnProperty(peer)) { - continue; - } - this.sendToPeer(peer, jsonmsg); - } - - return this.groupSession; -}; - -/** - * add a task to create an inbound group session - */ -DemoUser.prototype.createInboundSession = function( - peer_id, session_id, session_key, callback -) { - var self = this; - this.addTask("init inbound session", function(done) { - session = new Olm.InboundGroupSession(); - session.create(session_key); - if (!self.peerGroupSessions[peer_id]) { - self.peerGroupSessions[peer_id] = {}; - } - if (session_id != session.session_id()) { - throw new Error("Mismatched session_ids"); - } - self.peerGroupSessions[peer_id][session_id] = session; - done(session); - }, callback); -}; - -/** - * handler for receiving a group message - */ -DemoUser.prototype.receiveGroup = function(jsonpacket) { - var self = this; - var el = buttonAndTextElement("decrypt", jsonpacket, function(ev) { - self.decryptGroup(jsonpacket, function(result) { - var el2 = document.createElement("tt"); - el.appendChild(el2); - - var content = document.createTextNode(" -> "+result); - el2.appendChild(content); - }); - }); - this.groupInputDiv.appendChild(el); -}; - -/** - * add a task to decrypt a received group message. Calls the callback with the - * decrypted plaintext - */ -DemoUser.prototype.decryptGroup = function(jsonpacket, callback) { - var self = this; - this.addTask("decrypt group message", function(done) { - var packet = JSON.parse(jsonpacket); - - var sender = packet.sender_key; - var session_id = packet.session_id; - - var sender_signing_key = self.peerSigningKeys[sender]; - if (!sender_signing_key) { - throw new Error("No known signing key for sender "+sender); - } - - var olmUtility = new Olm.Utility(); - olmUtility.ed25519_verify( - sender_signing_key, packet.body, packet.signature - ); - - var peer_sessions = self.peerGroupSessions[sender]; - if (!peer_sessions) { - throw new Error("No sessions for sender "+sender); - } - - var session = peer_sessions[session_id]; - if (!session) { - throw new Error("Unknown session id " + session_id); - } - - var result = session.decrypt(packet.body); - done(result.plaintext); - }, callback); -}; - - - -/** - * add a task to encrypt, and prepare for sending, a group message. - * - * Will create a group session if necessary - */ -DemoUser.prototype.encrypt = function(message) { - var self = this; - var session = this.getGroupSession(); - - function sendJsonToPeers(json) { - for (var peer in self.peers) { - if (!self.peers.hasOwnProperty(peer)) { - continue; - } - self.peers[peer].receiveGroup(json); - } - } - - - self.addTask("encrypt group message", function(done) { - var encrypted = session.encrypt(message); - var signature = self.olmAccount.sign(encrypted); - - var packet = { - sender_key: self.idKey, - session_id: session.session_id(), - body: encrypted, - signature: signature, - }; - var json = JSON.stringify(packet); - - var el = buttonsAndText(json, { - send: function(ev) { - sendJsonToPeers(json); - }, - "send corrupted": function(ev) { - var p = JSON.parse(json); - p.body += " "; - sendJsonToPeers(JSON.stringify(p)); - }, - }); - self.groupOutputDiv.appendChild(el); - done(); - }); -}; - - -/* ************************************************************************** */ - -function initUserDiv(demoUser, div) { - demoUser.progressElement = div.getElementsByClassName("user_progress")[0]; - demoUser.cipherOutputDiv = div.getElementsByClassName("user_cipher_output")[0]; - demoUser.cipherInputDiv = div.getElementsByClassName("user_cipher_input")[0]; - demoUser.groupOutputDiv = div.getElementsByClassName("group_output")[0]; - demoUser.groupInputDiv = div.getElementsByClassName("group_input")[0]; - - var plain_input = div.getElementsByClassName("user_plain_input")[0]; - var encrypt = div.getElementsByClassName("user_encrypt")[0]; - - encrypt.addEventListener("click", function() { - demoUser.encrypt(plain_input.value); - }, false); - -} - -function startDemo() { - var user1 = new DemoUser(); - initUserDiv(user1, document.getElementById("user1")); - - var user2 = new DemoUser(); - initUserDiv(user2, document.getElementById("user2")); - - var user3 = new DemoUser(); - initUserDiv(user3, document.getElementById("user3")); - - var user4 = new DemoUser(); - initUserDiv(user4, document.getElementById("user4")); - - user1.addPeer(user2.remoteOps); - user1.addPeer(user3.remoteOps); - user1.addPeer(user4.remoteOps); - - user2.addPeer(user1.remoteOps); - user2.addPeer(user3.remoteOps); - user2.addPeer(user4.remoteOps); - - user3.addPeer(user1.remoteOps); - user3.addPeer(user2.remoteOps); - user3.addPeer(user4.remoteOps); - - user4.addPeer(user1.remoteOps); - user4.addPeer(user2.remoteOps); - user4.addPeer(user3.remoteOps); -} - - -document.addEventListener("DOMContentLoaded", function() { - Olm.init().then(function() { - startDemo(); - }); -}, false); diff --git a/javascript/demo/one_to_one_demo.html b/javascript/demo/one_to_one_demo.html deleted file mode 100644 index a225e4a..0000000 --- a/javascript/demo/one_to_one_demo.html +++ /dev/null @@ -1,147 +0,0 @@ - - - - - -
-

Alice

-
-

Encryption

- - -
-

Decryption

- - -
-
-
-

Bob

-
-

Encryption

- - -
-

Decryption

- - -
-
- - -- cgit v1.2.3