-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.rs
92 lines (79 loc) · 2.3 KB
/
auth.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::io::{self, BufRead, Write};
use clap::{Parser, Subcommand};
use django_auth::*;
#[derive(Parser)]
#[command(author, version, about, long_about = None)]
#[command(propagate_version = true)]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Encode a password in Django-style
Encode,
/// Verify a Django stored hashed password
Verify,
}
fn main() {
let cli = Cli::parse();
match &cli.command {
Commands::Encode => {
let (password, salt, iterations) = {
(
get_user_input("Input password: "),
get_user_input("Input salt: "),
get_user_input_number("Input number of iterations: "),
)
};
println!(
"✅ Encoded password: {}",
django_encode_password(&password, &salt, iterations).unwrap()
);
}
Commands::Verify => {
let (password, hashed_password) = {
(
get_user_input("Input password: "),
get_user_input("Input Django stored password: "),
)
};
let res = django_auth(&password, &hashed_password);
match res {
Ok(ok) => {
if ok {
println!("✅ Password verified!")
} else {
println!("❌ Password verification failed!")
}
}
Err(err) => println!("💔 Verification error: {:?}", err),
}
}
}
}
fn get_user_input(prompt: &str) -> String {
print!("{prompt}");
io::stdout().flush().expect("failed to write to stdout");
let stdin = io::stdin();
let line = stdin
.lock()
.lines()
.next()
.expect("failed to read password")
.expect("failed to read from stdin");
line
}
fn get_user_input_number(prompt: &str) -> u32 {
let res = get_user_input(prompt).parse::<u32>();
if let Ok(n) = res {
return n;
}
loop {
println!("Please input a number, try again!");
let res = get_user_input(prompt).parse::<u32>();
if let Ok(n) = res {
return n;
}
}
}