TalerUri.kt (2355B)
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.common 18 19 import java.util.Locale 20 21 public object TalerUri { 22 23 private const val SCHEME = "taler://" 24 private const val SCHEME_INSECURE = "taler+http://" 25 private const val AUTHORITY_PAY = "pay" 26 private const val AUTHORITY_WITHDRAW = "withdraw" 27 private const val AUTHORITY_REFUND = "refund" 28 private const val AUTHORITY_TIP = "tip" 29 30 public data class WithdrawUriResult( 31 val bankIntegrationApiBaseUrl: String, 32 val withdrawalOperationId: String 33 ) 34 35 /** 36 * Parses a withdraw URI and returns a bank status URL or null if the URI was invalid. 37 */ 38 public fun parseWithdrawUri(uri: String): WithdrawUriResult? { 39 val (resultScheme, prefix) = when { 40 uri.startsWith(SCHEME, ignoreCase = true) -> { 41 Pair("https://", "${SCHEME}${AUTHORITY_WITHDRAW}/") 42 } 43 uri.startsWith(SCHEME_INSECURE, ignoreCase = true) -> { 44 Pair("http://", "${SCHEME_INSECURE}${AUTHORITY_WITHDRAW}/") 45 } 46 else -> return null 47 } 48 if (!uri.startsWith(prefix, ignoreCase = true)) return null 49 val parts = uri.let { 50 (if (it.endsWith("/")) it.dropLast(1) else it).substring(prefix.length).split('/') 51 } 52 if (parts.size < 2) return null 53 val host = parts[0].lowercase(Locale.ROOT) 54 val pathSegments = parts.slice(1 until parts.size - 1).joinToString("/") 55 val withdrawId = parts.last() 56 if (withdrawId.isBlank()) return null 57 val url = "${resultScheme}${host}/${pathSegments}" 58 59 return WithdrawUriResult(url, withdrawId) 60 } 61 62 }