random token generation post PoW verification

This commit is contained in:
Aravinth Manivannan 2021-04-10 11:40:59 +05:30
parent 3941284890
commit 6f83b3cd0c
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
7 changed files with 42 additions and 11 deletions

View file

@ -5,12 +5,14 @@
- `HashCache` was extended to cache site keys when caching `PoW` configurations - `HashCache` was extended to cache site keys when caching `PoW` configurations
as a result: as a result:
- <strike>`Retrieve`</strike> `RetrievePoW` now returns `CachedPoWConfig` - <strike>`Retrieve`</strike> `RetrievePoW` now returns `CachedPoWConfig`
- random token generation post `PoW` verification
## Changed ## Changed
- `Cache` became `CachePoW` (`HashCache` extension) - `Cache` became `CachePoW` (`HashCache` extension)
- `Retrieve` became `RetrievePoW`(`HashCache` extension) - `Retrieve` became `RetrievePoW`(`HashCache` extension)
- `DeleteString` became `DeletePoW` (`HashCache` extension) - `DeleteString` became `DeletePoW` (`HashCache` extension)
- `Save` trait now requires three new message impls (`HashCache` extension_ - `Save` trait now requires three new message impls (`HashCache` extension_
- `System.verify_pow` now returns a `String` instead of `bool`
## Removed ## Removed
- `CachePoW` constructor was removed in favour of `CachwPoWBuilder` - `CachePoW` constructor was removed in favour of `CachwPoWBuilder`

View file

@ -109,8 +109,9 @@ async fn main() -> std::io::Result<()> {
// Server evaluates client's work. Returns true if everything // Server evaluates client's work. Returns true if everything
// checksout and Err() if something fishy is happening // checksout and Err() if something fishy is happening
let res = system.verify_pow(payload.clone()).await.unwrap(); let res = system.verify_pow(payload.clone()).await;
assert!(res); assert!(res.is_ok());
// TODO add server-sideverification
Ok(()) Ok(())
} }

View file

@ -38,6 +38,7 @@ impl HashCache {
let config: CachedPoWConfig = CachedPoWConfig { let config: CachedPoWConfig = CachedPoWConfig {
key: config.key, key: config.key,
difficulty_factor: config.difficulty_factor, difficulty_factor: config.difficulty_factor,
duration: config.duration,
}; };
self.difficulty_map.insert(challenge, config); self.difficulty_map.insert(challenge, config);

14
src/cache/mod.rs vendored
View file

@ -72,6 +72,7 @@ pub mod messages {
pub struct CachedPoWConfig { pub struct CachedPoWConfig {
pub key: String, pub key: String,
pub difficulty_factor: u32, pub difficulty_factor: u32,
pub duration: u64,
} }
/// Message to delete cached PoW difficulty factor and string /// Message to delete cached PoW difficulty factor and string
@ -91,6 +92,19 @@ pub mod messages {
pub duration: u64, pub duration: u64,
} }
impl From<CachedPoWConfig> for CacheResult {
fn from(c: CachedPoWConfig) -> Self {
use crate::utils::get_random;
CacheResultBuilder::default()
.key(c.key)
.duration(c.duration)
.result(get_random(32))
.build()
.unwrap()
}
}
/// Message to verify captcha result against /// Message to verify captcha result against
/// the stored captcha key /// the stored captcha key
#[derive(Message)] #[derive(Message)]

View file

@ -73,6 +73,10 @@ pub enum CaptchaError {
#[display(fmt = "PoW computed over configuration not intended for target sitekey")] #[display(fmt = "PoW computed over configuration not intended for target sitekey")]
MCaptchaKeyValidationFail, MCaptchaKeyValidationFail,
/// Submitted PoW is invalid
#[display(fmt = "Invalid PoW")]
InvalidPoW,
/// Used in builder structs when a value is not set /// Used in builder structs when a value is not set
#[display(fmt = "Please set value: {}", _0)] #[display(fmt = "Please set value: {}", _0)]
PleaseSetValue(#[error(not(source))] String), PleaseSetValue(#[error(not(source))] String),

View file

@ -157,8 +157,8 @@
//! //!
//! // Server evaluates client's work. Returns true if everything //! // Server evaluates client's work. Returns true if everything
//! // checksout and Err() if something fishy is happening //! // checksout and Err() if something fishy is happening
//! let res = system.verify_pow(payload.clone()).await.unwrap(); //! let res = system.verify_pow(payload.clone()).await;
//! assert!(res); //! assert!(res.is_ok());
//! //!
//! Ok(()) //! Ok(())
//! } //! }

View file

@ -37,8 +37,10 @@ pub struct System<T: Save> {
impl<T> System<T> impl<T> System<T>
where where
T: Save, T: Save,
<T as actix::Actor>::Context: <T as actix::Actor>::Context: ToEnvelope<T, messages::CachePoW>
ToEnvelope<T, messages::CachePoW> + ToEnvelope<T, messages::RetrivePoW>, + ToEnvelope<T, messages::RetrivePoW>
+ ToEnvelope<T, messages::CacheResult>
+ ToEnvelope<T, messages::VerifyCaptchaResult>,
{ {
/// utility function to get difficulty factor of site `id` and cache it /// utility function to get difficulty factor of site `id` and cache it
pub async fn get_pow(&self, id: String) -> Option<PoWConfig> { pub async fn get_pow(&self, id: String) -> Option<PoWConfig> {
@ -68,8 +70,8 @@ where
} }
/// utility function to verify [Work] /// utility function to verify [Work]
pub async fn verify_pow(&self, work: Work) -> CaptchaResult<bool> { pub async fn verify_pow(&self, work: Work) -> CaptchaResult<String> {
use crate::cache::messages::RetrivePoW; use crate::cache::messages::*;
let string = work.string.clone(); let string = work.string.clone();
let msg = RetrivePoW(string.clone()); let msg = RetrivePoW(string.clone());
@ -95,7 +97,14 @@ where
return Err(CaptchaError::InsuffiencientDifficulty); return Err(CaptchaError::InsuffiencientDifficulty);
} }
Ok(self.pow.is_valid_proof(&pow, &string)) if !self.pow.is_valid_proof(&pow, &string) {
return Err(CaptchaError::InvalidPoW);
}
let msg: CacheResult = cached_config.into();
let res = msg.result.clone();
self.cache.send(msg).await.unwrap()?;
Ok(res)
} }
} }
@ -164,8 +173,8 @@ mod tests {
key: MCAPTCHA_NAME.into(), key: MCAPTCHA_NAME.into(),
}; };
let res = actors.verify_pow(payload.clone()).await.unwrap(); let res = actors.verify_pow(payload.clone()).await;
assert!(res); assert!(res.is_ok());
payload.string = "wrongstring".into(); payload.string = "wrongstring".into();
let res = actors.verify_pow(payload.clone()).await; let res = actors.verify_pow(payload.clone()).await;