taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

Config.kt (2156B)


      1 /*
      2  * This file is part of GNU Taler
      3  * (C) 2020 Taler Systems S.A.
      4  *
      5  * GNU Taler is free software; you can redistribute it and/or modify it under the
      6  * terms of the GNU General Public License as published by the Free Software
      7  * Foundation; either version 3, or (at your option) any later version.
      8  *
      9  * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
     10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     11  * A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License along with
     14  * GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     15  */
     16 
     17 package net.taler.cashier.config
     18 
     19 import kotlinx.serialization.Serializable
     20 import net.taler.common.Challenge
     21 import net.taler.common.Timestamp
     22 import okhttp3.Credentials
     23 
     24 sealed class TokenResult {
     25     data class Success(
     26         val expiration: Timestamp,
     27         val accessToken: String,
     28     ): TokenResult()
     29 
     30     data class TanRequired(
     31         val challenges: List<Challenge>,
     32         val combiAnd: Boolean,
     33     ): TokenResult()
     34 
     35     data class Error(
     36         val authError: Boolean,
     37         val msg: String,
     38     ): TokenResult()
     39 }
     40 
     41 data class Config(
     42     val bankUrl: String,
     43     val username: String,
     44     val password: String = "",
     45     val accessToken: String? = null,
     46     val expiration: Timestamp? = null,
     47 ) {
     48     val basicAuth: String get() = Credentials.basic(username, password)
     49     val bearerAuth: String? get() = accessToken?.let { "Bearer $it" }
     50 
     51     fun isTokenValid(): Boolean {
     52         if (accessToken == null || expiration == null) return false
     53         return expiration > Timestamp.now()
     54     }
     55 }
     56 
     57 @Serializable
     58 data class ConfigResponse(
     59     val version: String,
     60     val currency: String,
     61 )
     62 
     63 sealed class ConfigResult {
     64     data class Error(val authError: Boolean, val msg: String) : ConfigResult()
     65     data object Offline : ConfigResult()
     66     data object Success : ConfigResult()
     67     data object Unknown : ConfigResult()
     68     data class TanRequired(val challenges: List<Challenge>, val combiAnd: Boolean): ConfigResult()
     69 }