Switch to returning json for all http requests
parent
aef1bbd2a7
commit
376fe024ca
41
README.md
41
README.md
|
@ -6,16 +6,39 @@ Send it the unhashed username and pin, and it'll store it in the `users.json` fi
|
|||
## API Documentation
|
||||
|
||||
`POST /api/register/<name>/<pin>/<pronouns>` Register the username with the pin provided if it doesn't already exist
|
||||
Returns status & reason json.
|
||||
|
||||
`GET /api/users/<name>` Check if the user exists
|
||||
Returns either
|
||||
|
||||
`{
|
||||
"status": "fail",
|
||||
"reason": "user not found",
|
||||
}`
|
||||
|
||||
or
|
||||
|
||||
`{
|
||||
"status": "ok",
|
||||
"user": {
|
||||
"name": "<name>",
|
||||
"pronouns": "<pronouns>",
|
||||
},
|
||||
}`
|
||||
|
||||
`GET /api/users/<name>/<pin>` Check if the user exists, and if the pin provided matches
|
||||
Returns status & reason json.
|
||||
|
||||
`POST /api/users/change/<name>/<pin>/<new-name>/<new-pin>` Change a users pin/name
|
||||
Returns status & reason json.
|
||||
|
||||
`GET /api/about/name/<name>` Get's the name of a user, probably just for checking if they exist
|
||||
|
||||
`GET /api/about/pronouns/<name>` Get's the pronouns of a user
|
||||
## Chat Documentation
|
||||
|
||||
`POST /api/message/send {"name":"username","body":"message body","date":"yyy-mm-dd","token":"USER_TOKEN"}` Post a json message.
|
||||
Returns status & reason json.
|
||||
|
||||
`GET /api/message/messages.json` Get a json file of all the messages
|
||||
|
||||
## Chat Planning
|
||||
|
||||
|
@ -25,23 +48,25 @@ Whenever user sends a message, client will send message & token and backend will
|
|||
|
||||
## To-Do:
|
||||
|
||||
- [x] Basic auth api
|
||||
- [x] Basic auth API
|
||||
- [ ] Return json instead of string
|
||||
- status is main thing to deal with, ok/fail/panic
|
||||
- reason is for more details, mainly just for debugging?
|
||||
- [ ] Basic messaging system
|
||||
- "status" shows wether request was succesful or not, either "ok" or "fail"
|
||||
- "reason" is for more details, mainly just for debugging?
|
||||
- [x] Basic messaging system
|
||||
- [x] Finish up `chat::create_message()`
|
||||
- [x] Create `chat::fetch_messages()`
|
||||
- [ ] Create `chat::delete_message()`
|
||||
- [ ] Different types of message events? eg. default, announcement, command
|
||||
- [ ] Emote support?
|
||||
- [ ] Token generation & storage
|
||||
- [ ] API to refresh token
|
||||
- [ ] Store token in json
|
||||
- [ ] API to check token?
|
||||
- [x] Pronouns
|
||||
- [x] Set pronouns
|
||||
- [ ] Change pronouns
|
||||
- [ ] Multiple sets of pronouns
|
||||
- [ ] Some form of plural support?
|
||||
- [ ] User management (banning, etc.)
|
||||
- [ ] Blacklist words from chat/names
|
||||
- [ ] More advanced chat features
|
||||
- [ ] Different types of message events? eg. default, announcement, command
|
||||
- [ ] Emote support?
|
||||
|
|
120
src/auth.rs
120
src/auth.rs
|
@ -1,6 +1,7 @@
|
|||
extern crate log;
|
||||
use crate::file_io::{append_json, read_json, write_json};
|
||||
use crate::user::User;
|
||||
use rocket_contrib::json::{Json, JsonValue};
|
||||
extern crate sha1;
|
||||
|
||||
#[get("/")]
|
||||
|
@ -22,13 +23,16 @@ pub fn index() -> &'static str {
|
|||
|
||||
// Post request to register a user and pin
|
||||
#[post("/api/register/<name>/<pin>/<pronouns>")]
|
||||
pub fn register_user(name: String, pin: i32, pronouns: String) -> String {
|
||||
pub fn register_user(name: String, pin: i32, pronouns: String) -> JsonValue {
|
||||
let mut users: Vec<User> = read_json(); // Create an array of users out of parsed json
|
||||
for i in &users {
|
||||
// loop through elements of the vector
|
||||
if i.name == name.to_lowercase() {
|
||||
warn!("Cannot create user {}! User is already in system.", i.name);
|
||||
return "User already exists!".to_string();
|
||||
return json!({
|
||||
"status": "fail",
|
||||
"reason": "user already exists",
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -52,16 +56,15 @@ pub fn register_user(name: String, pin: i32, pronouns: String) -> String {
|
|||
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
|
||||
);
|
||||
return json!({
|
||||
"status": "ok",
|
||||
"reason": format!("user {} registered", users[users.len()-1].name.to_string().to_lowercase()),
|
||||
});
|
||||
}
|
||||
|
||||
// Check if pin matches user
|
||||
#[get("/api/users/<name>/<pin>")]
|
||||
pub fn check_pin(name: String, pin: i32) -> String {
|
||||
pub fn check_pin(name: String, pin: i32) -> JsonValue {
|
||||
let users: Vec<User> = read_json();
|
||||
let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string();
|
||||
for i in &users {
|
||||
|
@ -69,10 +72,16 @@ pub fn check_pin(name: String, pin: i32) -> String {
|
|||
if i.name == name.to_lowercase() {
|
||||
if i.pin_hashed == hashed_pin_input {
|
||||
info!("pin correct for user {}", i.name);
|
||||
return "pin matches".to_string();
|
||||
return json!({
|
||||
"status": "ok",
|
||||
"reason": "pin matches",
|
||||
});
|
||||
} else {
|
||||
warn!("pin incorrect for user {}", i.name);
|
||||
return "Incorrect pin".to_string();
|
||||
return json!({
|
||||
"status": "fail",
|
||||
"reason": "incorrect pin",
|
||||
});
|
||||
};
|
||||
};
|
||||
}
|
||||
|
@ -80,12 +89,15 @@ pub fn check_pin(name: String, pin: i32) -> String {
|
|||
"cannot check pin for user {} as they do not exist",
|
||||
name.to_string().to_lowercase()
|
||||
);
|
||||
return format!("User {} does not exist.", name.to_string().to_lowercase());
|
||||
return json!({
|
||||
"status": "fail",
|
||||
"reason": format!("user {} doesn't exist", name.to_string().to_lowercase()),
|
||||
});
|
||||
}
|
||||
|
||||
// Change a users pin/name
|
||||
#[post("/api/users/change/<name>/<pin>/<new_name>/<new_pin>")]
|
||||
pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> String {
|
||||
pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> JsonValue {
|
||||
let mut users: Vec<User> = read_json();
|
||||
|
||||
let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string();
|
||||
|
@ -99,18 +111,16 @@ pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> String
|
|||
// Check wether to change name or name+pin
|
||||
if users[i].name == new_name.to_lowercase() {
|
||||
// check if new name already exists
|
||||
users[i].pin_hashed =
|
||||
sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
|
||||
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
|
||||
);
|
||||
return json!({
|
||||
"status": "ok",
|
||||
"reason": format!("changed {}'s pin", name.to_string().to_lowercase()),
|
||||
});
|
||||
} else {
|
||||
// check if new name already exists
|
||||
for n in &users {
|
||||
|
@ -120,10 +130,10 @@ pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> String
|
|||
name.to_lowercase(),
|
||||
new_name.to_lowercase()
|
||||
);
|
||||
return format!(
|
||||
"New name {} is already taken!",
|
||||
new_name.to_lowercase()
|
||||
);
|
||||
return json!({
|
||||
"status": "fail",
|
||||
"reason": format!("new name {} is already taken", new_name.to_lowercase()),
|
||||
});
|
||||
}
|
||||
}
|
||||
users[i].name = new_name.to_string().to_lowercase();
|
||||
|
@ -140,16 +150,17 @@ pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> 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()
|
||||
);
|
||||
return json!({
|
||||
"status": "ok",
|
||||
"reason": "successfully changed name and/or pin",
|
||||
});
|
||||
}
|
||||
} else {
|
||||
warn!("Incorrect pin given for user {}!", name.to_string());
|
||||
return format!("Incorrect pin for user {}!", name.to_string());
|
||||
return json!({
|
||||
"status": "fail",
|
||||
"reason": "incorrect pin for user",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -157,11 +168,14 @@ pub fn change(name: String, pin: i32, new_name: String, new_pin: i32) -> String
|
|||
"User {} not found, could not change pin and/or name.",
|
||||
name.to_string()
|
||||
);
|
||||
return format!("User {} not found.", name.to_string());
|
||||
return json!({
|
||||
"status": "fail",
|
||||
"reason": format!("user {} not found", name.to_string().to_lowercase()),
|
||||
});
|
||||
}
|
||||
|
||||
#[get("/api/users/<name>")]
|
||||
pub fn get_user(name: String) -> String {
|
||||
pub fn get_user(name: String) -> JsonValue {
|
||||
let users: Vec<User> = read_json();
|
||||
let found_user = users
|
||||
.iter()
|
||||
|
@ -169,36 +183,16 @@ pub fn get_user(name: String) -> String {
|
|||
.next();
|
||||
|
||||
match found_user {
|
||||
Some(user) => format!("User {}", &user.name),
|
||||
None => "User does not exist".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
/* Get data about a user */
|
||||
#[get("/api/about/name/<name>")]
|
||||
pub fn get_user_name(name: String) -> String {
|
||||
let users: Vec<User> = read_json();
|
||||
let found_user = users
|
||||
.iter()
|
||||
.filter(|u| u.name == name.to_lowercase())
|
||||
.next();
|
||||
|
||||
match found_user {
|
||||
Some(user) => user.name.to_string(),
|
||||
None => "NULL".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[get("/api/about/pronouns/<name>")]
|
||||
pub fn get_user_pronouns(name: String) -> String {
|
||||
let users: Vec<User> = read_json();
|
||||
let found_user = users
|
||||
.iter()
|
||||
.filter(|u| u.name == name.to_lowercase())
|
||||
.next();
|
||||
|
||||
match found_user {
|
||||
Some(user) => user.pronouns.to_string(),
|
||||
None => "NULL".to_string(),
|
||||
Some(user) => json!({
|
||||
"status":"ok",
|
||||
"user": {
|
||||
"name": user.name,
|
||||
"pronouns": user.pronouns,
|
||||
},
|
||||
}),
|
||||
None => json!({
|
||||
"status": "fail",
|
||||
"reason": format!("user {} not found", name),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -32,8 +32,6 @@ fn main() {
|
|||
auth::register_user,
|
||||
auth::check_pin,
|
||||
auth::change,
|
||||
auth::get_user_name,
|
||||
auth::get_user_pronouns,
|
||||
chat::send_message,
|
||||
chat::fetch_messages
|
||||
],
|
||||
|
|
Loading…
Reference in New Issue