config.rs (7944B)
1 /* 2 This file is part of TALER 3 Copyright (C) 2026 Taler Systems SA 4 5 TALER is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 use std::{str::FromStr as _, sync::LazyLock, time::Duration}; 18 19 use aws_lc_rs::signature::{ParsedPublicKey, RSA_PKCS1_2048_8192_SHA256}; 20 use compact_str::CompactString; 21 use http::HeaderValue; 22 use taler_api::{ 23 Serve, 24 config::{ApiCfg, DbCfg, WireGatewayCfg}, 25 }; 26 use taler_common::{ 27 config::{CfgErr, Config, Section}, 28 config_bail, map_config, 29 types::{ 30 amount::Currency, 31 payto::{ACH, BankID}, 32 }, 33 }; 34 use taler_macros::EnumMeta; 35 use url::Url; 36 37 use crate::payto::WiseAccount; 38 39 #[derive(Debug, Clone, Copy)] 40 pub enum AccountType { 41 Exchange, 42 Normal, 43 } 44 45 #[derive(Debug, Clone, Copy, EnumMeta)] 46 #[allow(non_camel_case_types)] 47 #[enum_meta(Str)] 48 pub enum ApiEnv { 49 production, 50 sandbox, 51 } 52 53 impl ApiEnv { 54 pub const fn api(self) -> &'static LazyLock<Url> { 55 static PROD: LazyLock<Url> = 56 LazyLock::new(|| Url::from_str("https://api.wise.com").unwrap()); 57 static SANDBOX: LazyLock<Url> = 58 LazyLock::new(|| Url::from_str("https://api.wise-sandbox.com").unwrap()); 59 match self { 60 ApiEnv::production => &PROD, 61 ApiEnv::sandbox => &SANDBOX, 62 } 63 } 64 65 pub fn public_key(self) -> ParsedPublicKey { 66 let pem = match self { 67 ApiEnv::production => { 68 "-----BEGIN PUBLIC KEY----- 69 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvO8vXV+JksBzZAY6GhSO 70 XdoTCfhXaaiZ+qAbtaDBiu2AGkGVpmEygFmWP4Li9m5+Ni85BhVvZOodM9epgW3F 71 bA5Q1SexvAF1PPjX4JpMstak/QhAgl1qMSqEevL8cmUeTgcMuVWCJmlge9h7B1CS 72 D4rtlimGZozG39rUBDg6Qt2K+P4wBfLblL0k4C4YUdLnpGYEDIth+i8XsRpFlogx 73 CAFyH9+knYsDbR43UJ9shtc42Ybd40Afihj8KnYKXzchyQ42aC8aZ/h5hyZ28yVy 74 Oj3Vos0VdBIs/gAyJ/4yyQFCXYte64I7ssrlbGRaco4nKF3HmaNhxwyKyJafz19e 75 HwIDAQAB 76 -----END PUBLIC KEY-----" 77 } 78 ApiEnv::sandbox => { 79 "-----BEGIN PUBLIC KEY----- 80 MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAwpb91cEYuyJNQepZAVfP 81 ZIlPZfNUefH+n6w9SW3fykqKu938cR7WadQv87oF2VuT+fDt7kqeRziTmPSUhqPU 82 ys/V2Q1rlfJuXbE+Gga37t7zwd0egQ+KyOEHQOpcTwKmtZ81ieGHynAQzsn1We3j 83 wt760MsCPJ7GMT141ByQM+yW1Bx+4SG3IGjXWyqOWrcXsxAvIXkpUD/jK/L958Cg 84 nZEgz0BSEh0QxYLITnW1lLokSx/dTianWPFEhMC9BgijempgNXHNfcVirg1lPSyg 85 z7KqoKUN0oHqWLr2U1A+7kqrl6O2nx3CKs1bj1hToT1+p4kcMoHXA7kA+VBLUpEs 86 VwIDAQAB 87 -----END PUBLIC KEY-----" 88 } 89 }; 90 let parsed = pem::parse(pem).unwrap(); 91 ParsedPublicKey::new(&RSA_PKCS1_2048_8192_SHA256, parsed.contents()).unwrap() 92 } 93 } 94 95 pub fn parse_db_cfg(cfg: &Config) -> Result<DbCfg, CfgErr> { 96 DbCfg::parse(cfg.section("wisedb-postgres")) 97 } 98 99 pub struct WiseBalance { 100 pub id: u32, 101 pub currency: Currency, 102 pub payto: WiseAccount, 103 } 104 105 fn balances(cfg: &Config) -> Result<Vec<WiseBalance>, CfgErr> { 106 let mut balances = Vec::new(); 107 let mut ids = std::collections::BTreeSet::new(); 108 for s in cfg 109 .sections() 110 .filter(|s| s.name.starts_with("wise-balance-")) 111 { 112 let id = s.number("ID").require()?; 113 if !ids.insert(id) { 114 return Err(s.invalid("ID", format!("duplicate balance ID {id}"))); 115 } 116 balances.push(WiseBalance { 117 id, 118 currency: s.currency("CURRENCY").require()?, 119 payto: map_config!(s, "payto type", "PAYTO_TYPE", 120 "iban" => { 121 WiseAccount::IBAN(BankID { 122 iban: s.parse("IBAN", "iban").require()?, 123 bic: s.parse("BIC", "bic").opt()? 124 }) 125 }, 126 "ach" => { 127 WiseAccount::ACH(ACH { 128 routing_number: s.parse("ACH routing number", "routing_number").require()?, 129 account_number: s.parse("ACH account_number", "account_number").require()? 130 }) 131 }, 132 ) 133 .require()?, 134 }); 135 } 136 Ok(balances) 137 } 138 139 /// taler-wise setup config 140 pub struct SetupCfg { 141 pub api: ApiEnv, 142 pub token: HeaderValue, 143 pub profile_id: Option<u64>, 144 pub name: Option<CompactString>, 145 pub balances: Vec<WiseBalance>, 146 } 147 148 impl SetupCfg { 149 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 150 let ms = cfg.section("wise"); 151 let s = cfg.section("wise-worker"); 152 Ok(Self { 153 api: ms.parse("env", "API_ENV").require()?, 154 profile_id: ms.number("PROFILE_ID").opt()?, 155 token: parse_token(&s)?, 156 name: ms.cstr("NAME").opt()?, 157 balances: balances(cfg)?, 158 }) 159 } 160 } 161 162 /// taler-wise webhook subscription config 163 pub struct SubscribeWebhooksCfg { 164 pub api: ApiEnv, 165 pub profile_id: u64, 166 pub token: HeaderValue, 167 } 168 169 impl SubscribeWebhooksCfg { 170 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 171 let wise = cfg.section("wise"); 172 let worker = cfg.section("wise-worker"); 173 Ok(Self { 174 api: wise.parse("env", "API_ENV").require()?, 175 profile_id: wise.number("PROFILE_ID").require()?, 176 token: parse_token(&worker)?, 177 }) 178 } 179 } 180 181 /// taler-wise httpd config 182 pub struct ServeCfg { 183 pub api: ApiEnv, 184 pub profile_id: u64, 185 pub name: CompactString, 186 pub serve: Serve, 187 pub balances: Vec<WiseBalance>, 188 pub wire_gateway: Option<WireGatewayCfg>, 189 pub revenue: Option<ApiCfg>, 190 pub observability: Option<ApiCfg>, 191 } 192 193 impl ServeCfg { 194 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 195 let s = cfg.section("wise"); 196 Ok(Self { 197 api: s.parse("env", "API_ENV").require()?, 198 profile_id: s.number("PROFILE_ID").require()?, 199 name: s.cstr("NAME").require()?, 200 serve: Serve::parse(&cfg.section("wise-httpd"))?, 201 wire_gateway: WireGatewayCfg::parse(cfg.section("wise-httpd-wire-gateway-api"))?, 202 revenue: ApiCfg::parse(cfg.section("wise-httpd-revenue-api"))?, 203 observability: ApiCfg::parse(cfg.section("wise-httpd-observability-api"))?, 204 balances: balances(cfg)?, 205 }) 206 } 207 } 208 209 /// taler-wise worker config 210 pub struct WorkerCfg { 211 pub api: ApiEnv, 212 pub profile_id: u64, 213 pub token: HeaderValue, 214 pub frequency: Duration, 215 pub balances: Vec<WiseBalance>, 216 } 217 218 fn parse_token(section: &Section<'_, '_>) -> Result<HeaderValue, CfgErr> { 219 section 220 .value("string", "TOKEN", |token| { 221 let mut value = HeaderValue::from_str(&format!("Bearer {token}"))?; 222 value.set_sensitive(true); 223 Ok::<_, http::header::InvalidHeaderValue>(value) 224 }) 225 .require() 226 } 227 228 impl WorkerCfg { 229 pub fn parse(cfg: &Config) -> Result<Self, CfgErr> { 230 let ms = cfg.section("wise"); 231 let s = cfg.section("wise-worker"); 232 let cfg = Self { 233 api: ms.parse("env", "API_ENV").require()?, 234 profile_id: ms.number("PROFILE_ID").require()?, 235 token: parse_token(&s)?, 236 frequency: s.duration("FREQUENCY").require()?, 237 balances: balances(cfg)?, 238 }; 239 if cfg.balances.is_empty() { 240 config_bail!("Missing configured balance in a section [wise-balance-*]"); 241 } 242 if cfg.frequency.is_zero() { 243 return Err(s.invalid("FREQUENCY", "must be positive")); 244 } 245 Ok(cfg) 246 } 247 }