Basic schema parser implemented

This commit is contained in:
2020-06-29 19:50:10 +02:00
parent 1d020c5110
commit a8e6c9e99b
45 changed files with 119 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ import kotlinx.android.synthetic.main.activity_create.*
class CreateActivity : AppCompatActivity() {
private var fragment_title: TextInput? = null
private var fragment_username: TextInput? = null
private var schema: QRSchema? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -19,6 +20,10 @@ class CreateActivity : AppCompatActivity() {
.replace(R.id.username, fragment_username!!)
.commit()
this.schema = QRSchema()
this.schema!!.decrypted_raw = "%JtuB4O9M42%Gitea|Nicolas|542superGoOD_pW&|klier.nicolas@protonmail.com|https://nicolasklier.de:3000|()What's your favorite series%Rick and morty|(2fa)otpauth://totp/OffPass%20Test?secret=d34gfkki5dkd5knifysrpgndd5xb2c7eddwki7ya4pvoisfa5c3ko5pv&issuer=Nicolas%20Klier"
this.schema!!.parse()
setSupportActionBar(findViewById(R.id.toolbar))
setContentView(R.layout.activity_create)

View File

@@ -0,0 +1,84 @@
package com.github.mondei1.offpass
import android.util.Log
import java.lang.Error
import java.util.regex.Matcher
import java.util.regex.Pattern
class QRSchema {
var raw: String = ""
var decrypted_raw: String = ""
// Parsed content
lateinit var session_key: String
lateinit var title: String
lateinit var username: String
lateinit var password: String
lateinit var email: String
lateinit var website_url: String
var custom: HashMap<String, String> = HashMap() // All defined custom/optional fields
var question_awnser: HashMap<String, String> = HashMap() // Used for security questions
constructor() {}
/**
* This function will take the already set raw content and tries to parse it.
*
* @return It will return `true` if the operation was successful and `false` if otherwise.
*/
fun parse(): Boolean {
if (decrypted_raw == "") {
throw Error("Tried to parse QR-Code schema but raw content must be first decrypted! " +
"Set raw content first and then use decrypt()")
}
// First will be to split the string into it's parts
var fields: MutableList<String> = this.decrypted_raw.split("|").toMutableList()
for (i in 0 until fields.size) {
// First four items have to exist and are title, username, password, URL in this order.
if (i == 0) {
// Check if there is a session key
if (fields[0].startsWith("%")) {
fields[0] = fields[0].replace("%", "")
session_key = fields[0].substring(0, 9) // Get first 10 chars, which are the
// session key
this.title = fields[0].substring(10, fields[0].length)
} else {
this.title = fields[0]
}
this.username = fields[1]
this.password = fields[2]
this.email = fields[3]
this.website_url = fields[4]
}
// Here are optional/custom fields
if (i > 4) {
if (fields[i].startsWith("(")) {
var closingBracket = fields[i].indexOf(")")
var key = fields[i].substring(1, closingBracket)
var value = fields[i].substring(closingBracket+1, fields[i].length)
// We got a security question/awnser
if (key == "") {
var qa = value.split("%")
question_awnser.put(qa[0], qa[1]);
} else {
custom.put(key, value)
}
Log.i("QR-Code schema", custom.toString())
} else {
throw Error("Custom/optional field should start with an open bracket: "
+ fields[i].toString())
}
}
}
Log.i("QR-Code schema", "Found: $session_key, $title, $username, $password, $email, $website_url. ${custom.toString()} and ${question_awnser.toString()}")
return true
}
}