style: cargo clippy and format
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
All checks were successful
ci/woodpecker/push/build-and-publish Pipeline was successful
This commit is contained in:
parent
5d0d666094
commit
97f7595cc5
10 changed files with 289 additions and 159 deletions
|
@ -1,15 +1,21 @@
|
|||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use actix_web::{error, post, web, Error, HttpResponse};
|
||||
use actix_web::{Error, HttpResponse, error, post, web};
|
||||
use argon2::{
|
||||
PasswordHasher,
|
||||
password_hash::{SaltString, rand_core::OsRng},
|
||||
};
|
||||
use futures::StreamExt;
|
||||
use log::error;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use futures::StreamExt;
|
||||
use uuid::Uuid;
|
||||
use argon2::{password_hash::{rand_core::OsRng, SaltString}, PasswordHasher};
|
||||
|
||||
use crate::{crypto::{generate_access_token, generate_refresh_token}, Data};
|
||||
use super::login::Response;
|
||||
use crate::{
|
||||
Data,
|
||||
crypto::{generate_access_token, generate_refresh_token},
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct AccountInformation {
|
||||
|
@ -70,68 +76,76 @@ pub async fn res(mut payload: web::Payload, data: web::Data<Data>) -> Result<Htt
|
|||
let email_regex = Regex::new(r"[-A-Za-z0-9!#$%&'*+/=?^_`{|}~]+(?:\.[-A-Za-z0-9!#$%&'*+/=?^_`{|}~]+)*@(?:[A-Za-z0-9](?:[-A-Za-z0-9]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[-A-Za-z0-9]*[A-Za-z0-9])?").unwrap();
|
||||
|
||||
if !email_regex.is_match(&account_information.email) {
|
||||
return Ok(HttpResponse::Forbidden().json(
|
||||
ResponseError {
|
||||
email_valid: false,
|
||||
..Default::default()
|
||||
}
|
||||
))
|
||||
return Ok(HttpResponse::Forbidden().json(ResponseError {
|
||||
email_valid: false,
|
||||
..Default::default()
|
||||
}));
|
||||
}
|
||||
|
||||
// FIXME: This regex doesnt seem to be working
|
||||
let username_regex = Regex::new(r"[a-zA-Z0-9.-_]").unwrap();
|
||||
|
||||
if !username_regex.is_match(&account_information.identifier) || account_information.identifier.len() < 3 || account_information.identifier.len() > 32 {
|
||||
return Ok(HttpResponse::Forbidden().json(
|
||||
ResponseError {
|
||||
gorb_id_valid: false,
|
||||
..Default::default()
|
||||
}
|
||||
))
|
||||
if !username_regex.is_match(&account_information.identifier)
|
||||
|| account_information.identifier.len() < 3
|
||||
|| account_information.identifier.len() > 32
|
||||
{
|
||||
return Ok(HttpResponse::Forbidden().json(ResponseError {
|
||||
gorb_id_valid: false,
|
||||
..Default::default()
|
||||
}));
|
||||
}
|
||||
|
||||
// Password is expected to be hashed using SHA3-384
|
||||
let password_regex = Regex::new(r"[0-9a-f]{96}").unwrap();
|
||||
|
||||
if !password_regex.is_match(&account_information.password) {
|
||||
return Ok(HttpResponse::Forbidden().json(
|
||||
ResponseError {
|
||||
password_hashed: false,
|
||||
..Default::default()
|
||||
}
|
||||
))
|
||||
return Ok(HttpResponse::Forbidden().json(ResponseError {
|
||||
password_hashed: false,
|
||||
..Default::default()
|
||||
}));
|
||||
}
|
||||
|
||||
let salt = SaltString::generate(&mut OsRng);
|
||||
|
||||
if let Ok(hashed_password) = data.argon2.hash_password(account_information.password.as_bytes(), &salt) {
|
||||
if let Ok(hashed_password) = data
|
||||
.argon2
|
||||
.hash_password(account_information.password.as_bytes(), &salt)
|
||||
{
|
||||
// TODO: Check security of this implementation
|
||||
return Ok(match sqlx::query(&format!("INSERT INTO users (uuid, username, password, email) VALUES ( '{}', $1, $2, $3 )", uuid))
|
||||
return Ok(
|
||||
match sqlx::query(&format!(
|
||||
"INSERT INTO users (uuid, username, password, email) VALUES ( '{}', $1, $2, $3 )",
|
||||
uuid
|
||||
))
|
||||
.bind(account_information.identifier)
|
||||
// FIXME: Password has no security currently, either from a client or server perspective
|
||||
.bind(hashed_password.to_string())
|
||||
.bind(account_information.email)
|
||||
.execute(&data.pool)
|
||||
.await {
|
||||
.await
|
||||
{
|
||||
Ok(_out) => {
|
||||
let refresh_token = generate_refresh_token();
|
||||
let access_token = generate_access_token();
|
||||
|
||||
if refresh_token.is_err() {
|
||||
error!("{}", refresh_token.unwrap_err());
|
||||
return Ok(HttpResponse::InternalServerError().finish())
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
|
||||
let refresh_token = refresh_token.unwrap();
|
||||
|
||||
if access_token.is_err() {
|
||||
error!("{}", access_token.unwrap_err());
|
||||
return Ok(HttpResponse::InternalServerError().finish())
|
||||
return Ok(HttpResponse::InternalServerError().finish());
|
||||
}
|
||||
|
||||
let access_token = access_token.unwrap();
|
||||
|
||||
let current_time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs() as i64;
|
||||
let current_time = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.unwrap()
|
||||
.as_secs() as i64;
|
||||
|
||||
if let Err(error) = sqlx::query(&format!("INSERT INTO refresh_tokens (token, uuid, created, device_name) VALUES ($1, '{}', $2, $3 )", uuid))
|
||||
.bind(&refresh_token)
|
||||
|
@ -153,32 +167,37 @@ pub async fn res(mut payload: web::Payload, data: web::Data<Data>) -> Result<Htt
|
|||
return Ok(HttpResponse::InternalServerError().finish())
|
||||
}
|
||||
|
||||
HttpResponse::Ok().json(
|
||||
Response {
|
||||
access_token,
|
||||
refresh_token,
|
||||
}
|
||||
)
|
||||
},
|
||||
HttpResponse::Ok().json(Response {
|
||||
access_token,
|
||||
refresh_token,
|
||||
})
|
||||
}
|
||||
Err(error) => {
|
||||
let err_msg = error.as_database_error().unwrap().message();
|
||||
|
||||
|
||||
match err_msg {
|
||||
err_msg if err_msg.contains("unique") && err_msg.contains("username_key") => HttpResponse::Forbidden().json(ResponseError {
|
||||
gorb_id_available: false,
|
||||
..Default::default()
|
||||
}),
|
||||
err_msg if err_msg.contains("unique") && err_msg.contains("email_key") => HttpResponse::Forbidden().json(ResponseError {
|
||||
email_available: false,
|
||||
..Default::default()
|
||||
}),
|
||||
err_msg
|
||||
if err_msg.contains("unique") && err_msg.contains("username_key") =>
|
||||
{
|
||||
HttpResponse::Forbidden().json(ResponseError {
|
||||
gorb_id_available: false,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
err_msg if err_msg.contains("unique") && err_msg.contains("email_key") => {
|
||||
HttpResponse::Forbidden().json(ResponseError {
|
||||
email_available: false,
|
||||
..Default::default()
|
||||
})
|
||||
}
|
||||
_ => {
|
||||
error!("{}", err_msg);
|
||||
HttpResponse::InternalServerError().finish()
|
||||
}
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Ok(HttpResponse::InternalServerError().finish())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue