Fixed all 35 compile time issues with new auth module
Ready for testing
This commit is contained in:
parent
306156407d
commit
220bc11ef0
@ -1,14 +1,21 @@
|
|||||||
// Handlers for the base auth routes
|
// Handlers for the base auth routes
|
||||||
use crate::users{self, Member};
|
use crate::{
|
||||||
use crate::rand_utils::new_key;
|
DBConn, schema,
|
||||||
|
models::{
|
||||||
|
Invite,
|
||||||
|
User
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
|
use rocket::response::{self, Responder, Response};
|
||||||
|
use rocket::request::{Form, Request};
|
||||||
|
use rocket_contrib::json::Json;
|
||||||
|
use diesel::{self, prelude::*};
|
||||||
use std::{error, fmt};
|
use std::{error, fmt};
|
||||||
use diesel;
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(FromForm)]
|
#[derive(FromForm)]
|
||||||
struct JoinParams {
|
pub struct JoinParams {
|
||||||
code: u64,
|
code: u64,
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
@ -21,7 +28,7 @@ pub struct AuthKey {
|
|||||||
|
|
||||||
pub type AuthResult<T, AuthErr> = std::result::Result<T, AuthErr>;
|
pub type AuthResult<T, AuthErr> = std::result::Result<T, AuthErr>;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct AuthErr {
|
pub struct AuthErr {
|
||||||
msg: &'static str,
|
msg: &'static str,
|
||||||
status: u16,
|
status: u16,
|
||||||
@ -48,73 +55,78 @@ impl<'r> Responder<'r> for AuthErr {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
#[post("/login")]
|
|
||||||
pub fn login() {
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
pub fn join(conn: DBConn, params: Form<JoinParams>, hashcode: u64) -> AuthResult<Json<User>, AuthErr>{
|
||||||
#[post("/join", data="<params>")]
|
|
||||||
pub fn join(conn: DBConn, params: JoinParams) -> AuthResult<Json<User>, AuthErr>{
|
|
||||||
/*
|
/*
|
||||||
* Requires <code:int> -> body
|
* Requires <code:int> -> body
|
||||||
* Requires <name:string> -> body
|
* Requires <name:string> -> body
|
||||||
* Struct JoinParams enforces this for us so if something is missing then rocket should 404
|
* Struct JoinParams enforces this for us so if something is missing then rocket should 404
|
||||||
*/
|
*/
|
||||||
|
use schema::invites::{self, dsl::*};
|
||||||
|
|
||||||
const expired: &'static str = "Invite expired";
|
const expired: &'static str = "Invite expired";
|
||||||
const negate: &'static str = "Malformed request";
|
const negate: &'static str = "Malformed request";
|
||||||
|
|
||||||
let diesel_result: Result<Invite, diesel::result::Error> = invites
|
let diesel_result: Result<Invite, diesel::result::Error> = invites
|
||||||
.filter(id.eq(code))
|
.filter(invites::dsl::id.eq(params.code))
|
||||||
.first(&conn.0);
|
.first(&conn.0);
|
||||||
|
|
||||||
if let Ok(data) = diesel_result {
|
if let Ok(data) = diesel_result {
|
||||||
match data.uses {
|
match data.uses {
|
||||||
1 ... std::i32::MAX => {
|
1 ..= std::i32::MAX => {
|
||||||
let new_user = users::new_member(conn):
|
let new_user = crate::users::create_new_user(conn, params.name);
|
||||||
// update the uses counter
|
// update the uses counter
|
||||||
diesel::update(users.filter(userid.eq(user.userid)))
|
let _ignored = diesel::update(invites.filter(invites::dsl::id.eq(hashcode)))
|
||||||
.set(uses.eq(data.uses - 1))
|
.set(uses.eq(data.uses - 1))
|
||||||
.execute(&conn.0)
|
.execute(&conn.0);
|
||||||
|
|
||||||
AuthResult(Json(new_user))
|
Ok(Json(new_user))
|
||||||
}
|
}
|
||||||
// The invite has been used up and thus should be removed
|
// The invite has been used up and thus should be removed
|
||||||
std::i32::MIN ... 0 => {
|
std::i32::MIN ..= 0 => {
|
||||||
let _ = diesel::delete(invites.filter(id.eq(data.id)))
|
let _ = diesel::delete(invites.filter(invites::dsl::id.eq(data.id)))
|
||||||
.execute(&conn.0)
|
.execute(&conn.0)
|
||||||
.expect("Could not delete invite");
|
.expect("Could not delete invite");
|
||||||
|
|
||||||
AuthResult(AuthErr{msg: expired})
|
Err(AuthErr{msg: expired, status: 404})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
AuthResult(AuthErr{msg:negate})
|
Err(AuthErr{msg:negate, status: 500})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/leave", data = "<api_key>")]
|
#[post("/leave", data = "<api_key>")]
|
||||||
pub fn leave(conn: DBConn, api_key: AuthKey) -> Status {
|
pub fn leave(conn: DBConn, api_key: Form<AuthKey>) -> Status {
|
||||||
/*
|
/*
|
||||||
* Basic removal of the user from our users table
|
* Basic removal of the user from our users table
|
||||||
*/
|
*/
|
||||||
diesel::delete(users.filter(id.eq(api_key.id), ))
|
use crate::schema::users::dsl::*;
|
||||||
let db_result = diesel::delete(users)
|
use crate::diesel::ExpressionMethods;
|
||||||
|
let db_result = diesel::delete(users
|
||||||
.filter(id.eq(api_key.id))
|
.filter(id.eq(api_key.id))
|
||||||
.filter(secret.eq(api_key.secret))
|
.filter(secret.eq(api_key.secret)))
|
||||||
.execute(&conn.0);
|
.execute(&conn.0).unwrap();
|
||||||
if let result = Ok(db_result) {
|
|
||||||
|
|
||||||
Status::Accepted
|
Status::Accepted
|
||||||
}
|
|
||||||
else {
|
|
||||||
Status::BadRequst
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
|
|
||||||
#[pust("/close")]
|
|
||||||
pub fn close() {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*/
|
#[cfg(test)]
|
||||||
|
mod auth_tests {
|
||||||
|
use super::*;
|
||||||
|
use rocket;
|
||||||
|
use diesel::mysql::MysqlConnection;
|
||||||
|
|
||||||
|
fn feed_n_leave() {
|
||||||
|
// Create an invite in our db manually
|
||||||
|
// Use that invite to join
|
||||||
|
// Then leave using our neato /auth/leave route
|
||||||
|
let app = rocket::ignite()
|
||||||
|
.mount("/auth", routes![crate::invites::use_invite, leave])
|
||||||
|
.attach(super::DBConn::fairing());
|
||||||
|
|
||||||
|
let conn = MysqlConnection::establish("mysql://freechat_dev:password@localhost:3306/freechat");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user