Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -313,13 +313,13 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
options: AuthResetPasswordOptions,
onSuccess: Consumer<AuthResetPasswordResult>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.resetPassword(username, options) }
) = enqueue(onSuccess, onError) { useCaseFactory.resetPassword().execute(username, options) }

override fun resetPassword(
username: String,
onSuccess: Consumer<AuthResetPasswordResult>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.resetPassword(username) }
) = enqueue(onSuccess, onError) { useCaseFactory.resetPassword().execute(username) }

override fun confirmResetPassword(
username: String,
Expand All @@ -329,7 +329,7 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
onSuccess: Action,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) {
queueFacade.confirmResetPassword(username, newPassword, confirmationCode, options)
useCaseFactory.confirmResetPassword().execute(username, newPassword, confirmationCode, options)
}

override fun confirmResetPassword(
Expand All @@ -338,14 +338,16 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
confirmationCode: String,
onSuccess: Action,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.confirmResetPassword(username, newPassword, confirmationCode) }
) = enqueue(onSuccess, onError) {
useCaseFactory.confirmResetPassword().execute(username, newPassword, confirmationCode)
}

override fun updatePassword(
oldPassword: String,
newPassword: String,
onSuccess: Action,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.updatePassword(oldPassword, newPassword) }
) = enqueue(onSuccess, onError) { useCaseFactory.updatePassword().execute(oldPassword, newPassword) }

override fun fetchUserAttributes(onSuccess: Consumer<List<AuthUserAttribute>>, onError: Consumer<AuthException>) =
enqueue(onSuccess, onError) { useCaseFactory.fetchUserAttributes().execute() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ import com.amplifyframework.auth.AuthProvider
import com.amplifyframework.auth.AuthSession
import com.amplifyframework.auth.cognito.options.FederateToIdentityPoolOptions
import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
import com.amplifyframework.auth.options.AuthConfirmResetPasswordOptions
import com.amplifyframework.auth.options.AuthConfirmSignInOptions
import com.amplifyframework.auth.options.AuthConfirmSignUpOptions
import com.amplifyframework.auth.options.AuthFetchSessionOptions
import com.amplifyframework.auth.options.AuthResendSignUpCodeOptions
import com.amplifyframework.auth.options.AuthResetPasswordOptions
import com.amplifyframework.auth.options.AuthSignInOptions
import com.amplifyframework.auth.options.AuthSignOutOptions
import com.amplifyframework.auth.options.AuthSignUpOptions
import com.amplifyframework.auth.options.AuthWebUISignInOptions
import com.amplifyframework.auth.result.AuthResetPasswordResult
import com.amplifyframework.auth.result.AuthSignInResult
import com.amplifyframework.auth.result.AuthSignOutResult
import com.amplifyframework.auth.result.AuthSignUpResult
Expand Down Expand Up @@ -194,60 +191,6 @@ internal class KotlinAuthFacadeInternal(private val delegate: RealAWSCognitoAuth
)
}

