为什么我使用了正确的用户名和密码,但总是出现“访问被拒绝”的情况?

问题描述 投票:0回答:1

在这里,我正在尝试建立一个简单的身份验证(用户名,密码程序)。程序运行文件。但是当我插入正确的用户名和密码(alreeasy 记录在 db.csv)文件中时,它仍然告诉我“错误!访问被拒绝!”。 Wgen 涉及其他类型或错误,例如错误的用户名(完美运行)、错误的密码(我想相信它运行良好)和错误的文件(也完美运行)。这是我现在遇到的主要问题。

use std::env;
use std::fs::File;
use std::path::Path;
use std::error::Error;
use csv::StringRecord;
use csv::ReaderBuilder;
use std::io::{self, Write};
use argon2::{self, Argon2, PasswordHash, PasswordHasher, PasswordVerifier};



fn main() -> Result<(), Box<dyn Error>> {

    let args : Vec<String> = env::args().collect();

    if args.len() < 2 {
        eprintln!("Error! password database  not found!");
        return Err("No database file provided".into());

    }
    let filename = &args[1];

    //Here I will check if teh database file exists
    if !Path::new(filename).exists(){
        eprintln!("Error! Password database not found!");
        return Err("Database file does not exist".into());
    }

    let (username, password) = get_user_input();
    match search_username_in_csv(filename, &username)?{
        Some(stored_password_hash)=>{
        // Here, we are verifying th epassword enterred by the user and the password that is already stored in our db.csv
       if verify_password(&password, &stored_password_hash){
        println!("Access granted!");
       } else{
        eprintln!("Error! Access denied!");
       }
    },
    None => {
        //Here we have the case where the username was not found
        println!("Error! Access denied!");
        }
    }
    Ok(())

}

fn search_username_in_csv(filename: &str, username: &str) -> Result<Option<String>, Box<dyn Error>> {
    let file = File::open(filename)?;
    let mut rdr = ReaderBuilder::new().has_headers(false).from_reader(file);

    for result in rdr.records(){
        let record = result?;

        if let Some (stored_username) = record.get(0){
            let stored_username = stored_username.trim_matches('"').trim();
            if stored_username == stored_username{
                if let Some(password_hash) = record.get(1){
                    return Ok(Some(password_hash.trim_matches('"').to_string()));
                }
            }

        }
    }
    Ok(None)
}


fn get_user_input() -> (String, String){
    let mut username = String::new();
    let mut password = String::new();

    print!("Enter username: ");
    io::stdout().flush().unwrap();
    io::stdin().read_line(&mut username).unwrap();
    let username = username.trim().to_string();

    print!("Enter password: ");
    io::stdout().flush().unwrap();
    io::stdin().read_line(&mut password).unwrap();
    let password = password.trim().to_string();

    (username, password)
}

fn verify_password(password: &str, stored_hash: &str) -> bool{
    let argon2 = Argon2::default();
    let parsed_hash = PasswordHash::new(stored_hash).unwrap(); 

    argon2
        .verify_password(password.as_bytes(), &parsed_hash)
        .is_ok() 
}
rust rust-cargo argon2-ffi
1个回答
0
投票

if stored_username == stored_username{
永远都是真的。

我想你的意思是

if stored_username == username{


您应该收到有关未使用的

username
参数的警告。

warning: unused variable: `username`
  --> src/main.rs:45:5
   |
45 |     username: &str,
   |     ^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_username`
   |
   = note: `#[warn(unused_variables)]` on by default
© www.soinside.com 2019 - 2024. All rights reserved.