A self hosted chat application with end-to-end encrypted messaging.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
2.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import { EncryptionService } from "../../../common/service/EncryptionService";
  2. import {
  3. ReencryptionDTO,
  4. DecryptedDTO,
  5. } from "../../../common/dto/ReencryptionDTO";
  6. import { Credentials } from "../../../common/global/Credentials";
  7. import { MessageCipher } from "../../../common/entity/MessageCipher";
  8. import log from "loglevel";
  9. import { NotificationService } from "../../../common/service/NotificationService";
  10. import { isPassphraseValid } from "../../../common/ajax/passphrase";
  11. import {
  12. getAllMessages,
  13. sendReencryptedMessages,
  14. } from "../../../common/ajax/messages";
  15. export async function changePassphrase(
  16. es: EncryptionService,
  17. ns: NotificationService
  18. ): Promise<void> {
  19. // $("#changePassphraseForm").val();
  20. const user =
  21. (document.getElementById("changePassphraseDropDown") as HTMLOptionElement)
  22. .value || "error";
  23. const passphraseOld: string = $("#passphraseOld").val() as string;
  24. const passphraseNew: string = $("#passphraseNew").val() as string;
  25. const valid = await isPassphraseValid(passphraseOld, user, es);
  26. if (!valid) {
  27. log.error("Please check your passphrase");
  28. ns.error("Please check your passphrase");
  29. return;
  30. }
  31. const messages = await getAllMessages(user, Credentials.authToken);
  32. const decrypted = Promise.all(
  33. messages.map(
  34. async (m): Promise<DecryptedDTO> => {
  35. const msg = await es.decryptAsPromise(passphraseOld, m.messageCipher);
  36. const m2 = { ...m, messageId: m.messageCipher.id, message: msg };
  37. return m2;
  38. }
  39. )
  40. );
  41. const reencrypted = (await decrypted).map((m) => {
  42. const mdto = es.encrypt(passphraseNew, m.message);
  43. const mc = <MessageCipher>{ id: m.messageId, ...mdto };
  44. const rdto = <ReencryptionDTO>{ ...m, messageCipher: mc };
  45. return rdto;
  46. });
  47. log.debug(reencrypted);
  48. /* Uncomment to see the reencryption result
  49. const decryptedAgain = Promise.all(
  50. reencrypted.map(async (m) => {
  51. return es.decryptAsPromise(passphraseNew, m.messageCipher);
  52. })
  53. );
  54. const deb = await decryptedAgain;
  55. log.debug(deb); */
  56. sendReencryptedMessages(reencrypted, Credentials.authToken);
  57. ns.success("Successfully changed passphrase");
  58. }