Skip to content

Commit bc09b9d

Browse files
authored
Refactor tentative_device_registration to use v2 types instead. (dfinity#3225)
* Refactor `tentative_device_registration` to use v2 types instead. This moves the mapping between types from the v2 methods (v1 -> v2) to the v1 methods (v2 -> v1). * Refactor `tentative_device_registration` to use v2 types instead. This moves the mapping between types from the v2 methods (v1 -> v2) to the v1 methods (v2 -> v1). * Refactor `tentative_device_registration` to use v2 types instead. This moves the mapping between types from the v2 methods (v1 -> v2) to the v1 methods (v2 -> v1).
1 parent 49ddf42 commit bc09b9d

2 files changed

Lines changed: 43 additions & 84 deletions

File tree

src/internet_identity/src/anchor_management/tentative_device_registration.rs

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use ic_cdk::api::time;
1010
use ic_cdk::{call, trap};
1111
use internet_identity_interface::internet_identity::types::*;
1212
use std::collections::{hash_map, HashMap};
13-
use TentativeDeviceRegistrationError::{AnotherDeviceTentativelyAdded, DeviceRegistrationModeOff};
1413

1514
// 15 mins
1615
const REGISTRATION_MODE_DURATION: u64 = secs_to_nanos(900);
@@ -76,28 +75,10 @@ pub fn exit_device_registration_mode(anchor_number: AnchorNumber) {
7675
});
7776
}
7877

79-
pub struct TentativeRegistrationInfo {
80-
pub verification_code: DeviceConfirmationCode,
81-
pub device_registration_timeout: Timestamp,
82-
}
83-
84-
#[derive(Debug)]
85-
pub enum TentativeDeviceRegistrationError {
86-
DeviceRegistrationModeOff,
87-
AnotherDeviceTentativelyAdded,
88-
IdentityUpdateError(IdentityUpdateError),
89-
}
90-
91-
impl From<IdentityUpdateError> for TentativeDeviceRegistrationError {
92-
fn from(err: IdentityUpdateError) -> Self {
93-
TentativeDeviceRegistrationError::IdentityUpdateError(err)
94-
}
95-
}
96-
9778
pub async fn add_tentative_device(
9879
anchor_number: AnchorNumber,
9980
tentative_device: DeviceData,
100-
) -> Result<TentativeRegistrationInfo, TentativeDeviceRegistrationError> {
81+
) -> Result<AuthnMethodConfirmationCode, AuthnMethodRegisterError> {
10182
let confirmation_code = new_confirmation_code().await;
10283
let now = time();
10384

@@ -108,12 +89,12 @@ pub async fn add_tentative_device(
10889
// Get registration else throw error
10990
let registration = registrations
11091
.get_mut(&anchor_number)
111-
.ok_or(DeviceRegistrationModeOff)?;
92+
.ok_or(AuthnMethodRegisterError::RegistrationModeOff)?;
11293

11394
match registration {
11495
// Make sure registration isn't expired
11596
TentativeDeviceRegistration { expiration, .. } if *expiration <= now => {
116-
Err(DeviceRegistrationModeOff)
97+
Err(AuthnMethodRegisterError::RegistrationModeOff)
11798
}
11899
// Add tentative session if registration mode is active
119100
TentativeDeviceRegistration {
@@ -125,25 +106,18 @@ pub async fn add_tentative_device(
125106
failed_attempts: 0,
126107
confirmation_code: confirmation_code.clone(),
127108
};
128-
Ok(TentativeRegistrationInfo {
129-
device_registration_timeout: registration.expiration,
130-
verification_code: confirmation_code,
109+
Ok(AuthnMethodConfirmationCode {
110+
expiration: registration.expiration,
111+
confirmation_code,
131112
})
132113
}
133114
// Else return error
134-
_ => Err(AnotherDeviceTentativelyAdded),
115+
_ => Err(AuthnMethodRegisterError::RegistrationAlreadyInProgress),
135116
}
136117
})
137118
})
138119
}
139120

