Skip to content
Snippets Groups Projects
Verified Commit d5a33bc7 authored by Torsten Grote's avatar Torsten Grote
Browse files

Render QR code on command line

parent 01bdeef5
No related branches found
No related tags found
1 merge request!35Encode QR code for pairing
Pipeline #7986 passed
...@@ -8,7 +8,10 @@ import com.github.ajalt.clikt.parameters.options.flag ...@@ -8,7 +8,10 @@ import com.github.ajalt.clikt.parameters.options.flag
import com.github.ajalt.clikt.parameters.options.option import com.github.ajalt.clikt.parameters.options.option
import org.briarproject.mailbox.core.CoreEagerSingletons import org.briarproject.mailbox.core.CoreEagerSingletons
import org.briarproject.mailbox.core.JavaCliEagerSingletons import org.briarproject.mailbox.core.JavaCliEagerSingletons
import org.briarproject.mailbox.core.db.Database
import org.briarproject.mailbox.core.lifecycle.LifecycleManager import org.briarproject.mailbox.core.lifecycle.LifecycleManager
import org.briarproject.mailbox.core.setup.QrCodeEncoder
import org.briarproject.mailbox.core.setup.SetupManager
import org.slf4j.LoggerFactory.getLogger import org.slf4j.LoggerFactory.getLogger
import java.util.logging.Level.ALL import java.util.logging.Level.ALL
import java.util.logging.Level.INFO import java.util.logging.Level.INFO
...@@ -38,6 +41,15 @@ class Main : CliktCommand( ...@@ -38,6 +41,15 @@ class Main : CliktCommand(
@Inject @Inject
internal lateinit var lifecycleManager: LifecycleManager internal lateinit var lifecycleManager: LifecycleManager
@Inject
internal lateinit var db: Database
@Inject
internal lateinit var setupManager: SetupManager
@Inject
internal lateinit var qrCodeEncoder: QrCodeEncoder
override fun run() { override fun run() {
// logging // logging
val levelSlf4j = if (debug) Level.DEBUG else when (verbosity) { val levelSlf4j = if (debug) Level.DEBUG else when (verbosity) {
...@@ -68,6 +80,18 @@ class Main : CliktCommand( ...@@ -68,6 +80,18 @@ class Main : CliktCommand(
lifecycleManager.startServices() lifecycleManager.startServices()
lifecycleManager.waitForStartup() lifecycleManager.waitForStartup()
// TODO this is obviously not the final code, just a stub to get us started
val setupTokenExists = db.transactionWithResult(true) { txn ->
setupManager.getSetupToken(txn) != null
}
val ownerTokenExists = db.transactionWithResult(true) { txn ->
setupManager.getOwnerToken(txn) != null
}
if (!setupTokenExists && !ownerTokenExists) setupManager.restartSetup()
qrCodeEncoder.getQrCodeBitMatrix()?.let {
println(QrCodeRenderer.getQrString(it))
}
} }
} }
......
package org.briarproject.mailbox.cli
import com.google.zxing.common.BitMatrix
object QrCodeRenderer {
private const val SET = "██"
private const val UNSET = " "
internal fun getQrString(bitMatrix: BitMatrix): String = StringBuilder().apply {
append(System.lineSeparator())
for (y in 0 until bitMatrix.height) {
for (x in 0 until bitMatrix.width) {
append(if (bitMatrix[x, y]) SET else UNSET)
}
append(System.lineSeparator())
}
}.toString()
}
...@@ -30,6 +30,8 @@ dependencies { ...@@ -30,6 +30,8 @@ dependencies {
// Base 32 // Base 32
implementation 'dev.keiji.rfc4648:rfc4648:1.0.0' implementation 'dev.keiji.rfc4648:rfc4648:1.0.0'
// QrCode
implementation 'com.google.zxing:core:3.4.1'
testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version" testImplementation "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_version" testImplementation "org.junit.jupiter:junit-jupiter-api:$junit_version"
......
package org.briarproject.mailbox.core.setup package org.briarproject.mailbox.core.setup
import com.google.zxing.BarcodeFormat.QR_CODE
import com.google.zxing.common.BitMatrix
import com.google.zxing.qrcode.QRCodeWriter
import dev.keiji.util.Base32 import dev.keiji.util.Base32
import org.briarproject.mailbox.core.db.Database import org.briarproject.mailbox.core.db.Database
import org.briarproject.mailbox.core.db.DbException import org.briarproject.mailbox.core.db.DbException
...@@ -8,6 +11,7 @@ import org.briarproject.mailbox.core.util.LogUtils.logException ...@@ -8,6 +11,7 @@ import org.briarproject.mailbox.core.util.LogUtils.logException
import org.briarproject.mailbox.core.util.StringUtils.fromHexString import org.briarproject.mailbox.core.util.StringUtils.fromHexString
import org.slf4j.LoggerFactory.getLogger import org.slf4j.LoggerFactory.getLogger
import java.nio.ByteBuffer import java.nio.ByteBuffer
import java.nio.charset.Charset
import javax.inject.Inject import javax.inject.Inject
private const val VERSION = 32 private const val VERSION = 32
...@@ -19,7 +23,14 @@ class QrCodeEncoder @Inject constructor( ...@@ -19,7 +23,14 @@ class QrCodeEncoder @Inject constructor(
private val torPlugin: TorPlugin, private val torPlugin: TorPlugin,
) { ) {
fun getQrCodeBytes(): ByteArray? { fun getQrCodeBitMatrix(edgeLen: Int = 0): BitMatrix? {
val bytes = getQrCodeBytes() ?: return null
// Use ISO 8859-1 to encode bytes directly as a string
val content = String(bytes, Charset.forName("ISO-8859-1"))
return QRCodeWriter().encode(content, QR_CODE, edgeLen, edgeLen)
}
private fun getQrCodeBytes(): ByteArray? {
val hiddenServiceBytes = getHiddenServiceBytes() ?: return null val hiddenServiceBytes = getHiddenServiceBytes() ?: return null
val setupTokenBytes = getSetupTokenBytes() ?: return null val setupTokenBytes = getSetupTokenBytes() ?: return null
return ByteBuffer.allocate(65) return ByteBuffer.allocate(65)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment