Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor authentication check to no longer trap #1623

Merged
merged 2 commits into from
May 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions src/internet_identity/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,9 @@ fn get_anchor_info(anchor_number: AnchorNumber) -> IdentityAnchorInfo {
#[query]
#[candid_method(query)]
fn get_principal(anchor_number: AnchorNumber, frontend: FrontendHostname) -> Principal {
trap_if_not_authenticated(anchor_number);
let Ok(_) = check_authentication(anchor_number) else {
trap(&format!("{} could not be authenticated.", caller()));
};
delegation::get_principal(anchor_number, frontend)
}

Expand Down Expand Up @@ -233,7 +235,9 @@ fn get_delegation(
session_key: SessionKey,
expiration: Timestamp,
) -> GetDelegationResponse {
trap_if_not_authenticated(anchor_number);
let Ok(_) = check_authentication(anchor_number) else {
trap(&format!("{} could not be authenticated.", caller()));
};
delegation::get_delegation(anchor_number, frontend, session_key, expiration)
}

Expand Down Expand Up @@ -426,7 +430,9 @@ fn update_root_hash() {
/// Note: this function reads / writes the anchor from / to stable memory. It is intended to be used by functions that
/// do not further modify the anchor.
fn authenticate_and_record_activity(anchor_number: AnchorNumber) -> Option<IIDomain> {
let (mut anchor, device_key) = trap_if_not_authenticated(anchor_number);
let Ok((mut anchor, device_key)) = check_authentication(anchor_number) else {
trap(&format!("{} could not be authenticated.", caller()));
};
let domain = anchor.device(&device_key).unwrap().ii_domain();
anchor_management::activity_bookkeeping(&mut anchor, &device_key);
state::storage_borrow_mut(|storage| storage.write(anchor_number, anchor)).unwrap_or_else(
Expand All @@ -449,7 +455,9 @@ fn authenticated_anchor_operation<R>(
anchor_number: AnchorNumber,
op: impl FnOnce(&mut Anchor) -> Result<(R, Operation), R>,
) -> R {
let (mut anchor, device_key) = trap_if_not_authenticated(anchor_number);
let Ok((mut anchor, device_key)) = check_authentication(anchor_number) else {
trap(&format!("{} could not be authenticated.", caller()));
};
anchor_management::activity_bookkeeping(&mut anchor, &device_key);

let result = op(&mut anchor);
Expand All @@ -469,8 +477,8 @@ fn authenticated_anchor_operation<R>(
}

/// Checks if the caller is authenticated against the anchor provided and returns a reference to the device used.
/// Traps if the caller is not authenticated.
fn trap_if_not_authenticated(anchor_number: AnchorNumber) -> (Anchor, DeviceKey) {
/// Returns an error if the caller cannot be authenticated.
fn check_authentication(anchor_number: AnchorNumber) -> Result<(Anchor, DeviceKey), ()> {
let anchor = state::anchor(anchor_number);
let caller = caller();

Expand All @@ -482,10 +490,10 @@ fn trap_if_not_authenticated(anchor_number: AnchorNumber) -> (Anchor, DeviceKey)
.is_ok()
})
{
return (anchor.clone(), device.pubkey.clone());
return Ok((anchor.clone(), device.pubkey.clone()));
}
}
trap(&format!("{} could not be authenticated.", caller))
Err(())
}

/// New v2 API that aims to eventually replace the current API.
Expand Down