Adding digital locker example (#117)
This commit is contained in:
+40
-38
@@ -142,34 +142,6 @@ fn account_login(
|
||||
client_login_finish_result.shared_secret == server_login_finish_result.shared_secret
|
||||
}
|
||||
|
||||
// A function run on the client which extracts a username and password from the CLI
|
||||
fn get_username_and_password(rl: &mut Editor<()>, username: Option<String>) -> (String, String) {
|
||||
let query = if username.is_none() {
|
||||
"Username: "
|
||||
} else {
|
||||
"Password: "
|
||||
};
|
||||
let readline = rl.readline(query);
|
||||
match readline {
|
||||
Ok(line) => match username {
|
||||
Some(x) => (x, line),
|
||||
None => get_username_and_password(rl, Some(line)),
|
||||
},
|
||||
Err(ReadlineError::Interrupted) => {
|
||||
println!("CTRL-C");
|
||||
exit(0)
|
||||
}
|
||||
Err(ReadlineError::Eof) => {
|
||||
println!("CTRL-D");
|
||||
exit(0)
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut rng = OsRng;
|
||||
let server_kp = Default::generate_random_keypair(&mut rng).unwrap();
|
||||
@@ -192,7 +164,7 @@ fn main() {
|
||||
println!("Error: Invalid option (either specify 1 or 2)");
|
||||
continue;
|
||||
}
|
||||
let (username, password) = get_username_and_password(&mut rl, None);
|
||||
let (username, password) = get_two_strings("Username", "Password", &mut rl, None);
|
||||
match line.as_ref() {
|
||||
"1" => {
|
||||
registered_users
|
||||
@@ -217,18 +189,48 @@ fn main() {
|
||||
_ => exit(0),
|
||||
}
|
||||
}
|
||||
Err(ReadlineError::Interrupted) => {
|
||||
println!("CTRL-C");
|
||||
exit(0)
|
||||
}
|
||||
Err(ReadlineError::Eof) => {
|
||||
println!("CTRL-D");
|
||||
exit(0)
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error: {:?}", err);
|
||||
handle_error(err);
|
||||
exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
|
||||
// Handle readline errors
|
||||
fn handle_error(err: ReadlineError) {
|
||||
match err {
|
||||
ReadlineError::Interrupted => {
|
||||
println!("CTRL-C");
|
||||
}
|
||||
ReadlineError::Eof => {
|
||||
println!("CTRL-D");
|
||||
}
|
||||
err => {
|
||||
println!("Error: {:?}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A function run on the client which extracts two strings from the CLI
|
||||
fn get_two_strings(
|
||||
s1: &str,
|
||||
s2: &str,
|
||||
rl: &mut Editor<()>,
|
||||
string1: Option<String>,
|
||||
) -> (String, String) {
|
||||
let query = if string1.is_none() { s1 } else { s2 };
|
||||
let readline = rl.readline(&format!("{}: ", query));
|
||||
match readline {
|
||||
Ok(line) => match string1 {
|
||||
Some(x) => (x, line),
|
||||
None => get_two_strings(s1, s2, rl, Some(line)),
|
||||
},
|
||||
Err(err) => {
|
||||
handle_error(err);
|
||||
exit(0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user