Skip to content

Commit 1bcfb17

Browse files
committed
fix: manipulate secret case by otp_type
1 parent c578e06 commit 1bcfb17

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/otp/otp_element.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ pub struct OTPElement {
142142
#[builder(default = "6")]
143143
pub digits: u64,
144144
#[serde(rename = "type")]
145-
#[builder(default)]
145+
#[builder(setter(custom), default)]
146146
pub type_: OTPType,
147147
#[builder(default)]
148148
pub algorithm: OTPAlgorithm,
@@ -235,6 +235,22 @@ impl OTPElementBuilder {
235235
self
236236
}
237237

238+
/// Makes the secret insertion case insensitive
239+
pub fn type_<VALUE: Into<OTPType>>(&mut self, value: VALUE) -> &mut Self {
240+
let otp_type: OTPType = value.into();
241+
242+
if otp_type == OTPType::Motp {
243+
// Motp codes must be lowercase
244+
self.secret = self.secret.as_ref().map(|s| s.to_lowercase())
245+
} else {
246+
// Base32 codes must be uppercase
247+
self.secret = self.secret.as_ref().map(|s| s.to_uppercase())
248+
}
249+
250+
self.type_ = Some(otp_type);
251+
self
252+
}
253+
238254
/// Check if the OTPElement is valid
239255
fn validate(&self) -> Result<(), ErrReport> {
240256
if self.secret.is_none() {
@@ -384,7 +400,7 @@ mod test {
384400
#[test]
385401
fn valid_hex_secret() {
386402
let result = OTPElementBuilder::default()
387-
.secret("aaaf")
403+
.secret("aAAf")
388404
.label("label")
389405
.issuer("")
390406
.type_(OTPType::Motp)

0 commit comments

Comments
 (0)