Move cert migration to GotifyApplication and synchronize calls
This commit is contained in:
@@ -4,9 +4,13 @@ import android.app.Application
|
||||
import android.app.NotificationManager
|
||||
import android.os.Build
|
||||
import androidx.preference.PreferenceManager
|
||||
import com.github.gotify.api.CertUtils
|
||||
import com.github.gotify.log.LoggerHelper
|
||||
import com.github.gotify.log.UncaughtExceptionHandler
|
||||
import com.github.gotify.settings.ThemeHelper
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import org.tinylog.kotlin.Logger
|
||||
|
||||
class GotifyApplication : Application() {
|
||||
@@ -26,6 +30,24 @@ class GotifyApplication : Application() {
|
||||
)
|
||||
}
|
||||
|
||||
val settings = Settings(this)
|
||||
if (settings.legacyCert != null) {
|
||||
Logger.info("Migrating legacy CA cert to new location")
|
||||
var legacyCert: String? = null
|
||||
try {
|
||||
legacyCert = settings.legacyCert
|
||||
settings.legacyCert = null
|
||||
val caCertFile = File(settings.filesDir, CertUtils.CA_CERT_NAME)
|
||||
FileOutputStream(caCertFile).use {
|
||||
it.write(legacyCert?.encodeToByteArray())
|
||||
}
|
||||
settings.caCertPath = caCertFile.absolutePath
|
||||
Logger.info("Migration of legacy CA cert succeeded")
|
||||
} catch (e: IOException) {
|
||||
Logger.error(e, "Migration of legacy CA cert failed")
|
||||
if (legacyCert != null) settings.legacyCert = legacyCert
|
||||
}
|
||||
}
|
||||
super.onCreate()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,10 +29,10 @@ internal class Settings(context: Context) {
|
||||
set(value) = sharedPreferences.edit().putString("version", value).apply()
|
||||
var legacyCert: String?
|
||||
get() = sharedPreferences.getString("cert", null)
|
||||
set(value) = sharedPreferences.edit().putString("cert", value).apply()
|
||||
set(value) = sharedPreferences.edit().putString("cert", value).commit().toUnit()
|
||||
var caCertPath: String?
|
||||
get() = sharedPreferences.getString("caCertPath", null)
|
||||
set(value) = sharedPreferences.edit().putString("caCertPath", value).apply()
|
||||
set(value) = sharedPreferences.edit().putString("caCertPath", value).commit().toUnit()
|
||||
var caCertCN: String?
|
||||
get() = sharedPreferences.getString("caCertCN", null)
|
||||
set(value) = sharedPreferences.edit().putString("caCertCN", value).apply()
|
||||
@@ -76,4 +76,7 @@ internal class Settings(context: Context) {
|
||||
clientCertPassword
|
||||
)
|
||||
}
|
||||
|
||||
@Suppress("UnusedReceiverParameter")
|
||||
private fun Any?.toUnit() = Unit
|
||||
}
|
||||
|
||||
@@ -7,10 +7,6 @@ import com.github.gotify.client.api.UserApi
|
||||
import com.github.gotify.client.api.VersionApi
|
||||
import com.github.gotify.client.auth.ApiKeyAuth
|
||||
import com.github.gotify.client.auth.HttpBasicAuth
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import org.tinylog.kotlin.Logger
|
||||
|
||||
internal object ClientFactory {
|
||||
private fun unauthorized(
|
||||
@@ -60,23 +56,6 @@ internal object ClientFactory {
|
||||
baseUrl: String = settings.url
|
||||
): ApiClient {
|
||||
val client = ApiClient(authentications)
|
||||
if (settings.legacyCert != null) {
|
||||
Logger.info("Migrating legacy CA cert to new location")
|
||||
var legacyCert: String? = null
|
||||
try {
|
||||
legacyCert = settings.legacyCert
|
||||
settings.legacyCert = null
|
||||
val caCertFile = File(settings.filesDir, CertUtils.CA_CERT_NAME)
|
||||
FileOutputStream(caCertFile).use {
|
||||
it.write(legacyCert?.encodeToByteArray())
|
||||
}
|
||||
settings.caCertPath = caCertFile.absolutePath
|
||||
Logger.info("Migration of legacy CA cert succeeded")
|
||||
} catch (e: IOException) {
|
||||
Logger.error(e, "Migration of legacy CA cert failed")
|
||||
if (legacyCert != null) settings.legacyCert = legacyCert
|
||||
}
|
||||
}
|
||||
CertUtils.applySslSettings(client.okBuilder, sslSettings)
|
||||
client.adapterBuilder.baseUrl("$baseUrl/")
|
||||
return client
|
||||
|
||||
Reference in New Issue
Block a user