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 users;
pub use users::{User, NewUser};
pub use tokens::{RefreshToken, NewRefreshToken};

View File

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

View File

@ -1,8 +1,5 @@
use rb::{
db::{
users as db_users,
users::{NewUser, User},
},
db,
errors::RBError,
};
use rocket::serde::json::Json;
@ -12,29 +9,29 @@ use crate::{guards::Admin, RbDbConn};
pub fn routes() -> Vec<rocket::Route>
{
routes![get_users, get_user_info]
routes![get_users, get_user_info, create_user]
}
#[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>")]
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
.run(move |c| db_users::create(c, &user.into_inner()))
.run(move |c| db::users::create(c, &user.into_inner()))
.await?)
}
#[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)?;
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)),
None => Err(RBError::UnknownUser),
}