-
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.
Add API v2 authn_method_remove method (#1809)
* Add API v2 authn_method_remove method Add the authn_method_remove method to slowly build the API v2. * 🤖 npm run generate auto-update * Add API v2 todos --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
bff85eb
commit 9adc86b
Showing
11 changed files
with
168 additions
and
23 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
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
79 changes: 79 additions & 0 deletions
79
src/internet_identity/tests/integration/v2_api/authn_method_remove.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,79 @@ | ||
use crate::v2_api::authn_method_test_helpers::{ | ||
create_identity_with_authn_method, sample_authn_method, | ||
}; | ||
use candid::Principal; | ||
use canister_tests::api::internet_identity::api_v2; | ||
use canister_tests::framework::{ | ||
env, expect_user_error_with_message, install_ii_canister, II_WASM, | ||
}; | ||
use canister_tests::match_value; | ||
use ic_test_state_machine_client::CallError; | ||
use ic_test_state_machine_client::ErrorCode::CanisterCalledTrap; | ||
use internet_identity_interface::internet_identity::types::IdentityInfoResponse; | ||
use internet_identity_interface::internet_identity::types::{ | ||
AuthnMethodAddResponse, AuthnMethodRemoveResponse, | ||
}; | ||
use regex::Regex; | ||
|
||
#[test] | ||
fn should_remove_authn_method() -> Result<(), CallError> { | ||
let env = env(); | ||
let canister_id = install_ii_canister(&env, II_WASM.clone()); | ||
let authn_method_1 = sample_authn_method(1); | ||
let principal = authn_method_1.principal(); | ||
let authn_method_2 = sample_authn_method(2); | ||
|
||
let identity_number = create_identity_with_authn_method(&env, canister_id, &authn_method_1); | ||
let result = api_v2::authn_method_add( | ||
&env, | ||
canister_id, | ||
principal, | ||
identity_number, | ||
&authn_method_2, | ||
)? | ||
.unwrap(); | ||
|
||
assert!(matches!(result, AuthnMethodAddResponse::Ok)); | ||
|
||
match_value!( | ||
api_v2::identity_info(&env, canister_id, principal, identity_number)?, | ||
Some(IdentityInfoResponse::Ok(identity_info)) | ||
); | ||
|
||
assert_eq!(identity_info.authn_methods.len(), 2); | ||
|
||
match_value!( | ||
api_v2::authn_method_remove( | ||
&env, | ||
canister_id, | ||
principal, | ||
identity_number, | ||
&authn_method_2.public_key(), | ||
)?, | ||
Some(AuthnMethodRemoveResponse::Ok) | ||
); | ||
Ok(()) | ||
} | ||
|
||
#[test] | ||
fn should_require_authentication_to_remove_authn_method() -> Result<(), CallError> { | ||
let env = env(); | ||
let canister_id = install_ii_canister(&env, II_WASM.clone()); | ||
let authn_method = sample_authn_method(1); | ||
|
||
let identity_number = create_identity_with_authn_method(&env, canister_id, &authn_method); | ||
|
||
let result = api_v2::authn_method_remove( | ||
&env, | ||
canister_id, | ||
Principal::anonymous(), | ||
identity_number, | ||
&authn_method.public_key(), | ||
); | ||
expect_user_error_with_message( | ||
result, | ||
CanisterCalledTrap, | ||
Regex::new("[a-z\\d-]+ could not be authenticated.").unwrap(), | ||
); | ||
Ok(()) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod authn_method_add; | ||
mod authn_method_remove; | ||
pub mod authn_method_test_helpers; | ||
mod identity_info; | ||
mod identity_metadata; |
15 changes: 11 additions & 4 deletions
15
src/internet_identity_interface/src/internet_identity/authn_method.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 |
---|---|---|
@@ -1,14 +1,21 @@ | ||
use crate::internet_identity::types::{AuthnMethod, AuthnMethodData, PublicKeyAuthn, WebAuthn}; | ||
use crate::internet_identity::types::{ | ||
AuthnMethod, AuthnMethodData, PublicKey, PublicKeyAuthn, WebAuthn, | ||
}; | ||
use candid::Principal; | ||
|
||
impl AuthnMethodData { | ||
/// Returns the principal of this authentication method (i.e. the caller principal observed in | ||
/// the II canister when signing canister calls with the given authentication method). | ||
pub fn principal(&self) -> Principal { | ||
let pubkey = match self.authn_method { | ||
Principal::self_authenticating(self.public_key()) | ||
} | ||
|
||
/// Returns the public key of this authentication method. | ||
pub fn public_key(&self) -> PublicKey { | ||
match self.authn_method { | ||
AuthnMethod::WebAuthn(WebAuthn { ref pubkey, .. }) => pubkey, | ||
AuthnMethod::PubKey(PublicKeyAuthn { ref pubkey }) => pubkey, | ||
}; | ||
Principal::self_authenticating(pubkey) | ||
} | ||
.clone() | ||
} | ||
} |
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