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.

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