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) {
val salt = jsonString.copyOfRange(0, 16)
val encrypted = jsonString.copyOfRange(16, jsonString.size)
val decryptedJson = PreferenceKeystore.decryptWithPassword(
password,
encrypted,
salt
)
val decryptedJson = try {
PreferenceKeystore.decryptWithPassword(
password,
encrypted,
salt
)
} catch (e: Exception) {
toast("Incorrect password")
return@passwordAlertDialog
}
if(PreferencePackager.unpack(decryptedJson))
println("Settings imported")
restartApp()
} else {
toast("Password cannot be empty")
}
}
} else {
} else if (name.endsWith(".ani")) {
val decryptedJson = jsonString.toString(Charsets.UTF_8)
if(PreferencePackager.unpack(decryptedJson))
restartApp()
} else {
toast("Invalid file type")
}
} catch (e: Exception) {
e.printStackTrace()

View file

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

View file

@ -1,7 +1,11 @@
package ani.dantotsu.settings.saving.internal
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.toast
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
@ -10,11 +14,18 @@ class PreferencePackager {
companion object {
/**
* @return a json string of the packed preferences
*/
fun pack(map: Map<Location, SharedPreferences>): String {
val prefsMap = packagePreferences(map)
val gson = Gson()
return gson.toJson(prefsMap)
}
/**
* @return true if successful, false if error
*/
fun unpack(decryptedJson: String): Boolean {
val gson = Gson()
val type = object : TypeToken<Map<String, Map<String, Map<String, Any>>>>() {}.type //oh god...
@ -47,6 +58,9 @@ class PreferencePackager {
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, *>> {
val result = mutableMapOf<String, Map<String, *>>()
for ((location, preferences) in map) {
@ -63,7 +77,6 @@ class PreferencePackager {
return result
}
/**
* @return true if successful, false if error
*/

View file

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