140-
#[derive(Debug)]
141-
pub enum VerifyTentativeDeviceError {
142-
WrongCode { retries_left: u8 },
143-
DeviceRegistrationModeOff,
144-
NoDeviceToVerify,
145-
}
146-
147121
/// Confirm the tentative device or session using the submitted `user_confirmation_code`.
148122
///
149123
/// # Returns
@@ -154,20 +128,21 @@ pub enum VerifyTentativeDeviceError {
154128
pub fn confirm_tentative_device_or_session(
155129
anchor_number: AnchorNumber,
156130
user_confirmation_code: DeviceConfirmationCode,
157-
) -> Result<Option<DeviceData>, VerifyTentativeDeviceError> {
131+
) -> Result<Option<DeviceData>, AuthnMethodConfirmationError> {
158132
state::tentative_device_registrations_mut(|registrations| {
159133
state::lookup_tentative_device_registration_mut(|lookup| {
160134
prune_expired_tentative_device_registrations(registrations, lookup);
161135

162136
// Get registration else throw error
163137
let registration = registrations
164138
.get_mut(&anchor_number)
165-
.ok_or(VerifyTentativeDeviceError::DeviceRegistrationModeOff)?;
139+
.ok_or(AuthnMethodConfirmationError::RegistrationModeOff)?;
166140

167141
let (should_remove, response) = match &mut registration.state {
168-
DeviceRegistrationModeActive | SessionTentativelyConfirmed { .. } => {
169-
(false, Err(VerifyTentativeDeviceError::NoDeviceToVerify))
170-
}
142+
DeviceRegistrationModeActive | SessionTentativelyConfirmed { .. } => (
143+
false,
144+
Err(AuthnMethodConfirmationError::NoAuthnMethodToConfirm),
145+
),
171146
DeviceTentativelyAdded {
172147
failed_attempts,
173148
confirmation_code,
@@ -183,7 +158,7 @@ pub fn confirm_tentative_device_or_session(
183158
// Remove registration if max attempts reached
184159
(
185160
*failed_attempts >= MAX_DEVICE_REGISTRATION_ATTEMPTS,
186-
Err(VerifyTentativeDeviceError::WrongCode {
161+
Err(AuthnMethodConfirmationError::WrongCode {
187162
retries_left: (MAX_DEVICE_REGISTRATION_ATTEMPTS - *failed_attempts),
188163
}),
189164
)
@@ -209,7 +184,7 @@ pub fn confirm_tentative_device_or_session(
209184
// Remove registration if max attempts reached
210185
(
211186
*failed_attempts >= MAX_DEVICE_REGISTRATION_ATTEMPTS,
212-
Err(VerifyTentativeDeviceError::WrongCode {
187+
Err(AuthnMethodConfirmationError::WrongCode {
213188
retries_left: (MAX_DEVICE_REGISTRATION_ATTEMPTS - *failed_attempts),
214189
}),
215190
)

src/internet_identity/src/main.rs

Lines changed: 28 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
use crate::anchor_management::tentative_device_registration;
2-
use crate::anchor_management::tentative_device_registration::{
3-
TentativeDeviceRegistrationError, TentativeRegistrationInfo, VerifyTentativeDeviceError,
4-
};
52
use crate::archive::ArchiveState;
63
use crate::assets::init_assets;
74
use crate::state::persistent_state;
@@ -98,23 +95,24 @@ async fn add_tentative_device(
9895
let result =
9996
tentative_device_registration::add_tentative_device(anchor_number, device_data).await;
10097
match result {
101-
Ok(TentativeRegistrationInfo {
102-
verification_code,
103-
device_registration_timeout,
98+
Ok(AuthnMethodConfirmationCode {
99+
confirmation_code,
100+
expiration,
104101
}) => AddTentativeDeviceResponse::AddedTentatively {
105-
verification_code,
106-
device_registration_timeout,
102+
verification_code: confirmation_code,
103+
device_registration_timeout: expiration,
107104
},
108105
Err(err) => match err {
109-
TentativeDeviceRegistrationError::DeviceRegistrationModeOff => {
106+
AuthnMethodRegisterError::RegistrationModeOff => {
110107
AddTentativeDeviceResponse::DeviceRegistrationModeOff
111108
}
112-
TentativeDeviceRegistrationError::AnotherDeviceTentativelyAdded => {
109+
AuthnMethodRegisterError::RegistrationAlreadyInProgress => {
113110
AddTentativeDeviceResponse::AnotherDeviceTentativelyAdded
114111
}
115-
TentativeDeviceRegistrationError::IdentityUpdateError(err) => {
116-
// Legacy API traps instead of returning an error.
117-
trap(String::from(err).as_str())
112+
AuthnMethodRegisterError::InvalidMetadata(_) => {
113+
// Unreachable since we don't convert from `AuthnMethodData` to `DeviceWithUsage`
114+
// in this legacy method in comparison to the newer `authn_method_register` method.
115+
trap("Unreachable error");
118116
}
119117
},
120118
}
@@ -144,15 +142,22 @@ fn verify_tentative_device(
144142
VerifyTentativeDeviceResponse::Verified
145143
}
146144
Err(err) => match err {
147-
VerifyTentativeDeviceError::DeviceRegistrationModeOff => {
145+
AuthnMethodConfirmationError::RegistrationModeOff => {
148146
VerifyTentativeDeviceResponse::DeviceRegistrationModeOff
149147
}
150-
VerifyTentativeDeviceError::NoDeviceToVerify => {
148+
AuthnMethodConfirmationError::NoAuthnMethodToConfirm => {
151149
VerifyTentativeDeviceResponse::NoDeviceToVerify
152150
}
153-
VerifyTentativeDeviceError::WrongCode { retries_left } => {
151+
AuthnMethodConfirmationError::WrongCode { retries_left } => {
154152
VerifyTentativeDeviceResponse::WrongCode { retries_left }
155153
}
154+
// Unreachable since these two errors already result in a trap in this legacy method.
155+
AuthnMethodConfirmationError::Unauthorized(principal) => {
156+
trap(&format!("{principal} could not be authenticated."))
157+
}
158+
AuthnMethodConfirmationError::InternalCanisterError(err) => {
159+
trap(&err.to_string());
160+
}
156161
},
157162
}
158163
}
@@ -915,22 +920,12 @@ mod v2_api {
915920
) -> Result<AuthnMethodConfirmationCode, AuthnMethodRegisterError> {
916921
let device = DeviceWithUsage::try_from(authn_method)
917922
.map_err(|err| AuthnMethodRegisterError::InvalidMetadata(err.to_string()))?;
918-
let result = add_tentative_device(identity_number, DeviceData::from(device)).await;
919-
match result {
920-
AddTentativeDeviceResponse::AddedTentatively {
921-
device_registration_timeout,
922-
verification_code,
923-
} => Ok(AuthnMethodConfirmationCode {
924-
expiration: device_registration_timeout,
925-
confirmation_code: verification_code,
926-
}),
927-
AddTentativeDeviceResponse::DeviceRegistrationModeOff => {
928-
Err(AuthnMethodRegisterError::RegistrationModeOff)
929-
}
930-
AddTentativeDeviceResponse::AnotherDeviceTentativelyAdded => {
931-
Err(AuthnMethodRegisterError::RegistrationAlreadyInProgress)
932-
}
933-
}
923+
924+
tentative_device_registration::add_tentative_device(
925+
identity_number,
926+
DeviceData::from(device),
927+
)
928+
.await
934929
}
935930

936931
#[update]
@@ -945,18 +940,7 @@ mod v2_api {
945940
tentative_device_registration::confirm_tentative_device_or_session(
946941
identity_number,
947942
confirmation_code,
948-
)
949-
.map_err(|err| match err {
950-
VerifyTentativeDeviceError::WrongCode { retries_left } => {
951-
AuthnMethodConfirmationError::WrongCode { retries_left }
952-
}
953-
VerifyTentativeDeviceError::DeviceRegistrationModeOff => {
954-
AuthnMethodConfirmationError::RegistrationModeOff
955-
}
956-
VerifyTentativeDeviceError::NoDeviceToVerify => {
957-
AuthnMethodConfirmationError::NoAuthnMethodToConfirm
958-
}
959-
})?;
943+
)?;
960944

961945
if let Some(confirmed_device) = maybe_confirmed_device {
962946
// Add device to anchor with bookkeeping if it has been confirmed

0 commit comments

Comments
 (0)