config.kt (2400B)
1 /* 2 * This file is part of LibEuFin. 3 * Copyright (C) 2024, 2025, 2026 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 import java.nio.file.Path 25 26 private val logger: Logger = LoggerFactory.getLogger("libeufin-config") 27 28 sealed interface ServerConfig { 29 data class Unix(val path: Path): ServerConfig 30 data class Tcp(val addr: String, val port: Int): ServerConfig 31 } 32 33 fun TalerConfig.loadServerConfig(section: String): ServerConfig { 34 val sect = section(section) 35 return sect.mapLambda("serve", "server method", mapOf( 36 "tcp" to { ServerConfig.Tcp(sect.string("address").orNull() ?: sect.string("bind_to").require(), sect.number("port").require()) }, 37 "unix" to { ServerConfig.Unix(sect.path("unixpath").require()) } 38 )).require() 39 } 40 41 fun TalerConfigSection.requireAuthMethod(): AuthMethod { 42 return mapLambda("auth_method", "auth method", mapOf( 43 "none" to { AuthMethod.None }, 44 "bearer-token" to { 45 logger.warn("Deprecated auth method option 'auth_method' used deprecated value 'bearer-token'") 46 val token = string("auth_bearer_token").require() 47 AuthMethod.Bearer(token) 48 }, 49 "bearer" to { 50 val token = string("token").require() 51 AuthMethod.Bearer(token) 52 }, 53 "basic" to { 54 val username = string("username").require() 55 val password = string("password").require() 56 AuthMethod.Basic("$username:$password".encodeBase64()) 57 } 58 )).require() 59 } 60 61 sealed interface AuthMethod { 62 data object None: AuthMethod 63 data class Bearer(val token: String): AuthMethod 64 data class Basic(val token: String): AuthMethod 65 }