diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4195385..74ddbb6 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
as a result:
- `Retrieve` `RetrievePoW` now returns `CachedPoWConfig`
- random token generation post `PoW` verification
+- token validation
## Changed
- `Cache` became `CachePoW` (`HashCache` extension)
diff --git a/examples/simple.rs b/examples/simple.rs
index 7735121..0fdfb31 100644
--- a/examples/simple.rs
+++ b/examples/simple.rs
@@ -1,5 +1,5 @@
use m_captcha::{
- cache::HashCache,
+ cache::{messages::VerifyCaptchaResult, HashCache},
master::{AddSiteBuilder, Master},
pow::{ConfigBuilder, Work},
system::SystemBuilder,
@@ -107,11 +107,29 @@ async fn main() -> std::io::Result<()> {
key: mcaptcha_name.into(),
};
- // Server evaluates client's work. Returns true if everything
+ // mCAptcha evaluates client's work. Returns a token if everything
// checksout and Err() if something fishy is happening
let res = system.verify_pow(payload.clone()).await;
assert!(res.is_ok());
- // TODO add server-sideverification
+
+ // The client should submit the token to the mCaptcha protected service
+ // The service should validate the token received from the client
+ // with the mCaptcha server before processing client's
+ // request
+
+ // mcaptcha protected service sends the following paylaod to mCaptcha
+ // server:
+ let verify_msg = VerifyCaptchaResult {
+ token: res.unwrap(),
+ key: mcaptcha_name.into(),
+ };
+
+ // on mCaptcha server:
+ let res = system.validate_verification_tokens(verify_msg).await;
+ // mCaptcha will return true if token is valid and false if
+ // token is invalid
+ assert!(res.is_ok());
+ assert!(res.unwrap());
Ok(())
}
diff --git a/src/cache/hashcache.rs b/src/cache/hashcache.rs
index 0d1ce16..f13e257 100644
--- a/src/cache/hashcache.rs
+++ b/src/cache/hashcache.rs
@@ -61,13 +61,13 @@ impl HashCache {
// save captcha result
fn save_captcha_result(&mut self, res: CacheResult) -> CaptchaResult<()> {
- self.result_map.insert(res.result, res.key);
+ self.result_map.insert(res.token, res.key);
Ok(())
}
// verify captcha result
fn verify_captcha_result(&mut self, challenge: VerifyCaptchaResult) -> CaptchaResult {
- if let Some(captcha_id) = self.remove_cache_result(&challenge.result) {
+ if let Some(captcha_id) = self.remove_cache_result(&challenge.token) {
if captcha_id == challenge.key {
return Ok(true);
} else {
@@ -141,7 +141,7 @@ impl Handler for HashCache {
let addr = ctx.address();
let del_msg = DeleteCaptchaResult {
- result: msg.result.clone(),
+ token: msg.token.clone(),
};
let duration: Duration = Duration::new(msg.duration.clone(), 0);
@@ -161,7 +161,7 @@ impl Handler for HashCache {
impl Handler for HashCache {
type Result = MessageResult;
fn handle(&mut self, msg: DeleteCaptchaResult, _ctx: &mut Self::Context) -> Self::Result {
- self.remove_cache_result(&msg.result);
+ self.remove_cache_result(&msg.token);
MessageResult(Ok(()))
}
}
@@ -252,7 +252,7 @@ mod tests {
let add_cache = CacheResult {
key: KEY.into(),
- result: RES.into(),
+ token: RES.into(),
duration: DURATION,
};
@@ -260,14 +260,14 @@ mod tests {
let verify_msg = VerifyCaptchaResult {
key: KEY.into(),
- result: RES.into(),
+ token: RES.into(),
};
assert!(addr.send(verify_msg).await.unwrap().unwrap());
let verify_msg = VerifyCaptchaResult {
key: "cz".into(),
- result: RES.into(),
+ token: RES.into(),
};
assert!(!addr.send(verify_msg).await.unwrap().unwrap());
@@ -276,7 +276,7 @@ mod tests {
let verify_msg = VerifyCaptchaResult {
key: KEY.into(),
- result: RES.into(),
+ token: RES.into(),
};
assert!(!addr.send(verify_msg).await.unwrap().unwrap());
}
diff --git a/src/cache/mod.rs b/src/cache/mod.rs
index 582c7fc..fd20d0c 100644
--- a/src/cache/mod.rs
+++ b/src/cache/mod.rs
@@ -42,7 +42,7 @@ pub mod messages {
use crate::errors::*;
/// Message to cache PoW difficulty factor and string
- #[derive(Message, Serialize, Deserialize, Builder)]
+ #[derive(Message, Serialize, Deserialize, Builder, Clone)]
#[rtype(result = "CaptchaResult<()>")]
pub struct CachePoW {
pub string: String,
@@ -68,7 +68,7 @@ pub mod messages {
#[rtype(result = "CaptchaResult