Skip to content

Commit

Permalink
fix: manipulate secret case by otp_type
Browse files Browse the repository at this point in the history
  • Loading branch information
replydev committed Jul 8, 2024
1 parent c578e06 commit 1bcfb17
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/otp/otp_element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ pub struct OTPElement {
#[builder(default = "6")]
pub digits: u64,
#[serde(rename = "type")]
#[builder(default)]
#[builder(setter(custom), default)]
pub type_: OTPType,
#[builder(default)]
pub algorithm: OTPAlgorithm,
Expand Down Expand Up @@ -235,6 +235,22 @@ impl OTPElementBuilder {
self
}

/// Makes the secret insertion case insensitive
pub fn type_<VALUE: Into<OTPType>>(&mut self, value: VALUE) -> &mut Self {
let otp_type: OTPType = value.into();

if otp_type == OTPType::Motp {
// Motp codes must be lowercase
self.secret = self.secret.as_ref().map(|s| s.to_lowercase())
} else {
// Base32 codes must be uppercase
self.secret = self.secret.as_ref().map(|s| s.to_uppercase())
}

self.type_ = Some(otp_type);
self
}

/// Check if the OTPElement is valid
fn validate(&self) -> Result<(), ErrReport> {
if self.secret.is_none() {
Expand Down Expand Up @@ -384,7 +400,7 @@ mod test {
#[test]
fn valid_hex_secret() {
let result = OTPElementBuilder::default()
.secret("aaaf")
.secret("aAAf")
.label("label")
.issuer("")
.type_(OTPType::Motp)
Expand Down

0 comments on commit 1bcfb17

Please sign in to comment.