Skip to content

Commit

Permalink
fix: make TOTP issuer field optional (#240)
Browse files Browse the repository at this point in the history
Both per:
- google authenticator wiki: https://github.com/google/google-authenticator/wiki/Key-Uri-Format#issuer
- yubikey website: https://docs.yubico.com/yesdk/users-manual/application-oath/uri-string-format.html

`issuer` parameter in `otpauth://` URI scheme is optional, even though
it's highly recommended to be present.

This parameter was actually optional in this repo initially, but was
changed to non optional. I'm changing it back to optional.

Co-authored-by: Stefan Seemayer <stefan@seemayer.de>
  • Loading branch information
tadic-luka and sseemayer authored Sep 9, 2024
1 parent 5c3390f commit fca1f20
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/db/otp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl std::str::FromStr for TOTPAlgorithm {
#[derive(Debug, PartialEq, Eq, Zeroize, ZeroizeOnDrop)]
pub struct TOTP {
pub label: String,
pub issuer: String,
pub issuer: Option<String>,
pub period: u64,
pub digits: u32,
pub algorithm: TOTPAlgorithm,
Expand Down Expand Up @@ -119,7 +119,6 @@ impl std::str::FromStr for TOTP {
}

let secret = secret.ok_or(TOTPError::MissingField("secret"))?;
let issuer = issuer.ok_or(TOTPError::MissingField("issuer"))?;

let secret =
base32::decode(base32::Alphabet::Rfc4648 { padding: true }, &secret).ok_or(TOTPError::Base32)?;
Expand Down Expand Up @@ -201,7 +200,7 @@ mod kdbx4_otp_tests {
let expected = TOTP {
label: "KeePassXC:none".to_string(),
secret: b"Hello!\xDE\xAD\xBE\xEF".to_vec(),
issuer: "KeePassXC".to_string(),
issuer: Some("KeePassXC".to_string()),
period: 30,
digits: 6,
algorithm: TOTPAlgorithm::Sha1,
Expand Down Expand Up @@ -231,7 +230,7 @@ mod kdbx4_otp_tests {
let expected = TOTP {
label: "sha512%20totp:none".to_string(),
secret: b"123456".to_vec(),
issuer: "sha512 totp".to_string(),
issuer: Some("sha512 totp".to_string()),
period: 30,
digits: 6,
algorithm: TOTPAlgorithm::Sha512,
Expand All @@ -247,7 +246,7 @@ mod kdbx4_otp_tests {
let totp = TOTP {
label: "KeePassXC:none".to_string(),
secret: b"Hello!\xDE\xAD\xBE\xEF".to_vec(),
issuer: "KeePassXC".to_string(),
issuer: Some("KeePassXC".to_string()),
period: 30,
digits: 6,
algorithm: TOTPAlgorithm::Sha1,
Expand Down Expand Up @@ -278,4 +277,22 @@ mod kdbx4_otp_tests {
Err(TOTPError::MissingField("secret"))
));
}

#[test]
fn totp_minimal() -> Result<(), TOTPError> {
let otp_str = "otpauth://totp/KeePassXC:none?secret=JBSWY3DPEHPK3PXP&period=30&digits=6";

let expected = TOTP {
label: "KeePassXC:none".to_string(),
secret: b"Hello!\xDE\xAD\xBE\xEF".to_vec(),
issuer: None,
period: 30,
digits: 6,
algorithm: TOTPAlgorithm::Sha1,
};

assert_eq!(otp_str.parse::<TOTP>()?, expected);

Ok(())
}
}

0 comments on commit fca1f20

Please sign in to comment.