/register & /login now use json post requests, change event uses enum
parent
a01cb8b489
commit
7319352950
90
src/auth.rs
90
src/auth.rs
|
@ -8,22 +8,22 @@ extern crate sha1;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
|
||||||
// Post request to register a user and pin
|
// Post request to register a user and pin
|
||||||
#[post("/register/<name>/<pin>/<pronouns>")]
|
#[post("/register", format = "json", data = "<data>")]
|
||||||
pub fn register_user(name: String, pin: i32, pronouns: String) -> JsonValue {
|
pub fn register(data: Json<RegisterEvent>) -> JsonValue {
|
||||||
// check if the user exists
|
// check if the user exists
|
||||||
if let Some(user) = db_read_user(&name).ok().flatten() {
|
if let Some(user) = db_read_user(&data.name).ok().flatten() {
|
||||||
warn!("Cannot create user {}! User is already in system.", name);
|
warn!("Cannot create user {}! User is already in system.", data.name);
|
||||||
return json!({
|
return json!({
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"reason": "user already exists",
|
"reason": "user already exists",
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
let pin_hashed = sha1::Sha1::from(&pin.to_string()).digest().to_string(); // hash the pin
|
let pin_hashed = sha1::Sha1::from(&data.pin).digest().to_string(); // hash the pin
|
||||||
|
|
||||||
let new_user: User = User {
|
let new_user: User = User {
|
||||||
name: name.to_string().to_lowercase(),
|
name: data.name.to_string().to_lowercase(),
|
||||||
pin_hashed,
|
pin_hashed,
|
||||||
pronouns: pronouns.to_string().to_lowercase(),
|
pronouns: data.pronouns.to_string().to_lowercase(),
|
||||||
session_token: "NULL".to_string(),
|
session_token: "NULL".to_string(),
|
||||||
role: UserType::Normal,
|
role: UserType::Normal,
|
||||||
};
|
};
|
||||||
|
@ -149,10 +149,10 @@ pub fn logout(info: Json<LogoutEvent>, mut cookies: Cookies) -> JsonValue {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if pin matches user
|
// Check if pin matches user
|
||||||
#[get("/users/<name>/<pin>")]
|
#[post("/login", format = "json", data = "<data>")]
|
||||||
pub fn login(mut cookies: Cookies, name: String, pin: i32) -> JsonValue {
|
pub fn login(data: Json<LoginEvent>, mut cookies: Cookies) -> JsonValue {
|
||||||
if let Some(user) = db_read_user(&name.to_lowercase()).ok().flatten() {
|
if let Some(user) = db_read_user(&data.name.to_lowercase()).ok().flatten() {
|
||||||
let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string();
|
let hashed_pin_input = sha1::Sha1::from(&data.pin.to_string()).digest().to_string();
|
||||||
|
|
||||||
if user.pin_hashed == hashed_pin_input { // check if pin hash matches
|
if user.pin_hashed == hashed_pin_input { // check if pin hash matches
|
||||||
info!("pin correct for user {}", &user.name);
|
info!("pin correct for user {}", &user.name);
|
||||||
|
@ -184,11 +184,11 @@ pub fn login(mut cookies: Cookies, name: String, pin: i32) -> JsonValue {
|
||||||
info!("removed private cookie");
|
info!("removed private cookie");
|
||||||
warn!(
|
warn!(
|
||||||
"cannot check pin for user {} as they do not exist",
|
"cannot check pin for user {} as they do not exist",
|
||||||
name.to_string().to_lowercase()
|
data.name.to_string().to_lowercase()
|
||||||
);
|
);
|
||||||
return json!({
|
return json!({
|
||||||
"status": "fail",
|
"status": "fail",
|
||||||
"reason": format!("user {} doesn't exist", name.to_string().to_lowercase()),
|
"reason": format!("user {} doesn't exist", data.name.to_string().to_lowercase()),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -221,36 +221,40 @@ pub fn change_info(input: Json<ChangeEvent>, mut cookies: Cookies) -> JsonValue
|
||||||
// find the user
|
// find the user
|
||||||
if let Some(mut user) = db_read_user(&input.name).ok().flatten() {
|
if let Some(mut user) = db_read_user(&input.name).ok().flatten() {
|
||||||
if token.value() == user.session_token { // & if token matches:
|
if token.value() == user.session_token { // & if token matches:
|
||||||
if input.changed_event == "name" {
|
match input.changed_event {
|
||||||
// remove the user first
|
ChangeEventType::Name => {
|
||||||
db_remove(&user);
|
// remove the user first
|
||||||
// change the name
|
db_remove(&user);
|
||||||
user.name = input.new_event.clone();
|
// change the name
|
||||||
info!("changed name of {} to {}", input.name, input.new_event);
|
user.name = input.new_event.clone();
|
||||||
db_add(&user);
|
info!("changed name of {} to {}", input.name, input.new_event);
|
||||||
return json!({
|
db_add(&user);
|
||||||
"status": "ok",
|
return json!({
|
||||||
"reason": format!("changed name of {} to {}", input.name, input.new_event),
|
"status": "ok",
|
||||||
});
|
"reason": format!("changed name of {} to {}", input.name, input.new_event),
|
||||||
} else if input.changed_event == "pin" {
|
});
|
||||||
// change the pin
|
},
|
||||||
let new_hashed_pin = sha1::Sha1::from(&input.new_event).digest().to_string();
|
ChangeEventType::Pin => {
|
||||||
user.pin_hashed = new_hashed_pin.clone();
|
// change the pin
|
||||||
db_add(&user);
|
let new_hashed_pin = sha1::Sha1::from(&input.new_event).digest().to_string();
|
||||||
info!("changed pin of {}", input.name);
|
user.pin_hashed = new_hashed_pin.clone();
|
||||||
return json!({
|
db_add(&user);
|
||||||
"status": "ok",
|
info!("changed pin of {}", input.name);
|
||||||
"reason": "changed pin",
|
return json!({
|
||||||
});
|
"status": "ok",
|
||||||
} else if input.changed_event == "pronouns" {
|
"reason": "changed pin",
|
||||||
// change the pronouns
|
});
|
||||||
user.pronouns = input.new_event.clone();
|
},
|
||||||
info!("changed pronouns of {} to {}", input.name, input.new_event);
|
ChangeEventType::Pronouns => {
|
||||||
db_add(&user);
|
// change the pronouns
|
||||||
return json!({
|
user.pronouns = input.new_event.clone();
|
||||||
"status": "ok",
|
info!("changed pronouns of {} to {}", input.name, input.new_event);
|
||||||
"reason": "successfully changed pronouns",
|
db_add(&user);
|
||||||
});
|
return json!({
|
||||||
|
"status": "ok",
|
||||||
|
"reason": "successfully changed pronouns",
|
||||||
|
});
|
||||||
|
},
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
warn!("incorrect pin for user {}", input.name);
|
warn!("incorrect pin for user {}", input.name);
|
||||||
|
|
|
@ -29,7 +29,7 @@ fn main() {
|
||||||
"/api",
|
"/api",
|
||||||
routes![
|
routes![
|
||||||
auth::get_user,
|
auth::get_user,
|
||||||
auth::register_user,
|
auth::register,
|
||||||
auth::login,
|
auth::login,
|
||||||
chat::send_message,
|
chat::send_message,
|
||||||
chat::fetch_messages,
|
chat::fetch_messages,
|
||||||
|
|
25
src/user.rs
25
src/user.rs
|
@ -44,11 +44,34 @@ pub struct LogoutEvent {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// register event struct
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct RegisterEvent {
|
||||||
|
pub name: String,
|
||||||
|
pub pin: String,
|
||||||
|
pub pronouns: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
// login event struct
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct LoginEvent {
|
||||||
|
pub name: String,
|
||||||
|
pub pin: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
// change event type
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub enum ChangeEventType {
|
||||||
|
Name,
|
||||||
|
Pin,
|
||||||
|
Pronouns,
|
||||||
|
}
|
||||||
|
|
||||||
// change info event struct
|
// change info event struct
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct ChangeEvent {
|
pub struct ChangeEvent {
|
||||||
pub name: String, // name of the user
|
pub name: String, // name of the user
|
||||||
pub pin: String, // user's pin
|
pub pin: String, // user's pin
|
||||||
pub changed_event: String, // which event to change
|
pub changed_event: ChangeEventType, // which event to change
|
||||||
pub new_event: String, // the new value for the event
|
pub new_event: String, // the new value for the event
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue