Skip to content

Commit

Permalink
Automatically import to existing codes #194
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Mar 27, 2022
1 parent e4de870 commit b1e67ac
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 57 deletions.
55 changes: 48 additions & 7 deletions app/application/src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,8 @@ if (!fs.existsSync(path.join(folder_path, "codes", "codes.authme"))) {
document.querySelector("#choose").style.display = "block"
}

// eslint-disable-next-line
let saved_codes = false
let text
let save_text
let save_text = ""
const query = []
const description_query = []
const name_query = []
Expand Down Expand Up @@ -96,10 +94,9 @@ const loadFile = () => {
const /** @type{LibAuthmeFile} */ loaded = JSON.parse(fs.readFileSync(filepath.toString(), "utf-8"))

if (loaded.role === "import" || loaded.role === "export") {
text = Buffer.from(loaded.codes, "base64").toString()
save_text = text
save_text = Buffer.from(loaded.codes, "base64").toString()

processData(text)
processData(save_text)
} else {
dialog.showMessageBox({
title: "Authme",
Expand All @@ -119,13 +116,57 @@ const loadFile = () => {
* Automatically import when creating import file
* @param {string} res
*/
const importedCodes = (res) => {
const importCodes = (res) => {
const text = Buffer.from(res, "base64").toString()
save_text = text

processData(text)
}

/**
* Automatically import when creating import file
* @param {string} res
*/
const importExistingCodes = async (res) => {
let password
let key

if (settings.security.require_password === true) {
password = Buffer.from(await ipc.invoke("request_password"))
key = Buffer.from(aes.generateKey(password, Buffer.from(settings.security.key, "base64")))
} else {
const /** @type{LibStorage} */ storage = dev ? JSON.parse(localStorage.getItem("dev_storage")) : JSON.parse(localStorage.getItem("storage"))

password = Buffer.from(storage.password, "base64")
key = Buffer.from(aes.generateKey(password, Buffer.from(storage.key, "base64")))
}

fs.readFile(path.join(folder_path, "codes", "codes.authme"), async (err, content) => {
if (err) {
logger.error(err)
}

const codes_file = JSON.parse(content)

const decrypted = aes.decrypt(Buffer.from(codes_file.codes, "base64"), key)

const text = Buffer.from(res, "base64").toString()
save_text = decrypted + text

for (let i = 0; i < query.length; i++) {
document.querySelector(`#codes${i}`).remove()
}

document.querySelector("#save").style.display = "block"

processData(save_text)

decrypted.fill(0)
password.fill(0)
key.fill(0)
})
}

/**
* Process data from saved source
* @param {string} text
Expand Down
43 changes: 19 additions & 24 deletions app/import/src/js/images.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,34 +39,29 @@ module.exports = {
if (images.length === i + 1) {
const save_exists = fs.existsSync(path.join(folder_path, "codes", "codes.authme"))

if (save_exists === true) {
await dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.close],
type: "info",
noLink: true,
defaultId: 0,
message: `${lang.import_dialog.correct_qrcode_found_0} ${lang.import_dialog.correct_qrcode_found_1}`,
})
const result = await dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.yes, lang.button.no],
type: "info",
noLink: true,
defaultId: 1,
cancelId: 1,
message: `${lang.import_dialog.correct_qrcode_found_0} ${lang.import_dialog.correct_qrcode_found_1}`,
})

if (result.response === 0) {
if (save_exists === true) {
ipc.invoke("importExistingCodes", Buffer.from(string).toString("base64"))
} else {
ipc.invoke("importCodes", Buffer.from(string).toString("base64"))
}

saveFile(string)
} else {
const result = await dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.yes, lang.button.no],
type: "info",
noLink: true,
defaultId: 1,
cancelId: 1,
message: `${lang.import_dialog.correct_qrcode_found_2} ${lang.import_dialog.correct_qrcode_found_3}`,
})

if (result.response === 1) {
ipc.invoke("importedCodes", Buffer.from(string).toString("base64"))
if (save_exists === true) {
ipc.invoke("importExistingCodes", Buffer.from(string).toString("base64"))
} else {
ipc.invoke("importedCodes", Buffer.from(string).toString("base64"))

saveFile(string)
ipc.invoke("importCodes", Buffer.from(string).toString("base64"))
}
}
}
Expand Down
43 changes: 19 additions & 24 deletions app/import/src/js/webcam.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,29 @@ module.exports = {

const save_exists = fs.existsSync(path.join(folder_path, "codes", "codes.authme"))

if (save_exists === true) {
await dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.close],
type: "info",
noLink: true,
defaultId: 0,
message: `${lang.import_dialog.correct_qrcode_found_0} ${lang.import_dialog.correct_qrcode_found_1}`,
})
const result = await dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.yes, lang.button.no],
type: "info",
noLink: true,
defaultId: 1,
cancelId: 1,
message: `${lang.import_dialog.correct_qrcode_found_0} ${lang.import_dialog.correct_qrcode_found_1}`,
})

if (result.response === 0) {
if (save_exists === true) {
ipc.invoke("importExistingCodes", Buffer.from(string).toString("base64"))
} else {
ipc.invoke("importCodes", Buffer.from(string).toString("base64"))
}

saveFile(string)
} else {
const result = await dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.yes, lang.button.no],
type: "info",
noLink: true,
defaultId: 1,
cancelId: 1,
message: `${lang.import_dialog.correct_qrcode_found_2} ${lang.import_dialog.correct_qrcode_found_3}`,
})

if (result.response === 1) {
ipc.invoke("importedCodes", Buffer.from(string).toString("base64"))
if (save_exists === true) {
ipc.invoke("importExistingCodes", Buffer.from(string).toString("base64"))
} else {
ipc.invoke("importedCodes", Buffer.from(string).toString("base64"))

saveFile(string)
ipc.invoke("importCodes", Buffer.from(string).toString("base64"))
}
}
} else {
Expand Down
15 changes: 13 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1588,11 +1588,22 @@ ipc.handle("statistics", () => {
/**
* Receive imported codes and send to application
*/
ipc.handle("importedCodes", (event, res) => {
ipc.handle("importCodes", (event, res) => {
window_application.webContents.executeJavaScript("location.reload()")

setTimeout(() => {
window_application.webContents.executeJavaScript(`importedCodes("${res}")`)
window_application.webContents.executeJavaScript(`importCodes("${res}")`)
}, 150)
})

/**
* Receive imported codes and send to application
*/
ipc.handle("importExistingCodes", (event, res) => {
window_application.webContents.executeJavaScript("location.reload()")

setTimeout(() => {
window_application.webContents.executeJavaScript(`importExistingCodes("${res}")`)
}, 150)
})

Expand Down

0 comments on commit b1e67ac

Please sign in to comment.