Skip to content

Commit

Permalink
Import from webcam #186
Browse files Browse the repository at this point in the history
  • Loading branch information
Levminer committed Feb 24, 2022
1 parent ccc5a8c commit 5121a38
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 1 deletion.
2 changes: 1 addition & 1 deletion app/import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ <h4 data-loc>Create your import file here. Choose the most convenient way for yo
<div class="chooseImages m-1">
<button class="buttoni" onclick="chooseImages()">
<svg xmlns="http://www.w3.org/2000/svg" class="h-6 w-6" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v1m6 11h2m-6 0h-2v4m0-11v3m0 0h.01M12 12h4.01M16 20h4M4 12h4m12 0h.01M5 8h2a1 1 0 001-1V5a1 1 0 00-1-1H5a1 1 0 00-1 1v2a1 1 0 001 1zm12 0h2a1 1 0 001-1V5a1 1 0 00-1-1h-2a1 1 0 00-1 1v2a1 1 0 001 1zM5 20h2a1 1 0 001-1v-2a1 1 0 00-1-1H5a1 1 0 00-1 1v2a1 1 0 001 1z" />
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z" />
</svg>
<span data-loc>Choose images</span>
</button>
Expand Down
119 changes: 119 additions & 0 deletions app/import/src/js/webcam.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
module.exports = {
/**
* Capture QR codes from the webcam
*/
useWebcam: async () => {
let string = ""

const hasWebcam = await webcamAvailable()

if (hasWebcam === false) {
dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.close],
type: "error",
noLink: true,
message: lang.import_dialog.no_webcam,
})

return logger.error("Not found webcam")
} else {
const video = document.querySelector("#qrVideo")
const button = document.querySelector("#qrStop")

const reader = new QrcodeDecoder()

button.addEventListener("click", () => {
video.style.display = "none"
button.style.display = "none"

reader.stop()
})

setTimeout(() => {
video.style.display = "block"
button.style.display = "inline"
}, 300)

try {
const res = await reader.decodeFromCamera(video)

if (res.data.startsWith("otpauth://totp/") || res.data.startsWith("otpauth-migration://")) {
if (res.data.startsWith("otpauth://totp/")) {
string += qrConvert(res.data)
} else {
string += gaConvert(res.data)
}

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}`,
})

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"))
} else {
ipc.invoke("importedCodes", Buffer.from(string).toString("base64"))

saveFile(string)
}
}
} else {
dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.close],
type: "error",
noLink: true,
message: lang.import_dialog.wrong_qrcode,
})

video.style.display = "none"
button.style.display = "none"

reader.stop()

return logger.error("Wrong QR code found (QR)")
}

video.style.display = "none"
button.style.display = "none"

reader.stop()
} catch (error) {
dialog.showMessageBox(currentWindow, {
title: "Authme",
buttons: [lang.button.close],
type: "error",
noLink: true,
message: lang.import_dialog.webcam_used,
})

video.style.display = "none"
button.style.display = "none"

reader.stop()

logger.error("Webcam probably in use", error)
}
}
},
}

0 comments on commit 5121a38

Please sign in to comment.