Skip to content

Commit

Permalink
correct naming, changing userId reference names to username
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerjroach committed Dec 12, 2024
1 parent d041ef4 commit b24d342
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,18 @@ class AWSCognitoLegacyCredentialStoreInstrumentationTest {

@Test
fun test_legacy_store_implementation_can_retrieve_device_metadata_using_aws_sdk() {
val user1DeviceMetadata = store.retrieveDeviceMetadata(credentialStoreUtil.user1UserId)
val user2DeviceMetadata = store.retrieveDeviceMetadata(credentialStoreUtil.user2UserId)
val user1DeviceMetadata = store.retrieveDeviceMetadata(credentialStoreUtil.user1Username)
val user2DeviceMetadata = store.retrieveDeviceMetadata(credentialStoreUtil.user2Username)

assertEquals(credentialStoreUtil.getUser1DeviceMetadata(), user1DeviceMetadata)
assertEquals(credentialStoreUtil.getUser2DeviceMetadata(), user2DeviceMetadata)
}

@Test
fun test_legacy_store_implementation_can_retrieve_userIds_for_device_metadata() {
val expectedUserIds = listOf(credentialStoreUtil.user1UserId, credentialStoreUtil.user2UserId)
val deviceMetadataUserIds = store.retrieveDeviceMetadataUserIdList()
fun test_legacy_store_implementation_can_retrieve_usernames_for_device_metadata() {
val expectedUsernames = listOf(credentialStoreUtil.user1Username, credentialStoreUtil.user2Username)
val deviceMetadataUsernames = store.retrieveDeviceMetadataUsernameList()

assertEquals(expectedUserIds, deviceMetadataUserIds)
assertEquals(expectedUsernames, deviceMetadataUsernames)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,11 @@ class CredentialStoreStateMachineInstrumentationTest {
assertEquals(credentialStoreUtil.getDefaultCredential(), credentialStore.retrieveCredential())
assertEquals(
credentialStoreUtil.getUser1DeviceMetadata(),
credentialStore.retrieveDeviceMetadata(credentialStoreUtil.user1UserId)
credentialStore.retrieveDeviceMetadata(credentialStoreUtil.user1Username)
)
assertEquals(
credentialStoreUtil.getUser2DeviceMetadata(),
credentialStore.retrieveDeviceMetadata(credentialStoreUtil.user2UserId)
credentialStore.retrieveDeviceMetadata(credentialStoreUtil.user2Username)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ import java.util.Date
return credential
}

val user1UserId = "2924030b-54c0-48bc-8bff-948418fba949"
val user2UserId = "7e001127-5f11-41fb-9d10-ab9d6cf41dba"
val user1Username = "2924030b-54c0-48bc-8bff-948418fba949"
val user2Username = "7e001127-5f11-41fb-9d10-ab9d6cf41dba"

fun getUser1DeviceMetadata(): DeviceMetadata.Metadata {
return DeviceMetadata.Metadata(
Expand Down Expand Up @@ -83,13 +83,13 @@ import java.util.Date
put("CognitoIdentityProvider.$appClientId.LastAuthUser", "testuser")
}

AWSKeyValueStore(context, "CognitoIdentityProviderDeviceCache.$userPoolId.$user1UserId", true).apply {
AWSKeyValueStore(context, "CognitoIdentityProviderDeviceCache.$userPoolId.$user1Username", true).apply {
put("DeviceKey", "DeviceKey1")
put("DeviceGroupKey", "DeviceGroupKey1")
put("DeviceSecret", "DeviceSecret1")
}

AWSKeyValueStore(context, "CognitoIdentityProviderDeviceCache.$userPoolId.$user2UserId", true).apply {
AWSKeyValueStore(context, "CognitoIdentityProviderDeviceCache.$userPoolId.$user2Username", true).apply {
put("DeviceKey", "DeviceKey2")
put("DeviceGroupKey", "DeviceGroupKey2")
put("DeviceSecret", "DeviceSecret2")
Expand Down Expand Up @@ -117,13 +117,13 @@ import java.util.Date
fun saveLegacyDeviceMetadata(
context: Context,
userPoolId: String,
userId: String,
username: String,
deviceMetadata: DeviceMetadata.Metadata
) {
val prefsName = "CognitoIdentityProviderDeviceCache.$userPoolId.$userId"
val prefsName = "CognitoIdentityProviderDeviceCache.$userPoolId.$username"
AWSKeyValueStore(
context,
"CognitoIdentityProviderDeviceCache.$userPoolId.$userId", true).apply {
"CognitoIdentityProviderDeviceCache.$userPoolId.$username", true).apply {
put("DeviceKey", deviceMetadata.deviceKey)
put("DeviceGroupKey", deviceMetadata.deviceGroupKey)
put("DeviceSecret", deviceMetadata.deviceSecret)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,25 @@ internal object CredentialStoreCognitoActions : CredentialStoreActions {

/*
Migrate Device Metadata
1. We first need to get the list of userIds that contain device metadata on the device.
2. For each userId, we check to see if the current credential store has device metadata for that user.
1. We first need to get the list of usernames that contain device metadata on the device.
2. For each username, we check to see if the current credential store has device metadata for that user.
3. If the current user does not have device metadata in the current store, migrate from legacy.
This is a possibility because of a bug where we were previously attempting to migrate using an aliased
userId lookup. This situation would happen if a user migrated, signed out, then signed back in. Upon
signing back in, they would be granted new device metadata. Since that new metadata is what is
associated with the refresh token, we do not want to overwrite it.
username lookup.
4. If the current user has device metadata in the current credential store, do not migrate from legacy.
5. Upon migration completion, we delete the legacy device metadata.
This situation would happen if a user updated from legacy, signed out, then signed back in. Upon
signing back in, they would be granted new device metadata. Since that new metadata is what is
associated with the refresh token, we do not want to overwrite it with legacy metadata.
5. Upon completed migration, we delete the legacy device metadata.
*/
legacyCredentialStore.retrieveDeviceMetadataUserIdList().forEach { userId ->
val deviceMetaData = legacyCredentialStore.retrieveDeviceMetadata(userId)
legacyCredentialStore.retrieveDeviceMetadataUsernameList().forEach { username ->
val deviceMetaData = legacyCredentialStore.retrieveDeviceMetadata(username)
if (deviceMetaData != DeviceMetadata.Empty) {
credentialStore.retrieveDeviceMetadata(userId)
if (credentialStore.retrieveDeviceMetadata(userId) == DeviceMetadata.Empty) {
credentialStore.saveDeviceMetadata(userId, deviceMetaData)
credentialStore.retrieveDeviceMetadata(username)
if (credentialStore.retrieveDeviceMetadata(username) == DeviceMetadata.Empty) {
credentialStore.saveDeviceMetadata(username, deviceMetaData)
}
legacyCredentialStore.deleteDeviceKeyCredential(userId)
legacyCredentialStore.deleteDeviceKeyCredential(username)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ internal class AWSCognitoLegacyCredentialStore(

/*
During migration away from the legacy credential store, we need to find all shared preference files that store
device metadata. These filenames contain the real userId (not aliased) for the tracked device metadata.
device metadata. These filenames contain the real username (not aliased) for the tracked device metadata.
*/
fun retrieveDeviceMetadataUserIdList(): List<String> {
fun retrieveDeviceMetadataUsernameList(): List<String> {
return try {
val sharedPrefsSuffix = ".xml"
File(context.dataDir, "shared_prefs").listFiles { _, filename ->
Expand Down

0 comments on commit b24d342

Please sign in to comment.