From 856d4867b41840022b240c57a311ba539e903a88 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Tue, 10 Nov 2020 15:00:42 +0100 Subject: [PATCH 1/7] Manage OTP links #556 --- app/src/main/AndroidManifest.xml | 7 + .../EntrySelectionLauncherActivity.kt | 163 ++++++++++++------ .../activities/FileDatabaseSelectActivity.kt | 13 ++ .../keepass/activities/GroupActivity.kt | 16 +- .../keepass/activities/PasswordActivity.kt | 26 ++- .../com/kunzisoft/keepass/model/EntryInfo.kt | 10 +- .../com/kunzisoft/keepass/model/SearchInfo.kt | 20 ++- 7 files changed, 192 insertions(+), 63 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4b546b2e1..20c78fb62 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -150,6 +150,13 @@ + + + + + + + { if ("text/plain" == intent.type) { - // Retrieve web domain - intent.getStringExtra(Intent.EXTRA_TEXT)?.let { - sharedWebDomain = Uri.parse(it).host + // Retrieve web domain or OTP + intent.getStringExtra(Intent.EXTRA_TEXT)?.let { extra -> + sharedWebDomain = Uri.parse(extra).host + otpString = checkOtpString(extra) } } } + Intent.ACTION_VIEW -> { + // Retrieve OTP + intent.dataString?.let { extra -> + otpString = checkOtpString(extra) + } + } else -> {} } - // Build search param + + // Build domain search param val searchInfo = SearchInfo().apply { - webDomain = sharedWebDomain + this.webDomain = sharedWebDomain + this.otpString = otpString } SearchInfo.getConcreteWebDomain(this, searchInfo.webDomain) { concreteWebDomain -> searchInfo.webDomain = concreteWebDomain @@ -67,63 +79,102 @@ class EntrySelectionLauncherActivity : AppCompatActivity() { super.onCreate(savedInstanceState) } - private fun launch(searchInfo: SearchInfo) { - // Setting to integrate Magikeyboard - val searchShareForMagikeyboard = PreferencesUtil.isKeyboardSearchShareEnable(this) + private fun checkOtpString(otpString: String?): String? { + return if (OtpEntryFields.parseFields { otpString } == null) null else otpString + } - // If database is open - val database = Database.getInstance() - val readOnly = database.isReadOnly - SearchHelper.checkAutoSearchInfo(this, - database, - searchInfo, - { items -> - // Items found - if (searchShareForMagikeyboard) { - if (items.size == 1) { - // Automatically populate keyboard - val entryPopulate = items[0] - populateKeyboardAndMoveAppToBackground(this, - entryPopulate, - intent) + private fun launch(searchInfo: SearchInfo) { + + if (!searchInfo.containsOnlyNullValues()) { + // Setting to integrate Magikeyboard + val searchShareForMagikeyboard = PreferencesUtil.isKeyboardSearchShareEnable(this) + + // If database is open + val database = Database.getInstance() + val readOnly = database.isReadOnly + SearchHelper.checkAutoSearchInfo(this, + database, + searchInfo, + { items -> + // Items found + if (searchInfo.otpString != null) { + if (!readOnly) { + GroupActivity.launchForSaveResult(this, + searchInfo, + false) + } else { + Toast.makeText(applicationContext, + R.string.autofill_read_only_save, + Toast.LENGTH_LONG) + .show() + } + } else if (searchShareForMagikeyboard) { + if (items.size == 1) { + // Automatically populate keyboard + val entryPopulate = items[0] + populateKeyboardAndMoveAppToBackground(this, + entryPopulate, + intent) + } else { + // Select the one we want + GroupActivity.launchForKeyboardSelectionResult(this, + readOnly, + searchInfo, + true) + } } else { - // Select the one we want - GroupActivity.launchForKeyboardSelectionResult(this, + GroupActivity.launchForSearchResult(this, readOnly, searchInfo, true) } - } else { - GroupActivity.launchForSearchResult(this, - readOnly, - searchInfo, - true) + }, + { + // Show the database UI to select the entry + if (searchInfo.otpString != null) { + if (!readOnly) { + GroupActivity.launchForSaveResult(this, + searchInfo, + false) + } else { + Toast.makeText(applicationContext, + R.string.autofill_read_only_save, + Toast.LENGTH_LONG) + .show() + } + } else if (readOnly || searchShareForMagikeyboard) { + GroupActivity.launchForKeyboardSelectionResult(this, + readOnly, + searchInfo, + false) + } else { + GroupActivity.launchForSaveResult(this, + searchInfo, + false) + } + }, + { + // If database not open + if (searchInfo.otpString != null) { + if (!readOnly) { + FileDatabaseSelectActivity.launchForSaveResult(this, + searchInfo) + } else { + Toast.makeText(applicationContext, + R.string.autofill_read_only_save, + Toast.LENGTH_LONG) + .show() + } + } else if (searchShareForMagikeyboard) { + FileDatabaseSelectActivity.launchForKeyboardSelectionResult(this, + searchInfo) + } else { + FileDatabaseSelectActivity.launchForSearchResult(this, + searchInfo) + } } - }, - { - // Show the database UI to select the entry - if (readOnly || searchShareForMagikeyboard) { - GroupActivity.launchForKeyboardSelectionResult(this, - readOnly, - searchInfo, - false) - } else { - GroupActivity.launchForSaveResult(this, - searchInfo, - false) - } - }, - { - // If database not open - if (searchShareForMagikeyboard) { - FileDatabaseSelectActivity.launchForKeyboardSelectionResult(this, - searchInfo) - } else { - FileDatabaseSelectActivity.launchForSearchResult(this, - searchInfo) - } - } - ) + ) + } finish() } } diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/FileDatabaseSelectActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/FileDatabaseSelectActivity.kt index 6fb1dac8f..60058a243 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/FileDatabaseSelectActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/FileDatabaseSelectActivity.kt @@ -468,6 +468,19 @@ class FileDatabaseSelectActivity : SpecialModeActivity(), searchInfo) } + /* + * ------------------------- + * Save Launch + * ------------------------- + */ + + fun launchForSaveResult(context: Context, + searchInfo: SearchInfo) { + EntrySelectionHelper.startActivityForSaveModeResult(context, + Intent(context, FileDatabaseSelectActivity::class.java), + searchInfo) + } + /* * ------------------------- * Keyboard Launch diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt index eb83127ed..b8b1b57cf 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/GroupActivity.kt @@ -1370,8 +1370,20 @@ class GroupActivity : LockingActivity(), } ) }, - { - // Nothing with Save Info, only pass by search first + { searchInfo -> + // Save info used with OTP + if (!readOnly) { + GroupActivity.launchForSaveResult(activity, + searchInfo, + false) + onLaunchActivitySpecialMode() + } else { + Toast.makeText(activity.applicationContext, + R.string.autofill_read_only_save, + Toast.LENGTH_LONG) + .show() + onCancelSpecialMode() + } }, { searchInfo -> SearchHelper.checkAutoSearchInfo(activity, diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt index 5e2f3ef46..5c9498240 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/PasswordActivity.kt @@ -795,6 +795,25 @@ open class PasswordActivity : SpecialModeActivity() { } } + /* + * ------------------------- + * Save Launch + * ------------------------- + */ + + @Throws(FileNotFoundException::class) + fun launchForSaveResult(activity: Activity, + databaseFile: Uri, + keyFile: Uri?, + searchInfo: SearchInfo) { + buildAndLaunchIntent(activity, databaseFile, keyFile) { intent -> + EntrySelectionHelper.startActivityForSaveModeResult( + activity, + intent, + searchInfo) + } + } + /* * ------------------------- * Keyboard Launch @@ -877,8 +896,11 @@ open class PasswordActivity : SpecialModeActivity() { searchInfo) onLaunchActivitySpecialMode() }, - { // Save Action - // Not directly used, a search is performed before + { searchInfo -> // Save Action + PasswordActivity.launchForSaveResult(activity, + databaseUri, keyFile, + searchInfo) + onLaunchActivitySpecialMode() }, { searchInfo -> // Keyboard Selection Action PasswordActivity.launchForKeyboardResult(activity, diff --git a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt index caac917e5..3d96ef2e1 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt @@ -28,6 +28,7 @@ import com.kunzisoft.keepass.database.element.icon.IconImage import com.kunzisoft.keepass.database.element.icon.IconImageStandard import com.kunzisoft.keepass.database.element.security.ProtectedString import com.kunzisoft.keepass.otp.OtpElement +import com.kunzisoft.keepass.otp.OtpEntryFields import com.kunzisoft.keepass.otp.OtpEntryFields.OTP_TOKEN_FIELD import kotlin.collections.ArrayList @@ -125,7 +126,14 @@ class EntryInfo : Parcelable { } fun saveSearchInfo(database: Database?, searchInfo: SearchInfo) { - searchInfo.webDomain?.let { webDomain -> + searchInfo.otpString?.let { otpString -> + // Replace the OTP field + OtpEntryFields.parseFields { otpString }?.let { otpElement -> + (customFields as ArrayList).add( + OtpEntryFields.buildOtpField(otpElement, null, null) + ) + } + } ?: searchInfo.webDomain?.let { webDomain -> // If unable to save web domain in custom field or URL not populated, save in URL val scheme = searchInfo.webScheme val webScheme = if (scheme.isNullOrEmpty()) "http" else scheme diff --git a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt index c49d0db2a..400f3cb13 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt @@ -4,6 +4,7 @@ import android.content.Context import android.content.res.Resources import android.os.Parcel import android.os.Parcelable +import com.kunzisoft.keepass.otp.OtpEntryFields import com.kunzisoft.keepass.settings.PreferencesUtil import com.kunzisoft.keepass.utils.ObjectNameResource import kotlinx.coroutines.CoroutineScope @@ -35,6 +36,7 @@ class SearchInfo : ObjectNameResource, Parcelable { get() { return if (webDomain == null) null else field } + var otpString: String? = null constructor() @@ -42,6 +44,7 @@ class SearchInfo : ObjectNameResource, Parcelable { applicationId = toCopy?.applicationId webDomain = toCopy?.webDomain webScheme = toCopy?.webScheme + otpString = toCopy?.otpString } private constructor(parcel: Parcel) { @@ -51,6 +54,8 @@ class SearchInfo : ObjectNameResource, Parcelable { webDomain = if (readDomain.isNullOrEmpty()) null else readDomain val readScheme = parcel.readString() webScheme = if (readScheme.isNullOrEmpty()) null else readScheme + val readOtp = parcel.readString() + otpString = if (readOtp.isNullOrEmpty()) null else readOtp } override fun describeContents(): Int { @@ -61,14 +66,23 @@ class SearchInfo : ObjectNameResource, Parcelable { parcel.writeString(applicationId ?: "") parcel.writeString(webDomain ?: "") parcel.writeString(webScheme ?: "") + parcel.writeString(otpString ?: "") } override fun getName(resources: Resources): String { + if (otpString != null) { + OtpEntryFields.parseFields { otpString }?.let { otpElement -> + return "${otpElement.type} (${otpElement.name})" + } + } return toString() } fun containsOnlyNullValues(): Boolean { - return applicationId == null && webDomain == null && webScheme == null + return applicationId == null + && webDomain == null + && webScheme == null + && otpString == null } override fun equals(other: Any?): Boolean { @@ -80,6 +94,7 @@ class SearchInfo : ObjectNameResource, Parcelable { if (applicationId != other.applicationId) return false if (webDomain != other.webDomain) return false if (webScheme != other.webScheme) return false + if (otpString != other.otpString) return false return true } @@ -88,11 +103,12 @@ class SearchInfo : ObjectNameResource, Parcelable { var result = applicationId?.hashCode() ?: 0 result = 31 * result + (webDomain?.hashCode() ?: 0) result = 31 * result + (webScheme?.hashCode() ?: 0) + result = 31 * result + (otpString?.hashCode() ?: 0) return result } override fun toString(): String { - return webDomain ?: applicationId ?: "" + return otpString ?: webDomain ?: applicationId ?: "" } companion object { From 03047ae6dd0efc9d83d3779db68957dbae5a116e Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 12 Nov 2020 12:30:25 +0100 Subject: [PATCH 2/7] Replace OTP when already exists #556 --- .../main/java/com/kunzisoft/keepass/model/EntryInfo.kt | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt index 3d96ef2e1..a1a49941f 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt @@ -129,9 +129,12 @@ class EntryInfo : Parcelable { searchInfo.otpString?.let { otpString -> // Replace the OTP field OtpEntryFields.parseFields { otpString }?.let { otpElement -> - (customFields as ArrayList).add( - OtpEntryFields.buildOtpField(otpElement, null, null) - ) + val mutableCustomFields = customFields as ArrayList + val otpField = OtpEntryFields.buildOtpField(otpElement, null, null) + if (mutableCustomFields.contains(otpField)) { + mutableCustomFields.remove(otpField) + } + mutableCustomFields.add(otpField) } } ?: searchInfo.webDomain?.let { webDomain -> // If unable to save web domain in custom field or URL not populated, save in URL From fe48955b9483f3887c1ac0e99817aa1e4c48ff30 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 12 Nov 2020 13:19:05 +0100 Subject: [PATCH 3/7] Check OTP Auth URI #556 --- .../EntrySelectionLauncherActivity.kt | 10 ++++---- .../com/kunzisoft/keepass/model/EntryInfo.kt | 2 +- .../com/kunzisoft/keepass/model/SearchInfo.kt | 4 ++-- .../kunzisoft/keepass/otp/OtpEntryFields.kt | 23 ++++++++++++++++++- 4 files changed, 29 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntrySelectionLauncherActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntrySelectionLauncherActivity.kt index 55987f891..d605385e8 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntrySelectionLauncherActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntrySelectionLauncherActivity.kt @@ -52,14 +52,16 @@ class EntrySelectionLauncherActivity : AppCompatActivity() { // Retrieve web domain or OTP intent.getStringExtra(Intent.EXTRA_TEXT)?.let { extra -> sharedWebDomain = Uri.parse(extra).host - otpString = checkOtpString(extra) + if (OtpEntryFields.isOTPUri(extra)) + otpString = extra } } } Intent.ACTION_VIEW -> { // Retrieve OTP intent.dataString?.let { extra -> - otpString = checkOtpString(extra) + if (OtpEntryFields.isOTPUri(extra)) + otpString = extra } } else -> {} @@ -79,10 +81,6 @@ class EntrySelectionLauncherActivity : AppCompatActivity() { super.onCreate(savedInstanceState) } - private fun checkOtpString(otpString: String?): String? { - return if (OtpEntryFields.parseFields { otpString } == null) null else otpString - } - private fun launch(searchInfo: SearchInfo) { if (!searchInfo.containsOnlyNullValues()) { diff --git a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt index a1a49941f..dd82778b9 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt @@ -128,7 +128,7 @@ class EntryInfo : Parcelable { fun saveSearchInfo(database: Database?, searchInfo: SearchInfo) { searchInfo.otpString?.let { otpString -> // Replace the OTP field - OtpEntryFields.parseFields { otpString }?.let { otpElement -> + OtpEntryFields.parseOTPUri(otpString)?.let { otpElement -> val mutableCustomFields = customFields as ArrayList val otpField = OtpEntryFields.buildOtpField(otpElement, null, null) if (mutableCustomFields.contains(otpField)) { diff --git a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt index 400f3cb13..39819a8f2 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt @@ -70,8 +70,8 @@ class SearchInfo : ObjectNameResource, Parcelable { } override fun getName(resources: Resources): String { - if (otpString != null) { - OtpEntryFields.parseFields { otpString }?.let { otpElement -> + otpString?.let { otpString -> + OtpEntryFields.parseOTPUri(otpString)?.let { otpElement -> return "${otpElement.type} (${otpElement.name})" } } diff --git a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt index b1e13d332..2255786db 100644 --- a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt +++ b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt @@ -49,6 +49,9 @@ object OtpEntryFields { private const val ENCODER_URL_PARAM = "encoder" private const val COUNTER_URL_PARAM = "counter" + // OTPauth URI + private const val REGEX_OTP_AUTH = "^(?:otpauth://([ht]otp)/)(?:(?:([^:?#]*): *)?([^:?#]*))(?:\\?([^#]+))$" + // Key-values (maybe from plugin or old KeePassXC) private const val SEED_KEY = "key" private const val DIGITS_KEY = "size" @@ -91,7 +94,25 @@ object OtpEntryFields { // HOTP fields from KeePass 2 if (parseHOTPFromField(getField, otpElement)) return otpElement + return null + } + /** + * Tell if [otpUri] is a valid Otp URI + */ + fun isOTPUri(otpUri: String): Boolean { + if (Pattern.matches(REGEX_OTP_AUTH, otpUri)) + return true + return false + } + + /** + * Get OtpElement from [otpUri] + */ + fun parseOTPUri(otpUri: String): OtpElement? { + val otpElement = OtpElement() + if (parseOTPUri({ key -> if (key == OTP_FIELD) otpUri else null }, otpElement)) + return otpElement return null } @@ -104,7 +125,7 @@ object OtpEntryFields { */ private fun parseOTPUri(getField: (id: String) -> String?, otpElement: OtpElement): Boolean { val otpPlainText = getField(OTP_FIELD) - if (otpPlainText != null && otpPlainText.isNotEmpty()) { + if (otpPlainText != null && otpPlainText.isNotEmpty() && isOTPUri(otpPlainText)) { val uri = Uri.parse(replaceSpaceChars(otpPlainText)) if (uri.scheme == null || OTP_SCHEME != uri.scheme!!.toLowerCase(Locale.ENGLISH)) { From eec61994138cc9defc5229709f479101f293d166 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 12 Nov 2020 14:39:33 +0100 Subject: [PATCH 4/7] Fix decoding OTP Auth #556 --- .../EntrySelectionLauncherActivity.kt | 3 +- .../com/kunzisoft/keepass/model/SearchInfo.kt | 3 +- .../kunzisoft/keepass/otp/OtpEntryFields.kt | 28 ++++++++++--------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/activities/EntrySelectionLauncherActivity.kt b/app/src/main/java/com/kunzisoft/keepass/activities/EntrySelectionLauncherActivity.kt index d605385e8..7927c502a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/activities/EntrySelectionLauncherActivity.kt +++ b/app/src/main/java/com/kunzisoft/keepass/activities/EntrySelectionLauncherActivity.kt @@ -51,9 +51,10 @@ class EntrySelectionLauncherActivity : AppCompatActivity() { if ("text/plain" == intent.type) { // Retrieve web domain or OTP intent.getStringExtra(Intent.EXTRA_TEXT)?.let { extra -> - sharedWebDomain = Uri.parse(extra).host if (OtpEntryFields.isOTPUri(extra)) otpString = extra + else + sharedWebDomain = Uri.parse(extra).host } } } diff --git a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt index 39819a8f2..72ce250c4 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt @@ -2,6 +2,7 @@ package com.kunzisoft.keepass.model import android.content.Context import android.content.res.Resources +import android.net.Uri import android.os.Parcel import android.os.Parcelable import com.kunzisoft.keepass.otp.OtpEntryFields @@ -72,7 +73,7 @@ class SearchInfo : ObjectNameResource, Parcelable { override fun getName(resources: Resources): String { otpString?.let { otpString -> OtpEntryFields.parseOTPUri(otpString)?.let { otpElement -> - return "${otpElement.type} (${otpElement.name})" + return "${otpElement.type} (${Uri.decode(otpElement.issuer)} : ${Uri.decode(otpElement.name)})" } } return toString() diff --git a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt index 2255786db..445e177f7 100644 --- a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt +++ b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt @@ -26,7 +26,6 @@ import com.kunzisoft.keepass.database.element.security.ProtectedString import com.kunzisoft.keepass.model.Field import com.kunzisoft.keepass.otp.OtpElement.Companion.replaceSpaceChars import com.kunzisoft.keepass.otp.TokenCalculator.* -import java.net.URLEncoder import java.util.* import java.util.regex.Pattern @@ -156,8 +155,15 @@ object OtpEntryFields { } val nameParam = validateAndGetNameInPath(uri.path) - if (nameParam != null && nameParam.isNotEmpty()) - otpElement.name = nameParam + if (nameParam != null && nameParam.isNotEmpty()) { + val userIdArray = nameParam.split(":", "%3A") + if (userIdArray.size > 1) { + otpElement.issuer = userIdArray[0] + otpElement.name = userIdArray[1] + } else { + otpElement.name = nameParam + } + } val issuerParam = uri.getQueryParameter(ISSUER_URL_PARAM) if (issuerParam != null && issuerParam.isNotEmpty()) @@ -232,14 +238,14 @@ object OtpEntryFields { } val issuer = if (title != null && title.isNotEmpty()) - replaceCharsForUrl(title) + Uri.encode(title) else - replaceCharsForUrl(otpElement.issuer) + Uri.encode(otpElement.issuer) val accountName = if (username != null && username.isNotEmpty()) - replaceCharsForUrl(username) + Uri.encode(username) else - replaceCharsForUrl(otpElement.name) + Uri.encode(otpElement.name) val uriString = StringBuilder("otpauth://$otpAuthority/$issuer:$accountName" + "?$SECRET_URL_PARAM=${otpElement.getBase32Secret()}" + "&$counterOrPeriodLabel=$counterOrPeriodValue" + @@ -254,10 +260,6 @@ object OtpEntryFields { return Uri.parse(uriString.toString()) } - private fun replaceCharsForUrl(parameter: String): String { - return URLEncoder.encode(replaceSpaceChars(parameter), "UTF-8") - } - private fun parseTOTPKeyValues(getField: (id: String) -> String?, otpElement: OtpElement): Boolean { val plainText = getField(OTP_FIELD) if (plainText != null && plainText.isNotEmpty()) { @@ -342,7 +344,7 @@ object OtpEntryFields { // path is "/name", so remove leading "/", and trailing white spaces val name = path.substring(1).trim { it <= ' ' } return if (name.isEmpty()) { - null // only white spaces. + null } else name } @@ -359,7 +361,7 @@ object OtpEntryFields { /** * Build Otp field from an OtpElement */ - fun buildOtpField(otpElement: OtpElement, title: String?, username: String?): Field { + fun buildOtpField(otpElement: OtpElement, title: String? = null, username: String? = null): Field { return Field(OTP_FIELD, ProtectedString(true, buildOtpUri(otpElement, title, username).toString())) } From c57515fed568b8b42873caf9abe843f7909919b2 Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 12 Nov 2020 14:59:39 +0100 Subject: [PATCH 5/7] Fix small OTP Auth syntax #556 --- .../com/kunzisoft/keepass/otp/OtpEntryFields.kt | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt index 445e177f7..404e1272d 100644 --- a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt +++ b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt @@ -238,15 +238,15 @@ object OtpEntryFields { } val issuer = if (title != null && title.isNotEmpty()) - Uri.encode(title) + encodeParameter(title) else - Uri.encode(otpElement.issuer) + encodeParameter(otpElement.issuer) val accountName = if (username != null && username.isNotEmpty()) - Uri.encode(username) + encodeParameter(username) else - Uri.encode(otpElement.name) - val uriString = StringBuilder("otpauth://$otpAuthority/$issuer:$accountName" + + encodeParameter(otpElement.name) + val uriString = StringBuilder("otpauth://$otpAuthority/$issuer%3A$accountName" + "?$SECRET_URL_PARAM=${otpElement.getBase32Secret()}" + "&$counterOrPeriodLabel=$counterOrPeriodValue" + "&$DIGITS_URL_PARAM=${otpElement.digits}" + @@ -260,6 +260,10 @@ object OtpEntryFields { return Uri.parse(uriString.toString()) } + private fun encodeParameter(parameter: String): String { + return Uri.encode(parameter.replace("[\\r|\\n|\\t|\\u00A0]+".toRegex(), "")) + } + private fun parseTOTPKeyValues(getField: (id: String) -> String?, otpElement: OtpElement): Boolean { val plainText = getField(OTP_FIELD) if (plainText != null && plainText.isNotEmpty()) { From 22a3541b7b66be4fa59aef390190bee6125d255f Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 12 Nov 2020 15:08:27 +0100 Subject: [PATCH 6/7] Auto populate Title and Username from OTP Auth --- app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt | 5 +++++ app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt | 2 +- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt index dd82778b9..283a65bb6 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/EntryInfo.kt @@ -129,6 +129,11 @@ class EntryInfo : Parcelable { searchInfo.otpString?.let { otpString -> // Replace the OTP field OtpEntryFields.parseOTPUri(otpString)?.let { otpElement -> + if (title.isEmpty()) + title = otpElement.issuer + if (username.isEmpty()) + username = otpElement.name + // Add OTP field val mutableCustomFields = customFields as ArrayList val otpField = OtpEntryFields.buildOtpField(otpElement, null, null) if (mutableCustomFields.contains(otpField)) { diff --git a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt index 72ce250c4..ac1f37b3a 100644 --- a/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt +++ b/app/src/main/java/com/kunzisoft/keepass/model/SearchInfo.kt @@ -73,7 +73,7 @@ class SearchInfo : ObjectNameResource, Parcelable { override fun getName(resources: Resources): String { otpString?.let { otpString -> OtpEntryFields.parseOTPUri(otpString)?.let { otpElement -> - return "${otpElement.type} (${Uri.decode(otpElement.issuer)} : ${Uri.decode(otpElement.name)})" + return "${otpElement.type} (${Uri.decode(otpElement.issuer)}:${Uri.decode(otpElement.name)})" } } return toString() From 61b8fa116ae579ec3f0d1a7595cb4b28a59eb4ad Mon Sep 17 00:00:00 2001 From: J-Jamet Date: Thu, 12 Nov 2020 15:14:14 +0100 Subject: [PATCH 7/7] Fix small chars issue --- .../java/com/kunzisoft/keepass/otp/OtpElement.kt | 8 ++++++-- .../com/kunzisoft/keepass/otp/OtpEntryFields.kt | 15 ++++++++------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/kunzisoft/keepass/otp/OtpElement.kt b/app/src/main/java/com/kunzisoft/keepass/otp/OtpElement.kt index 0128b0ea4..682f7889e 100644 --- a/app/src/main/java/com/kunzisoft/keepass/otp/OtpElement.kt +++ b/app/src/main/java/com/kunzisoft/keepass/otp/OtpElement.kt @@ -216,13 +216,17 @@ data class OtpElement(var otpModel: OtpModel = OtpModel()) { return secret.isNotEmpty() && checkBase64Secret(secret) } - fun replaceSpaceChars(parameter: String): String { + fun removeLineChars(parameter: String): String { + return parameter.replace("[\\r|\\n|\\t|\\u00A0]+".toRegex(), "") + } + + fun removeSpaceChars(parameter: String): String { return parameter.replace("[\\r|\\n|\\t|\\s|\\u00A0]+".toRegex(), "") } fun replaceBase32Chars(parameter: String): String { // Add 'A' at end if not Base32 length - var parameterNewSize = replaceSpaceChars(parameter.toUpperCase(Locale.ENGLISH)) + var parameterNewSize = removeSpaceChars(parameter.toUpperCase(Locale.ENGLISH)) while (parameterNewSize.length % 8 != 0) { parameterNewSize += 'A' } diff --git a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt index 404e1272d..38106feca 100644 --- a/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt +++ b/app/src/main/java/com/kunzisoft/keepass/otp/OtpEntryFields.kt @@ -24,7 +24,8 @@ import android.net.Uri import android.util.Log import com.kunzisoft.keepass.database.element.security.ProtectedString import com.kunzisoft.keepass.model.Field -import com.kunzisoft.keepass.otp.OtpElement.Companion.replaceSpaceChars +import com.kunzisoft.keepass.otp.OtpElement.Companion.removeLineChars +import com.kunzisoft.keepass.otp.OtpElement.Companion.removeSpaceChars import com.kunzisoft.keepass.otp.TokenCalculator.* import java.util.* import java.util.regex.Pattern @@ -125,7 +126,7 @@ object OtpEntryFields { private fun parseOTPUri(getField: (id: String) -> String?, otpElement: OtpElement): Boolean { val otpPlainText = getField(OTP_FIELD) if (otpPlainText != null && otpPlainText.isNotEmpty() && isOTPUri(otpPlainText)) { - val uri = Uri.parse(replaceSpaceChars(otpPlainText)) + val uri = Uri.parse(removeSpaceChars(otpPlainText)) if (uri.scheme == null || OTP_SCHEME != uri.scheme!!.toLowerCase(Locale.ENGLISH)) { Log.e(TAG, "Invalid or missing scheme in uri") @@ -158,16 +159,16 @@ object OtpEntryFields { if (nameParam != null && nameParam.isNotEmpty()) { val userIdArray = nameParam.split(":", "%3A") if (userIdArray.size > 1) { - otpElement.issuer = userIdArray[0] - otpElement.name = userIdArray[1] + otpElement.issuer = removeLineChars(userIdArray[0]) + otpElement.name = removeLineChars(userIdArray[1]) } else { - otpElement.name = nameParam + otpElement.name = removeLineChars(nameParam) } } val issuerParam = uri.getQueryParameter(ISSUER_URL_PARAM) if (issuerParam != null && issuerParam.isNotEmpty()) - otpElement.issuer = issuerParam + otpElement.issuer = removeLineChars(issuerParam) val secretParam = uri.getQueryParameter(SECRET_URL_PARAM) if (secretParam != null && secretParam.isNotEmpty()) { @@ -261,7 +262,7 @@ object OtpEntryFields { } private fun encodeParameter(parameter: String): String { - return Uri.encode(parameter.replace("[\\r|\\n|\\t|\\u00A0]+".toRegex(), "")) + return Uri.encode(OtpElement.removeLineChars(parameter)) } private fun parseTOTPKeyValues(getField: (id: String) -> String?, otpElement: OtpElement): Boolean {