Skip to content
This repository has been archived by the owner on Oct 19, 2022. It is now read-only.

Commit

Permalink
Import/Export page new encryption
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Aug 31, 2022
1 parent bf19043 commit 0a2e002
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
67 changes: 31 additions & 36 deletions interface/windows/edit/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { textConverter } from "../../libraries/convert"
import { encrypt, decrypt, generateMasterKey } from "../../libraries/auth"
import { dialog, fs, path } from "@tauri-apps/api"
import { getSettings, setSettings } from "../../stores/settings"
import { generateTimestamp } from "../../libraries/time"
import { getState, setState } from "../../stores/state"
import { getState } from "../../stores/state"
import { navigate } from "../../libraries/navigate"
import { decryptData, encryptData } from "interface/libraries/encryption"

const state = getState()
const settings = getSettings()
let names: string[] = []
let issuers: string[] = []
let secrets: string[] = []

/**
* Generate the edit elements from the saved codes
*/
const generateEditElements = () => {
document.querySelector(".editSavedCodes").style.display = "block"
document.querySelector(".loadedCodes").style.display = "block"
Expand Down Expand Up @@ -64,26 +68,19 @@ const generateEditElements = () => {
}
}

/**
* Load the saved codes
*/
export const loadSavedCodes = async () => {
const settings = getSettings()
const filePath = await path.join(await path.configDir(), "Levminer", "Authme 4", "codes", "codes.authme")

let file: LibAuthmeFile
const codes = settings.vault.codes
const encryptionKey = state.encryptionKey

try {
file = JSON.parse(await fs.readTextFile(filePath))
} catch (error) {
if (codes === null) {
return dialog.message("No save file found. \n\nGo to the codes or the import page and import your codes!", { type: "error" })
}

const password = Buffer.from(settings.security.password, "base64")
const key = Buffer.from(settings.security.key, "base64")

const masterKey = await generateMasterKey(password, key)

const decrypted = await decrypt(file.codes, masterKey)

const data = textConverter(decrypted, 0)
const decryptedText = await decryptData(encryptionKey, codes)
const data = textConverter(decryptedText, 0)

names = data.names
issuers = data.issuers
Expand All @@ -92,43 +89,35 @@ export const loadSavedCodes = async () => {
generateEditElements()
}

/**
* Save the current changes
*/
export const saveChanges = async () => {
const confirm = await dialog.ask("Are you sure you want to save the changes? \n\nThis will overwrite your saved codes!", { type: "warning" })

if (confirm === false) {
return
}

const settings = getSettings()

let saveText = ""

for (let i = 0; i < names.length; i++) {
const string = `\nName: ${names[i]} \nSecret: ${secrets[i]} \nIssuer: ${issuers[i]} \nType: OTP_TOTP\n`
saveText += string
}

const password = Buffer.from(settings.security.password, "base64")
const key = Buffer.from(settings.security.key, "base64")
const encryptionKey = state.encryptionKey
const encryptedText = await encryptData(encryptionKey, saveText)

const masterKey = await generateMasterKey(password, key)

const encrypted = await encrypt(saveText, masterKey)

const fileContents: LibAuthmeFile = {
codes: encrypted,
encrypted: true,
version: 3,
role: "codes",
date: generateTimestamp(),
}
const filePath = await path.join(await path.configDir(), "Levminer", "Authme 4", "codes", "codes.authme")

await fs.writeTextFile(filePath, JSON.stringify(fileContents, null, "\t"))
settings.vault.codes = encryptedText
setSettings(settings)

navigate("codes")
}

/**
* Revert all current changes
*/
export const revertChanges = async () => {
const confirm = await dialog.ask("Are you sure you want to revert all changes? \n\nYou will lose all current changes!", { type: "warning" })

Expand Down Expand Up @@ -159,6 +148,9 @@ export const deleteCodes = async () => {
}
}

/**
* Edit a specific code
*/
export const editCode = (id: number) => {
const issuer: HTMLInputElement = document.querySelector(`#issuer${id}`)
const name: HTMLInputElement = document.querySelector(`#name${id}`)
Expand Down Expand Up @@ -190,6 +182,9 @@ export const editCode = (id: number) => {
}
}

/**
* Delete a specific code
*/
export const deleteCode = async (id: number) => {
const res = await dialog.ask("Are you sure you want to delete this code? \n\nYou can save or revert this change at the top of the page.", { type: "warning" })

Expand Down
33 changes: 20 additions & 13 deletions interface/windows/export/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
import { fs, path, dialog } from "@tauri-apps/api"
import { generateMasterKey, decrypt } from "../../libraries/auth"
import { fs, dialog } from "@tauri-apps/api"
import { generateTimestamp } from "../../libraries/time"
import { textConverter } from "../../libraries/convert"
import { getSettings } from "../../stores/settings"
import qrcode from "qrcode-generator"
import { getState } from "interface/stores/state"
import { decryptData } from "interface/libraries/encryption"

let codesArray: LibImportFile
let codesText: string

/**
* Export the saved codes
*/
export const exportCodes = async () => {
const settings = getSettings()
const filePath = await path.join(await path.configDir(), "Levminer", "Authme 4", "codes", "codes.authme")
const state = getState()

try {
const saveFile: LibAuthmeFile = JSON.parse(await fs.readTextFile(filePath))
const codes = settings.vault.codes
const encryptionKey = state.encryptionKey

if (codes !== null) {
document.querySelector(".saveExportedCodes").style.display = "block"
document.querySelector(".exportCodes").style.display = "none"

const password = Buffer.from(settings.security.password, "base64")
const key = Buffer.from(settings.security.key, "base64")
const decryptedText = await decryptData(encryptionKey, codes)

const masterKey = await generateMasterKey(password, key)
const decrypted = await decrypt(saveFile.codes, masterKey)

codesArray = textConverter(decrypted, 0)
codesText = decrypted
} catch (error) {
codesArray = textConverter(decryptedText, 0)
codesText = decryptedText
} else {
return dialog.message("No save file found. \n\nGo to the codes or the import page and import your codes!", { type: "error" })
}
}

/**
* Save the exported codes as an .authme file
*/
export const exportAuthmeFile = async () => {
const saveFile: LibAuthmeFile = {
role: "codes",
Expand All @@ -47,6 +51,9 @@ export const exportAuthmeFile = async () => {
}
}

/**
* Save the exported codes as an .html file with pictures
*/
export const exportHtmlFile = async () => {
const names = codesArray.names
const secrets = codesArray.secrets
Expand Down

0 comments on commit 0a2e002

Please sign in to comment.