First draft stuff

develop
Jef Roosens 2021-08-27 08:50:48 +02:00
parent 1ee9b78d81
commit a100ea52a0
Signed by untrusted user: Jef Roosens
GPG Key ID: 955C0660072F691F
3 changed files with 36 additions and 30 deletions

View File

@ -1,2 +1,5 @@
pub mod tokens; pub mod tokens;
pub mod users; pub mod users;
pub use users::{User, NewUser};
pub use tokens::{RefreshToken, NewRefreshToken};

View File

@ -7,35 +7,41 @@ use rocket::{
}; };
#[derive(Debug)] #[derive(Debug)]
pub enum RBError pub enum RbError
{ {
/// When the login requests an unknown user AuthUnknownUser,
UnknownUser, AuthBlockedUser,
BlockedUser, AuthInvalidPassword,
/// Invalid login password. AuthUnauthorized,
InvalidPassword, AuthTokenExpired,
/// When a non-admin user tries to use an admin endpoint AuthRefreshTokenExpired,
Unauthorized, AuthInvalidRefreshToken,
/// When an expired JWT token is used for auth. AuthDuplicateRefreshToken,
JWTTokenExpired,
/// Umbrella error for when something goes wrong whilst creating a JWT token pair Custom(&'static str),
JWTCreationError,
JWTError,
MissingJWTKey,
PWSaltError,
AdminCreationError, AdminCreationError,
TokenExpired,
InvalidRefreshToken,
DuplicateRefreshToken,
DBError, DBError,
DuplicateUser, DuplicateUser,
} }
impl RbError {
pub fn status(&self) -> Status {
Status::NotFound
}
pub fn message(&self) -> &'static str {
match self {
}
}
}
impl<'r> Responder<'r, 'static> for RBError impl<'r> Responder<'r, 'static> for RBError
{ {
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static>
{ {
let (status, message): (Status, &str) = match self { let (status, message): (Status, &'static str) = match self {
RBError::UnknownUser => (Status::NotFound, "Unknown user"), RBError::UnknownUser => (Status::NotFound, "Unknown user"),
RBError::BlockedUser => (Status::Unauthorized, "This user is blocked"), RBError::BlockedUser => (Status::Unauthorized, "This user is blocked"),
RBError::InvalidPassword => (Status::Unauthorized, "Invalid password"), RBError::InvalidPassword => (Status::Unauthorized, "Invalid password"),

View File

@ -1,8 +1,5 @@
use rb::{ use rb::{
db::{ db,
users as db_users,
users::{NewUser, User},
},
errors::RBError, errors::RBError,
}; };
use rocket::serde::json::Json; use rocket::serde::json::Json;
@ -12,29 +9,29 @@ use crate::{guards::Admin, RbDbConn};
pub fn routes() -> Vec<rocket::Route> pub fn routes() -> Vec<rocket::Route>
{ {
routes![get_users, get_user_info] routes![get_users, get_user_info, create_user]
} }
#[get("/users")] #[get("/users")]
async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result<Json<Vec<User>>> async fn get_users(admin: Admin, conn: RbDbConn) -> rb::Result<Json<Vec<db::User>>>
{ {
Ok(Json(conn.run(|c| rb::db::users::all(c)).await?)) Ok(Json(conn.run(|c| db::users::all(c)).await?))
} }
#[post("/users", data = "<user>")] #[post("/users", data = "<user>")]
async fn create_user(admin: Admin, conn: RbDbConn, user: Json<NewUser>) -> rb::Result<()> async fn create_user(admin: Admin, conn: RbDbConn, user: Json<db::NewUser>) -> rb::Result<()>
{ {
Ok(conn Ok(conn
.run(move |c| db_users::create(c, &user.into_inner())) .run(move |c| db::users::create(c, &user.into_inner()))
.await?) .await?)
} }
#[get("/users/<user_id_str>")] #[get("/users/<user_id_str>")]
async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: &str) -> rb::Result<Json<User>> async fn get_user_info(_admin: Admin, conn: RbDbConn, user_id_str: &str) -> rb::Result<Json<db::User>>
{ {
let user_id = Uuid::parse_str(user_id_str).map_err(|_| RBError::UnknownUser)?; let user_id = Uuid::parse_str(user_id_str).map_err(|_| RBError::UnknownUser)?;
match conn.run(move |c| db_users::find(c, user_id)).await { match conn.run(move |c| db::users::find(c, user_id)).await {
Some(user) => Ok(Json(user)), Some(user) => Ok(Json(user)),
None => Err(RBError::UnknownUser), None => Err(RBError::UnknownUser),
} }