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-18 17:16:00 +00:00
|
|
|
use rocket::http::{Cookie, Cookies};
|
2021-07-17 18:45:52 +00:00
|
|
|
use crate::user::User;
|
2021-07-18 18:11:54 +00:00
|
|
|
use rocket_contrib::json::JsonValue;
|
|
|
|
use random_string::generate;
|
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-18 17:16:00 +00:00
|
|
|
#[post("/register/<name>/<pin>/<pronouns>")]
|
2021-07-18 16:06:05 +00:00
|
|
|
pub fn register_user(name: String, pin: i32, pronouns: String) -> JsonValue {
|
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);
|
2021-07-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": "user already exists",
|
|
|
|
});
|
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 18:11:54 +00:00
|
|
|
let new_user: User = 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-18 18:11:54 +00:00
|
|
|
}; // append the user to the vec
|
2021-07-17 19:53:10 +00:00
|
|
|
|
2021-07-17 18:45:52 +00:00
|
|
|
// append to the json file
|
2021-07-18 18:11:54 +00:00
|
|
|
match append_json(&new_user) {
|
2021-07-17 18:45:52 +00:00
|
|
|
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
|
|
|
|
);
|
2021-07-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "ok",
|
|
|
|
"reason": format!("user {} registered", users[users.len()-1].name.to_string().to_lowercase()),
|
|
|
|
});
|
2021-07-17 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 17:16:00 +00:00
|
|
|
fn create_token(name: String, mut users: Vec<User>) -> String {
|
2021-07-18 18:11:54 +00:00
|
|
|
let charset = "1234567890abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
2021-07-18 17:16:00 +00:00
|
|
|
for i in 0..users.len() {
|
|
|
|
if users[i].name == name {
|
2021-07-18 18:11:54 +00:00
|
|
|
users[i].session_token = generate(12, charset);
|
|
|
|
match write_json(&users) {
|
|
|
|
Err(why) => panic!("coudln't write to file: {}", why),
|
|
|
|
Ok(()) => info!("succesfully wrote to file"),
|
|
|
|
};
|
2021-07-18 17:16:00 +00:00
|
|
|
info!("succesfully created token for user {}", name);
|
|
|
|
let token = users[i].session_token.clone();
|
|
|
|
return token;
|
|
|
|
};
|
|
|
|
};
|
2021-07-18 17:26:26 +00:00
|
|
|
warn!("something bad happened while creating a token and idk what");
|
2021-07-18 17:16:00 +00:00
|
|
|
return "NULL".to_string();
|
|
|
|
}
|
|
|
|
|
2021-07-17 15:40:05 +00:00
|
|
|
// Check if pin matches user
|
2021-07-18 17:16:00 +00:00
|
|
|
#[get("/users/<name>/<pin>")]
|
2021-07-18 17:26:26 +00:00
|
|
|
pub fn check_pin(mut cookies: Cookies, name: String, pin: i32) -> JsonValue {
|
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);
|
2021-07-18 17:16:00 +00:00
|
|
|
// Create token for user & set a cookie
|
2021-07-18 17:26:26 +00:00
|
|
|
let token = create_token(i.name.clone(), users);
|
|
|
|
cookies.add(Cookie::new("token", token));
|
|
|
|
info!("set the token cookie");
|
|
|
|
|
2021-07-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "ok",
|
|
|
|
"reason": "pin matches",
|
|
|
|
});
|
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-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": "incorrect pin",
|
|
|
|
});
|
2021-07-17 15:40:05 +00:00
|
|
|
};
|
|
|
|
};
|
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-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": format!("user {} doesn't exist", name.to_string().to_lowercase()),
|
|
|
|
});
|
2021-07-17 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Change a users pin/name
|
2021-07-18 17:16:00 +00:00
|
|
|
#[post("/users/change/<name>/<pin>/<new_name>/<new_pin>")]
|
2021-07-18 16:06:05 +00:00
|
|
|
pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> JsonValue {
|
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 16:06:05 +00:00
|
|
|
users[i].pin_hashed = sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
|
2021-07-18 00:33:22 +00:00
|
|
|
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());
|
2021-07-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "ok",
|
|
|
|
"reason": format!("changed {}'s pin", name.to_string().to_lowercase()),
|
|
|
|
});
|
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()
|
|
|
|
);
|
2021-07-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": 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()
|
|
|
|
);
|
2021-07-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "ok",
|
|
|
|
"reason": "successfully changed name and/or pin",
|
|
|
|
});
|
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-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": "incorrect pin for user",
|
|
|
|
});
|
2021-07-17 15:40:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-18 00:33:22 +00:00
|
|
|
warn!(
|
|
|
|
"User {} not found, could not change pin and/or name.",
|
|
|
|
name.to_string()
|
|
|
|
);
|
2021-07-18 16:06:05 +00:00
|
|
|
return json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": format!("user {} not found", name.to_string().to_lowercase()),
|
|
|
|
});
|
2021-07-17 15:40:05 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 17:16:00 +00:00
|
|
|
#[get("/users/<name>")]
|
2021-07-18 16:06:05 +00:00
|
|
|
pub fn get_user(name: String) -> JsonValue {
|
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 {
|
2021-07-18 16:06:05 +00:00
|
|
|
Some(user) => json!({
|
|
|
|
"status":"ok",
|
|
|
|
"user": {
|
|
|
|
"name": user.name,
|
|
|
|
"pronouns": user.pronouns,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
None => json!({
|
|
|
|
"status": "fail",
|
|
|
|
"reason": format!("user {} not found", name),
|
|
|
|
}),
|
2021-07-17 19:53:10 +00:00
|
|
|
}
|
|
|
|
}
|