checks if secret from qr code is valid

- secret size must be greater than 32 chars
  an only contain letters and '-'
- context check in camera fragment now fixed
This commit is contained in:
Raphael Maenle 2022-01-04 10:21:30 +01:00
parent 0a94cb517c
commit 35c072abf6
2 changed files with 42 additions and 27 deletions

View File

@ -49,6 +49,8 @@ class CameraFragment: Fragment() {
private var _binding: FragmentCameraBinding? = null
private val binding get() = _binding!!
private var secretScanned = false
private val screenAspectRatio: Int
get() {
val activity = requireActivity()
@ -179,15 +181,26 @@ class CameraFragment: Fragment() {
val inputImage =
InputImage.fromMediaImage(imageProxy.image!!, imageProxy.imageInfo.rotationDegrees)
var blockingScanned = false
barcodeScanner.process(inputImage)
.addOnSuccessListener { barcodes ->
barcodes.forEach {
Log.d(TAG, it.rawValue!!)
val bump = BumpProcessor.getInstance(requireContext())
bump.addSecret(requireContext(), it.rawValue!!)
findNavController().navigateUp()
if(blockingScanned) {
return@addOnSuccessListener
}
blockingScanned = true
barcodes.forEach { barcode ->
if(!secretScanned) {
context?.let {
val bump = BumpProcessor.getInstance(it)
if (bump.addSecret(requireContext(), barcode.rawValue!!)) {
secretScanned = true
findNavController().navigateUp()
}
}
}
}
blockingScanned = false
}
.addOnFailureListener {
Log.e(TAG, it.message ?: it.toString())

View File

@ -13,14 +13,11 @@ import java.io.File
class BumpProcessor constructor(context: Context) {
private var rest: RestSingleton = RestSingleton.getInstance(context)
private var secret: String? = null
private lateinit var log: JSONArray
private var messenger: MessageProcessor? = null
init {
secret = getSecret(context)
log = getLog(context)
messenger = secret?.let { MessageProcessor(it) }
}
companion object {
@ -50,13 +47,6 @@ class BumpProcessor constructor(context: Context) {
}
}
fun hasSecret(): Boolean {
secret?.let {
return true
}
return false
}
private fun getSecret(context: Context):String? {
val local = LocalData(context)
return local.code
@ -76,7 +66,10 @@ class BumpProcessor constructor(context: Context) {
}
private fun updateFromServer(context: Context) {
messenger?.let {rest.list(it.sender) {list -> updateLog(context, list)}}
LocalData(context).code?.let {
val messenger = MessageProcessor(it)
rest.list(messenger.sender) { list -> updateLog(context, list) }
}
}
private fun addToLog(context: Context, line: JSONObject) {
@ -84,12 +77,21 @@ class BumpProcessor constructor(context: Context) {
logFile.appendText(line.toString())
}
fun addSecret(context: Context, newCode: String) {
fun addSecret(context: Context, newCode: String): Boolean {
return if(!checkCodeValidity(newCode)) {
false
} else {
val local = LocalData(context)
local.code = newCode
secret = getSecret(context)
setFirebaseToken()
setFirebaseToken(context)
true
}
}
private fun checkCodeValidity(newCode: String): Boolean {
val validChars = newCode.filter{ it in 'A'..'Z' || it in 'a'..'z' || it == '-'}.length
return newCode.length == validChars && newCode.length >= 32
}
private fun getLog(context: Context): JSONArray {
@ -104,11 +106,11 @@ class BumpProcessor constructor(context: Context) {
return log
}
private fun setFirebaseToken() {
private fun setFirebaseToken(context: Context) {
val tokenTask = FirebaseMessaging.getInstance().token
tokenTask.addOnSuccessListener { token -> (
messenger?.let { rest.firebase(it.sender, token) {result -> Log.d("result", result.toString())}})
}
val secret = LocalData(context).code
val messenger = secret?.let { MessageProcessor(it) }
tokenTask.addOnSuccessListener { token -> ( messenger?.sender?.let { rest.firebase(it, token) { result -> Log.d("result", result.toString())} } ) }
}
}