From 930cb8bf33049ab1c436d30fd041c9c1d257901e Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Thu, 27 Apr 2023 11:38:17 +0200 Subject: [PATCH] Remove port from domain on TOPT URI --- src/data/app_config.rs | 6 ++++++ src/data/totp_key.rs | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/data/app_config.rs b/src/data/app_config.rs index a70309d..461f009 100644 --- a/src/data/app_config.rs +++ b/src/data/app_config.rs @@ -95,6 +95,12 @@ impl AppConfig { pub fn domain_name(&self) -> &str { self.website_origin.split('/').nth(2).unwrap_or(APP_NAME) } + + /// Get the domain without the port + pub fn domain_name_without_port(&self) -> &str { + let domain = self.domain_name(); + domain.split_once(':').map(|i| i.0).unwrap_or(domain) + } } #[cfg(test)] diff --git a/src/data/totp_key.rs b/src/data/totp_key.rs index 53adf13..80348e5 100644 --- a/src/data/totp_key.rs +++ b/src/data/totp_key.rs @@ -40,10 +40,10 @@ impl TotpKey { pub fn url_for_user(&self, u: &User, conf: &AppConfig) -> String { format!( "otpauth://totp/{}:{}?secret={}&issuer={}&algorithm=SHA1&digits={}&period={}", - urlencoding::encode(conf.domain_name()), + urlencoding::encode(conf.domain_name_without_port()), urlencoding::encode(&u.username), self.encoded, - urlencoding::encode(conf.domain_name()), + urlencoding::encode(conf.domain_name_without_port()), NUM_DIGITS, PERIOD, ) @@ -53,7 +53,7 @@ impl TotpKey { pub fn account_name(&self, u: &User, conf: &AppConfig) -> String { format!( "{}:{}", - urlencoding::encode(conf.domain_name()), + urlencoding::encode(conf.domain_name_without_port()), urlencoding::encode(&u.username) ) }