aboutsummaryrefslogtreecommitdiff
path: root/cashier
diff options
context:
space:
mode:
authorTorsten Grote <t@grobox.de>2020-09-01 09:49:05 -0300
committerTorsten Grote <t@grobox.de>2020-09-01 09:49:05 -0300
commita0b0ee2b13b72d1ec6a489150c717c8bfa863158 (patch)
treecae49508511fc7584d2b37a68ca1400d43fbb2da /cashier
parente71c580652fe32cf887feeba67f1c75c3c2d7237 (diff)
downloadtaler-android-a0b0ee2b13b72d1ec6a489150c717c8bfa863158.tar.gz
taler-android-a0b0ee2b13b72d1ec6a489150c717c8bfa863158.tar.bz2
taler-android-a0b0ee2b13b72d1ec6a489150c717c8bfa863158.zip
[cashier] check authentication credentials against auth endpoint
before accepting config This was an unannounced change at the server which removed auth from the /config endpoint.
Diffstat (limited to 'cashier')
-rw-r--r--cashier/src/main/java/net/taler/cashier/Response.kt2
-rw-r--r--cashier/src/main/java/net/taler/cashier/config/ConfigManager.kt37
2 files changed, 26 insertions, 13 deletions
diff --git a/cashier/src/main/java/net/taler/cashier/Response.kt b/cashier/src/main/java/net/taler/cashier/Response.kt
index 0ad39d0..c5b1c32 100644
--- a/cashier/src/main/java/net/taler/cashier/Response.kt
+++ b/cashier/src/main/java/net/taler/cashier/Response.kt
@@ -55,7 +55,7 @@ class Response<out T> private constructor(
}
}
- private val isFailure: Boolean get() = value is Failure
+ val isFailure: Boolean get() = value is Failure
suspend fun onSuccess(block: suspend (result: T) -> Unit): Response<T> {
@Suppress("UNCHECKED_CAST")
diff --git a/cashier/src/main/java/net/taler/cashier/config/ConfigManager.kt b/cashier/src/main/java/net/taler/cashier/config/ConfigManager.kt
index a18073d..f83c7ba 100644
--- a/cashier/src/main/java/net/taler/cashier/config/ConfigManager.kt
+++ b/cashier/src/main/java/net/taler/cashier/config/ConfigManager.kt
@@ -24,7 +24,10 @@ import androidx.annotation.WorkerThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.security.crypto.EncryptedSharedPreferences
+import androidx.security.crypto.EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV
+import androidx.security.crypto.EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
import androidx.security.crypto.MasterKeys
+import androidx.security.crypto.MasterKeys.AES256_GCM_SPEC
import io.ktor.client.HttpClient
import io.ktor.client.request.get
import io.ktor.client.request.header
@@ -56,11 +59,9 @@ class ConfigManager(
val configDestination = ConfigFragmentDirections.actionGlobalConfigFragment()
- private val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
+ private val masterKeyAlias = MasterKeys.getOrCreate(AES256_GCM_SPEC)
private val prefs = EncryptedSharedPreferences.create(
- PREF_NAME, masterKeyAlias, app,
- EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
- EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
+ PREF_NAME, masterKeyAlias, app, AES256_SIV, AES256_GCM
)
internal var config = Config(
@@ -111,17 +112,29 @@ class ConfigManager(
}
}
- private suspend fun checkConfig(config: Config): Response<ConfigResponse> =
- withContext(Dispatchers.IO) {
- val url = "${config.bankUrl}/config"
- Log.d(TAG, "Checking config: $url")
- response {
- httpClient.get(url) {
- // TODO why does that not fail already?
+ private suspend fun checkConfig(config: Config) = withContext(Dispatchers.IO) {
+ val url = "${config.bankUrl}/config"
+ Log.d(TAG, "Checking config: $url")
+ val configResponse = response {
+ httpClient.get(url) as ConfigResponse
+ }
+ if (configResponse.isFailure) {
+ configResponse
+ } else {
+ // we need to check an endpoint that requires authentication as well
+ // to see if the credentials are valid
+ val balanceResponse = response {
+ val authUrl = "${config.bankUrl}/accounts/${config.username}/balance"
+ Log.d(TAG, "Checking auth: $authUrl")
+ httpClient.get<Unit>(authUrl) {
header(Authorization, config.basicAuth)
- } as ConfigResponse
+ }
}
+ @Suppress("UNCHECKED_CAST") // The type doesn't matter for failures
+ if (balanceResponse.isFailure) balanceResponse as Response<ConfigResponse>
+ else configResponse
}
+ }
@WorkerThread
@SuppressLint("ApplySharedPref")