Compare commits
No commits in common. "a370b94d8b38e8e55241095ec9ad5fc0480029f9" and "6ff16702f623c08203b2a77e4a86aececca1711c" have entirely different histories.
a370b94d8b
...
6ff16702f6
7 changed files with 68 additions and 69 deletions
|
@ -1,5 +1,3 @@
|
||||||
mod auth;
|
|
||||||
mod user;
|
mod user;
|
||||||
|
|
||||||
pub use auth::AuthController;
|
|
||||||
pub use user::UserController;
|
pub use user::UserController;
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
use actix_web::{
|
|
||||||
error::{ErrorBadRequest, ErrorInternalServerError},
|
|
||||||
web, Responder,
|
|
||||||
};
|
|
||||||
use argon2::{Argon2, PasswordHash, PasswordVerifier};
|
|
||||||
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
|
|
||||||
use serde::Deserialize;
|
|
||||||
|
|
||||||
use crate::AppState;
|
|
||||||
|
|
||||||
use super::user::UserWithoutPassword;
|
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
|
||||||
pub struct LoginRequest {
|
|
||||||
email: String,
|
|
||||||
password: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct AuthController;
|
|
||||||
|
|
||||||
impl AuthController {
|
|
||||||
pub async fn login(
|
|
||||||
state: web::Data<AppState>,
|
|
||||||
login: web::Json<LoginRequest>,
|
|
||||||
) -> actix_web::Result<impl Responder> {
|
|
||||||
let db = &state.db;
|
|
||||||
let login = login.into_inner();
|
|
||||||
|
|
||||||
let user = entity::user::Entity::find()
|
|
||||||
.filter(entity::user::Column::Email.eq(login.email))
|
|
||||||
.one(db)
|
|
||||||
.await
|
|
||||||
.map_err(ErrorInternalServerError)?
|
|
||||||
.ok_or(ErrorBadRequest("Login Failed"))?;
|
|
||||||
|
|
||||||
let argon2 = Argon2::default();
|
|
||||||
let parsed_hash = PasswordHash::new(&user.hash).map_err(ErrorInternalServerError)?;
|
|
||||||
argon2
|
|
||||||
.verify_password(login.password.as_bytes(), &parsed_hash)
|
|
||||||
.map_err(ErrorBadRequest)?;
|
|
||||||
|
|
||||||
Ok(web::Json(UserWithoutPassword::from(user)))
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -3,7 +3,7 @@ use argon2::{
|
||||||
password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
|
password_hash::{rand_core::OsRng, PasswordHasher, SaltString},
|
||||||
Argon2,
|
Argon2,
|
||||||
};
|
};
|
||||||
use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait};
|
use sea_orm::{ActiveModelTrait, ActiveValue, DatabaseConnection, EntityTrait};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
|
@ -69,6 +69,7 @@ impl UserController {
|
||||||
name: ActiveValue::Set(user.name),
|
name: ActiveValue::Set(user.name),
|
||||||
email: ActiveValue::Set(user.email),
|
email: ActiveValue::Set(user.email),
|
||||||
hash: ActiveValue::Set(password_hash.to_string()),
|
hash: ActiveValue::Set(password_hash.to_string()),
|
||||||
|
salt: ActiveValue::Set(salt.to_string()),
|
||||||
};
|
};
|
||||||
|
|
||||||
let result = user.insert(db).await.map_err(ErrorInternalServerError)?;
|
let result = user.insert(db).await.map_err(ErrorInternalServerError)?;
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use actix_web::{web, App, HttpServer};
|
use actix_web::{web, App, HttpResponse, HttpServer, Responder};
|
||||||
use migration::MigratorTrait;
|
|
||||||
use sea_orm::{Database, DatabaseConnection};
|
use sea_orm::{Database, DatabaseConnection};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
|
@ -25,15 +24,8 @@ async fn main() -> std::io::Result<()> {
|
||||||
.await
|
.await
|
||||||
.expect("Connecting to Database failed");
|
.expect("Connecting to Database failed");
|
||||||
|
|
||||||
println!("Running Migrations");
|
|
||||||
migration::Migrator::up(&conn, None)
|
|
||||||
.await
|
|
||||||
.expect("Running migrations failed");
|
|
||||||
println!("Finished running migrations");
|
|
||||||
|
|
||||||
let state = AppState { db: conn };
|
let state = AppState { db: conn };
|
||||||
|
|
||||||
println!("Listening for connections...");
|
|
||||||
HttpServer::new(move || {
|
HttpServer::new(move || {
|
||||||
let cors = if cfg!(debug_assertions) {
|
let cors = if cfg!(debug_assertions) {
|
||||||
actix_cors::Cors::permissive()
|
actix_cors::Cors::permissive()
|
||||||
|
@ -49,3 +41,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.run()
|
.run()
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn index() -> impl Responder {
|
||||||
|
HttpResponse::Ok().body("API Test Response")
|
||||||
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use crate::controller::{AuthController, UserController};
|
use crate::controller::UserController;
|
||||||
use actix_web::web;
|
use actix_web::web;
|
||||||
|
|
||||||
pub fn config(cfg: &mut web::ServiceConfig) {
|
pub fn config(cfg: &mut web::ServiceConfig) {
|
||||||
|
@ -9,7 +9,6 @@ pub fn config(cfg: &mut web::ServiceConfig) {
|
||||||
.get(UserController::list_users)
|
.get(UserController::list_users)
|
||||||
.post(UserController::create_user),
|
.post(UserController::create_user),
|
||||||
)
|
)
|
||||||
.service(web::resource("/users/{user_id}"))
|
.service(web::resource("/users/{user_id}")),
|
||||||
.service(web::scope("/auth").route("/login", web::post().to(AuthController::login))),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,18 +2,65 @@
|
||||||
|
|
||||||
use sea_orm::entity::prelude::*;
|
use sea_orm::entity::prelude::*;
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
#[derive(Copy, Clone, Default, Debug, DeriveEntity)]
|
||||||
#[sea_orm(table_name = "user")]
|
pub struct Entity;
|
||||||
pub struct Model {
|
|
||||||
#[sea_orm(primary_key, auto_increment = false)]
|
impl EntityName for Entity {
|
||||||
pub id: Uuid,
|
fn table_name(&self) -> &str {
|
||||||
pub name: String,
|
"user"
|
||||||
#[sea_orm(unique)]
|
}
|
||||||
pub email: String,
|
|
||||||
pub hash: String,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
#[derive(Clone, Debug, PartialEq, DeriveModel, DeriveActiveModel, Eq)]
|
||||||
|
pub struct Model {
|
||||||
|
pub id: Uuid,
|
||||||
|
pub name: String,
|
||||||
|
pub email: String,
|
||||||
|
pub hash: String,
|
||||||
|
pub salt: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
|
||||||
|
pub enum Column {
|
||||||
|
Id,
|
||||||
|
Name,
|
||||||
|
Email,
|
||||||
|
Hash,
|
||||||
|
Salt,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
|
||||||
|
pub enum PrimaryKey {
|
||||||
|
Id,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PrimaryKeyTrait for PrimaryKey {
|
||||||
|
type ValueType = Uuid;
|
||||||
|
fn auto_increment() -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, EnumIter)]
|
||||||
pub enum Relation {}
|
pub enum Relation {}
|
||||||
|
|
||||||
|
impl ColumnTrait for Column {
|
||||||
|
type EntityName = Entity;
|
||||||
|
fn def(&self) -> ColumnDef {
|
||||||
|
match self {
|
||||||
|
Self::Id => ColumnType::Uuid.def(),
|
||||||
|
Self::Name => ColumnType::String(None).def(),
|
||||||
|
Self::Email => ColumnType::String(None).def().unique(),
|
||||||
|
Self::Hash => ColumnType::String(None).def(),
|
||||||
|
Self::Salt => ColumnType::String(None).def(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RelationTrait for Relation {
|
||||||
|
fn def(&self) -> RelationDef {
|
||||||
|
panic!("No RelationDef")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
impl ActiveModelBehavior for ActiveModel {}
|
||||||
|
|
|
@ -21,6 +21,7 @@ impl MigrationTrait for Migration {
|
||||||
.col(ColumnDef::new(User::Name).string().not_null())
|
.col(ColumnDef::new(User::Name).string().not_null())
|
||||||
.col(ColumnDef::new(User::Email).string().not_null().unique_key())
|
.col(ColumnDef::new(User::Email).string().not_null().unique_key())
|
||||||
.col(ColumnDef::new(User::Hash).string().not_null())
|
.col(ColumnDef::new(User::Hash).string().not_null())
|
||||||
|
.col(ColumnDef::new(User::Salt).string().not_null())
|
||||||
.to_owned(),
|
.to_owned(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
@ -40,4 +41,5 @@ enum User {
|
||||||
Name,
|
Name,
|
||||||
Email,
|
Email,
|
||||||
Hash,
|
Hash,
|
||||||
|
Salt,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue