Password list get's loaded in the background
- User has to repeat his passphrase - Passwords can now be toggled - Error message on wrong passphrase on decryption
This commit is contained in:
@@ -10,6 +10,8 @@ import android.os.Bundle
|
||||
import android.print.PrintAttributes
|
||||
import android.print.PrintJob
|
||||
import android.print.PrintManager
|
||||
import android.text.method.HideReturnsTransformationMethod
|
||||
import android.text.method.PasswordTransformationMethod
|
||||
import android.util.Base64
|
||||
import android.util.Log
|
||||
import android.view.ContextThemeWrapper
|
||||
@@ -27,11 +29,12 @@ import com.google.zxing.integration.android.IntentResult
|
||||
import com.journeyapps.barcodescanner.BarcodeEncoder
|
||||
import dev.turingcomplete.kotlinonetimepassword.GoogleAuthenticator
|
||||
import kotlinx.android.synthetic.main.activity_create.*
|
||||
import kotlinx.android.synthetic.main.dialogpassphrase.*
|
||||
import kotlinx.android.synthetic.main.dialogpassphrase.view.*
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.lang.IllegalArgumentException
|
||||
import java.util.*
|
||||
import kotlin.collections.ArrayList
|
||||
|
||||
class CreateActivity : AppCompatActivity() {
|
||||
private var schema: QRSchema? = null
|
||||
@@ -43,6 +46,8 @@ class CreateActivity : AppCompatActivity() {
|
||||
|
||||
private var fa_uri: Uri? = null
|
||||
|
||||
private var known_passwords = ArrayList<String>()
|
||||
|
||||
/**
|
||||
* Gets triggered when the user clicks on printing button (top left) and renders the paper to
|
||||
* print.
|
||||
@@ -130,17 +135,7 @@ class CreateActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
fun passwordKnown(password: String): Boolean {
|
||||
val known_passwords = String(this.resources.openRawResource(
|
||||
this.resources.getIdentifier("passwordlist", "raw", this.packageName)
|
||||
).readBytes())
|
||||
|
||||
val known_passwords_array = runBlocking {
|
||||
known_passwords.split("\n")
|
||||
}
|
||||
|
||||
Log.i("Create Activity", "Size of passwords: ${known_passwords_array.size}")
|
||||
|
||||
return known_passwords_array.contains(password)
|
||||
return this.known_passwords.contains(password)
|
||||
}
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
@@ -158,6 +153,15 @@ class CreateActivity : AppCompatActivity() {
|
||||
|
||||
setSupportActionBar(findViewById(R.id.toolbar))
|
||||
|
||||
// Split word list in the background (this process can take a few seconds to complete)
|
||||
GlobalScope.async {
|
||||
val known_passwords_ = String(resources.openRawResource(
|
||||
resources.getIdentifier("passwordlist", "raw", packageName)
|
||||
).readBytes())
|
||||
|
||||
known_passwords = known_passwords_.split("\n") as ArrayList<String>
|
||||
}
|
||||
|
||||
setContentView(R.layout.activity_create)
|
||||
back.setOnClickListener {
|
||||
Log.i("CREATE", "Back got clicked!")
|
||||
@@ -186,6 +190,12 @@ class CreateActivity : AppCompatActivity() {
|
||||
showError("Weak passphrase", "Passphrase has to be at least 8 characters long.")
|
||||
return@OnClickListener
|
||||
}
|
||||
if (passphrase != editTextLayout.passphrase2_input.text.toString()) {
|
||||
// Make a new alert, telling the user that his passphrase doesn't match the repeat field.
|
||||
dialogInterface.cancel()
|
||||
showError("Passphrase mismatch", "Both passphrases do not match.")
|
||||
return@OnClickListener
|
||||
}
|
||||
|
||||
// Check if password is found in our offline password list
|
||||
if(passwordKnown(passphrase)) {
|
||||
@@ -207,6 +217,16 @@ class CreateActivity : AppCompatActivity() {
|
||||
|
||||
|
||||
}
|
||||
password_hide.setOnClickListener {
|
||||
if (password_input.transformationMethod == HideReturnsTransformationMethod.getInstance()) {
|
||||
password_input.transformationMethod = PasswordTransformationMethod.getInstance()
|
||||
password_hide.setImageResource(R.drawable.ic_visibility_on)
|
||||
} else {
|
||||
password_input.transformationMethod = HideReturnsTransformationMethod.getInstance()
|
||||
password_hide.setImageResource(R.drawable.ic_visibility_off)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
fa_input.isFocusable = false
|
||||
|
||||
@@ -242,6 +262,12 @@ class CreateActivity : AppCompatActivity() {
|
||||
if (result.contents.startsWith("otpauth://totp/")) {
|
||||
fa_progress.visibility = View.VISIBLE
|
||||
fa_uri = Uri.parse(result.contents)
|
||||
|
||||
if (fa_uri?.getQueryParameter("secret").toString() == "" || fa_uri?.getQueryParameter("secret") == null) {
|
||||
showError("Corrupt 2FA", "The scanned 2FA secret doesn't contain a secret!")
|
||||
return
|
||||
}
|
||||
|
||||
val fa_generator = GoogleAuthenticator(base32secret = fa_uri?.getQueryParameter("secret").toString())
|
||||
this.schema!!.custom.put("2fa", fa_uri?.getQueryParameter("secret").toString())
|
||||
|
||||
@@ -253,7 +279,12 @@ class CreateActivity : AppCompatActivity() {
|
||||
// TODO: There is a bug. When the user changes his current app, this coroutine stops.
|
||||
fa_coroutine = GlobalScope.launch(Dispatchers.Main) {
|
||||
while (true) {
|
||||
fa_input.setText(fa_generator.generate())
|
||||
try {
|
||||
fa_input.setText(fa_generator.generate())
|
||||
} catch (error: IllegalArgumentException) {
|
||||
showError("Corrupt 2FA", "The scanned 2FA secret is corrupt and therefore no codes can be generated.")
|
||||
return@launch
|
||||
}
|
||||
|
||||
var seconds_remaining = CryptoOperations().getRemainingTOTPTime()
|
||||
Log.i("2FA Generator", "Remaining: $seconds_remaining")
|
||||
@@ -275,6 +306,8 @@ class CreateActivity : AppCompatActivity() {
|
||||
}
|
||||
|
||||
fa_label.text = "${fa_uri?.path?.replace("/", "")} (${fa_uri?.getQueryParameter("issuer")})"
|
||||
} else {
|
||||
showError("No 2FA", "The scanned QR-Code has the wrong format.")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user