Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriangalliat committed Sep 28, 2021
1 parent 54276ff commit cde1cd3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ public/bulma.min.css: node_modules/bulma/css/bulma.min.css node_modules/bulma-re

public/qr-scanner-worker.min.js: node_modules/qr-scanner/qr-scanner-worker.min.js
cp $< $@

serve:
cd public && python3 -m http.server 8888
33 changes: 11 additions & 22 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,16 @@ const { username, password, scanCode, scanImage, showDetails, file, cancel } = f
const { secret, algorithm, digits, period, authyMode, resetDefaults } = formDetails.elements

function updatePasswordFromDetails () {
if (!password.value.startsWith('otpauth://')) {
const search = new URLSearchParams()
search.set('secret', secret.value)
search.set('algorithm', algorithm.value)
search.set('digits', digits.value)
search.set('period', period.value)

password.value = `otpauth://totp/${username.value}?${search}`
return
}

const url = new URL(password.value)
const search = new URLSearchParams(url.search)
const uri = new URL(password.value.startsWith('otpauth://') ? password.value : `otpauth://totp/${encodeURIComponent(username.value)}`)
const search = new URLSearchParams(uri.search)

search.set('secret', secret.value)
search.set('algorithm', algorithm.value)
search.set('digits', digits.value)
search.set('period', period.value)

url.search = search
password.value = url.toString()
uri.search = search
password.value = uri.toString()
}

function updateDetailsFromPassword () {
Expand All @@ -56,7 +45,7 @@ function updateDetailsFromPassword () {
period.value = search.get('period') || 30
}

function totpFromUrlOrSecret (value) {
function totpFromUriOrSecret (value) {
if (!value.startsWith('otpauth://')) {
// Directly the secret, use default options.
return totp(value)
Expand All @@ -69,7 +58,7 @@ function totpFromUrlOrSecret (value) {
}

function generateCode () {
const code = totpFromUrlOrSecret(password.value)
const code = totpFromUriOrSecret(password.value)
result.classList.remove('is-hidden')
result.style.lineHeight = `${resultCode.offsetHeight}px`
resultCode.size = code.length - 2 // Not sure why but this fits perfectly.
Expand All @@ -78,19 +67,19 @@ function generateCode () {
navigator.clipboard.writeText(code)
}

function handleTotpUrl (url) {
const search = new URLSearchParams(new URL(url).search)
function handleTotpUri (uri) {
const search = new URLSearchParams(new URL(uri).search)

username.value = search.get('issuer')
password.value = url
password.value = uri
updateDetailsFromPassword()
}

function handleFile (file) {
QrScanner.scanImage(file)
.then(result => {
error.classList.add('is-hidden')
handleTotpUrl(result)
handleTotpUri(result)
})
.catch(err => {
console.error(err)
Expand All @@ -110,7 +99,7 @@ const qrScanner = new QrScanner(previewVideo, result => {
preview.classList.add('is-hidden')
error.classList.add('is-hidden')
qrScanner.stop()
handleTotpUrl(result)
handleTotpUri(result)
})

// Force width to avoid pixel shift with rounding.
Expand Down

0 comments on commit cde1cd3

Please sign in to comment.