Skip to content

Commit

Permalink
Move cert migration to GotifyApplication and synchronize calls
Browse files Browse the repository at this point in the history
  • Loading branch information
cyb3rko committed Jun 2, 2024
1 parent 11448f7 commit d74bca7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 23 deletions.
22 changes: 22 additions & 0 deletions app/src/main/kotlin/com/github/gotify/GotifyApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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()
}
}
7 changes: 5 additions & 2 deletions app/src/main/kotlin/com/github/gotify/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -76,4 +76,7 @@ internal class Settings(context: Context) {
clientCertPassword
)
}

@Suppress("UnusedReceiverParameter")
private fun Any?.toUnit() = Unit
}
21 changes: 0 additions & 21 deletions app/src/main/kotlin/com/github/gotify/api/ClientFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d74bca7

Please sign in to comment.