Skip to content

Commit

Permalink
fix(auth): device metadata migration (#2114)
Browse files Browse the repository at this point in the history
  • Loading branch information
div5yesh authored and Banji Jolaoso committed Nov 22, 2022
1 parent 1bbdc48 commit e69c425
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ internal object CredentialStoreCognitoActions : CredentialStoreActions {
// migrate credentials
credentialStore.saveCredential(credentials)
legacyCredentialStore.deleteCredential()
}

// migrate device data
if (credentials is AmplifyCredential.UserPoolTypeCredential) {
val username = credentials.signedInData.username
val deviceMetaData = legacyCredentialStore.retrieveDeviceMetadata(username)
if (deviceMetaData != DeviceMetadata.Empty) {
credentialStore.saveDeviceMetadata(username, deviceMetaData)
legacyCredentialStore.deleteDeviceKeyCredential(username)
}
// migrate device data
val lastAuthUserId = legacyCredentialStore.retrieveLastAuthUserId()
lastAuthUserId?.let {
val deviceMetaData = legacyCredentialStore.retrieveDeviceMetadata(lastAuthUserId)
if (deviceMetaData != DeviceMetadata.Empty) {
credentialStore.saveDeviceMetadata(lastAuthUserId, deviceMetaData)
legacyCredentialStore.deleteDeviceKeyCredential(lastAuthUserId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ internal class AWSCognitoLegacyCredentialStore(
override fun saveDeviceMetadata(username: String, deviceMetadata: DeviceMetadata) = Unit
override fun saveASFDevice(device: AmplifyCredential.ASFDevice) = Unit

@Synchronized
fun retrieveLastAuthUserId(): String? {
val keys = getTokenKeys()
return keys[APP_LAST_AUTH_USER]?.let { tokensKeyValue.get(it) }
}

@Synchronized
override fun retrieveCredential(): AmplifyCredential {
val signedInData = retrieveSignedInData()
Expand Down Expand Up @@ -133,6 +139,9 @@ internal class AWSCognitoLegacyCredentialStore(
}

override fun deleteDeviceKeyCredential(username: String) {
val keys = getTokenKeys()
keys[APP_LAST_AUTH_USER]?.let { tokensKeyValue.remove(it) }

deviceKeyValue.apply {
remove(DEVICE_KEY)
remove(DEVICE_GROUP_KEY)
Expand All @@ -147,7 +156,6 @@ internal class AWSCognitoLegacyCredentialStore(

private fun deleteCognitoUserPoolTokens() {
val keys = getTokenKeys()
keys[APP_LAST_AUTH_USER]?.let { tokensKeyValue.remove(it) }
keys[TOKEN_TYPE_ID]?.let { tokensKeyValue.remove(it) }
keys[TOKEN_TYPE_ACCESS]?.let { tokensKeyValue.remove(it) }
keys[TOKEN_TYPE_REFRESH]?.let { tokensKeyValue.remove(it) }
Expand Down

0 comments on commit e69c425

Please sign in to comment.