diff --git a/chatto/src/main/javascript/ts/src/model/ChatModel.ts b/chatto/src/main/javascript/ts/src/model/ChatModel.ts index 300047e..6b082a8 100644 --- a/chatto/src/main/javascript/ts/src/model/ChatModel.ts +++ b/chatto/src/main/javascript/ts/src/model/ChatModel.ts @@ -126,4 +126,9 @@ export class ChatModel implements Subject { return cVMs; } + + public async isPassphraseValid(passphrase: string, userName: string): Promise { + let valid = await this._chatModelHelper.isPassphraseValid(passphrase, userName); + return valid; + } } \ No newline at end of file diff --git a/chatto/src/main/javascript/ts/src/model/ChatModelHelper.ts b/chatto/src/main/javascript/ts/src/model/ChatModelHelper.ts index c49856f..7eebf3b 100644 --- a/chatto/src/main/javascript/ts/src/model/ChatModelHelper.ts +++ b/chatto/src/main/javascript/ts/src/model/ChatModelHelper.ts @@ -12,26 +12,37 @@ export class ChatModelHelper { private readonly _notificationService: NotificationService; - constructor(encryptionService: EncryptionService, notificationService: NotificationService) { + constructor(encryptionService: EncryptionService, notificationService: NotificationService) { this._encryptionService = encryptionService; this._notificationService = notificationService; - } - + } + public async getMessages(userName: string, passphrase: string, page: number | null, lastMessageTime: string | null, op: string): Promise { switch (lastMessageTime) { case null: { - const data: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(userName, page!); - const cVMs = Promise.all(data.map(vm => this._toChatMessageVMAsync(vm, passphrase)).reverse()); - return cVMs; + const data: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(userName, page!); + const cVMs = Promise.all(data.map(vm => this._toChatMessageVMAsync(vm, passphrase)).reverse()); + return cVMs; } default: { - const data: ChatMessageDTO[] = await this._getNewMessagesAjax(userName, lastMessageTime); + const data: ChatMessageDTO[] = await this._getNewMessagesAjax(userName, lastMessageTime!); return data.map(vm => this._toChatMessageVM(vm, passphrase)); } } } + public async isPassphraseValid(passphrase: string, userName: string): Promise { + const messages: ChatMessageDTO[] = await this._getPaginatedMessagesAjax(userName, 0); + if(messages.length === 0) return true; + try { + this._encryptionService.decrypt(passphrase, messages[0].messageCipher) + } catch (error) { + return false; + } + return true + } + private async _toChatMessageVMAsync(chatMessageDTO: ChatMessageDTO, passphrase: string): Promise { const vm = new ChatMessageViewModel(); vm.fromUser = chatMessageDTO.fromUser; diff --git a/chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts b/chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts index e81a62b..cf92d56 100644 --- a/chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts +++ b/chatto/src/main/javascript/ts/src/service/AlertifyNotificationService.ts @@ -30,7 +30,7 @@ export class AlertifyNotificationService implements NotificationService { } passphrasePrompt(vm: ActiveUserViewModel, vms: ActiveUserViewModel[], cb1: (contactName: string, passphrase: string, lastMessageTime: string | null, op: string) => void, - cb2: () => any): void { + cb2: () => any, cb3: (...x: any) => any): void { // alertify.myprompt || alertify.dialog('myprompt', function () { @@ -91,9 +91,15 @@ export class AlertifyNotificationService implements NotificationService { bootbox.prompt({ title: "Please enter the passphrase", inputType: 'password', - callback: function (result) { + callback: async function (result) { if (result) { log.debug(result); + const valid = await cb3(result, vm.userName); + if (!valid) { + bootbox.alert("Some error occured. Please check your password"); + log.error("invalid password"); + return; + } cb1(vm.userName!, result, null, "new"); vm.unlocked = true vms.filter(v => v.userName == vm.userName).map(v => { v.passphrase = result; v.unlocked = true }) diff --git a/chatto/src/main/javascript/ts/src/service/NotificationService.ts b/chatto/src/main/javascript/ts/src/service/NotificationService.ts index 5c97f86..101d59e 100644 --- a/chatto/src/main/javascript/ts/src/service/NotificationService.ts +++ b/chatto/src/main/javascript/ts/src/service/NotificationService.ts @@ -6,5 +6,8 @@ export interface NotificationService { errorWithDelay(message: string, delay: number): void; warning(message: string): void; message(message: string): void; - passphrasePrompt(vm: ActiveUserViewModel, vms: ActiveUserViewModel[], cb1: (...x: any) => any, cb2: (...x: any) => any): void; + passphrasePrompt(vm: ActiveUserViewModel, vms: ActiveUserViewModel[], + cb1: (...x: any) => any, + cb2: (...x: any) => any, + cb3: (...x: any) => any): void; } \ No newline at end of file diff --git a/chatto/src/main/javascript/ts/src/view/ChatView.ts b/chatto/src/main/javascript/ts/src/view/ChatView.ts index b9aa89f..7dd4269 100644 --- a/chatto/src/main/javascript/ts/src/view/ChatView.ts +++ b/chatto/src/main/javascript/ts/src/view/ChatView.ts @@ -138,6 +138,7 @@ export class ChatView implements Observer { const chatInput = document.getElementById('chatInput') as HTMLInputElement; const passphraseInput = document.getElementById('passphrase') as HTMLInputElement; + const passphrase = this._userModel.activeUsersList.find(u => u.userName == JsonAPI.contactName)?.passphrase if (chatInput.value == '' || chatInput.value == null) { this._notificationService.error("Please enter a message"); @@ -145,11 +146,11 @@ export class ChatView implements Observer { return; } - if (passphraseInput.value == '' || passphraseInput.value == null) { - this._notificationService.error("Please enter a passphrase"); - log.error("Passphrase is null."); - return; - } + // if (passphraseInput.value == '' || passphraseInput.value == null) { + // this._notificationService.error("Please enter a passphrase"); + // log.error("Passphrase is null."); + // return; + // } const messageContent = chatInput.value; const msgTime = new Date(); @@ -162,7 +163,7 @@ export class ChatView implements Observer { this.update({data: new Array(context), op: "new"}) - let messageCipher: MessageCipherDTO = this._encryptionService.encrypt(passphraseInput.value, messageContent) + let messageCipher: MessageCipherDTO = this._encryptionService.encrypt(passphrase!, messageContent) let chatMessageDTO = { "fromUser": JsonAPI.principleName || "", "toUser": contactName, diff --git a/chatto/src/main/javascript/ts/src/view/UserView.ts b/chatto/src/main/javascript/ts/src/view/UserView.ts index 9744a6b..ec5f75a 100644 --- a/chatto/src/main/javascript/ts/src/view/UserView.ts +++ b/chatto/src/main/javascript/ts/src/view/UserView.ts @@ -69,19 +69,19 @@ export class UserView implements Observer { let passphrase: string = ''; if (current.length > 0) { - let passphraseInput = document.getElementById('passphrase') as any; - - if (passphraseInput == null) { - log.error('passphraseInput element reference is null'); - return; - } - passphrase = passphraseInput.value - if (passphrase == '' || passphrase == null) { - // alert('Please input passphrase') - // alertify.error('Please enter a passphrase'); - log.error('passphrase is empty or null'); - return; - } + // let passphraseInput = document.getElementById('passphrase') as any; + + // if (passphraseInput == null) { + // log.error('passphraseInput element reference is null'); + // return; + // } + // passphrase = passphraseInput.value + // if (passphrase == '' || passphrase == null) { + // // alert('Please input passphrase') + // // alertify.error('Please enter a passphrase'); + // log.error('passphrase is empty or null'); + // return; + // } current[0].className = current[0].className.replace(" active", ""); } @@ -92,13 +92,13 @@ export class UserView implements Observer { log.error('passphraseInput element reference is null'); return; } - passphrase = elem.value; - if (passphrase == '' || passphrase == null) { - // // alert('Please input passphrase') - // // alertify.error('Please enter a passphrase'); - log.error('passphrase is empty or null'); - return; - } + // passphrase = elem.value; + // if (passphrase == '' || passphrase == null) { + // // // alert('Please input passphrase') + // // // alertify.error('Please enter a passphrase'); + // log.error('passphrase is empty or null'); + // return; + // } // @ts-ignore: Object is possibly 'null'. document.getElementById('no-user-selected').hidden = true; // @ts-ignore: Object is possibly 'null'. @@ -119,7 +119,7 @@ export class UserView implements Observer { } this._notificationService.passphrasePrompt(vm, this._model.activeUsersList, this._chatModel.getMessages.bind(this._chatModel), - this._promptHandler.bind(this)); + this._promptHandler.bind(this), this._chatModel.isPassphraseValid.bind(this._chatModel)); // this._chatModel.getMessages(userName, vm.passphrase, null, "new"); el.className += " active"; } diff --git a/chatto/src/main/resources/static/css/chat.css b/chatto/src/main/resources/static/css/chat.css index 4ea7881..4bffe1c 100644 --- a/chatto/src/main/resources/static/css/chat.css +++ b/chatto/src/main/resources/static/css/chat.css @@ -290,6 +290,10 @@ html { backdrop-filter: blur(6px); } +.modal-dialog { + color: #000000; +} + @media(max-width: 576px) { .contacts_card { margin-bottom: 15px !important; diff --git a/chatto/src/main/resources/templates/chat.html b/chatto/src/main/resources/templates/chat.html index 509de84..92e7da8 100644 --- a/chatto/src/main/resources/templates/chat.html +++ b/chatto/src/main/resources/templates/chat.html @@ -152,7 +152,7 @@
- +