2021-07-17 18:45:52 +00:00
|
|
|
extern crate log;
|
2021-07-18 00:33:22 +00:00
|
|
|
use crate::file_io::{append_json, read_json, write_json};
|
2021-07-17 18:45:52 +00:00
|
|
|
use crate::user::User;
|
2021-07-17 15:40:05 +00:00
|
|
|
extern crate sha1;
|
2021-07-18 00:32:57 +00:00
|
|
|
|
2021-07-17 15:40:05 +00:00
|
|
|
#[get("/")]
|
|
|
|
pub fn index() -> &'static str {
|
|
|
|
"API Info:
|
|
|
|
|
2021-07-17 19:59:58 +00:00
|
|
|
`POST /api/register/<name>/<pin>/<pronouns>` Register the username with the pin provided if it doesn't already exist
|
2021-07-17 15:40:05 +00:00
|
|
|
|
|
|
|
`GET /api/users/<name>` Check if the user exists
|
|
|
|
|
|
|
|
`GET /api/users/<name>/<pin>` Check if the user exists, and if the pin provided matches
|
|
|
|
|
2021-07-17 19:59:58 +00:00
|
|
|
`POST /api/users/change/<name>/<pin>/<new-name>/<new-pin>` Change a users name and/or pin
|
|
|
|
|
|
|
|
`GET /api/about/name/<name>` Get the name of a user
|
|
|
|
|
|
|
|
`GET /api/about/pronouns/<name>` Get the pronouns of a user"
|
2021-07-17 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post request to register a user and pin
|
2021-07-17 19:53:10 +00:00
|
|
|
#[post("/api/register/<name>/<pin>/<pronouns>")]
|
2021-07-18 00:32:57 +00:00
|
|
|
pub fn register_user(name: String, pin: i32, pronouns: String) -> String {
|
2021-07-17 15:40:05 +00:00
|
|
|
let mut users: Vec<User> = read_json(); // Create an array of users out of parsed json
|
2021-07-18 00:33:22 +00:00
|
|
|
for i in &users {
|
|
|
|
// loop through elements of the vector
|
2021-07-17 15:40:05 +00:00
|
|
|
if i.name == name.to_lowercase() {
|
2021-07-17 18:45:52 +00:00
|
|
|
warn!("Cannot create user {}! User is already in system.", i.name);
|
|
|
|
return "User already exists!".to_string();
|
2021-07-17 15:40:05 +00:00
|
|
|
};
|
2021-07-18 00:33:22 +00:00
|
|
|
}
|
2021-07-17 18:45:52 +00:00
|
|
|
|
|
|
|
let pin_hashed = sha1::Sha1::from(&pin.to_string()).digest().to_string(); // hash the pin
|
2021-07-17 19:53:10 +00:00
|
|
|
|
2021-07-18 00:33:22 +00:00
|
|
|
users.push(User {
|
2021-07-17 19:53:10 +00:00
|
|
|
name: name.to_string().to_lowercase(),
|
|
|
|
pin_hashed: pin_hashed,
|
|
|
|
pronouns: pronouns.to_string().to_lowercase(),
|
2021-07-18 00:33:22 +00:00
|
|
|
session_token: "NULL".to_string(),
|
2021-07-17 19:53:10 +00:00
|
|
|
}); // append the user to the vec
|
|
|
|
|
2021-07-17 18:45:52 +00:00
|
|
|
// append to the json file
|
|
|
|
match append_json(&users) {
|
|
|
|
Err(why) => panic!("couldn't append json: {}", why),
|
|
|
|
Ok(()) => info!("Succesfully appended to json"),
|
|
|
|
};
|
|
|
|
|
2021-07-18 00:33:22 +00:00
|
|
|
info!(
|
|
|
|
"succesfully created user {} with pin hash {}",
|
|
|
|
users[users.len() - 1].name.to_string(),
|
|
|
|
users[users.len() - 1].pin_hashed
|
|
|
|
);
|
|
|
|
return format!(
|
|
|
|
"User {} registered with pin hash: {}",
|
|
|
|
users[users.len() - 1].name.to_string().to_lowercase(),
|
|
|
|
users[users.len() - 1].pin_hashed
|
|
|
|
);
|
2021-07-17 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if pin matches user
|
|
|
|
#[get("/api/users/<name>/<pin>")]
|
2021-07-18 00:32:57 +00:00
|
|
|
pub fn check_pin(name: String, pin: i32) -> String {
|
2021-07-17 15:40:05 +00:00
|
|
|
let users: Vec<User> = read_json();
|
|
|
|
let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string();
|
2021-07-18 00:33:22 +00:00
|
|
|
for i in &users {
|
|
|
|
// loop through the vector
|
2021-07-17 15:40:05 +00:00
|
|
|
if i.name == name.to_lowercase() {
|
|
|
|
if i.pin_hashed == hashed_pin_input {
|
2021-07-17 18:45:52 +00:00
|
|
|
info!("pin correct for user {}", i.name);
|
|
|
|
return "pin matches".to_string();
|
2021-07-17 15:40:05 +00:00
|
|
|
} else {
|
2021-07-17 18:45:52 +00:00
|
|
|
warn!("pin incorrect for user {}", i.name);
|
2021-07-17 15:40:05 +00:00
|
|
|
return "Incorrect pin".to_string();
|
|
|
|
};
|
|
|
|
};
|
2021-07-18 00:33:22 +00:00
|
|
|
}
|
|
|
|
warn!(
|
|
|
|
"cannot check pin for user {} as they do not exist",
|
|
|
|
name.to_string().to_lowercase()
|
|
|
|
);
|
2021-07-17 15:40:05 +00:00
|
|
|
return format!("User {} does not exist.", name.to_string().to_lowercase());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change a users pin/name
|
|
|
|
#[post("/api/users/change/<name>/<pin>/<new_name>/<new_pin>")]
|
2021-07-18 00:32:57 +00:00
|
|
|
pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> String {
|
2021-07-17 15:40:05 +00:00
|
|
|
let mut users: Vec<User> = read_json();
|
|
|
|
|
|
|
|
let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string();
|
|
|
|
|
|
|
|
// Loop over elements in vector
|
|
|
|
for i in 0..users.len() {
|
2021-07-18 00:33:22 +00:00
|
|
|
if users[i].name == name.to_lowercase() {
|
|
|
|
// make sure name exists
|
|
|
|
if users[i].pin_hashed == hashed_pin_input {
|
|
|
|
// check if pin is correct
|
2021-07-17 15:40:05 +00:00
|
|
|
// Check wether to change name or name+pin
|
|
|
|
if users[i].name == new_name.to_lowercase() {
|
|
|
|
// check if new name already exists
|
2021-07-18 00:33:22 +00:00
|
|
|
users[i].pin_hashed =
|
|
|
|
sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
|
|
|
|
match write_json(&users) {
|
|
|
|
Err(why) => panic!("Cannot write to json! {}", why),
|
|
|
|
Ok(()) => info!("succesfully wrote to json file"),
|
|
|
|
}
|
|
|
|
info!("Changed pin of {}", name.to_string().to_lowercase());
|
|
|
|
return format!(
|
|
|
|
"User {}'s new pin hash is {}.",
|
|
|
|
name.to_string().to_lowercase(),
|
|
|
|
users[i].pin_hashed
|
|
|
|
);
|
2021-07-17 15:40:05 +00:00
|
|
|
} else {
|
|
|
|
// check if new name already exists
|
|
|
|
for n in &users {
|
|
|
|
if n.name == new_name.to_lowercase() {
|
2021-07-18 00:33:22 +00:00
|
|
|
warn!(
|
|
|
|
"Could not change name of {} to {}, as new name is already taken.",
|
|
|
|
name.to_lowercase(),
|
|
|
|
new_name.to_lowercase()
|
|
|
|
);
|
|
|
|
return format!(
|
|
|
|
"New name {} is already taken!",
|
|
|
|
new_name.to_lowercase()
|
|
|
|
);
|
2021-07-17 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-17 18:45:52 +00:00
|
|
|
users[i].name = new_name.to_string().to_lowercase();
|
2021-07-18 00:33:22 +00:00
|
|
|
users[i].pin_hashed =
|
|
|
|
sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
|
|
|
|
|
2021-07-17 18:45:52 +00:00
|
|
|
match write_json(&users) {
|
|
|
|
Err(why) => panic!("couldn't write to json file! {}", why),
|
|
|
|
Ok(()) => info!("succesfully wrote to json file"),
|
|
|
|
}
|
2021-07-18 00:33:22 +00:00
|
|
|
info!(
|
|
|
|
"Changed name of {} to {}. New pin hash is {}",
|
|
|
|
name.to_string(),
|
|
|
|
users[i].name.to_string(),
|
|
|
|
users[i].pin_hashed.to_string()
|
|
|
|
);
|
|
|
|
return format!(
|
|
|
|
"User previously known as {} is now {}. Pin hash, if different, is {}",
|
|
|
|
name.to_string(),
|
|
|
|
users[i].name.to_string(),
|
|
|
|
users[i].pin_hashed.to_string()
|
|
|
|
);
|
2021-07-17 15:40:05 +00:00
|
|
|
}
|
|
|
|
} else {
|
2021-07-17 18:45:52 +00:00
|
|
|
warn!("Incorrect pin given for user {}!", name.to_string());
|
2021-07-17 15:40:05 +00:00
|
|
|
return format!("Incorrect pin for user {}!", name.to_string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 00:33:22 +00:00
|
|
|
warn!(
|
|
|
|
"User {} not found, could not change pin and/or name.",
|
|
|
|
name.to_string()
|
|
|
|
);
|
2021-07-17 15:40:05 +00:00
|
|
|
return format!("User {} not found.", name.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/api/users/<name>")]
|
2021-07-18 00:32:57 +00:00
|
|
|
pub fn get_user(name: String) -> String {
|
2021-07-17 15:40:05 +00:00
|
|
|
let users: Vec<User> = read_json();
|
2021-07-18 00:33:22 +00:00
|
|
|
let found_user = users
|
|
|
|
.iter()
|
|
|
|
.filter(|u| u.name == name.to_lowercase())
|
|
|
|
.next();
|
2021-07-17 15:40:05 +00:00
|
|
|
|
|
|
|
match found_user {
|
|
|
|
Some(user) => format!("User {}", &user.name),
|
|
|
|
None => "User does not exist".to_string(),
|
|
|
|
}
|
|
|
|
}
|
2021-07-17 19:53:10 +00:00
|
|
|
|
|
|
|
/* Get data about a user */
|
|
|
|
#[get("/api/about/name/<name>")]
|
2021-07-18 00:32:57 +00:00
|
|
|
pub fn get_user_name(name: String) -> String {
|
2021-07-17 19:53:10 +00:00
|
|
|
let users: Vec<User> = read_json();
|
2021-07-18 00:33:22 +00:00
|
|
|
let found_user = users
|
|
|
|
.iter()
|
|
|
|
.filter(|u| u.name == name.to_lowercase())
|
|
|
|
.next();
|
2021-07-17 19:53:10 +00:00
|
|
|
|
|
|
|
match found_user {
|
|
|
|
Some(user) => user.name.to_string(),
|
|
|
|
None => "NULL".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/api/about/pronouns/<name>")]
|
2021-07-18 00:32:57 +00:00
|
|
|
pub fn get_user_pronouns(name: String) -> String {
|
2021-07-17 19:53:10 +00:00
|
|
|
let users: Vec<User> = read_json();
|
2021-07-18 00:33:22 +00:00
|
|
|
let found_user = users
|
|
|
|
.iter()
|
|
|
|
.filter(|u| u.name == name.to_lowercase())
|
|
|
|
.next();
|
2021-07-17 19:53:10 +00:00
|
|
|
|
|
|
|
match found_user {
|
|
|
|
Some(user) => user.pronouns.to_string(),
|
|
|
|
None => "NULL".to_string(),
|
|
|
|
}
|
|
|
|
}
|