forked from Chewing_Bever/rusty-bever
Some broken shit
parent
1c524f181f
commit
d90dbcdc2a
|
@ -214,6 +214,7 @@ dependencies = [
|
|||
"diesel_derives",
|
||||
"pq-sys",
|
||||
"r2d2",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1076,6 +1077,7 @@ dependencies = [
|
|||
"rocket_sync_db_pools",
|
||||
"rust-argon2",
|
||||
"serde",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1470,6 +1472,12 @@ version = "0.2.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
version = "0.2.15"
|
||||
|
|
|
@ -16,18 +16,19 @@ path = "src/rbs/main.rs"
|
|||
|
||||
[dependencies]
|
||||
# ORM
|
||||
diesel = { version = "1.4.7", features = ["postgres"] }
|
||||
diesel = { version = "1.4.7", features = ["postgres", "uuidv07"] }
|
||||
diesel_migrations = "1.4.0"
|
||||
# To properly compile libpq statically
|
||||
openssl = "0.10.36"
|
||||
# For password hashing & verification
|
||||
rust-argon2 = "0.8.3"
|
||||
rand = "0.8.4"
|
||||
uuid = "0.8.2"
|
||||
|
||||
# Backend web framework
|
||||
[dependencies.rocket]
|
||||
version = "0.5.0-rc.1"
|
||||
features = [ "json" ]
|
||||
features = ["json"]
|
||||
|
||||
# Used to (de)serialize JSON
|
||||
[dependencies.serde]
|
||||
|
|
|
@ -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),
|
||||
}
|
||||
}
|
|
@ -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;
|
|
@ -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};
|
||||
|
|
Loading…
Reference in New Issue