fix: crash on incorrect password

This commit is contained in:
rebelonion 2024-02-06 01:53:49 -06:00
parent 0c6fad91ca
commit 8d7b86a667
4 changed files with 40 additions and 14 deletions

View file

@ -53,22 +53,28 @@ class LoginFragment : Fragment() {
if (password != null) { if (password != null) {
val salt = jsonString.copyOfRange(0, 16) val salt = jsonString.copyOfRange(0, 16)
val encrypted = jsonString.copyOfRange(16, jsonString.size) val encrypted = jsonString.copyOfRange(16, jsonString.size)
val decryptedJson = PreferenceKeystore.decryptWithPassword( val decryptedJson = try {
PreferenceKeystore.decryptWithPassword(
password, password,
encrypted, encrypted,
salt salt
) )
} catch (e: Exception) {
toast("Incorrect password")
return@passwordAlertDialog
}
if(PreferencePackager.unpack(decryptedJson)) if(PreferencePackager.unpack(decryptedJson))
println("Settings imported")
restartApp() restartApp()
} else { } else {
toast("Password cannot be empty") toast("Password cannot be empty")
} }
} }
} else { } else if (name.endsWith(".ani")) {
val decryptedJson = jsonString.toString(Charsets.UTF_8) val decryptedJson = jsonString.toString(Charsets.UTF_8)
if(PreferencePackager.unpack(decryptedJson)) if(PreferencePackager.unpack(decryptedJson))
restartApp() restartApp()
} else {
toast("Invalid file type")
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()

View file

@ -97,21 +97,28 @@ class SettingsActivity : AppCompatActivity(), SimpleDialog.OnDialogResultListene
if (password != null) { if (password != null) {
val salt = jsonString.copyOfRange(0, 16) val salt = jsonString.copyOfRange(0, 16)
val encrypted = jsonString.copyOfRange(16, jsonString.size) val encrypted = jsonString.copyOfRange(16, jsonString.size)
val decryptedJson = PreferenceKeystore.decryptWithPassword( val decryptedJson = try {
PreferenceKeystore.decryptWithPassword(
password, password,
encrypted, encrypted,
salt salt
) )
} catch (e: Exception) {
toast("Incorrect password")
return@passwordAlertDialog
}
if(PreferencePackager.unpack(decryptedJson)) if(PreferencePackager.unpack(decryptedJson))
restartApp() restartApp()
} else { } else {
toast("Password cannot be empty") toast("Password cannot be empty")
} }
} }
} else { } else if (name.endsWith(".ani")) {
val decryptedJson = jsonString.toString(Charsets.UTF_8) val decryptedJson = jsonString.toString(Charsets.UTF_8)
if(PreferencePackager.unpack(decryptedJson)) if(PreferencePackager.unpack(decryptedJson))
restartApp() restartApp()
} else {
toast("Unknown file type")
} }
} catch (e: Exception) { } catch (e: Exception) {
e.printStackTrace() e.printStackTrace()

View file

@ -1,7 +1,11 @@
package ani.dantotsu.settings.saving.internal package ani.dantotsu.settings.saving.internal
import android.content.SharedPreferences import android.content.SharedPreferences
import android.net.Uri
import androidx.documentfile.provider.DocumentFile
import ani.dantotsu.connections.discord.serializers.Activity
import ani.dantotsu.settings.saving.PrefManager import ani.dantotsu.settings.saving.PrefManager
import ani.dantotsu.toast
import com.google.gson.Gson import com.google.gson.Gson
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
@ -10,11 +14,18 @@ class PreferencePackager {
companion object { companion object {
/**
* @return a json string of the packed preferences
*/
fun pack(map: Map<Location, SharedPreferences>): String { fun pack(map: Map<Location, SharedPreferences>): String {
val prefsMap = packagePreferences(map) val prefsMap = packagePreferences(map)
val gson = Gson() val gson = Gson()
return gson.toJson(prefsMap) return gson.toJson(prefsMap)
} }
/**
* @return true if successful, false if error
*/
fun unpack(decryptedJson: String): Boolean { fun unpack(decryptedJson: String): Boolean {
val gson = Gson() val gson = Gson()
val type = object : TypeToken<Map<String, Map<String, Map<String, Any>>>>() {}.type //oh god... val type = object : TypeToken<Map<String, Map<String, Map<String, Any>>>>() {}.type //oh god...
@ -47,6 +58,9 @@ class PreferencePackager {
return unpackagePreferences(deserializedMap) return unpackagePreferences(deserializedMap)
} }
/**
* @return a map of location names to a map of preference names to their values
*/
private fun packagePreferences(map: Map<Location, SharedPreferences>): Map<String, Map<String, *>> { private fun packagePreferences(map: Map<Location, SharedPreferences>): Map<String, Map<String, *>> {
val result = mutableMapOf<String, Map<String, *>>() val result = mutableMapOf<String, Map<String, *>>()
for ((location, preferences) in map) { for ((location, preferences) in map) {
@ -63,7 +77,6 @@ class PreferencePackager {
return result return result
} }
/** /**
* @return true if successful, false if error * @return true if successful, false if error
*/ */

View file

@ -42,13 +42,13 @@
android:layout_gravity="bottom|center_horizontal" android:layout_gravity="bottom|center_horizontal"
android:layout_marginTop="32dp" android:layout_marginTop="32dp"
android:layout_marginBottom="16dp" android:layout_marginBottom="16dp"
android:backgroundTint="?attr/colorPrimaryContainer" android:backgroundTint="?attr/colorPrimary"
android:fontFamily="@font/poppins_bold" android:fontFamily="@font/poppins_bold"
android:text="@string/login" android:text="@string/login"
android:textColor="?attr/colorOnPrimaryContainer" android:textColor="?attr/colorOnPrimary"
app:cornerRadius="16dp" app:cornerRadius="16dp"
app:icon="@drawable/ic_anilist" app:icon="@drawable/ic_anilist"
app:iconTint="?attr/colorOnPrimaryContainer" /> app:iconTint="?attr/colorOnPrimary" />
<Button <Button
android:id="@+id/importSettingsButton" android:id="@+id/importSettingsButton"
android:layout_width="156dp" android:layout_width="156dp"