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,13 +1,16 @@
|
|||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use actix_web::{error, post, web, Error, HttpResponse};
|
||||
use actix_web::{Error, HttpResponse, error, post, web};
|
||||
use argon2::{PasswordHash, PasswordVerifier};
|
||||
use futures::StreamExt;
|
||||
use log::error;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use futures::StreamExt;
|
||||
|
||||
use crate::{crypto::{generate_access_token, generate_refresh_token}, Data};
|
||||
use crate::{
|
||||
Data,
|
||||
crypto::{generate_access_token, generate_refresh_token},
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct LoginInformation {
|
||||
|
@ -25,7 +28,10 @@ pub struct Response {
|
|||
const MAX_SIZE: usize = 262_144;
|
||||
|
||||
#[post("/login")]
|
||||
pub async fn response(mut payload: web::Payload, data: web::Data<Data>) -> Result<HttpResponse, Error> {
|
||||
pub async fn response(
|
||||
mut payload: web::Payload,
|
||||
data: web::Data<Data>,
|
||||
) -> Result<HttpResponse, Error> {
|
||||
let mut body = web::BytesMut::new();
|
||||
while let Some(chunk) = payload.next().await {
|
||||
let chunk = chunk?;
|
||||
|
@ -51,45 +57,82 @@ pub async fn response(mut payload: web::Payload, data: web::Data<Data>) -> Resul
|
|||
}
|
||||
|
||||
if email_regex.is_match(&login_information.username) {
|
||||
if let Ok(row) = sqlx::query_as("SELECT CAST(uuid as VARCHAR), password FROM users WHERE email = $1").bind(login_information.username).fetch_one(&data.pool).await {
|
||||
if let Ok(row) =
|
||||
sqlx::query_as("SELECT CAST(uuid as VARCHAR), password FROM users WHERE email = $1")
|
||||
.bind(login_information.username)
|
||||
.fetch_one(&data.pool)
|
||||
.await
|
||||
{
|
||||
let (uuid, password): (String, String) = row;
|
||||
return Ok(login(data.clone(), uuid, login_information.password, password, login_information.device_name).await)
|
||||
return Ok(login(
|
||||
data.clone(),
|
||||
uuid,
|
||||
login_information.password,
|
||||
password,
|
||||
login_information.device_name,
|
||||
)
|
||||
.await);
|
||||
}
|
||||
|
||||
return Ok(HttpResponse::Unauthorized().finish())
|
||||
return Ok(HttpResponse::Unauthorized().finish());
|
||||
} else if username_regex.is_match(&login_information.username) {
|
||||
if let Ok(row) = sqlx::query_as("SELECT CAST(uuid as VARCHAR), password FROM users WHERE username = $1").bind(login_information.username).fetch_one(&data.pool).await {
|
||||
if let Ok(row) =
|
||||
sqlx::query_as("SELECT CAST(uuid as VARCHAR), password FROM users WHERE username = $1")
|
||||
.bind(login_information.username)
|
||||
.fetch_one(&data.pool)
|
||||
.await
|
||||
{
|
||||
let (uuid, password): (String, String) = row;
|
||||
return Ok(login(data.clone(), uuid, login_information.password, password, login_information.device_name).await)
|
||||
return Ok(login(
|
||||
data.clone(),
|
||||
uuid,
|
||||
login_information.password,
|
||||
password,
|
||||
login_information.device_name,
|
||||
)
|
||||
.await);
|
||||
}
|
||||
|
||||
return Ok(HttpResponse::Unauthorized().finish())
|
||||
return Ok(HttpResponse::Unauthorized().finish());
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Unauthorized().finish())
|
||||
}
|
||||
|
||||
async fn login(data: actix_web::web::Data<Data>, uuid: String, request_password: String, database_password: String, device_name: String) -> HttpResponse {
|
||||
async fn login(
|
||||
data: actix_web::web::Data<Data>,
|
||||
uuid: String,
|
||||
request_password: String,
|
||||
database_password: String,
|
||||
device_name: String,
|
||||
) -> HttpResponse {
|
||||
if let Ok(parsed_hash) = PasswordHash::new(&database_password) {
|
||||
if data.argon2.verify_password(request_password.as_bytes(), &parsed_hash).is_ok() {
|
||||
if data
|
||||
.argon2
|
||||
.verify_password(request_password.as_bytes(), &parsed_hash)
|
||||
.is_ok()
|
||||
{
|
||||
let refresh_token = generate_refresh_token();
|
||||
let access_token = generate_access_token();
|
||||
|
||||
if refresh_token.is_err() {
|
||||
error!("{}", refresh_token.unwrap_err());
|
||||
return HttpResponse::InternalServerError().finish()
|
||||
return HttpResponse::InternalServerError().finish();
|
||||
}
|
||||
|
||||
let refresh_token = refresh_token.unwrap();
|
||||
|
||||
if access_token.is_err() {
|
||||
error!("{}", access_token.unwrap_err());
|
||||
return HttpResponse::InternalServerError().finish()
|
||||
return 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)
|
||||
|
@ -114,10 +157,10 @@ async fn login(data: actix_web::web::Data<Data>, uuid: String, request_password:
|
|||
return HttpResponse::Ok().json(Response {
|
||||
access_token,
|
||||
refresh_token,
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
return HttpResponse::Unauthorized().finish()
|
||||
return HttpResponse::Unauthorized().finish();
|
||||
}
|
||||
|
||||
HttpResponse::InternalServerError().finish()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue