Skip to content

Commit 5160224

Browse files
committed
Fix cotp database import
1 parent 73f443c commit 5160224

File tree

4 files changed

+38
-19
lines changed

4 files changed

+38
-19
lines changed

src/argument_functions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use zeroize::Zeroize;
1010
pub fn import(matches: &ArgMatches, database: &mut OTPDatabase) -> Result<String, String> {
1111
let path = matches.get_one::<String>("path").unwrap();
1212

13-
let result = if matches.get_flag("cotp") || matches.get_flag("andotp") {
13+
let result = if matches.get_flag("cotp") {
14+
importers::cotp::import(path)
15+
} else if matches.get_flag("andotp") {
1416
importers::and_otp::import(path)
1517
} else if matches.get_flag("aegis") {
1618
importers::aegis::import(path)

src/importers/and_otp.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use std::fs::read_to_string;
2-
3-
use crate::otp::otp_element::OTPElement;
4-
5-
//no need to declare andOTP json struct cause it's the same as OTP element
6-
7-
pub fn import(filepath: &str) -> Result<Vec<OTPElement>, String> {
8-
let file_to_import_contents = match read_to_string(filepath) {
9-
Ok(result) => result,
10-
Err(e) => return Err(format!("Error during file reading: {:?}", e)),
11-
};
12-
let result: Result<Vec<OTPElement>, serde_json::Error> =
13-
serde_json::from_str(&file_to_import_contents);
14-
match result {
15-
Ok(element) => Ok(element),
16-
Err(e) => Err(format!("Failed to serialize file: {}", e)),
17-
}
18-
}
1+
use std::fs::read_to_string;
2+
3+
use crate::otp::otp_element::OTPElement;
4+
5+
//no need to declare andOTP json struct cause it's the same as OTP element
6+
pub fn import(filepath: &str) -> Result<Vec<OTPElement>, String> {
7+
let file_to_import_contents = match read_to_string(filepath) {
8+
Ok(result) => result,
9+
Err(e) => return Err(format!("Error during file reading: {:?}", e)),
10+
};
11+
12+
match serde_json::from_str::<Vec<OTPElement>>(&file_to_import_contents) {
13+
Ok(elements) => Ok(elements),
14+
Err(e) => Err(format!("Failed to serialize file: {}", e)),
15+
}
16+
}

src/importers/cotp.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
use std::fs::read_to_string;
2+
3+
use crate::otp::otp_element::{OTPDatabase, OTPElement};
4+
5+
pub fn import(filepath: &str) -> Result<Vec<OTPElement>, String> {
6+
let file_to_import_contents = match read_to_string(filepath) {
7+
Ok(result) => result,
8+
Err(e) => return Err(format!("Error during file reading: {:?}", e)),
9+
};
10+
11+
match serde_json::from_str::<OTPDatabase>(&file_to_import_contents) {
12+
Ok(database) => Ok(database.elements),
13+
Err(_e) => match serde_json::from_str::<Vec<OTPElement>>(&file_to_import_contents) {
14+
Ok(elements) => Ok(elements),
15+
Err(e) => Err(format!("Failed to serialize file: {}", e)),
16+
},
17+
}
18+
}

src/importers/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ pub mod aegis_encrypted;
33
pub mod and_otp;
44
pub mod authy_remote_debug;
55
pub mod converted;
6+
pub mod cotp;
67
pub mod freeotp_plus;

0 commit comments

Comments
 (0)