Further refactor and impl passphrase change again
This commit is contained in:
parent
cfa6dc3c0b
commit
9a4aca88b3
2
.gitignore
vendored
2
.gitignore
vendored
@ -33,6 +33,8 @@ build/
|
|||||||
node_modules
|
node_modules
|
||||||
bundle.js
|
bundle.js
|
||||||
bundle.min.js
|
bundle.min.js
|
||||||
|
adminBundle.js
|
||||||
|
adminBundle.min.js
|
||||||
worker.js
|
worker.js
|
||||||
node
|
node
|
||||||
src/main/javascript/node/
|
src/main/javascript/node/
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
import { changePassphrase } from "./pages/user/ChangePassphrase";
|
import { changePassphrase } from "./pages/user/ChangePassphrase";
|
||||||
import { EncryptionServiceFactory } from "../common/service/EncryptionServiceFactory";
|
import { EncryptionServiceFactory } from "../common/service/EncryptionServiceFactory";
|
||||||
import log from "loglevel";
|
import log from "loglevel";
|
||||||
|
import { AlertifyNotificationService } from "../common/service/AlertifyNotificationService";
|
||||||
|
|
||||||
log.setLevel("TRACE");
|
log.setLevel("TRACE");
|
||||||
const es = EncryptionServiceFactory.getEncryptionService();
|
const es = EncryptionServiceFactory.getEncryptionService();
|
||||||
|
const ns = new AlertifyNotificationService();
|
||||||
$("#changePassphraseForm").on("submit", (event) => {
|
$("#changePassphraseForm").on("submit", (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
changePassphrase(es);
|
changePassphrase(es, ns);
|
||||||
});
|
});
|
||||||
|
@ -1,6 +1,4 @@
|
|||||||
import { EncryptionService } from "../../../common/service/EncryptionService";
|
import { EncryptionService } from "../../../common/service/EncryptionService";
|
||||||
import { Routes } from "../../../common/routes/Routes";
|
|
||||||
import { ChatMessageDTO } from "../../../common/dto/ChatMessageDTO";
|
|
||||||
import {
|
import {
|
||||||
ReencryptionDTO,
|
ReencryptionDTO,
|
||||||
DecryptedDTO,
|
DecryptedDTO,
|
||||||
@ -8,8 +6,17 @@ import {
|
|||||||
import { Credentials } from "../../../common/global/Credentials";
|
import { Credentials } from "../../../common/global/Credentials";
|
||||||
import { MessageCipher } from "../../../common/entity/MessageCipher";
|
import { MessageCipher } from "../../../common/entity/MessageCipher";
|
||||||
import log from "loglevel";
|
import log from "loglevel";
|
||||||
|
import { NotificationService } from "../../../common/service/NotificationService";
|
||||||
|
import { isPassphraseValid } from "../../../common/ajax/passphrase";
|
||||||
|
import {
|
||||||
|
getAllMessages,
|
||||||
|
sendReencryptedMessages,
|
||||||
|
} from "../../../common/ajax/messages";
|
||||||
|
|
||||||
export async function changePassphrase(es: EncryptionService) {
|
export async function changePassphrase(
|
||||||
|
es: EncryptionService,
|
||||||
|
ns: NotificationService
|
||||||
|
): Promise<void> {
|
||||||
// $("#changePassphraseForm").val();
|
// $("#changePassphraseForm").val();
|
||||||
|
|
||||||
const user =
|
const user =
|
||||||
@ -19,7 +26,14 @@ export async function changePassphrase(es: EncryptionService) {
|
|||||||
const passphraseOld: string = $("#passphraseOld").val() as string;
|
const passphraseOld: string = $("#passphraseOld").val() as string;
|
||||||
const passphraseNew: string = $("#passphraseNew").val() as string;
|
const passphraseNew: string = $("#passphraseNew").val() as string;
|
||||||
|
|
||||||
log.debug(Credentials.authToken);
|
const valid = await isPassphraseValid(passphraseOld, user, es);
|
||||||
|
|
||||||
|
if (!valid) {
|
||||||
|
log.error("Please check your passphrase");
|
||||||
|
ns.error("Please check your passphrase");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const messages = await getAllMessages(user, Credentials.authToken);
|
const messages = await getAllMessages(user, Credentials.authToken);
|
||||||
|
|
||||||
const decrypted = Promise.all(
|
const decrypted = Promise.all(
|
||||||
@ -41,6 +55,7 @@ export async function changePassphrase(es: EncryptionService) {
|
|||||||
|
|
||||||
log.debug(reencrypted);
|
log.debug(reencrypted);
|
||||||
|
|
||||||
|
/* Uncomment to see the reencryption result
|
||||||
const decryptedAgain = Promise.all(
|
const decryptedAgain = Promise.all(
|
||||||
reencrypted.map(async (m) => {
|
reencrypted.map(async (m) => {
|
||||||
return es.decryptAsPromise(passphraseNew, m.messageCipher);
|
return es.decryptAsPromise(passphraseNew, m.messageCipher);
|
||||||
@ -48,49 +63,9 @@ export async function changePassphrase(es: EncryptionService) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const deb = await decryptedAgain;
|
const deb = await decryptedAgain;
|
||||||
log.debug(deb);
|
log.debug(deb); */
|
||||||
|
|
||||||
sendReencryptedMessages(reencrypted, Credentials.authToken);
|
sendReencryptedMessages(reencrypted, Credentials.authToken);
|
||||||
}
|
|
||||||
|
|
||||||
async function getAllMessages(user: string, authToken: string) {
|
ns.success("Successfully changed passphrase");
|
||||||
let headers = new Headers();
|
|
||||||
// headers.append('Accept','application/json')
|
|
||||||
// headers.append('Content-Type', 'application/json');
|
|
||||||
headers.append("X-AUTH-TOKEN", authToken);
|
|
||||||
let response = await fetch(`${Routes.Admin.getAllMessagesURL}${user}`, {
|
|
||||||
method: "GET",
|
|
||||||
headers: headers,
|
|
||||||
});
|
|
||||||
return response.json() as Promise<ReencryptionDTO[]>;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getAllRegularUsers(authToken: string) {
|
|
||||||
let headers = new Headers();
|
|
||||||
// headers.append('Accept','application/json')
|
|
||||||
// headers.append('Content-Type', 'application/json');
|
|
||||||
headers.append("X-AUTH-TOKEN", authToken);
|
|
||||||
let response = await fetch(`${Routes.Admin.getAllRegularUsersURL}`, {
|
|
||||||
method: "GET",
|
|
||||||
headers: headers,
|
|
||||||
});
|
|
||||||
let data = (await response.json()) as string[];
|
|
||||||
return data;
|
|
||||||
}
|
|
||||||
|
|
||||||
function sendReencryptedMessages(
|
|
||||||
rrencryptionDTOs: ReencryptionDTO[],
|
|
||||||
authToken: string
|
|
||||||
) {
|
|
||||||
let headers = new Headers();
|
|
||||||
// console.log("Token = " + btoa("hmm" + ":" + "hmm"))
|
|
||||||
|
|
||||||
// headers.append('Accept','application/json')
|
|
||||||
headers.append("Content-Type", "application/json");
|
|
||||||
headers.append("X-AUTH-TOKEN", authToken);
|
|
||||||
fetch(Routes.Admin.reencryptURL, {
|
|
||||||
method: "POST",
|
|
||||||
headers: headers,
|
|
||||||
body: JSON.stringify(rrencryptionDTOs),
|
|
||||||
}).then((response) => console.log(response));
|
|
||||||
}
|
}
|
||||||
|
82
src/main/frontend/common/ajax/messages.ts
Normal file
82
src/main/frontend/common/ajax/messages.ts
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
import { Sprintf } from "../global/Sprintf";
|
||||||
|
import { Routes } from "../routes/Routes";
|
||||||
|
import { ReencryptionDTO } from "../dto/ReencryptionDTO";
|
||||||
|
import { ChatMessageDTO } from "../dto/ChatMessageDTO";
|
||||||
|
import { JsonAPI } from "../../chat/singleton/JsonAPI";
|
||||||
|
import * as log from "loglevel";
|
||||||
|
|
||||||
|
export async function getAllMessages(user: string, authToken: string) {
|
||||||
|
let headers = new Headers();
|
||||||
|
// headers.append('Accept','application/json')
|
||||||
|
// headers.append('Content-Type', 'application/json');
|
||||||
|
headers.append("X-AUTH-TOKEN", authToken);
|
||||||
|
let response = await fetch(`${Routes.Admin.getAllMessagesURL}${user}`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
return response.json() as Promise<ReencryptionDTO[]>;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getAllRegularUsers(authToken: string) {
|
||||||
|
let headers = new Headers();
|
||||||
|
// headers.append('Accept','application/json')
|
||||||
|
// headers.append('Content-Type', 'application/json');
|
||||||
|
headers.append("X-AUTH-TOKEN", authToken);
|
||||||
|
let response = await fetch(`${Routes.Admin.getAllRegularUsersURL}`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
let data = (await response.json()) as string[];
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function sendReencryptedMessages(
|
||||||
|
rencryptionDTOs: ReencryptionDTO[],
|
||||||
|
authToken: string
|
||||||
|
) {
|
||||||
|
let headers = new Headers();
|
||||||
|
// console.log("Token = " + btoa("hmm" + ":" + "hmm"))
|
||||||
|
|
||||||
|
// headers.append('Accept','application/json')
|
||||||
|
headers.append("Content-Type", "application/json");
|
||||||
|
headers.append("X-AUTH-TOKEN", authToken);
|
||||||
|
fetch(Routes.Admin.reencryptURL, {
|
||||||
|
method: "POST",
|
||||||
|
headers: headers,
|
||||||
|
body: JSON.stringify(rencryptionDTOs),
|
||||||
|
}).then((response) => console.log(response));
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function getOneMessage(
|
||||||
|
toUser: string,
|
||||||
|
page: number
|
||||||
|
): Promise<ChatMessageDTO[]> {
|
||||||
|
const headers = new Headers();
|
||||||
|
if (JsonAPI.authToken == null) {
|
||||||
|
log.error("authToken null");
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
headers.append("X-AUTH-TOKEN", JsonAPI.authToken);
|
||||||
|
const url = Sprintf(JsonAPI.CHAT_MESSAGE_PAGE_GET, toUser, page, 1);
|
||||||
|
log.debug(url);
|
||||||
|
const response = await fetch(url, {
|
||||||
|
method: "GET",
|
||||||
|
headers: headers,
|
||||||
|
});
|
||||||
|
log.debug(response.clone());
|
||||||
|
// if (fetchErrorHandler(response.clone(), this._notificationService)) {
|
||||||
|
// return null;
|
||||||
|
// }
|
||||||
|
const data: Promise<any> = await response.json();
|
||||||
|
function func(data: any) {
|
||||||
|
const d1 = data.map((d: any) => {
|
||||||
|
if (d.messageTime == null) return null;
|
||||||
|
|
||||||
|
d.messageTime = new Date(d.messageTime);
|
||||||
|
return d;
|
||||||
|
});
|
||||||
|
return d1;
|
||||||
|
}
|
||||||
|
const data2 = func(data);
|
||||||
|
return data2;
|
||||||
|
}
|
20
src/main/frontend/common/ajax/passphrase.ts
Normal file
20
src/main/frontend/common/ajax/passphrase.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import { EncryptionService } from "../service/EncryptionService";
|
||||||
|
import { ChatMessageDTO } from "../dto/ChatMessageDTO";
|
||||||
|
import * as log from "loglevel";
|
||||||
|
import { getOneMessage } from "./messages";
|
||||||
|
|
||||||
|
export async function isPassphraseValid(
|
||||||
|
passphrase: string,
|
||||||
|
userName: string,
|
||||||
|
es: EncryptionService
|
||||||
|
): Promise<boolean> {
|
||||||
|
const messages: ChatMessageDTO[] = await getOneMessage(userName, 0);
|
||||||
|
if (messages.length === 0) return true;
|
||||||
|
try {
|
||||||
|
es.decrypt(passphrase, messages[0].messageCipher);
|
||||||
|
} catch (error) {
|
||||||
|
log.debug("here");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user