Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(auth): Fix for checking current preferred type before setting the MFAType as enabled to ensure the current preference is not cleared out #2580

Merged
merged 18 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2190,7 +2190,7 @@ internal class RealAWSCognitoAuthPlugin(
var enabledSet: MutableSet<MFAType>? = null
var preferred: MFAType? = null
if (!response.userMfaSettingList.isNullOrEmpty()) {
enabledSet = mutableSetOf<MFAType>()
enabledSet = mutableSetOf()
response.userMfaSettingList?.forEach { mfaType ->
enabledSet.add(getMFAType(mfaType))
}
Expand Down Expand Up @@ -2223,45 +2223,56 @@ internal class RealAWSCognitoAuthPlugin(
onSuccess: Action,
onError: Consumer<AuthException>
) {
authStateMachine.getCurrentState { authState ->
when (authState.authNState) {
is AuthenticationState.SignedIn -> {
GlobalScope.launch {
try {
val accessToken = getSession().userPoolTokensResult.value?.accessToken
accessToken?.let { token ->
authEnvironment.cognitoAuthService.cognitoIdentityProviderClient?.setUserMfaPreference {
this.accessToken = token
this.smsMfaSettings = sms?.let {
SmsMfaSettingsType.invoke {
enabled = it.mfaEnabled
it.mfaPreferred ?.let { preferred -> preferredMfa = preferred }
}
}
this.softwareTokenMfaSettings = totp?.let {
SoftwareTokenMfaSettingsType.invoke {
enabled = it.mfaEnabled
it.mfaPreferred ?.let { preferred -> preferredMfa = preferred }
fetchMFAPreference({ userPreference ->
authStateMachine.getCurrentState { authState ->
when (authState.authNState) {
is AuthenticationState.SignedIn -> {
GlobalScope.launch {
try {
val accessToken = getSession().userPoolTokensResult.value?.accessToken
accessToken?.let { token ->
authEnvironment
.cognitoAuthService
.cognitoIdentityProviderClient
?.setUserMfaPreference {
this.accessToken = token
this.smsMfaSettings = sms?.let { it ->
val preferredMFASetting = it.mfaPreferred
?: (userPreference.preferred == MFAType.SMS && it.mfaEnabled)
gpanshu marked this conversation as resolved.
Show resolved Hide resolved
SmsMfaSettingsType.invoke {
enabled = it.mfaEnabled
preferredMfa = preferredMFASetting
}
}
this.softwareTokenMfaSettings = totp?.let { it ->
val preferredMFASetting = it.mfaPreferred
?: (userPreference.preferred == MFAType.TOTP && it.mfaEnabled)
SoftwareTokenMfaSettingsType.invoke {
enabled = it.mfaEnabled
preferredMfa = preferredMFASetting
}
}
}?.also {
onSuccess.call()
}
}
}?.also {
onSuccess.call()
}
} ?: onError.accept(SignedOutException())
} catch (error: Exception) {
onError.accept(
CognitoAuthExceptionConverter.lookup(
error,
"Amazon Cognito cannot update the MFA preferences"
} ?: onError.accept(SignedOutException())
} catch (error: Exception) {
onError.accept(
CognitoAuthExceptionConverter.lookup(
error,
"Amazon Cognito cannot update the MFA preferences"
)
)
)
}
}
}
}

else -> onError.accept(InvalidStateException())
else -> onError.accept(InvalidStateException())
}
}
}
}, {
onError.accept(it)
gpanshu marked this conversation as resolved.
Show resolved Hide resolved
})
}

private fun verifyTotp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ data class UserMFAPreference(
/**
* Input for updating the MFA preference for a MFA Type
*/
enum class MFAPreference(internal val mfaEnabled: Boolean, internal val mfaPreferred: Boolean? = null) {
enum class MFAPreference(
internal val mfaEnabled: Boolean,
internal val mfaPreferred: Boolean? = null
) {
/**
* MFA not enabled
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ import com.amplifyframework.auth.exceptions.ServiceException
* Could not perform the action because there are incorrect parameters.
* @param cause The underlying cause of this exception
*/
open class InvalidParameterException(cause: Throwable?) :
open class InvalidParameterException(cause: Throwable? = null) :
ServiceException("One or more parameters are incorrect.", "Enter correct parameters.", cause)
Loading
Loading