mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2025-03-29 04:38:59 +03:00
feat: implement create captcha for sqlx postgres
This commit is contained in:
parent
00dca4a069
commit
d64b05c84f
3 changed files with 30 additions and 1 deletions
|
@ -33,6 +33,8 @@ pub fn map_register_err(e: Error) -> DBError {
|
||||||
DBError::EmailTaken
|
DBError::EmailTaken
|
||||||
} else if msg.contains("mcaptcha_users_secret_key") {
|
} else if msg.contains("mcaptcha_users_secret_key") {
|
||||||
DBError::SecretTaken
|
DBError::SecretTaken
|
||||||
|
} else if msg.contains("mcaptcha_config_key_key") {
|
||||||
|
DBError::CaptchaKeyTaken
|
||||||
} else {
|
} else {
|
||||||
DBError::DBError(Box::new(Error::Database(err)))
|
DBError::DBError(Box::new(Error::Database(err)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -278,6 +278,23 @@ impl MCDatabase for Database {
|
||||||
.map_err(map_register_err)?;
|
.map_err(map_register_err)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// create new captcha
|
||||||
|
async fn create_captcha(&self, username: &str, p: &CreateCaptcha) -> DBResult<()> {
|
||||||
|
sqlx::query!(
|
||||||
|
"INSERT INTO mcaptcha_config
|
||||||
|
(key, user_id, duration, name)
|
||||||
|
VALUES ($1, (SELECT ID FROM mcaptcha_users WHERE name = $2), $3, $4)",
|
||||||
|
p.key,
|
||||||
|
username,
|
||||||
|
p.duration as i32,
|
||||||
|
p.description,
|
||||||
|
)
|
||||||
|
.execute(&self.pool)
|
||||||
|
.await
|
||||||
|
.map_err(map_register_err)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn now_unix_time_stamp() -> i64 {
|
fn now_unix_time_stamp() -> i64 {
|
||||||
|
|
|
@ -29,6 +29,10 @@ async fn everyting_works() {
|
||||||
const NAME: &str = "postgresuser";
|
const NAME: &str = "postgresuser";
|
||||||
const PASSWORD: &str = "pasdfasdfasdfadf";
|
const PASSWORD: &str = "pasdfasdfasdfadf";
|
||||||
const SECRET1: &str = "postgressecret1";
|
const SECRET1: &str = "postgressecret1";
|
||||||
|
// captcha config
|
||||||
|
const CAPTCHA_SECRET: &str = "postgrescaptchasecret";
|
||||||
|
const CAPTCHA_DESCRIPTION: &str = "postgrescaptchadescription";
|
||||||
|
const CAPTCHA_DURATION: i32 = 30;
|
||||||
|
|
||||||
let url = env::var("POSTGRES_DATABASE_URL").unwrap();
|
let url = env::var("POSTGRES_DATABASE_URL").unwrap();
|
||||||
let pool_options = PgPoolOptions::new().max_connections(2);
|
let pool_options = PgPoolOptions::new().max_connections(2);
|
||||||
|
@ -42,5 +46,11 @@ async fn everyting_works() {
|
||||||
hash: PASSWORD,
|
hash: PASSWORD,
|
||||||
secret: SECRET1,
|
secret: SECRET1,
|
||||||
};
|
};
|
||||||
database_works(&db, &p).await;
|
|
||||||
|
let c = CreateCaptcha {
|
||||||
|
duration: CAPTCHA_DURATION,
|
||||||
|
key: CAPTCHA_SECRET,
|
||||||
|
description: CAPTCHA_DESCRIPTION,
|
||||||
|
};
|
||||||
|
database_works(&db, &p, &c).await;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue