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
|
## API Documentation
|
||||||
|
|
||||||
`POST /api/register/<name>/<pin>/<pronouns>` Register the username with the pin provided if it doesn't already exist
|
`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
|
`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
|
`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
|
`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
|
## Chat Planning
|
||||||
|
|
||||||
|
@ -25,23 +48,25 @@ Whenever user sends a message, client will send message & token and backend will
|
||||||
|
|
||||||
## To-Do:
|
## To-Do:
|
||||||
|
|
||||||
- [x] Basic auth api
|
- [x] Basic auth API
|
||||||
- [ ] Return json instead of string
|
- [ ] Return json instead of string
|
||||||
- status is main thing to deal with, ok/fail/panic
|
- "status" shows wether request was succesful or not, either "ok" or "fail"
|
||||||
- reason is for more details, mainly just for debugging?
|
- "reason" is for more details, mainly just for debugging?
|
||||||
- [ ] Basic messaging system
|
- [x] Basic messaging system
|
||||||
- [x] Finish up `chat::create_message()`
|
- [x] Finish up `chat::create_message()`
|
||||||
- [x] Create `chat::fetch_messages()`
|
- [x] Create `chat::fetch_messages()`
|
||||||
- [ ] Create `chat::delete_message()`
|
- [ ] Create `chat::delete_message()`
|
||||||
- [ ] Different types of message events? eg. default, announcement, command
|
|
||||||
- [ ] Emote support?
|
|
||||||
- [ ] Token generation & storage
|
- [ ] Token generation & storage
|
||||||
- [ ] API to refresh token
|
- [ ] API to refresh token
|
||||||
- [ ] Store token in json
|
- [ ] Store token in json
|
||||||
- [ ] API to check token?
|
- [ ] API to check token?
|
||||||
- [x] Pronouns
|
- [x] Pronouns
|
||||||
|
- [x] Set pronouns
|
||||||
- [ ] Change pronouns
|
- [ ] Change pronouns
|
||||||
- [ ] Multiple sets of pronouns
|
- [ ] Multiple sets of pronouns
|
||||||
- [ ] Some form of plural support?
|
- [ ] Some form of plural support?
|
||||||
- [ ] User management (banning, etc.)
|
- [ ] User management (banning, etc.)
|
||||||
- [ ] Blacklist words from chat/names
|
- [ ] 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;
|
extern crate log;
|
||||||
use crate::file_io::{append_json, read_json, write_json};
|
use crate::file_io::{append_json, read_json, write_json};
|
||||||
use crate::user::User;
|
use crate::user::User;
|
||||||
|
use rocket_contrib::json::{Json, JsonValue};
|
||||||
extern crate sha1;
|
extern crate sha1;
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
|
@ -22,13 +23,16 @@ pub fn index() -> &'static str {
|
||||||
|
|
||||||
// Post request to register a user and pin
|
// Post request to register a user and pin
|
||||||
#[post("/api/register/<name>/<pin>/<pronouns>")]
|
#[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
|
let mut users: Vec<User> = read_json(); // Create an array of users out of parsed json
|
||||||
for i in &users {
|
for i in &users {
|
||||||
// loop through elements of the vector
|
// loop through elements of the vector
|
||||||
if i.name == name.to_lowercase() {
|
if i.name == name.to_lowercase() {
|
||||||
warn!("Cannot create user {}! User is already in system.", i.name);
|
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].name.to_string(),
|
||||||
users[users.len() - 1].pin_hashed
|
users[users.len() - 1].pin_hashed
|
||||||
);
|
);
|
||||||
return format!(
|
return json!({
|
||||||
"User {} registered with pin hash: {}",
|
"status": "ok",
|
||||||
users[users.len() - 1].name.to_string().to_lowercase(),
|
"reason": format!("user {} registered", users[users.len()-1].name.to_string().to_lowercase()),
|
||||||
users[users.len() - 1].pin_hashed
|
});
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if pin matches user
|
// Check if pin matches user
|
||||||
#[get("/api/users/<name>/<pin>")]
|
#[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 users: Vec<User> = read_json();
|
||||||
let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string();
|
let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string();
|
||||||
for i in &users {
|
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.name == name.to_lowercase() {
|
||||||
if i.pin_hashed == hashed_pin_input {
|
if i.pin_hashed == hashed_pin_input {
|
||||||
info!("pin correct for user {}", i.name);
|
info!("pin correct for user {}", i.name);
|
||||||
return "pin matches".to_string();
|
return json!({
|
||||||
|
"status": "ok",
|
||||||
|
"reason": "pin matches",
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
warn!("pin incorrect for user {}", i.name);
|
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",
|
"cannot check pin for user {} as they do not exist",
|
||||||
name.to_string().to_lowercase()
|
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
|
// Change a users pin/name
|
||||||
#[post("/api/users/change/<name>/<pin>/<new_name>/<new_pin>")]
|
#[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 mut users: Vec<User> = read_json();
|
||||||
|
|
||||||
let hashed_pin_input = sha1::Sha1::from(&pin.to_string()).digest().to_string();
|
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
|
// Check wether to change name or name+pin
|
||||||
if users[i].name == new_name.to_lowercase() {
|
if users[i].name == new_name.to_lowercase() {
|
||||||
// check if new name already exists
|
// check if new name already exists
|
||||||
users[i].pin_hashed =
|
users[i].pin_hashed = sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
|
||||||
sha1::Sha1::from(&new_pin.to_string()).digest().to_string();
|
|
||||||
match write_json(&users) {
|
match write_json(&users) {
|
||||||
Err(why) => panic!("Cannot write to json! {}", why),
|
Err(why) => panic!("Cannot write to json! {}", why),
|
||||||
Ok(()) => info!("succesfully wrote to json file"),
|
Ok(()) => info!("succesfully wrote to json file"),
|
||||||
}
|
}
|
||||||
info!("Changed pin of {}", name.to_string().to_lowercase());
|
info!("Changed pin of {}", name.to_string().to_lowercase());
|
||||||
return format!(
|
return json!({
|
||||||
"User {}'s new pin hash is {}.",
|
"status": "ok",
|
||||||
name.to_string().to_lowercase(),
|
"reason": format!("changed {}'s pin", name.to_string().to_lowercase()),
|
||||||
users[i].pin_hashed
|
});
|
||||||
);
|
|
||||||
} else {
|
} else {
|
||||||
// check if new name already exists
|
// check if new name already exists
|
||||||
for n in &users {
|
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(),
|
name.to_lowercase(),
|
||||||
new_name.to_lowercase()
|
new_name.to_lowercase()
|
||||||
);
|
);
|
||||||
return format!(
|
return json!({
|
||||||
"New name {} is already taken!",
|
"status": "fail",
|
||||||
new_name.to_lowercase()
|
"reason": format!("new name {} is already taken", new_name.to_lowercase()),
|
||||||
);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
users[i].name = new_name.to_string().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].name.to_string(),
|
||||||
users[i].pin_hashed.to_string()
|
users[i].pin_hashed.to_string()
|
||||||
);
|
);
|
||||||
return format!(
|
return json!({
|
||||||
"User previously known as {} is now {}. Pin hash, if different, is {}",
|
"status": "ok",
|
||||||
name.to_string(),
|
"reason": "successfully changed name and/or pin",
|
||||||
users[i].name.to_string(),
|
});
|
||||||
users[i].pin_hashed.to_string()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
warn!("Incorrect pin given for user {}!", name.to_string());
|
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.",
|
"User {} not found, could not change pin and/or name.",
|
||||||
name.to_string()
|
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>")]
|
#[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 users: Vec<User> = read_json();
|
||||||
let found_user = users
|
let found_user = users
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -169,36 +183,16 @@ pub fn get_user(name: String) -> String {
|
||||||
.next();
|
.next();
|
||||||
|
|
||||||
match found_user {
|
match found_user {
|
||||||
Some(user) => format!("User {}", &user.name),
|
Some(user) => json!({
|
||||||
None => "User does not exist".to_string(),
|
"status":"ok",
|
||||||
}
|
"user": {
|
||||||
}
|
"name": user.name,
|
||||||
|
"pronouns": user.pronouns,
|
||||||
/* Get data about a user */
|
},
|
||||||
#[get("/api/about/name/<name>")]
|
}),
|
||||||
pub fn get_user_name(name: String) -> String {
|
None => json!({
|
||||||
let users: Vec<User> = read_json();
|
"status": "fail",
|
||||||
let found_user = users
|
"reason": format!("user {} not found", name),
|
||||||
.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(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,6 @@ fn main() {
|
||||||
auth::register_user,
|
auth::register_user,
|
||||||
auth::check_pin,
|
auth::check_pin,
|
||||||
auth::change,
|
auth::change,
|
||||||
auth::get_user_name,
|
|
||||||
auth::get_user_pronouns,
|
|
||||||
chat::send_message,
|
chat::send_message,
|
||||||
chat::fetch_messages
|
chat::fetch_messages
|
||||||
],
|
],
|
||||||
|
|
Loading…
Reference in New Issue