fix: associate challenges with usernames

This commit is contained in:
Aravinth Manivannan
2023-06-17 16:13:48 +05:30
parent e7b01a5b06
commit 6f029d2945
10 changed files with 193 additions and 111 deletions

View File

@@ -136,6 +136,15 @@ impl FromStr for ChallengeReason {
}
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
/// Minimal user representation for use in challenge verification
pub struct ChallengeUser {
/// username of the user
pub username: String,
/// email ID of the user
pub email: String,
}
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)]
/// Email challenge
pub struct Challenge {
@@ -313,10 +322,14 @@ pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
async fn fetch_confirm(&self, user: &str, key: &str) -> DBResult<Vec<i64>>;
/// Record challenge in database
async fn new_challenge(&self, challenge: &mut Challenge) -> DBResult<()>;
async fn new_challenge(&self, user: &str, challenge: &mut Challenge)
-> DBResult<()>;
/// Record challenge in database
async fn fetch_challenge(&self, challenge: &Challenge) -> DBResult<Challenge>;
async fn fetch_challenge_user(
&self,
challenge: &Challenge,
) -> DBResult<ChallengeUser>;
/// Delete a challenge from database
async fn delete_challenge(&self, challenge: &Challenge) -> DBResult<()>;

View File

@@ -295,15 +295,13 @@ pub async fn database_works<'a, T: MCDatabase>(
// delete captcha; updated key = p.username so invoke delete with it
db.delete_captcha(p.username, p.username).await.unwrap();
assert!(!db.captcha_exists(Some(p.username), c.key).await.unwrap());
}
/// test all challenge routines
pub async fn challenges_works<'a, T: MCDatabase>(db: &T) {
let mut challenge = Challenge::new(ChallengeReason::PasswordReset);
db.new_challenge(&mut challenge).await.unwrap();
db.new_challenge(&mut challenge).await.unwrap();
let c = db.fetch_challenge(&challenge).await.unwrap();
assert_eq!(c, challenge);
db.new_challenge(p.username, &mut challenge).await.unwrap();
db.new_challenge(p.username, &mut challenge).await.unwrap();
let c = db.fetch_challenge_user(&challenge).await.unwrap();
assert_eq!(c.username, p.username);
assert_eq!(&c.email, p.email.as_ref().unwrap());
db.delete_challenge(&challenge).await.unwrap();
assert!(db.fetch_challenge(&challenge).await.is_err())
assert!(db.fetch_challenge_user(&challenge).await.is_err())
}