Skip to content

Commit

Permalink
chore: combined device secrets and asf device Id into one function
Browse files Browse the repository at this point in the history
  • Loading branch information
khatruong2009 committed Dec 16, 2023
1 parent 38543bd commit 7b46c5a
Show file tree
Hide file tree
Showing 12 changed files with 245 additions and 286 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,25 @@ open class AmplifyAuthCognitoPlugin :
)
}

/**
Legacy Device Secrets Storage.
*/
private val legacyDeviceSecretsStore: LegacyKeyValueStore by lazy {
LegacyKeyValueStore(
applicationContext!!,
"CognitoIdentityProviderDeviceCache.<userPoolId>.<username>"
)
}

/**
ASF Device Secrets Storage.
*/
private val asfDeviceSecretsStore: LegacyKeyValueStore by lazy {
LegacyKeyValueStore(
applicationContext!!,
"AWS.Cognito.ContextData"
)

override fun onAttachedToEngine(binding: FlutterPlugin.FlutterPluginBinding) {
Log.d(TAG, "onAttachedToEngine")
applicationContext = binding.applicationContext
Expand Down Expand Up @@ -273,6 +292,40 @@ open class AmplifyAuthCognitoPlugin :
callback(Result.success(data.build()))
}

/**
* Get Legacy Device Secrets
*/
override fun fetchLegacyDeviceSecrets(userPoolId: String?, appClientId: String?, callback: (Result<LegacyDeviceDetails>) -> Unit) {
val data = LegacyDeviceDetails.builder()

if (appClientId != null) {
val lastAuthUser = legacyUserPoolStore["CognitoIdentityProvider.$appClientId.LastAuthUser"]

val newLegacyDeviceSecretsStore = new LegacyKeyValueStore(
applicationContext!!,
"CognitoIdentityProviderDeviceCache.$userPoolId.$lastAuthUser"
)

val deviceKey = newLegacyDeviceSecretsStore["DeviceKey"]
val deviceSecret = newLegacyDeviceSecretsStore["DeviceSecret"]
val deviceGroup = newLegacyDeviceSecretsStore["DeviceGroupKey"]

data.apply {
this.deviceKey = deviceKey
this.deviceSecret = deviceSecret
this.deviceGroupKey = deviceGroup
}

}

val asfDeviceId = asfDeviceSecretsStore["CognitoDeviceId"]
data.apply {
this.asfDeviceId = asfDeviceId
}

callback(Result.success(data.build()))
}

/**
* Clears the legacy credentials set by the Android SDK
*/
Expand Down Expand Up @@ -572,3 +625,19 @@ class LegacyCredentialStoreDataBuilder(
idToken,
)
}

fun LegacyDeviceDetails.Companion.builder() = LegacyDeviceDetailsBuilder()

class LegacyDeviceDetailsBuilder(
var deviceKey: String? = null,
var deviceSecret: String? = null,
var deviceGroupKey: String? = null,
var asfDeviceId: String? = null,
) {
fun build(): LegacyDeviceDetails = LegacyDeviceDetails(
deviceKey,
deviceSecret,
deviceGroupKey,
asfDeviceId,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,24 @@ package com.amazonaws.amplify.amplify_auth_cognito
import android.content.Context
import io.flutter.plugin.common.PluginRegistry

class LegacySecretHandlerImpl(private val context: Context) : LegacySecretHandler {
class LegacySecretHandlerImpl(private val context: Context) : LegacySecretHandler, MethodChannel.MethodCallHandler {

private val channel = MethodChannel(registrar.messenger(), "com.amazonaws.amplify/amplify_auth_cognito")

override fun fetchLegacyDeviceSecrets(userPoolConfig: CognitoUserPoolConfig?): LegacyDeviceDetails?{
return null
}

override fun deleteLegacyDeviceSecrets(userPoolConfig: CognitoUserPoolConfig?) {
channel.invokeMethod("deleteLegacyDeviceSecrets", userPoolConfig?.poolId)
}

override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
if (call.method == "getDeviceId") {
result.success(getDeviceId())
} else {
result.notImplemented()
}
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7b46c5a

Please sign in to comment.