config.kt (2371B)
1 /* 2 * This file is part of LibEuFin. 3 * Copyright (C) 2024-2025 Taler Systems S.A. 4 5 * LibEuFin is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU Affero General Public License as 7 * published by the Free Software Foundation; either version 3, or 8 * (at your option) any later version. 9 10 * LibEuFin is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General 13 * Public License for more details. 14 15 * You should have received a copy of the GNU Affero General Public 16 * License along with LibEuFin; see the file COPYING. If not, see 17 * <http://www.gnu.org/licenses/> 18 */ 19 20 package tech.libeufin.common 21 22 import org.slf4j.Logger 23 import org.slf4j.LoggerFactory 24 25 private val logger: Logger = LoggerFactory.getLogger("libeufin-config") 26 27 sealed interface ServerConfig { 28 data class Unix(val path: String): ServerConfig 29 data class Tcp(val addr: String, val port: Int): ServerConfig 30 } 31 32 fun TalerConfig.loadServerConfig(section: String): ServerConfig { 33 val sect = section(section) 34 return sect.mapLambda("serve", "server method", mapOf( 35 "tcp" to { ServerConfig.Tcp(sect.string("address").orNull() ?: sect.string("bind_to").require(), sect.number("port").require()) }, 36 "unix" to { ServerConfig.Unix(sect.string("unixpath").require()) } 37 )).require() 38 } 39 40 fun TalerConfigSection.requireAuthMethod(): AuthMethod { 41 return mapLambda("auth_method", "auth method", mapOf( 42 "none" to { AuthMethod.None }, 43 "bearer-token" to { 44 logger.warn("Deprecated auth method option 'auth_method' used deprecated value 'bearer-token'") 45 val token = string("auth_bearer_token").require() 46 AuthMethod.Bearer(token) 47 }, 48 "bearer" to { 49 val token = string("token").require() 50 AuthMethod.Bearer(token) 51 }, 52 "basic" to { 53 val username = string("username").require() 54 val password = string("password").require() 55 AuthMethod.Basic("$username:$password".encodeBase64()) 56 } 57 )).require() 58 } 59 60 sealed interface AuthMethod { 61 data object None: AuthMethod 62 data class Bearer(val token: String): AuthMethod 63 data class Basic(val token: String): AuthMethod 64 }