dumy commit as work will continue on new async branch
This commit is contained in:
parent
83aafa9ff5
commit
62204e52ca
92
server/src/auth.rs
Normal file
92
server/src/auth.rs
Normal file
@ -0,0 +1,92 @@
|
||||
// Handlers for the base auth routes
|
||||
use crate::users;
|
||||
use std::{error, fmt};
|
||||
|
||||
|
||||
#[derive(FromForm)]
|
||||
struct JoinParams {
|
||||
code: u64,
|
||||
name: String,
|
||||
}
|
||||
|
||||
|
||||
pub type AuthResult<T, AuthErr> = std::result::Result<T, AuthErr>;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AuthErr {
|
||||
msg: &'static str,
|
||||
status: u16,
|
||||
}
|
||||
|
||||
impl fmt::Display for AuthErr {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "Authentication error")
|
||||
}
|
||||
}
|
||||
|
||||
impl error::Error for AuthErr {
|
||||
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<'r> Responder<'r> for AuthErr {
|
||||
fn respond_to(self, _:&Request) -> response::Result<'r> {
|
||||
Response::build()
|
||||
.status(Status::InternalServerError)
|
||||
.raw_header("db-error", self.msg)
|
||||
.ok()
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/login")]
|
||||
pub fn login() {
|
||||
}
|
||||
|
||||
#[post("/join", data="<data>")]
|
||||
pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<>, AuthErr>{
|
||||
/*
|
||||
* Requires <code:int> -> body
|
||||
* Requires <name:string> -> body
|
||||
*/
|
||||
const expired: &'static str = "Invite expired";
|
||||
const negate: &'static str = "Malformed request";
|
||||
let diesel_result: Result<Invite, diesel::result::Error> = invites
|
||||
.filter(id.eq(code))
|
||||
.first(&conn.0);
|
||||
|
||||
if let Ok(data) = diesel_result {
|
||||
match data.uses {
|
||||
1 ... std::i32::MAX => {
|
||||
let user = users::new_user();
|
||||
// update the uses counter
|
||||
diesel::update(users.filter(userid.eq(user.userid)))
|
||||
.set(uses.eq(data.uses - 1))
|
||||
.execute(&conn.0)
|
||||
|
||||
AuthResult(Json(user))
|
||||
}
|
||||
// The invite has been used up and thus should be removed
|
||||
std::i32::MIN ... 0 => {
|
||||
let _ = diesel::delete(invites.filter(id.eq(data.id)))
|
||||
.execute(&conn.0)
|
||||
.expect("Could not delete invite");
|
||||
|
||||
AuthResult(AuthErr{msg: expired})
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
AuthResult(AuthErr{msg:negate})
|
||||
}
|
||||
}
|
||||
|
||||
#[post("/leave", data = "<>")]
|
||||
pub fn leave() {
|
||||
}
|
||||
|
||||
#[pust("/close")]
|
||||
pub fn close() {
|
||||
}
|
||||
|
||||
|
@ -5,13 +5,11 @@
|
||||
// This structure allows us to provide some critical data for the client to reconnect to
|
||||
// the server without having to go through a sign in process everytime
|
||||
|
||||
// TODO: refactor this so that we don't need any optional fields
|
||||
#[derive(Serialize, Deserialize, Debug)]
|
||||
pub struct NewUserResponse {
|
||||
pub userid: Option<u64>,
|
||||
pub username: Option<String>,
|
||||
pub key: Option<String>,
|
||||
pub err: Option<&'static str>,
|
||||
pub id: u64,
|
||||
pub secret: String,
|
||||
pub discriminator: i32,
|
||||
}
|
||||
|
||||
// This is basically anyone that's online at the moment
|
||||
|
@ -7,18 +7,29 @@ use crate::{DBConn, schema};
|
||||
use diesel::{self, prelude::*};
|
||||
use diesel::result::Error;
|
||||
|
||||
pub fn create_new_user() -> User {
|
||||
let uid = new_user_id();
|
||||
let uname = format!("User:#{}", uid);
|
||||
let rstring = new_key();
|
||||
User {
|
||||
userid: uid,
|
||||
username: uname,
|
||||
key: rstring,
|
||||
date: Utc::now().timestamp() as u64,
|
||||
pub fn create_new_user(conn: DBConn, name: String) -> User {
|
||||
use schema::users::dsl::*;
|
||||
use diesel::result::Error;
|
||||
use crate::models::InsertableUser;
|
||||
|
||||
let ins = InsertableUser {
|
||||
name: name,
|
||||
secret: new_key(),
|
||||
date: time,
|
||||
status: USER_ONLINE,
|
||||
};
|
||||
// insert the nwe user data then return usable user data to the client
|
||||
diesel::insert_into(users::table)
|
||||
.values(&ins)
|
||||
.execute(&conn.0);
|
||||
|
||||
let new_user_data : Result<User, diesel::result::Error> = users
|
||||
.filter(userid.eq(ins.id))
|
||||
.first(&conn.0);
|
||||
|
||||
new_user_data.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub fn new_user_response(user: &Option<User>, msg: Option<&'static str>) -> NewUserResponse {
|
||||
if let Some(u) = user {
|
||||
|
Loading…
Reference in New Issue
Block a user