Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update users file #389

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/usr/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ use core::str;
use hmac::Hmac;
use sha2::Sha256;

const PASSWORDS: &str = "/ini/passwords.csv";
const USERS: &str = "/ini/users.csv";
const COMMANDS: [&str; 2] = ["create", "login"];
const DISABLE_EMPTY_PASSWORD: bool = false;

pub fn main(args: &[&str]) -> Result<(), ExitCode> {
if args.len() == 1 || !COMMANDS.contains(&args[1]) {
Expand Down Expand Up @@ -42,8 +43,8 @@ fn usage() -> Result<(), ExitCode> {

// TODO: Add max number of attempts
pub fn login(username: &str) -> Result<(), ExitCode> {
if !fs::exists(PASSWORDS) {
error!("Could not read '{}'", PASSWORDS);
if !fs::exists(USERS) {
error!("Could not read '{}'", USERS);
return Err(ExitCode::Failure);
}

Expand Down Expand Up @@ -98,7 +99,7 @@ pub fn create(username: &str) -> Result<(), ExitCode> {
print!("\x1b[12h"); // Enable echo
println!();

if password.is_empty() {
if password.is_empty() && DISABLE_EMPTY_PASSWORD {
return Err(ExitCode::Failure);
}

Expand Down Expand Up @@ -184,8 +185,8 @@ pub fn hash(password: &str) -> String {

fn read_hashed_passwords() -> BTreeMap<String, String> {
let mut hashed_passwords = BTreeMap::new();
if let Ok(contents) = api::fs::read_to_string(PASSWORDS) {
for line in contents.split('\n') {
if let Ok(csv) = api::fs::read_to_string(USERS) {
for line in csv.split('\n') {
let mut rows = line.split(',');
if let Some(username) = rows.next() {
if let Some(hash) = rows.next() {
Expand All @@ -211,5 +212,5 @@ fn save_hashed_password(username: &str, hash: &str) -> Result<usize, ()> {
csv.push_str(&format!("{},{}\n", u, h));
}

fs::write(PASSWORDS, csv.as_bytes())
fs::write(USERS, csv.as_bytes())
}