Functional E2EE submitting

This commit is contained in:
Jiiks 2018-08-10 16:22:30 +03:00
parent 3c83dbe0ff
commit 3d4204f080
3 changed files with 48 additions and 7 deletions

View File

@ -8,8 +8,9 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
import { Settings } from 'modules';
import BuiltinModule from './BuiltinModule'; import BuiltinModule from './BuiltinModule';
import { WebpackModules, ReactComponents, MonkeyPatch, Patcher } from 'modules'; import { WebpackModules, ReactComponents, MonkeyPatch, Patcher, DiscordApi } from 'modules';
import { VueInjector, Reflection } from 'ui'; import { VueInjector, Reflection } from 'ui';
import E2EEComponent from './E2EEComponent.vue'; import E2EEComponent from './E2EEComponent.vue';
import aes256 from 'aes256'; import aes256 from 'aes256';
@ -20,10 +21,32 @@ export default new class E2EE extends BuiltinModule {
return ['security', 'default', 'e2ee']; return ['security', 'default', 'e2ee'];
} }
get master() {
return 'temporarymasterkey';
}
get database() {
return Settings.getSet('security').settings.find(s => s.id === 'e2eedb').settings[0].value;
}
encrypt(key, content, prefix = '$:') {
return prefix + aes256.encrypt(key, content);
}
decrypt(key, content, prefix = '$:') {
return aes256.decrypt(key, content.substr(2));
}
getKey(channelId) {
const haveKey = this.database.find(kvp => kvp.value.key === channelId);
if (!haveKey) return null;
return haveKey.value.value;
}
async enabled(e) { async enabled(e) {
const ctaComponent = await ReactComponents.getComponent('ChannelTextArea'); const ctaComponent = await ReactComponents.getComponent('ChannelTextArea');
MonkeyPatch('BD:E2EE', ctaComponent.component.prototype).after('render', this.render); MonkeyPatch('BD:E2EE', ctaComponent.component.prototype).after('render', this.render);
MonkeyPatch('BD:E2EE', ctaComponent.component.prototype).before('handleSubmit', this.handleSubmit); MonkeyPatch('BD:E2EE', ctaComponent.component.prototype).before('handleSubmit', this.handleSubmit.bind(this));
} }
render(component, args, retVal) { render(component, args, retVal) {
@ -34,7 +57,9 @@ export default new class E2EE extends BuiltinModule {
} }
handleSubmit(component, args, retVal) { handleSubmit(component, args, retVal) {
component.props.value = aes256.encrypt('randomkey', component.props.value); const key = this.getKey(DiscordApi.currentChannel.id);
if (!key) return;
component.props.value = this.encrypt(this.decrypt(this.master, key), component.props.value);
} }
disabled(e) { disabled(e) {

View File

@ -12,17 +12,17 @@
<div class="bd-e2eeTaContainer"> <div class="bd-e2eeTaContainer">
<template v-if="error"> <template v-if="error">
<div class="bd-e2eeTaBtn bd-e2eeLock bd-error"> <div class="bd-e2eeTaBtn bd-e2eeLock bd-error">
<MiLock v-tooltip="'E2EE ERROR!'" /> <MiLock v-tooltip="error" />
</div> </div>
</template> </template>
<template v-else-if="state === 'loading'"> <template v-else-if="state === 'loading'">
<div class="bd-e2eeTaBtn bd-e2eeLock bd-warn"> <div class="bd-e2eeTaBtn bd-e2eeLock bd-warn">
<MiLock v-tooltip="'E2EE is loading'" /> <MiLock v-tooltip="'Loading'" />
</div> </div>
</template> </template>
<template v-else> <template v-else>
<div class="bd-e2eeTaBtn bd-e2eeLock bd-ok"> <div class="bd-e2eeTaBtn bd-e2eeLock bd-ok">
<MiLock v-tooltip="'E2EE Ready!'" /> <MiLock v-tooltip="'Ready!'" />
</div> </div>
</template> </template>
<div class="bd-taDivider"></div> <div class="bd-taDivider"></div>
@ -30,6 +30,8 @@
</template> </template>
<script> <script>
import { E2EE } from 'builtin';
import { DiscordApi } from 'modules';
import { MiLock } from '../ui/components/common/MaterialIcon'; import { MiLock } from '../ui/components/common/MaterialIcon';
export default { export default {
components: { MiLock }, components: { MiLock },
@ -39,6 +41,19 @@
error: null error: null
}; };
}, },
methods: {} methods: {},
mounted() {
if (!E2EE.master) {
this.error = 'No master key set!';
return;
}
const haveKey = E2EE.getKey(DiscordApi.currentChannel.id);
if (!haveKey) {
this.error = 'No key for channel!';
return;
}
this.state = 'OK';
this.error = null;
}
} }
</script> </script>

View File

@ -3,3 +3,4 @@ export { default as ReactDevtoolsModule } from './ReactDevtoolsModule';
export { default as VueDevtoolsModule } from './VueDevToolsModule'; export { default as VueDevtoolsModule } from './VueDevToolsModule';
export { default as TrackingProtection } from './TrackingProtection'; export { default as TrackingProtection } from './TrackingProtection';
export { default as BuiltinManager } from './Manager'; export { default as BuiltinManager } from './Manager';
export { default as E2EE } from './E2EE';