mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-03-14 13:08:27 +03:00
mcaptcha token generation unique constration err handling
This commit is contained in:
parent
08ec215709
commit
36b505ef59
2 changed files with 32 additions and 30 deletions
|
@ -14,6 +14,7 @@
|
|||
* 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/>.
|
||||
*/
|
||||
use std::borrow::Cow;
|
||||
|
||||
use actix_identity::Identity;
|
||||
use actix_web::{post, web, HttpResponse, Responder};
|
||||
|
@ -38,28 +39,43 @@ pub struct MCaptchaDetails {
|
|||
pub async fn add_mcaptcha(data: web::Data<Data>, id: Identity) -> ServiceResult<impl Responder> {
|
||||
is_authenticated(&id)?;
|
||||
let username = id.identity().unwrap();
|
||||
let key = get_random(32);
|
||||
let mut key;
|
||||
|
||||
let res = sqlx::query!(
|
||||
"INSERT INTO mcaptcha_config
|
||||
let resp;
|
||||
|
||||
loop {
|
||||
key = get_random(32);
|
||||
|
||||
let res = sqlx::query!(
|
||||
"INSERT INTO mcaptcha_config
|
||||
(key, user_id)
|
||||
VALUES ($1, (SELECT ID FROM mcaptcha_users WHERE name = $2))",
|
||||
&key,
|
||||
&username,
|
||||
)
|
||||
.execute(&data.db)
|
||||
.await;
|
||||
&key,
|
||||
&username,
|
||||
)
|
||||
.execute(&data.db)
|
||||
.await;
|
||||
|
||||
match res {
|
||||
Err(e) => {
|
||||
println!("{}", &e);
|
||||
Err(dup_error(e, ServiceError::TokenNameTaken))
|
||||
}
|
||||
Ok(_) => {
|
||||
let resp = MCaptchaDetails { key, name: None };
|
||||
Ok(HttpResponse::Ok().json(resp))
|
||||
match res {
|
||||
Err(sqlx::Error::Database(err)) => {
|
||||
if err.code() == Some(Cow::from("23505"))
|
||||
&& err.message().contains("mcaptcha_config_key_key")
|
||||
{
|
||||
continue;
|
||||
} else {
|
||||
Err(sqlx::Error::Database(err))?;
|
||||
}
|
||||
}
|
||||
Err(e) => Err(e)?,
|
||||
|
||||
Ok(_) => {
|
||||
resp = MCaptchaDetails { key, name: None };
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(HttpResponse::Ok().json(resp))
|
||||
}
|
||||
|
||||
#[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))]
|
||||
pub type ServiceResult<V> = std::result::Result<V, ServiceError>;
|
||||
|
|
Loading…
Add table
Reference in a new issue