Skip to content

Commit

Permalink
Migrate from Buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Mar 11, 2023
1 parent 532c34b commit a7618f6
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 13 deletions.
14 changes: 14 additions & 0 deletions interface/utils/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,17 @@ export const markdownConverter = (text: string) => {

return body
}

/**
* Convert base64 to text
*/
export const decodeBase64 = (text: string) => {
return atob(text)
}

/**
* Convert text to base64
*/
export const encodeBase64 = (text: string) => {
return btoa(text)
}
4 changes: 2 additions & 2 deletions interface/utils/encryption.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const service = dev ? "authme_dev" : "authme"
/**
* Generates random key
*/
export const generateRandomKey = async (length: number): Promise<Buffer> => {
return Buffer.from(await invoke("random_values", { length }))
export const generateRandomKey = async (length: number): Promise<ArrayBuffer> => {
return new ArrayBuffer(await invoke("random_values", { length }))
}

/**
Expand Down
4 changes: 2 additions & 2 deletions interface/windows/codes/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { textConverter } from "../../utils/convert"
import { decodeBase64, textConverter } from "../../utils/convert"
import { TOTP } from "otpauth"
import { clipboard, dialog, fs } from "@tauri-apps/api"
import { getSettings, setSettings } from "../../stores/settings"
Expand Down Expand Up @@ -258,7 +258,7 @@ export const chooseImportFile = async () => {
if (filePath !== null) {
const loadedFile = await fs.readTextFile(filePath.toString())
const file: LibAuthmeFile = JSON.parse(loadedFile)
const importString = Buffer.from(file.codes, "base64").toString()
const importString = decodeBase64(file.codes)

saveText = importString

Expand Down
3 changes: 2 additions & 1 deletion interface/windows/confirm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { getSettings } from "../../stores/settings"
import { dialog, invoke } from "@tauri-apps/api"
import { getState, setState } from "../../stores/state"
import { sendEncryptionKey, verifyWebAuthnLogin } from "interface/utils/encryption"
import { decodeBase64 } from "@utils/convert"

export const confirmPassword = async () => {
const settings = getSettings()
const state = getState()
const input = document.querySelector(".passwordInput").value

const result = await invoke("verify_password", { password: input, hash: Buffer.from(settings.security.password, "base64").toString() })
const result = await invoke("verify_password", { password: input, hash: decodeBase64(settings.security.password) })

if (result === true) {
if (settings.security.hardwareAuthentication === true) {
Expand Down
4 changes: 2 additions & 2 deletions interface/windows/export/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { fs, dialog } from "@tauri-apps/api"
import { generateTimestamp } from "../../utils/time"
import { textConverter } from "../../utils/convert"
import { encodeBase64, textConverter } from "../../utils/convert"
import { getSettings } from "../../stores/settings"
import qrcode from "qrcode-generator"
import { decryptData, verifyWebAuthnLogin } from "interface/utils/encryption"
Expand Down Expand Up @@ -44,7 +44,7 @@ export const exportAuthmeFile = async () => {
const saveFile: LibAuthmeFile = {
role: "codes",
encrypted: false,
codes: Buffer.from(codesText).toString("base64"),
codes: encodeBase64(codesText),
date: generateTimestamp(),
version: 3,
}
Expand Down
4 changes: 2 additions & 2 deletions interface/windows/import/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BarcodeDetectorPolyfill } from "@undecaf/barcode-detector-polyfill"
import { fs, dialog } from "@tauri-apps/api"
import { getState, setState } from "../../stores/state"
import { totpImageConverter, migrationImageConverter } from "../../utils/convert"
import { navigate } from "../../utils/navigate"
import logger from "interface/utils/logger"
import { decodeBase64, migrationImageConverter, totpImageConverter } from "@utils/convert"

/**
* Choose images, then read QR codes
Expand Down Expand Up @@ -160,7 +160,7 @@ export const chooseFile = async () => {
if (filePath !== null) {
const loadedFile = await fs.readTextFile(filePath.toString())
const file: LibAuthmeFile = JSON.parse(loadedFile)
const importString = Buffer.from(file.codes, "base64").toString()
const importString = decodeBase64(file.codes)

dialog.message("Codes imported. \n\nYou can edit your codes on the edit page.")

Expand Down
8 changes: 4 additions & 4 deletions interface/windows/landing/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getState, setState } from "../../stores/state"
import { dialog, invoke } from "@tauri-apps/api"
import { setEntry, generateRandomKey, setEncryptionKey, createWebAuthnLogin, verifyWebAuthnLogin } from "interface/utils/encryption"
import { search } from "interface/utils/password"
import { encodeBase64 } from "@utils/convert"

export const noPassword = async () => {
const settings = getSettings()
Expand All @@ -24,8 +25,9 @@ export const noPassword = async () => {
}

const key = await generateRandomKey(32)
const decoder = new TextDecoder()

await setEntry("encryptionKey", key.toString("base64"))
await setEntry("encryptionKey", encodeBase64(decoder.decode(key)))
await setEncryptionKey()

settings.security.requireAuthentication = false
Expand Down Expand Up @@ -76,9 +78,7 @@ export const createPassword = async () => {
}
}

const password = Buffer.from(await invoke("encrypt_password", { password: input0.value }))

settings.security.password = password.toString("base64")
settings.security.password = encodeBase64(await invoke("encrypt_password", { password: input0.value }))
settings.security.requireAuthentication = true

setSettings(settings)
Expand Down

0 comments on commit a7618f6

Please sign in to comment.