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
|
// 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
|
// 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)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
pub struct NewUserResponse {
|
pub struct NewUserResponse {
|
||||||
pub userid: Option<u64>,
|
pub id: u64,
|
||||||
pub username: Option<String>,
|
pub secret: String,
|
||||||
pub key: Option<String>,
|
pub discriminator: i32,
|
||||||
pub err: Option<&'static str>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This is basically anyone that's online at the moment
|
// This is basically anyone that's online at the moment
|
||||||
|
@ -7,19 +7,30 @@ use crate::{DBConn, schema};
|
|||||||
use diesel::{self, prelude::*};
|
use diesel::{self, prelude::*};
|
||||||
use diesel::result::Error;
|
use diesel::result::Error;
|
||||||
|
|
||||||
pub fn create_new_user() -> User {
|
pub fn create_new_user(conn: DBConn, name: String) -> User {
|
||||||
let uid = new_user_id();
|
use schema::users::dsl::*;
|
||||||
let uname = format!("User:#{}", uid);
|
use diesel::result::Error;
|
||||||
let rstring = new_key();
|
use crate::models::InsertableUser;
|
||||||
User {
|
|
||||||
userid: uid,
|
let ins = InsertableUser {
|
||||||
username: uname,
|
name: name,
|
||||||
key: rstring,
|
secret: new_key(),
|
||||||
date: Utc::now().timestamp() as u64,
|
date: time,
|
||||||
status: USER_ONLINE,
|
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 {
|
pub fn new_user_response(user: &Option<User>, msg: Option<&'static str>) -> NewUserResponse {
|
||||||
if let Some(u) = user {
|
if let Some(u) = user {
|
||||||
NewUserResponse {
|
NewUserResponse {
|
||||||
|
Loading…
Reference in New Issue
Block a user