-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only require captcha if threshold rate is exceeded (#2638)
* Only require captcha if threshold rate is exceeded This PR enables the dynamic captcha feature: I.e. if the captcha is configured to be dynamic _and_ the configured threshold has been exceeded, then require a captcha to be solved. Otherwise skip the captcha entirely. * Implement review feedback
- Loading branch information
Frederik Rothenberger
authored
Oct 2, 2024
1 parent
33114b7
commit ee07a98
Showing
6 changed files
with
171 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/internet_identity/tests/integration/v2_api/identity_register/dynamic_captcha.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use crate::v2_api::authn_method_test_helpers::{ | ||
create_identity_with_authn_method, test_authn_method, | ||
}; | ||
use canister_tests::api::internet_identity::api_v2; | ||
use canister_tests::framework::{ | ||
arg_with_dynamic_captcha, env, install_ii_canister_with_arg, test_principal, II_WASM, | ||
}; | ||
use internet_identity_interface::internet_identity::types::RegistrationFlowNextStep; | ||
use std::time::Duration; | ||
|
||
#[test] | ||
fn should_not_require_captcha_below_threshold_rate() { | ||
let env = env(); | ||
let canister_id = | ||
install_ii_canister_with_arg(&env, II_WASM.clone(), arg_with_dynamic_captcha()); | ||
let authn_method = test_authn_method(); | ||
|
||
let flow_principal = test_principal(0); | ||
let result = api_v2::identity_registration_start(&env, canister_id, flow_principal) | ||
.expect("API call failed") | ||
.expect("registration start failed"); | ||
|
||
assert!(matches!(result.next_step, RegistrationFlowNextStep::Finish)); | ||
|
||
api_v2::identity_registration_finish(&env, canister_id, flow_principal, &authn_method) | ||
.expect("API call failed") | ||
.expect("registration finish failed"); | ||
} | ||
|
||
#[test] | ||
fn should_require_captcha_above_threshold_rate() { | ||
let env = env(); | ||
let canister_id = | ||
install_ii_canister_with_arg(&env, II_WASM.clone(), arg_with_dynamic_captcha()); | ||
let authn_method = test_authn_method(); | ||
|
||
// initialize a base rate of one registration every 2 seconds | ||
for _ in 0..10 { | ||
create_identity_with_authn_method(&env, canister_id, &authn_method); | ||
env.advance_time(Duration::from_secs(2)) | ||
} | ||
|
||
// Double the rate of registrations to one per second | ||
// The 20% threshold rate should allow 5 registrations before the captcha kicks in | ||
for i in 0..5 { | ||
let flow_principal = test_principal(i); | ||
let result = api_v2::identity_registration_start(&env, canister_id, flow_principal) | ||
.expect("API call failed") | ||
.expect("registration start failed"); | ||
|
||
assert!(matches!(result.next_step, RegistrationFlowNextStep::Finish)); | ||
|
||
api_v2::identity_registration_finish(&env, canister_id, flow_principal, &authn_method) | ||
.expect("API call failed") | ||
.expect("registration finish failed"); | ||
env.advance_time(Duration::from_secs(1)); | ||
} | ||
|
||
let result = api_v2::identity_registration_start(&env, canister_id, test_principal(99)) | ||
.expect("API call failed") | ||
.expect("registration start failed"); | ||
|
||
// captcha kicks in | ||
assert!(matches!( | ||
result.next_step, | ||
RegistrationFlowNextStep::CheckCaptcha { .. } | ||
)); | ||
} |