mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-18 15:44:36 +00:00
ip address, duplicate record handling
This commit is contained in:
1 parent
03eee526a0
commit
215df96f03
5 files changed
+136
-29
No files matched your search
Generated
+45
-6
@@ -83,7 +83,7 @@ dependencies = [
|
||||
"actix-service",
|
||||
"actix-threadpool",
|
||||
"actix-utils 2.0.0",
|
||||
"base64",
|
||||
"base64 0.13.0",
|
||||
"bitflags",
|
||||
"brotli2",
|
||||
"bytes 0.5.6",
|
||||
@@ -572,7 +572,7 @@ dependencies = [
|
||||
"actix-http",
|
||||
"actix-rt 1.1.1",
|
||||
"actix-service",
|
||||
"base64",
|
||||
"base64 0.13.0",
|
||||
"bytes 0.5.6",
|
||||
"cfg-if 1.0.0",
|
||||
"derive_more",
|
||||
@@ -592,12 +592,30 @@ version = "0.2.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4521f3e3d031370679b3b140beb36dfe4801b09ac77e30c61941f97df3ef28b"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.12.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3441f0f7b02788e948e47f457ca01f1d7e6d92c693bc132c22b087d3141c03ff"
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd"
|
||||
|
||||
[[package]]
|
||||
name = "bcrypt"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2cab630912253fb9dc92c0e2fabd0a7b51f5a5a4007177cfa31e517015b7204"
|
||||
dependencies = [
|
||||
"base64 0.12.3",
|
||||
"blowfish",
|
||||
"byteorder",
|
||||
"getrandom 0.1.16",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
@@ -625,6 +643,15 @@ dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "block-cipher"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f337a3e6da609650eb74e02bc9fac7b735049f7623ab12f2e4c719316fcc7e80"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "blocking"
|
||||
version = "1.0.2"
|
||||
@@ -639,6 +666,17 @@ dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "blowfish"
|
||||
version = "0.6.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0f06850ba969bc59388b2cc0a4f186fc6d9d37208863b15b84ae3866ac90ac06"
|
||||
dependencies = [
|
||||
"block-cipher",
|
||||
"byteorder",
|
||||
"opaque-debug",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "brotli-sys"
|
||||
version = "0.3.2"
|
||||
@@ -1758,7 +1796,7 @@ version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd56cbd21fea48d0c440b41cd69c589faacade08c992d9a54e471b79d0fd13eb"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"base64 0.13.0",
|
||||
"once_cell",
|
||||
"regex",
|
||||
]
|
||||
@@ -2067,7 +2105,7 @@ version = "0.19.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "35edb675feee39aec9c99fa5ff985081995a06d594114ae14cbe797ad7b7a6d7"
|
||||
dependencies = [
|
||||
"base64",
|
||||
"base64 0.13.0",
|
||||
"log",
|
||||
"ring",
|
||||
"sct",
|
||||
@@ -2287,7 +2325,7 @@ checksum = "de52d1d473cebb2abb79c886ef6a8023e965e34c0676a99cfeac2cc7f0fde4c1"
|
||||
dependencies = [
|
||||
"ahash 0.7.2",
|
||||
"atoi",
|
||||
"base64",
|
||||
"base64 0.13.0",
|
||||
"bitflags",
|
||||
"byteorder",
|
||||
"bytes 1.0.1",
|
||||
@@ -2431,7 +2469,8 @@ dependencies = [
|
||||
"actix-utils 3.0.0",
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
"base64",
|
||||
"base64 0.13.0",
|
||||
"bcrypt",
|
||||
"chrono",
|
||||
"derive_more",
|
||||
"diesel",
|
||||
|
||||
@@ -15,6 +15,7 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
anyhow = "1.0.40"
|
||||
base64 = "0.13.0"
|
||||
bcrypt = "0.8"
|
||||
chrono = { version = "0.4.19", features = ["serde"] }
|
||||
derive_more = "0.99.14"
|
||||
easyenv = "0.0.5"
|
||||
|
||||
@@ -3,13 +3,17 @@ use actix_http::http::header;
|
||||
use actix_web::dev::HttpResponseBuilder;
|
||||
use actix_web::error::ResponseError;
|
||||
use actix_web::http::StatusCode;
|
||||
use actix_web::{Responder, web, HttpResponse, error};
|
||||
use crate::endpoints::users::create_account::CreateAccountError::BadInput;
|
||||
use actix_web::{Responder, web, HttpResponse, error, HttpRequest};
|
||||
use crate::endpoints::users::create_account::CreateAccountError::{BadInput, ServerError, UsernameTaken, EmailTaken};
|
||||
use crate::server_state::ServerState;
|
||||
use crate::util::random::random_token;
|
||||
use derive_more::{Display, Error};
|
||||
use log::{info, warn, log};
|
||||
use std::sync::Arc;
|
||||
use crate::util::ip_address::get_request_ip;
|
||||
use sqlx::mysql::MySqlDatabaseError;
|
||||
use sqlx::error::DatabaseError;
|
||||
use sqlx::error::Error::Database;
|
||||
|
||||
const NEW_USER_ROLE: &'static str = "new-user";
|
||||
|
||||
@@ -35,6 +39,8 @@ pub struct ErrorResponse {
|
||||
#[derive(Debug, Display)]
|
||||
pub enum CreateAccountError {
|
||||
BadInput(String),
|
||||
UsernameTaken,
|
||||
EmailTaken,
|
||||
ServerError,
|
||||
}
|
||||
|
||||
@@ -42,6 +48,8 @@ impl ResponseError for CreateAccountError {
|
||||
fn status_code(&self) -> StatusCode {
|
||||
match *self {
|
||||
CreateAccountError::BadInput(_) => StatusCode::BAD_REQUEST,
|
||||
CreateAccountError::UsernameTaken => StatusCode::BAD_REQUEST,
|
||||
CreateAccountError::EmailTaken => StatusCode::BAD_REQUEST,
|
||||
CreateAccountError::ServerError => StatusCode::INTERNAL_SERVER_ERROR,
|
||||
}
|
||||
}
|
||||
@@ -49,6 +57,8 @@ impl ResponseError for CreateAccountError {
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
let error_reason = match self {
|
||||
BadInput(reason) => reason.to_string(),
|
||||
CreateAccountError::UsernameTaken => "username already taken".to_string(),
|
||||
CreateAccountError::EmailTaken => "email already taken".to_string(),
|
||||
CreateAccountError::ServerError => "server error".to_string(),
|
||||
};
|
||||
|
||||
@@ -69,6 +79,7 @@ impl ResponseError for CreateAccountError {
|
||||
}
|
||||
|
||||
pub async fn create_account_handler(
|
||||
http_request: HttpRequest,
|
||||
request: web::Json<CreateAccountRequest>,
|
||||
server_state: web::Data<Arc<ServerState>>) -> Result<HttpResponse, CreateAccountError>
|
||||
{
|
||||
@@ -88,13 +99,22 @@ pub async fn create_account_handler(
|
||||
return Err(CreateAccountError::BadInput("invalid email address".to_string()));
|
||||
}
|
||||
|
||||
let token = random_token(32);
|
||||
let password_hash = "temp";
|
||||
//let token = random_token(32);
|
||||
let token = random_token(15);
|
||||
|
||||
let password_hash = match bcrypt::hash(&request.password, bcrypt::DEFAULT_COST) {
|
||||
Ok(hash) => hash,
|
||||
Err(err) => {
|
||||
warn!("Bcrypt error: {:?}", err);
|
||||
return Err(ServerError);
|
||||
}
|
||||
};
|
||||
|
||||
let profile_markdown = "";
|
||||
let profile_rendered_html = "";
|
||||
let ip_address = "1.1.1.1";
|
||||
let ip_address = get_request_ip(&http_request);
|
||||
|
||||
let record_id = sqlx::query!(
|
||||
let result = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO users (
|
||||
token,
|
||||
@@ -124,25 +144,39 @@ VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
|
||||
ip_address.to_string(),
|
||||
)
|
||||
.execute(&server_state.mysql_pool)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
warn!("Query error: {:?}", e);
|
||||
return CreateAccountError::ServerError;
|
||||
})?
|
||||
.last_insert_id();
|
||||
.await;
|
||||
|
||||
let record_id = match result {
|
||||
Ok(res) => {
|
||||
res.last_insert_id()
|
||||
},
|
||||
Err(err) => {
|
||||
warn!("Query error: {:?}", err);
|
||||
|
||||
// NB: SQLSTATE[23000]: Integrity constraint violation
|
||||
// NB: MySQL Error Code 1062: Duplicate key insertion (this is harder to access)
|
||||
match err {
|
||||
Database(err) => {
|
||||
let maybe_code = err.code().map(|c| c.into_owned());
|
||||
match maybe_code.as_deref() {
|
||||
Some("23000") => {
|
||||
if err.message().contains("username") {
|
||||
return Err(UsernameTaken);
|
||||
} else if err.message().contains("email_address") {
|
||||
return Err(EmailTaken);
|
||||
}
|
||||
}
|
||||
_ => {},
|
||||
}
|
||||
},
|
||||
_ => {},
|
||||
}
|
||||
return Err(ServerError);
|
||||
}
|
||||
};
|
||||
|
||||
info!("new user id: {}", record_id);
|
||||
|
||||
//Ok(record_id)
|
||||
/*Ok(web::Json())*/
|
||||
|
||||
if true {
|
||||
//return Err(error::ErrorBadRequest(CreateAccountResponse { success: false } ));
|
||||
//return Err(CreateAccountResponseError::UsernameExists("foo".to_string()));
|
||||
}
|
||||
|
||||
// .map_err(error::ErrorInternalServerError)?;
|
||||
// error::ErrorBadRequest(msg)
|
||||
let response = CreateAccountSuccessResponse {
|
||||
success: true,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
use actix_web::HttpRequest;
|
||||
use actix_web::http::HeaderName;
|
||||
use log::{info};
|
||||
|
||||
pub fn get_request_ip(request: &HttpRequest) -> String {
|
||||
match request.headers().get(HeaderName::from_static("x-voder-proxy-for")) {
|
||||
Some(ip_address) => {
|
||||
// Unfortunately the upstream Rust proxy is replacing the `forwarded` and `x-forwarded-for`
|
||||
// headers, so we populate this custom header as a workaround.
|
||||
info!("Proxied IP address: {:?}", ip_address);
|
||||
ip_address.to_str()
|
||||
.unwrap_or("")
|
||||
.to_string()
|
||||
},
|
||||
None => {
|
||||
// If we're running without the upstream Rust proxy, we can grab 'x-forarded-for', which is
|
||||
// populated by the DigitalOcean load balancer.
|
||||
let ip_address_and_port = request.connection_info()
|
||||
.remote_addr()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let ip_address = ip_address_and_port.split(":")
|
||||
.collect::<Vec<&str>>()
|
||||
.get(0)
|
||||
.copied()
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
info!("Forwarded IP address: {}", &ip_address);
|
||||
ip_address
|
||||
},
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -1 +1,2 @@
|
||||
pub mod random;
|
||||
pub mod random;
|
||||
pub mod ip_address;
|
||||
Reference in new issue
Block a user