add password hashing and simple ci
Some checks failed
continuous-integration/drone Build is failing

This commit is contained in:
2022-04-26 23:03:45 +02:00
parent 2c91e99a4d
commit 601b2d4f42
8 changed files with 87 additions and 15 deletions

View File

@ -1,11 +1,13 @@
use std::{collections::HashMap, sync::Arc};
use argon2::{password_hash::SaltString, Argon2, PasswordHasher};
use axum::{
extract::Query,
http::StatusCode,
routing::{get, post},
Extension, Json,
};
use rand_core::OsRng;
use sqlx::SqlitePool;
use crate::responses::registration::RegistrationResponse;
@ -17,7 +19,7 @@ use crate::{
models::users::User,
requests::registration::RegistrationRequest,
responses::username_available::UsernameAvailable,
types::{authentication_data::AuthenticationData, matrix_user_id::UserId},
types::{authentication_data::AuthenticationData, user_id::UserId},
Config,
};
@ -79,14 +81,18 @@ async fn post_register(
.then(|| ())
.ok_or(RegistrationError::UserIdTaken)?;
let password = auth_data.password();
let salt = SaltString::generate(OsRng);
let argon2 = Argon2::default();
let pw_hash = argon2
.hash_password(auth_data.password().as_bytes(), &salt)?
.to_string();
let display_name = match body.initial_device_display_name() {
Some(display_name) => display_name.as_ref(),
None => "Random displayname",
};
let user = User::create(&db, &user_id, &user_id.to_string(), password).await?;
let user = User::create(&db, &user_id, &user_id.to_string(), &pw_hash).await?;
let device = Device::create(&db, &user, "test", display_name).await?;
(user, device)

View File

@ -96,7 +96,7 @@ impl IntoResponse for ApiError {
)),
)
.into_response()
}
},
ApiError::Generic(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
Json(ErrorResponse::new(

View File

@ -7,5 +7,5 @@ pub enum RegistrationError {
#[error("The desired user ID is not a valid user name")]
InvalidUserId,
#[error("The desired user ID is already taken")]
UserIdTaken,
UserIdTaken
}