mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-12 02:25:41 +00:00
feat: impl auth challenges interfaces for mariadb
This commit is contained in:
@@ -95,6 +95,22 @@ impl Migrate for Database {
|
||||
.run(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DBError::DBError(Box::new(e)))?;
|
||||
|
||||
for reason in [
|
||||
ChallengeReason::EmailVerification,
|
||||
ChallengeReason::PasswordReset,
|
||||
] {
|
||||
sqlx::query!(
|
||||
"INSERT IGNORE INTO
|
||||
mcaptcha_challenge_reason (name)
|
||||
VALUES (?)",
|
||||
reason.to_str()
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await
|
||||
.map_err(|e| DBError::DBError(Box::new(e)))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -895,6 +911,73 @@ impl MCDatabase for Database {
|
||||
|
||||
Ok(Date::dates_to_unix(records))
|
||||
}
|
||||
|
||||
/// Record challenge in database
|
||||
async fn new_challenge(&self, challenge: &mut Challenge) -> DBResult<()> {
|
||||
let now = now_unix_time_stamp();
|
||||
loop {
|
||||
let res = sqlx::query!(
|
||||
"INSERT INTO mcaptcha_challenge (challenge_id, received, reason)
|
||||
VALUES (?, ?, (SELECT id FROM mcaptcha_challenge_reason WHERE name = ?));
|
||||
",
|
||||
&challenge.challenge.to_string(),
|
||||
now,
|
||||
challenge.reason.to_str()
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await;
|
||||
if let Err(Error::Database(err)) = res {
|
||||
use std::borrow::Cow;
|
||||
|
||||
if err.code() == Some(Cow::from("23505")) {
|
||||
let msg = err.message();
|
||||
if msg.contains("for key 'challenge_id'") {
|
||||
challenge.new_id();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Record challenge in database
|
||||
async fn fetch_challenge(&self, challenge: &Challenge) -> DBResult<Challenge> {
|
||||
struct C {
|
||||
id: i32,
|
||||
}
|
||||
sqlx::query_as!(
|
||||
C,
|
||||
"SELECT id
|
||||
FROM mcaptcha_challenge
|
||||
WHERE
|
||||
challenge_id = ?
|
||||
AND reason = (SELECT id FROM mcaptcha_challenge_reason WHERE name = ?);",
|
||||
&challenge.challenge.to_string(),
|
||||
challenge.reason.to_str(),
|
||||
)
|
||||
.fetch_one(&self.pool)
|
||||
.await
|
||||
.map_err(map_register_err)?;
|
||||
Ok(challenge.clone())
|
||||
}
|
||||
|
||||
/// Delete a challenge from database
|
||||
async fn delete_challenge(&self, challenge: &Challenge) -> DBResult<()> {
|
||||
let _ = sqlx::query!(
|
||||
"DELETE
|
||||
FROM mcaptcha_challenge
|
||||
WHERE
|
||||
challenge_id = ?
|
||||
AND reason = (SELECT id FROM mcaptcha_challenge_reason WHERE name = ?);",
|
||||
&challenge.challenge.to_string(),
|
||||
challenge.reason.to_str(),
|
||||
)
|
||||
.execute(&self.pool)
|
||||
.await;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
|
||||
@@ -89,4 +89,5 @@ async fn everyting_works() {
|
||||
description: CAPTCHA_DESCRIPTION,
|
||||
};
|
||||
database_works(&db, &p, &c, &LEVELS, &TRAFFIC_PATTERN, &ADD_NOTIFICATION).await;
|
||||
challenges_works(&db).await;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user