Skip to content

Commit d74bca7

Browse files
committed
Move cert migration to GotifyApplication and synchronize calls
1 parent 11448f7 commit d74bca7

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

app/src/main/kotlin/com/github/gotify/GotifyApplication.kt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import android.app.Application
44
import android.app.NotificationManager
55
import android.os.Build
66
import androidx.preference.PreferenceManager
7+
import com.github.gotify.api.CertUtils
78
import com.github.gotify.log.LoggerHelper
89
import com.github.gotify.log.UncaughtExceptionHandler
910
import com.github.gotify.settings.ThemeHelper
11+
import java.io.File
12+
import java.io.FileOutputStream
13+
import java.io.IOException
1014
import org.tinylog.kotlin.Logger
1115

1216
class GotifyApplication : Application() {
@@ -26,6 +30,24 @@ class GotifyApplication : Application() {
2630
)
2731
}
2832

33+
val settings = Settings(this)
34+
if (settings.legacyCert != null) {
35+
Logger.info("Migrating legacy CA cert to new location")
36+
var legacyCert: String? = null
37+
try {
38+
legacyCert = settings.legacyCert
39+
settings.legacyCert = null
40+
val caCertFile = File(settings.filesDir, CertUtils.CA_CERT_NAME)
41+
FileOutputStream(caCertFile).use {
42+
it.write(legacyCert?.encodeToByteArray())
43+
}
44+
settings.caCertPath = caCertFile.absolutePath
45+
Logger.info("Migration of legacy CA cert succeeded")
46+
} catch (e: IOException) {
47+
Logger.error(e, "Migration of legacy CA cert failed")
48+
if (legacyCert != null) settings.legacyCert = legacyCert
49+
}
50+
}
2951
super.onCreate()
3052
}
3153
}

app/src/main/kotlin/com/github/gotify/Settings.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ internal class Settings(context: Context) {
2929
set(value) = sharedPreferences.edit().putString("version", value).apply()
3030
var legacyCert: String?
3131
get() = sharedPreferences.getString("cert", null)
32-
set(value) = sharedPreferences.edit().putString("cert", value).apply()
32+
set(value) = sharedPreferences.edit().putString("cert", value).commit().toUnit()
3333
var caCertPath: String?
3434
get() = sharedPreferences.getString("caCertPath", null)
35-
set(value) = sharedPreferences.edit().putString("caCertPath", value).apply()
35+
set(value) = sharedPreferences.edit().putString("caCertPath", value).commit().toUnit()
3636
var caCertCN: String?
3737
get() = sharedPreferences.getString("caCertCN", null)
3838
set(value) = sharedPreferences.edit().putString("caCertCN", value).apply()
@@ -76,4 +76,7 @@ internal class Settings(context: Context) {
7676
clientCertPassword
7777
)
7878
}
79+
80+
@Suppress("UnusedReceiverParameter")
81+
private fun Any?.toUnit() = Unit
7982
}

app/src/main/kotlin/com/github/gotify/api/ClientFactory.kt

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import com.github.gotify.client.api.UserApi
77
import com.github.gotify.client.api.VersionApi
88
import com.github.gotify.client.auth.ApiKeyAuth
99
import com.github.gotify.client.auth.HttpBasicAuth
10-
import java.io.File
11-
import java.io.FileOutputStream
12-
import java.io.IOException
13-
import org.tinylog.kotlin.Logger
1410

1511
internal object ClientFactory {
1612
private fun unauthorized(
@@ -60,23 +56,6 @@ internal object ClientFactory {
6056
baseUrl: String = settings.url
6157
): ApiClient {
6258
val client = ApiClient(authentications)
63-
if (settings.legacyCert != null) {
64-
Logger.info("Migrating legacy CA cert to new location")
65-
var legacyCert: String? = null
66-
try {
67-
legacyCert = settings.legacyCert
68-
settings.legacyCert = null
69-
val caCertFile = File(settings.filesDir, CertUtils.CA_CERT_NAME)
70-
FileOutputStream(caCertFile).use {
71-
it.write(legacyCert?.encodeToByteArray())
72-
}
73-
settings.caCertPath = caCertFile.absolutePath
74-
Logger.info("Migration of legacy CA cert succeeded")
75-
} catch (e: IOException) {
76-
Logger.error(e, "Migration of legacy CA cert failed")
77-
if (legacyCert != null) settings.legacyCert = legacyCert
78-
}
79-
}
8059
CertUtils.applySslSettings(client.okBuilder, sslSettings)
8160
client.adapterBuilder.baseUrl("$baseUrl/")
8261
return client

0 commit comments

Comments
 (0)