Skip to content
Merged
5 changes: 5 additions & 0 deletions src/internet_identity/src/account_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ pub async fn prepare_account_delegation(
});
update_root_hash();

// Update last used timestamp
storage_borrow_mut(|storage| {
storage.set_account_last_used(anchor_number, origin.clone(), account_number, time());
});

delegation_bookkeeping(origin, ii_domain.clone(), session_duration_ns);

Ok(PrepareAccountDelegation {
Expand Down
38 changes: 24 additions & 14 deletions src/internet_identity/src/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,25 @@ impl<M: Memory + Clone> Storage<M> {
result
}

pub fn set_account_last_used(
&mut self,
anchor_number: AnchorNumber,
origin: FrontendHostname,
account_number: Option<AccountNumber>,
now: Timestamp,
) -> Option<()> {
let application_number = self.lookup_application_number_with_origin(&origin);

self.with_account_mut(
anchor_number,
application_number,
account_number,
|account_reference, _| {
account_reference.last_used = Some(now);
},
)
}

pub fn lookup_anchor_application_config(
&self,
anchor_number: AnchorNumber,
Expand Down Expand Up @@ -1061,7 +1080,7 @@ impl<M: Memory + Clone> Storage<M> {
// Update counters with one more account.
self.update_counters(app_num, anchor_number, AccountType::Account)?;

// last_used will be set once the user signs in to the new account.
// last_used will be set once the user signs in to the respective account.
let last_used = None;

// Process account references
Expand Down Expand Up @@ -1303,19 +1322,13 @@ impl<M: Memory + Clone> Storage<M> {
// Update account and write back to storage
storable_account.name = name.clone();

// The `last_used` field will be set when the user signs in.
let last_used = None;

// Update last-used timestamp in account reference
account_reference.last_used = last_used;

// Return a user-facing account structure
Some(Account::new_full(
anchor_number,
origin,
Some(name),
Some(account_number),
last_used,
account_reference.last_used,
storable_account.seed_from_anchor,
))
},
Expand Down Expand Up @@ -1368,9 +1381,6 @@ impl<M: Memory + Clone> Storage<M> {
// Update counters with one more account.
self.update_counters(application_number, anchor_number, AccountType::Account)?;

// The `last_used` field will be set when the user signs in.
let last_used = None;

let account_references_key = (anchor_number, application_number);
match self
.stable_account_reference_list_memory
Expand All @@ -1382,7 +1392,8 @@ impl<M: Memory + Clone> Storage<M> {
// This is because we don't create default accounts explicitly.
let new_ref = AccountReference {
account_number: Some(new_account_number),
last_used,
// The `last_used` field will be set when the user signs into this account.
last_used: None,
};
self.stable_account_reference_list_memory
.insert(account_references_key, vec![new_ref].into());
Expand All @@ -1401,7 +1412,6 @@ impl<M: Memory + Clone> Storage<M> {
if r_mut.account_number.is_none() {
// Found the default account reference.
r_mut.account_number = Some(new_account_number);
r_mut.last_used = last_used;
found_and_updated = true;
break;
}
Expand All @@ -1425,7 +1435,7 @@ impl<M: Memory + Clone> Storage<M> {
origin,
Some(storable_account.name),
Some(new_account_number),
last_used,
None,
storable_account.seed_from_anchor,
))
}
Expand Down
37 changes: 28 additions & 9 deletions src/internet_identity/tests/integration/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::time::Duration;

use canister_tests::{
api::internet_identity::{
api_v2::{
Expand All @@ -19,7 +17,9 @@ use internet_identity_interface::internet_identity::types::{
PrepareAccountDelegation,
};
use pocket_ic::RejectResponse;
use pretty_assertions::assert_eq;
use serde_bytes::ByteBuf;
use std::time::Duration;

/// Verifies that one account can be created
#[test]
Expand Down Expand Up @@ -75,6 +75,8 @@ fn should_list_accounts() -> Result<(), RejectResponse> {
.unwrap()
.unwrap();

let expected_first_last_used = Some(env.get_time().as_nanos_since_unix_epoch());

let second_created_account = create_account(
&env,
canister_id,
Expand All @@ -86,6 +88,8 @@ fn should_list_accounts() -> Result<(), RejectResponse> {
.unwrap()
.unwrap();

let expected_second_last_used = Some(env.get_time().as_nanos_since_unix_epoch());

let third_created_account = create_account(
&env,
canister_id,
Expand All @@ -97,6 +101,8 @@ fn should_list_accounts() -> Result<(), RejectResponse> {
.unwrap()
.unwrap();

let expected_third_last_used = Some(env.get_time().as_nanos_since_unix_epoch());

let accounts_list = get_accounts(
&env,
canister_id,
Expand All @@ -120,19 +126,19 @@ fn should_list_accounts() -> Result<(), RejectResponse> {
AccountInfo {
account_number: first_created_account.account_number,
origin: origin.clone(),
last_used: None,
last_used: expected_first_last_used,
name: Some(name)
},
AccountInfo {
account_number: second_created_account.account_number,
origin: origin.clone(),
last_used: None,
last_used: expected_second_last_used,
name: Some(name_two)
},
AccountInfo {
account_number: third_created_account.account_number,
origin,
last_used: None,
last_used: expected_third_last_used,
name: Some(name_three)
},
]
Expand Down Expand Up @@ -195,6 +201,8 @@ fn should_list_only_own_accounts() -> Result<(), RejectResponse> {
.unwrap()
.unwrap();

let expected_first_last_used = Some(env.get_time().as_nanos_since_unix_epoch());

let second_created_account = create_account(
&env,
canister_id,
Expand All @@ -206,6 +214,8 @@ fn should_list_only_own_accounts() -> Result<(), RejectResponse> {
.unwrap()
.unwrap();

let expected_second_last_used = Some(env.get_time().as_nanos_since_unix_epoch());

let another_identity_account = create_account(
&env,
canister_id,
Expand All @@ -217,6 +227,8 @@ fn should_list_only_own_accounts() -> Result<(), RejectResponse> {
.unwrap()
.unwrap();

let expected_another_last_used = Some(env.get_time().as_nanos_since_unix_epoch());

let accounts_list = get_accounts(
&env,
canister_id,
Expand Down Expand Up @@ -250,13 +262,13 @@ fn should_list_only_own_accounts() -> Result<(), RejectResponse> {
AccountInfo {
account_number: first_created_account.account_number,
origin: origin.clone(),
last_used: None,
last_used: expected_first_last_used,
name: Some(name)
},
AccountInfo {
account_number: second_created_account.account_number,
origin: origin.clone(),
last_used: None,
last_used: expected_second_last_used,
name: Some(name_two)
},
]
Expand All @@ -275,7 +287,7 @@ fn should_list_only_own_accounts() -> Result<(), RejectResponse> {
AccountInfo {
account_number: another_identity_account.account_number,
origin,
last_used: None,
last_used: expected_another_last_used,
name: Some(name_three)
},
]
Expand Down Expand Up @@ -303,6 +315,8 @@ fn should_update_account() -> Result<(), RejectResponse> {
.unwrap()
.unwrap();

let creation_time = Some(env.get_time().as_nanos_since_unix_epoch());

let new_name = Some("Laniakea".to_string());

let update = AccountUpdate {
Expand All @@ -321,11 +335,16 @@ fn should_update_account() -> Result<(), RejectResponse> {
.unwrap()
.unwrap();

let update_time = Some(env.get_time().as_nanos_since_unix_epoch());

// Smoke test
assert_ne!(update_time, creation_time);

assert_eq!(
updated_account,
AccountInfo {
account_number: created_account.account_number,
last_used: None,
last_used: update_time,
origin,
name: new_name
}
Expand Down
Loading