Some broken shit
This commit is contained in:
parent
1c524f181f
commit
d90dbcdc2a
9 changed files with 67 additions and 12 deletions
22
src/rb/auth.rs
Normal file
22
src/rb/auth.rs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
use crate::errors::AuthError;
|
||||
use crate::models::User;
|
||||
use crate::schema::users::dsl as users;
|
||||
use argon2::verify_encoded;
|
||||
use diesel::prelude::*;
|
||||
use diesel::PgConnection;
|
||||
|
||||
pub fn verify_user(conn: &PgConnection, username: &str, password: &str) -> Result<User, AuthError> {
|
||||
// TODO handle non-"NotFound" Diesel errors accordingely
|
||||
let user = match users::users
|
||||
.filter(users::username.eq(username))
|
||||
.first::<User>(conn)
|
||||
{
|
||||
Err(_) => return Err(AuthError::UnknownUser),
|
||||
Ok(user) => user,
|
||||
};
|
||||
|
||||
match verify_encoded(user.password.as_str(), password.as_bytes()) {
|
||||
Ok(true) => Ok(user),
|
||||
_ => Err(AuthError::InvalidPassword),
|
||||
}
|
||||
}
|
||||
4
src/rb/errors.rs
Normal file
4
src/rb/errors.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub enum AuthError {
|
||||
UnknownUser,
|
||||
InvalidPassword,
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
#[macro_use]
|
||||
extern crate diesel;
|
||||
|
||||
pub mod auth;
|
||||
pub mod errors;
|
||||
mod models;
|
||||
pub(crate) mod schema;
|
||||
11
src/rb/models.rs
Normal file
11
src/rb/models.rs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
use diesel::Queryable;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Queryable)]
|
||||
pub struct User {
|
||||
id: Uuid,
|
||||
username: String,
|
||||
pub password: String,
|
||||
blocked: bool,
|
||||
admin: bool,
|
||||
}
|
||||
|
|
@ -19,7 +19,4 @@ table! {
|
|||
|
||||
joinable!(refresh_tokens -> users (user_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
refresh_tokens,
|
||||
users,
|
||||
);
|
||||
allow_tables_to_appear_in_same_query!(refresh_tokens, users,);
|
||||
|
|
|
|||
|
|
@ -1,16 +1,19 @@
|
|||
use crate::RbDbConn;
|
||||
use serde::Deserialize;
|
||||
use rb::auth::verify_user;
|
||||
use rocket::serde::json::Json;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Credentials {
|
||||
username: String,
|
||||
password: String
|
||||
password: String,
|
||||
}
|
||||
|
||||
#[post("/login", data="<credentials>")]
|
||||
#[post("/login", data = "<credentials>")]
|
||||
async fn login(conn: RbDbConn, credentials: Json<Credentials>) {
|
||||
|
||||
let user = conn
|
||||
.run(move |c| verify_user(c, &credentials.username, &credentials.password))
|
||||
.await;
|
||||
}
|
||||
|
||||
// /refresh
|
||||
|
|
|
|||
|
|
@ -2,8 +2,10 @@
|
|||
// compilation succeeds
|
||||
extern crate openssl;
|
||||
|
||||
#[macro_use] extern crate rocket;
|
||||
#[macro_use] extern crate diesel_migrations;
|
||||
#[macro_use]
|
||||
extern crate rocket;
|
||||
#[macro_use]
|
||||
extern crate diesel_migrations;
|
||||
|
||||
use rocket::{fairing::AdHoc, Build, Rocket};
|
||||
use rocket_sync_db_pools::{database, diesel};
|
||||
|
|
|
|||
Reference in a new issue