Skip to content

Commit

Permalink
Fix OTP Uri export with no issuer
Browse files Browse the repository at this point in the history
  • Loading branch information
replydev committed Sep 28, 2023
1 parent 65d1081 commit 9ebfb5e
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions src/otp/otp_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,8 @@ impl OTPElement {

fn get_label(issuer: &str, label: &str) -> String {
let encoded_label = urlencoding::encode(label);
if !issuer.is_empty() {
let encoded_issuer = urlencoding::encode(issuer);
return format!("{encoded_issuer}:{encoded_label}");
}
encoded_label.to_string()
let encoded_issuer = urlencoding::encode(issuer);
format!("{encoded_issuer}:{encoded_label}")
}

#[cfg(test)]
Expand All @@ -236,7 +233,7 @@ mod test {
use crate::otp::from_otp_uri::FromOtpUri;

#[test]
fn test_serialization_otp_uri() {
fn test_serialization_otp_uri_full_element() {
let otp_element = OTPElement {
secret: String::from("xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g"),
issuer: String::from("IssuerText"),
Expand All @@ -248,7 +245,23 @@ mod test {
counter: None,
pin: None,
};
assert_eq!(otp_element.get_otpauth_uri().as_str(), "otpauth://totp/IssuerText:LabelText?secret=xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g&algorithm=SHA1&digits=6&period=30&lock=false");
assert_eq!("otpauth://totp/IssuerText:LabelText?secret=xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g&algorithm=SHA1&digits=6&period=30&lock=false",otp_element.get_otpauth_uri().as_str());
}

#[test]
fn test_serialization_otp_uri_no_issuer() {
let otp_element = OTPElement {
secret: String::from("xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g"),
issuer: String::from(""),
label: String::from("LabelText"),
digits: 6,
type_: Totp,
algorithm: Sha1,
period: 30,
counter: None,
pin: None,
};
assert_eq!("otpauth://totp/:LabelText?secret=xr5gh44x7bprcqgrdtulafeevt5rxqlbh5wvked22re43dh2d4mapv5g&algorithm=SHA1&digits=6&period=30&lock=false",otp_element.get_otpauth_uri().as_str());
}

#[test]
Expand Down

0 comments on commit 9ebfb5e

Please sign in to comment.