Files
advanced_db/backend/src/api/user.rs

176 lines
5.0 KiB
Rust
Raw Normal View History

2023-04-27 23:59:14 -04:00
use sha2::{Sha256, Digest};
use rand::{prelude::Rng, distributions::Alphanumeric };
use oracle::{Connection, Error};
use log::{info, warn, error};
use actix_identity::Identity;
2023-04-29 22:10:54 -04:00
use actix_web::{web, Responder, HttpRequest, HttpMessage, HttpResponse, cookie};
2023-04-27 23:59:14 -04:00
use serde::{Deserialize, Serialize};
static SQL_USERNAME: &str = "group09_user";
static SQL_PASSWORD: &str = "group09_user";
static SALT_LEN: usize = 16;
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct Entry {
net_id: String,
password: String
}
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct User {
id: String,
first_name: String,
last_name: String
}
2023-04-28 00:46:47 -04:00
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct NewUser {
2023-04-29 22:10:54 -04:00
net_id: String,
2023-04-28 00:46:47 -04:00
password: String,
first_name: String,
last_name: String
}
2023-04-27 23:59:14 -04:00
pub async fn login(request: HttpRequest, body: web::Json<Entry>) -> impl Responder {
//TODO: finish login, for now will simply login for everyone
let net_id: &str = body.net_id.as_str();
let password: &str = body.password.as_str();
//Identity::login(&request.extensions(), "User1".into());
println!("{:?}", request);
println!("{:?}", body);
match authenticate(net_id, password) {
Some(user) => {
2023-04-29 22:10:54 -04:00
let id = Identity::login(&request.extensions(), net_id.into()).unwrap();
2023-04-28 00:19:45 -04:00
web::Json(user);
HttpResponse::Ok()
2023-04-27 23:59:14 -04:00
},
2023-04-28 00:19:45 -04:00
None => {
web::Json(User::default());
HttpResponse::Unauthorized()
}
2023-04-27 23:59:14 -04:00
}
}
pub async fn logout(user: Identity) -> impl Responder {
user.logout();
HttpResponse::Ok()
}
2023-04-28 00:46:47 -04:00
pub async fn signup(request: HttpRequest, body: web::Json<NewUser>) -> impl Responder {
let body = body.into_inner();
2023-04-29 22:10:54 -04:00
match create_user(body.net_id.as_str(), body.password.as_str(), body.first_name.as_str(), body.last_name.as_str()) {
2023-04-28 00:46:47 -04:00
Ok(_) => {
2023-04-29 22:10:54 -04:00
Identity::login(&request.extensions(), body.net_id.clone()).unwrap();
web::Json(User { id: body.net_id, first_name: body.first_name, last_name: body.last_name});
2023-04-28 00:46:47 -04:00
HttpResponse::Ok()
}
Err(e) => {
error!("Error creating new user: {}", e);
HttpResponse::InternalServerError()
}
}
}
2023-04-27 23:59:14 -04:00
fn authenticate(username: &str, password: &str) -> Option<User> {
info!("Authenticating user: {}", username);
let conn = match Connection::connect(SQL_USERNAME,SQL_PASSWORD, ""){
Ok(c) => c,
Err(e) => {
error!("unable to open connection to server: {}", e);
return None;
}
};
let mut stmt = match conn.statement("select * from student where net_id = :1").build(){
Ok(s) => s,
Err(e) => {
error!("unable to build statement: {}", e);
return None;
}
};
let row = stmt.query_row_as::<(String, String, String, String, String)>(&[&username]).unwrap_or_default();
let true_pword = row.3;
let salt = row.4;
let mut hasher = Sha256::new();
hasher.update(password);
hasher.update(salt);
let hash = hasher.finalize();
let mut tmp: String = String::new();
for value in hash{
tmp += &format!("{:x}", value);
}
conn.close().unwrap_or_default();
if true_pword.eq(&tmp) {
info!("User {} successfully authenticated", username);
Some( User { id: row.0, first_name: row.1,last_name: row.2} )
}
else{
warn!("User {} failed authentication", username);
None
}
}
fn create_user(username: &str, password: &str, first_name: &str, last_name: &str) -> Result<(), Error> {
info!("Creating user: {}", username);
let conn = Connection::connect(SQL_USERNAME, SQL_PASSWORD, "")?;
2023-04-29 22:10:54 -04:00
let mut stmt = conn.statement("insert into student values(:net_id, :first_name, :last_name, :pswd, :salt)").build()?;
2023-04-27 23:59:14 -04:00
let salt: String = rand::thread_rng().sample_iter(&Alphanumeric).take(SALT_LEN).map(char::from).collect();
let mut hasher = Sha256::new();
hasher.update(&password);
hasher.update(&salt);
let hash = hasher.finalize();
let mut hash_string = String::new();
for value in hash{
hash_string += &format!("{:x}", value);
}
2023-04-29 22:10:54 -04:00
match stmt.execute_named(&[("net_id", &username), ("first_name", &first_name), ("last_name", &last_name), ("pswd", &hash_string), ("salt", &salt)]) {
2023-04-27 23:59:14 -04:00
Ok(_) => {
info!("User {} successfully created", username);
conn.commit()?;
},
Err(_) => {
warn!("Failed to create user {}", username);
conn.rollback()?;
}
};
2023-04-29 22:10:54 -04:00
2023-04-30 16:01:13 -04:00
let mut new_table = conn.statement("create table :net_id ( item_id number(5), foreign key (item_id) references menu_item (id))").build()?;
2023-04-29 22:10:54 -04:00
match new_table.execute_named(&[("net_id", &username)]) {
Ok(_) => {
info!("User {} week table created", username);
conn.commit()?;
},
Err(_) => {
warn!("Failed to create week table for {}", username);
conn.rollback()?;
}
};
2023-04-27 23:59:14 -04:00
conn.close()?;
Ok(())
}