mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 10:05:41 +00:00
mcaptcha token generation unique constration err handling
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
* You should have received a copy of the GNU Affero General Public License
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
use std::borrow::Cow;
|
||||||
|
|
||||||
use actix_identity::Identity;
|
use actix_identity::Identity;
|
||||||
use actix_web::{post, web, HttpResponse, Responder};
|
use actix_web::{post, web, HttpResponse, Responder};
|
||||||
@@ -38,7 +39,12 @@ pub struct MCaptchaDetails {
|
|||||||
pub async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
|
pub async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
|
||||||
is_authenticated(&id)?;
|
is_authenticated(&id)?;
|
||||||
let username = id.identity().unwrap();
|
let username = id.identity().unwrap();
|
||||||
let key = get_random(32);
|
let mut key;
|
||||||
|
|
||||||
|
let resp;
|
||||||
|
|
||||||
|
loop {
|
||||||
|
key = get_random(32);
|
||||||
|
|
||||||
let res = sqlx::query!(
|
let res = sqlx::query!(
|
||||||
"INSERT INTO mcaptcha_config
|
"INSERT INTO mcaptcha_config
|
||||||
@@ -51,15 +57,25 @@ pub async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<
|
|||||||
.await;
|
.await;
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Err(e) => {
|
Err(sqlx::Error::Database(err)) => {
|
||||||
println!("{}", &e);
|
if err.code() == Some(Cow::from("23505"))
|
||||||
Err(dup_error(e, ServiceError::TokenNameTaken))
|
&& err.message().contains("mcaptcha_config_key_key")
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
Err(sqlx::Error::Database(err))?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Err(e) => Err(e)?,
|
||||||
|
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
let resp = MCaptchaDetails { key, name: None };
|
resp = MCaptchaDetails { key, name: None };
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(HttpResponse::Ok().json(resp))
|
Ok(HttpResponse::Ok().json(resp))
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/api/v1/mcaptcha/update/key")]
|
#[post("/api/v1/mcaptcha/update/key")]
|
||||||
|
|||||||
@@ -196,19 +196,5 @@ impl From<sqlx::Error> for ServiceError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dup_error(e: sqlx::Error, dup_error: ServiceError) -> ServiceError {
|
|
||||||
use sqlx::error::Error;
|
|
||||||
use std::borrow::Cow;
|
|
||||||
if let Error::Database(err) = e {
|
|
||||||
if err.code() == Some(Cow::from("23505")) {
|
|
||||||
dup_error
|
|
||||||
} else {
|
|
||||||
ServiceError::InternalServerError
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
ServiceError::InternalServerError
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(tarpaulin_include))]
|
#[cfg(not(tarpaulin_include))]
|
||||||
pub type ServiceResult<V> = std::result::Result<V, ServiceError>;
|
pub type ServiceResult<V> = std::result::Result<V, ServiceError>;
|
||||||
|
|||||||
Reference in New Issue
Block a user