aboutsummaryrefslogtreecommitdiffhomepage
path: root/public
diff options
context:
space:
mode:
Diffstat (limited to 'public')
-rw-r--r--public/scripts/chat.js74
-rw-r--r--public/scripts/encryption.js29
2 files changed, 58 insertions, 45 deletions
diff --git a/public/scripts/chat.js b/public/scripts/chat.js
index 3123926..a4bf74f 100644
--- a/public/scripts/chat.js
+++ b/public/scripts/chat.js
@@ -16,35 +16,19 @@ let connectedPeers = []; // TODO: Save new peers in array
const generator = new xkcdPassword();
generator.initWithWordList(wordList);
-// setup encryption
+/**
+ * Sets up encryption, user etc.
+ */
(async () => {
- peerId = await generator.generate().then(words => words.join('-'));
+ // generate peerId
+ if (localStorage.getItem('peer_id') === null) {
+ peerId = await generator.generate().then(words => words.join('-'));
+ localStorage.setItem('peer_id', peerId);
+ } else
+ peerId = localStorage.getItem('peer_id');
+
encryption.setup();
- if (localStorage.getItem('database') === 'success' && await encryption.check()) {
- pinInput.init(async (pin, tryCount) => {
- try {
- await encryption.decryptPrivate(await encryption.getPrivate(), pin);
- chat()
- } catch (e) {
- if (tryCount === 3) {
- encryption.reset();
- console.error('Too many tries!');
- pinInput.failure('Account was deleted, this site will reload.');
- setTimeout(() => location.reload(), 1500)
- } else {
- console.error('Passphrase is wrong!');
- pinInput.failure('Passphrase is wrong!');
- }
- }
- });
- } else {
- pinInput.init(pin => {
- console.log('[LOG] No existing keys found! Generating...');
- pinInput.generate();
- passphrase = pin;
- (async () => await encryption.generate(peerId, passphrase).then(() => chat()))()
- });
- }
+ await evaluateKeyGeneration();
})();
/**
@@ -141,4 +125,38 @@ function chat() {
});
}
-//encryption.test(); // TESTING IF ENCRYPTION WORKS
+/**
+ * Evaluates whether a key generation is needed and initializes regarding actions
+ * @returns {Promise<void>}
+ */
+async function evaluateKeyGeneration() {
+ if (localStorage.getItem('database') === 'success' && await encryption.check()) {
+ pinInput.init(async (pin, tryCount) => {
+ try {
+ if (await encryption.getId(await encryption.getPublic()) !== peerId) throw "Not verified!";
+ await encryption.decryptPrivate(await encryption.getPrivate(), pin);
+ chat()
+ } catch (e) { // decrypting failed
+ if (tryCount === 3) {
+ encryption.reset();
+ console.error('Too many tries!');
+ pinInput.failure('This account got removed, the site will reload.');
+ setTimeout(() => location.reload(), 1500)
+ } else if (e === 'Not verified!') {
+ console.error(e);
+ pinInput.failure(e);
+ } else {
+ console.error('Passphrase is wrong!');
+ pinInput.failure('Passphrase is wrong!');
+ }
+ }
+ });
+ } else {
+ pinInput.init(pin => {
+ console.log('[LOG] No existing keys found! Generating...');
+ pinInput.generate();
+ passphrase = pin;
+ (async () => await encryption.generate(peerId, passphrase).then(() => chat()))()
+ });
+ }
+}
diff --git a/public/scripts/encryption.js b/public/scripts/encryption.js
index 81a592a..323a5e0 100644
--- a/public/scripts/encryption.js
+++ b/public/scripts/encryption.js
@@ -163,8 +163,7 @@ async function getPeerPublicKey(peerId) {
let publicKey;
if (res.length > 0) {
publicKey = res[0]['key_data'];
- const publicKeyUserId = (await (await openpgp.key.readArmored(publicKey)).keys[0].getPrimaryUser()
- .then(obj => obj.user.userId.userid));
+ const publicKeyUserId = await getPublicKeyUserId(publicKey);
if (publicKeyUserId !== peerId) {
publicKey = '';
console.error('[LOG] Public key verification failed! The peers real identity is ' + publicKeyUserId)
@@ -177,28 +176,24 @@ async function getPeerPublicKey(peerId) {
}
/**
+ * Returns user id of a public key
+ * @param publicKey
+ * @returns {Promise<String>}
+ */
+async function getPublicKeyUserId(publicKey) {
+ return await (await openpgp.key.readArmored(publicKey)).keys[0].getPrimaryUser().then(obj => obj.user.userId.userid) || '';
+}
+
+/**
* Resets the database/encryption
*/
function reset() {
db.delete();
localStorage.removeItem('database');
+ localStorage.removeItem('peer_id');
console.log('[LOG] Database has been deleted!')
}
-/**
- * Just a general test case
- */
-function testEncryption() {
- generateKeys('test_id', 'supersecure').then(() => {
- encrypt('The meaning of life', getPublicKey()).then(encrypted => {
- decrypt(encrypted, getPublicKey(), getPrivateKey(), 'supersecure').then(decrypted => {
- if (decrypted === 'The meaning of life')
- console.log("YEEHA, Test succeeded!")
- })
- })
- })
-}
-
exports.setup = setupDatabase;
exports.generate = generateKeys;
exports.getPrivate = getPrivateKey;
@@ -209,5 +204,5 @@ exports.decryptPrivate = decryptPrivateKey;
exports.check = isEncrypted;
exports.store = storePeerPublicKey;
exports.get = getPeerPublicKey;
+exports.getId = getPublicKeyUserId;
exports.reset = reset;
-exports.test = testEncryption;