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

@@ -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
}
}