suspend fun resetPassword(username: String): AuthResetPasswordResult = suspendCoroutine { continuation ->
delegate.resetPassword(
username,
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun resetPassword(username: String, options: AuthResetPasswordOptions): AuthResetPasswordResult =
suspendCoroutine { continuation ->
delegate.resetPassword(
username,
options,
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun confirmResetPassword(username: String, newPassword: String, confirmationCode: String) =
suspendCoroutine { continuation ->
delegate.confirmResetPassword(
username,
newPassword,
confirmationCode,
{ continuation.resume(Unit) },
{ continuation.resumeWithException(it) }
)
}

suspend fun confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String,
options: AuthConfirmResetPasswordOptions
) = suspendCoroutine { continuation ->
delegate.confirmResetPassword(
username,
newPassword,
confirmationCode,
options,
{ continuation.resume(Unit) },
{ continuation.resumeWithException(it) }
)
}

suspend fun updatePassword(oldPassword: String, newPassword: String) = suspendCoroutine { continuation ->
delegate.updatePassword(
oldPassword,
newPassword,
{ continuation.resume(Unit) },
{ continuation.resumeWithException(it) }
)
}

suspend fun signOut(): AuthSignOutResult = suspendCoroutine { continuation ->
delegate.signOut { continuation.resume(it) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package com.amplifyframework.auth.cognito
import android.app.Activity
import android.content.Intent
import androidx.annotation.WorkerThread
import aws.sdk.kotlin.services.cognitoidentityprovider.confirmForgotPassword
import aws.sdk.kotlin.services.cognitoidentityprovider.getUser
import aws.sdk.kotlin.services.cognitoidentityprovider.model.AnalyticsMetadataType
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChallengeNameType
import aws.sdk.kotlin.services.cognitoidentityprovider.model.ChangePasswordRequest
import aws.sdk.kotlin.services.cognitoidentityprovider.model.EmailMfaSettingsType
import aws.sdk.kotlin.services.cognitoidentityprovider.model.SmsMfaSettingsType
import aws.sdk.kotlin.services.cognitoidentityprovider.model.SoftwareTokenMfaSettingsType
Expand Down Expand Up @@ -58,7 +56,6 @@ import com.amplifyframework.auth.cognito.helpers.getMFATypeOrNull
import com.amplifyframework.auth.cognito.helpers.identityProviderName
import com.amplifyframework.auth.cognito.helpers.isMfaSetupSelectionChallenge
import com.amplifyframework.auth.cognito.helpers.value
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmResetPasswordOptions
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignInOptions
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthConfirmSignUpOptions
import com.amplifyframework.auth.cognito.options.AWSCognitoAuthResendSignUpCodeOptions
Expand All @@ -73,25 +70,22 @@ import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
import com.amplifyframework.auth.cognito.result.GlobalSignOutError
import com.amplifyframework.auth.cognito.result.HostedUIError
import com.amplifyframework.auth.cognito.result.RevokeTokenError
import com.amplifyframework.auth.cognito.usecases.ResetPasswordUseCase
import com.amplifyframework.auth.cognito.util.toAuthCodeDeliveryDetails
import com.amplifyframework.auth.exceptions.ConfigurationException
import com.amplifyframework.auth.exceptions.InvalidStateException
import com.amplifyframework.auth.exceptions.NotAuthorizedException
import com.amplifyframework.auth.exceptions.ServiceException
import com.amplifyframework.auth.exceptions.SessionExpiredException
import com.amplifyframework.auth.exceptions.SignedOutException
import com.amplifyframework.auth.exceptions.UnknownException
import com.amplifyframework.auth.options.AuthConfirmResetPasswordOptions
import com.amplifyframework.auth.options.AuthConfirmSignInOptions
import com.amplifyframework.auth.options.AuthConfirmSignUpOptions
import com.amplifyframework.auth.options.AuthFetchSessionOptions
import com.amplifyframework.auth.options.AuthResendSignUpCodeOptions
import com.amplifyframework.auth.options.AuthResetPasswordOptions
import com.amplifyframework.auth.options.AuthSignInOptions
import com.amplifyframework.auth.options.AuthSignOutOptions
import com.amplifyframework.auth.options.AuthSignUpOptions
import com.amplifyframework.auth.options.AuthWebUISignInOptions
import com.amplifyframework.auth.result.AuthResetPasswordResult
import com.amplifyframework.auth.result.AuthSignInResult
import com.amplifyframework.auth.result.AuthSignOutResult
import com.amplifyframework.auth.result.AuthSignUpResult
Expand Down Expand Up @@ -144,9 +138,7 @@ import java.util.concurrent.atomic.AtomicReference
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.drop
import kotlinx.coroutines.flow.onStart
Expand Down Expand Up @@ -486,21 +478,7 @@ internal class RealAWSCognitoAuthPlugin(
encodedContextData?.let { this.userContextData { encodedData = it } }
}

val deliveryDetails = response?.codeDeliveryDetails?.let { details ->
mapOf(
"DESTINATION" to details.destination,
"MEDIUM" to details.deliveryMedium?.value,
"ATTRIBUTE" to details.attributeName
)
}

val codeDeliveryDetails = AuthCodeDeliveryDetails(
deliveryDetails?.getValue("DESTINATION") ?: "",
AuthCodeDeliveryDetails.DeliveryMedium.fromString(
deliveryDetails?.getValue("MEDIUM")
),
deliveryDetails?.getValue("ATTRIBUTE")
)
val codeDeliveryDetails = response?.codeDeliveryDetails.toAuthCodeDeliveryDetails()
onSuccess.accept(codeDeliveryDetails)
logger.verbose("ResendSignUpCode Execution complete")
} catch (exception: Exception) {
Expand Down Expand Up @@ -1466,154 +1444,6 @@ internal class RealAWSCognitoAuthPlugin(
)
}

@OptIn(DelicateCoroutinesApi::class)
fun resetPassword(
username: String,
options: AuthResetPasswordOptions,
onSuccess: Consumer<AuthResetPasswordResult>,
onError: Consumer<AuthException>
) {
try {
val cognitoIdentityProviderClient = requireNotNull(
authEnvironment.cognitoAuthService.cognitoIdentityProviderClient
)

val appClient = requireNotNull(configuration.userPool?.appClient)
GlobalScope.launch {
val encodedData = authEnvironment.getUserContextData(username)
val pinpointEndpointId = authEnvironment.getPinpointEndpointId()

ResetPasswordUseCase(
cognitoIdentityProviderClient,
appClient,
configuration.userPool?.appClientSecret
).execute(
username,
options,
encodedData,
pinpointEndpointId,
onSuccess,
onError
)
}
} catch (ex: Exception) {
onError.accept(InvalidUserPoolConfigurationException())
}
}

fun resetPassword(
username: String,
onSuccess: Consumer<AuthResetPasswordResult>,
onError: Consumer<AuthException>
) {
resetPassword(username, AuthResetPasswordOptions.defaults(), onSuccess, onError)
}

fun confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String,
options: AuthConfirmResetPasswordOptions,
onSuccess: Action,
onError: Consumer<AuthException>
) {
authStateMachine.getCurrentState { authState ->
if (authState.authNState is AuthenticationState.NotConfigured) {
onError.accept(
ConfigurationException(
"Confirm Reset Password failed.",
"Cognito User Pool not configured. Please check amplifyconfiguration.json file."
)
)
return@getCurrentState
}

GlobalScope.launch {
try {
val encodedContextData = authEnvironment.getUserContextData(username)
val pinpointEndpointId = authEnvironment.getPinpointEndpointId()

authEnvironment.cognitoAuthService.cognitoIdentityProviderClient!!.confirmForgotPassword {
this.username = username
this.confirmationCode = confirmationCode
password = newPassword
secretHash = AuthHelper.getSecretHash(
username,
configuration.userPool?.appClient,
configuration.userPool?.appClientSecret
)
clientMetadata =
(options as? AWSCognitoAuthConfirmResetPasswordOptions)?.metadata ?: mapOf()
clientId = configuration.userPool?.appClient
encodedContextData?.let { this.userContextData { encodedData = it } }
pinpointEndpointId?.let {
this.analyticsMetadata = AnalyticsMetadataType.invoke { analyticsEndpointId = it }
}
}.let { onSuccess.call() }
} catch (ex: Exception) {
onError.accept(
CognitoAuthExceptionConverter.lookup(ex, AmplifyException.REPORT_BUG_TO_AWS_SUGGESTION)
)
}
}
}
}

fun confirmResetPassword(
username: String,
newPassword: String,
confirmationCode: String,
onSuccess: Action,
onError: Consumer<AuthException>
) {
confirmResetPassword(
username,
newPassword,
confirmationCode,
AuthConfirmResetPasswordOptions.defaults(),
onSuccess,
onError
)
}

fun updatePassword(oldPassword: String, newPassword: String, onSuccess: Action, onError: Consumer<AuthException>) {
authStateMachine.getCurrentState { authState ->
when (authState.authNState) {
// Check if user signed in
is AuthenticationState.SignedIn -> {
_updatePassword(oldPassword, newPassword, onSuccess, onError)
}
is AuthenticationState.SignedOut -> onError.accept(SignedOutException())
else -> onError.accept(InvalidStateException())
}
}
}

private fun _updatePassword(
oldPassword: String,
newPassword: String,
onSuccess: Action,
onError: Consumer<AuthException>
) {
GlobalScope.async {
val tokens = getSession().userPoolTokensResult
val changePasswordRequest = ChangePasswordRequest.invoke {
previousPassword = oldPassword
proposedPassword = newPassword
this.accessToken = tokens.value?.accessToken
}
try {
authEnvironment.cognitoAuthService
.cognitoIdentityProviderClient?.changePassword(
changePasswordRequest
)
onSuccess.call()
} catch (e: Exception) {
onError.accept(CognitoAuthExceptionConverter.lookup(e, e.toString()))
}
}
}

fun signOut(onComplete: Consumer<AuthSignOutResult>) {
signOut(AuthSignOutOptions.builder().build(), onComplete)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import aws.sdk.kotlin.services.cognitoidentityprovider.confirmSignUp
import aws.sdk.kotlin.services.cognitoidentityprovider.model.AnalyticsMetadataType
import aws.sdk.kotlin.services.cognitoidentityprovider.model.AttributeType
import aws.sdk.kotlin.services.cognitoidentityprovider.signUp
import com.amplifyframework.auth.AuthCodeDeliveryDetails
import com.amplifyframework.auth.cognito.AuthEnvironment
import com.amplifyframework.auth.cognito.helpers.AuthHelper
import com.amplifyframework.auth.cognito.util.toAuthCodeDeliveryDetails
import com.amplifyframework.auth.result.AuthSignUpResult
import com.amplifyframework.auth.result.step.AuthNextSignUpStep
import com.amplifyframework.auth.result.step.AuthSignUpStep
Expand Down Expand Up @@ -68,13 +68,7 @@ internal object SignUpCognitoActions : SignUpActions {
}
}

val codeDeliveryDetails = AuthCodeDeliveryDetails(
response?.codeDeliveryDetails?.destination ?: "",
AuthCodeDeliveryDetails.DeliveryMedium.fromString(
response?.codeDeliveryDetails?.deliveryMedium?.value
),
response?.codeDeliveryDetails?.attributeName
)
val codeDeliveryDetails = response?.codeDeliveryDetails.toAuthCodeDeliveryDetails()
val signUpData = SignUpData(
username,
event.signUpData.validationData,
Expand Down
Loading