summaryrefslogtreecommitdiff
path: root/merchant-terminal/src/main/java/net/taler/merchantpos/config/ConfigManager.kt
blob: edb80592425d1d302934dc0069b4a8a0f1f2c6eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * This file is part of GNU Taler
 * (C) 2020 Taler Systems S.A.
 *
 * GNU Taler is free software; you can redistribute it and/or modify it under the
 * terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 3, or (at your option) any later version.
 *
 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
 */

package net.taler.merchantpos.config

import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.util.Base64.NO_WRAP
import android.util.Base64.encodeToString
import android.util.Log
import androidx.annotation.UiThread
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.android.volley.Request.Method.GET
import com.android.volley.RequestQueue
import com.android.volley.Response.ErrorListener
import com.android.volley.Response.Listener
import com.android.volley.VolleyError
import com.android.volley.toolbox.JsonObjectRequest
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import net.taler.merchantpos.R
import org.json.JSONObject

private const val SETTINGS_NAME = "taler-merchant-terminal"

private const val SETTINGS_CONFIG_URL = "configUrl"
private const val SETTINGS_USERNAME = "username"
private const val SETTINGS_PASSWORD = "password"

internal const val CONFIG_URL_DEMO = "https://docs.taler.net/_static/sample-pos-config.json"
internal const val CONFIG_USERNAME_DEMO = ""
internal const val CONFIG_PASSWORD_DEMO = ""

private val TAG = ConfigManager::class.java.simpleName

interface ConfigurationReceiver {
    /**
     * Returns null if the configuration was valid, or a error string for user display otherwise.
     */
    suspend fun onConfigurationReceived(json: JSONObject, currency: String): String?
}

class ConfigManager(
    private val context: Context,
    private val scope: CoroutineScope,
    private val mapper: ObjectMapper,
    private val queue: RequestQueue
) {

    private val prefs = context.getSharedPreferences(SETTINGS_NAME, MODE_PRIVATE)
    private val configurationReceivers = ArrayList<ConfigurationReceiver>()

    var config = Config(
        configUrl = prefs.getString(SETTINGS_CONFIG_URL, CONFIG_URL_DEMO)!!,
        username = prefs.getString(SETTINGS_USERNAME, CONFIG_USERNAME_DEMO)!!,
        password = prefs.getString(SETTINGS_PASSWORD, CONFIG_PASSWORD_DEMO)!!
    )
    var merchantConfig: MerchantConfig? = null
        private set

    private val mConfigUpdateResult = MutableLiveData<ConfigUpdateResult>()
    val configUpdateResult: LiveData<ConfigUpdateResult> = mConfigUpdateResult

    fun addConfigurationReceiver(receiver: ConfigurationReceiver) {
        configurationReceivers.add(receiver)
    }

    @UiThread
    fun fetchConfig(config: Config, save: Boolean, savePassword: Boolean = false) {
        mConfigUpdateResult.value = null
        val configToSave = if (save) {
            if (savePassword) config else config.copy(password = "")
        } else null

        val stringRequest = object : JsonObjectRequest(GET, config.configUrl, null,
            Listener { onConfigReceived(it, configToSave) },
            ErrorListener { onNetworkError(it) }
        ) {
            // send basic auth header
            override fun getHeaders(): MutableMap<String, String> {
                val credentials = "${config.username}:${config.password}"
                val auth = ("Basic ${encodeToString(credentials.toByteArray(), NO_WRAP)}")
                return mutableMapOf("Authorization" to auth)
            }
        }
        queue.add(stringRequest)
    }

    @UiThread
    private fun onConfigReceived(json: JSONObject, config: Config?) {
        val merchantConfig: MerchantConfig = try {
            mapper.readValue(json.getString("config"))
        } catch (e: Exception) {
            Log.e(TAG, "Error parsing merchant config", e)
            val msg = context.getString(R.string.config_error_malformed)
            mConfigUpdateResult.value = ConfigUpdateResult.Error(msg)
            return
        }

        val params = mapOf("instance" to merchantConfig.instance)
        val req = MerchantRequest(GET, merchantConfig, "config", params, null,
            Listener { onMerchantConfigReceived(config, json, merchantConfig, it) },
            ErrorListener { onNetworkError(it) }
        )
        queue.add(req)
    }

    private fun onMerchantConfigReceived(
        newConfig: Config?,
        configJson: JSONObject,
        merchantConfig: MerchantConfig,
        json: JSONObject
    ) = scope.launch(Dispatchers.Default) {
        val currency = json.getString("currency")

        for (receiver in configurationReceivers) {
            val result = try {
                receiver.onConfigurationReceived(configJson, currency)
            } catch (e: Exception) {
                Log.e(TAG, "Error handling configuration by ${receiver::class.java.simpleName}", e)
                context.getString(R.string.config_error_unknown)
            }
            if (result != null) {  // error
                mConfigUpdateResult.postValue(ConfigUpdateResult.Error(result))
                return@launch
            }
        }
        newConfig?.let {
            config = it
            saveConfig(it)
        }
        this@ConfigManager.merchantConfig = merchantConfig.copy(currency = currency)
        mConfigUpdateResult.postValue(ConfigUpdateResult.Success(currency))
    }

    fun forgetPassword() {
        config = config.copy(password = "")
        saveConfig(config)
        merchantConfig = null
    }

    private fun saveConfig(config: Config) {
        prefs.edit()
            .putString(SETTINGS_CONFIG_URL, config.configUrl)
            .putString(SETTINGS_USERNAME, config.username)
            .putString(SETTINGS_PASSWORD, config.password)
            .apply()
    }

    @UiThread
    private fun onNetworkError(it: VolleyError?) {
        val msg = context.getString(
            if (it?.networkResponse?.statusCode == 401) R.string.config_auth_error
            else R.string.config_error_network
        )
        mConfigUpdateResult.value = ConfigUpdateResult.Error(msg)
    }

}

sealed class ConfigUpdateResult {
    data class Error(val msg: String) : ConfigUpdateResult()
    data class Success(val currency: String) : ConfigUpdateResult()
}