Skip to content

Commit

Permalink
chore(auth): Move Attribute management into usecase classes (#2984)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattcreaser authored Feb 25, 2025
1 parent 416c971 commit 7f4e639
Show file tree
Hide file tree
Showing 16 changed files with 804 additions and 902 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -348,53 +348,53 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
) = enqueue(onSuccess, onError) { queueFacade.updatePassword(oldPassword, newPassword) }

override fun fetchUserAttributes(onSuccess: Consumer<List<AuthUserAttribute>>, onError: Consumer<AuthException>) =
enqueue(onSuccess, onError) { queueFacade.fetchUserAttributes() }
enqueue(onSuccess, onError) { useCaseFactory.fetchUserAttributes().execute() }

override fun updateUserAttribute(
attribute: AuthUserAttribute,
options: AuthUpdateUserAttributeOptions,
onSuccess: Consumer<AuthUpdateAttributeResult>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.updateUserAttribute(attribute, options) }
) = enqueue(onSuccess, onError) { useCaseFactory.updateUserAttributes().execute(attribute, options) }

override fun updateUserAttribute(
attribute: AuthUserAttribute,
onSuccess: Consumer<AuthUpdateAttributeResult>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.updateUserAttribute(attribute) }
) = enqueue(onSuccess, onError) { useCaseFactory.updateUserAttributes().execute(attribute) }

override fun updateUserAttributes(
attributes: List<AuthUserAttribute>,
options: AuthUpdateUserAttributesOptions,
onSuccess: Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.updateUserAttributes(attributes, options) }
) = enqueue(onSuccess, onError) { useCaseFactory.updateUserAttributes().execute(attributes, options) }

override fun updateUserAttributes(
attributes: List<AuthUserAttribute>,
onSuccess: Consumer<Map<AuthUserAttributeKey, AuthUpdateAttributeResult>>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.updateUserAttributes(attributes) }
) = enqueue(onSuccess, onError) { useCaseFactory.updateUserAttributes().execute(attributes) }

override fun resendUserAttributeConfirmationCode(
attributeKey: AuthUserAttributeKey,
options: AuthResendUserAttributeConfirmationCodeOptions,
onSuccess: Consumer<AuthCodeDeliveryDetails>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.resendUserAttributeConfirmationCode(attributeKey, options) }
) = enqueue(onSuccess, onError) { useCaseFactory.resendUserAttributeConfirmation().execute(attributeKey, options) }

override fun resendUserAttributeConfirmationCode(
attributeKey: AuthUserAttributeKey,
onSuccess: Consumer<AuthCodeDeliveryDetails>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.resendUserAttributeConfirmationCode(attributeKey) }
) = enqueue(onSuccess, onError) { useCaseFactory.resendUserAttributeConfirmation().execute(attributeKey) }

override fun confirmUserAttribute(
attributeKey: AuthUserAttributeKey,
confirmationCode: String,
onSuccess: Action,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { queueFacade.confirmUserAttribute(attributeKey, confirmationCode) }
) = enqueue(onSuccess, onError) { useCaseFactory.confirmUserAttribute().execute(attributeKey, confirmationCode) }

override fun getCurrentUser(onSuccess: Consumer<AuthUser>, onError: Consumer<AuthException>) =
enqueue(onSuccess, onError) { useCaseFactory.getCurrentUser().execute() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.amplifyframework.auth.AWSAuthSessionBehavior
import com.amplifyframework.auth.AWSCognitoUserPoolTokens
import com.amplifyframework.auth.AWSCredentials
import com.amplifyframework.auth.AuthException
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
import com.amplifyframework.auth.cognito.helpers.SessionHelper
import com.amplifyframework.auth.exceptions.ConfigurationException
import com.amplifyframework.auth.exceptions.InvalidStateException
Expand Down Expand Up @@ -54,20 +55,20 @@ data class AWSCognitoAuthSession internal constructor(
override val accessToken = userPoolTokensResult.value?.accessToken
}

internal fun AmplifyCredential.isValid(): Boolean {
return when (this) {
is AmplifyCredential.UserPool -> SessionHelper.isValidTokens(signedInData.cognitoUserPoolTokens)
is AmplifyCredential.UserAndIdentityPool ->
SessionHelper.isValidTokens(signedInData.cognitoUserPoolTokens) && SessionHelper.isValidSession(credentials)
is AmplifyCredential.IdentityPoolTypeCredential -> SessionHelper.isValidSession(credentials)
else -> false
}
internal fun AWSCognitoAuthSession.requireAccessToken(): String =
accessToken ?: throw InvalidUserPoolConfigurationException()

internal fun AmplifyCredential.isValid(): Boolean = when (this) {
is AmplifyCredential.UserPool -> SessionHelper.isValidTokens(signedInData.cognitoUserPoolTokens)
is AmplifyCredential.UserAndIdentityPool ->
SessionHelper.isValidTokens(signedInData.cognitoUserPoolTokens) && SessionHelper.isValidSession(credentials)
is AmplifyCredential.IdentityPoolTypeCredential -> SessionHelper.isValidSession(credentials)
else -> false
}

internal fun AmplifyCredential.getCognitoSession(
exception: AuthException? = null
): AWSAuthSessionBehavior<AWSCognitoUserPoolTokens> {

fun getCredentialsResult(
awsCredentials: CognitoCredentials,
exception: AuthException?
Expand All @@ -83,15 +84,14 @@ internal fun AmplifyCredential.getCognitoSession(
} ?: AuthSessionResult.failure(UnknownException("Failed to fetch AWS credentials."))
}

fun getIdentityIdResult(identityId: String, exception: AuthException?): AuthSessionResult<String> {
return if (exception != null && exception !is SignedOutException) {
fun getIdentityIdResult(identityId: String, exception: AuthException?): AuthSessionResult<String> =
if (exception != null && exception !is SignedOutException) {
AuthSessionResult.failure(exception)
} else if (identityId.isNotEmpty()) {
AuthSessionResult.success(identityId)
} else {
AuthSessionResult.failure(UnknownException("Failed to fetch identity id."))
}
}

fun getUserSubResult(userPoolTokens: CognitoUserPoolTokens?, exception: AuthException?): AuthSessionResult<String> {
if (exception != null && exception !is SignedOutException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import android.content.Intent
import com.amplifyframework.auth.AuthCodeDeliveryDetails
import com.amplifyframework.auth.AuthProvider
import com.amplifyframework.auth.AuthSession
import com.amplifyframework.auth.AuthUserAttribute
import com.amplifyframework.auth.AuthUserAttributeKey
import com.amplifyframework.auth.TOTPSetupDetails
import com.amplifyframework.auth.cognito.options.FederateToIdentityPoolOptions
import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
Expand All @@ -30,20 +28,16 @@ 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.AuthResendUserAttributeConfirmationCodeOptions
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.AuthUpdateUserAttributeOptions
import com.amplifyframework.auth.options.AuthUpdateUserAttributesOptions
import com.amplifyframework.auth.options.AuthVerifyTOTPSetupOptions
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
import com.amplifyframework.auth.result.AuthUpdateAttributeResult
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
Expand Down Expand Up @@ -256,87 +250,6 @@ internal class KotlinAuthFacadeInternal(private val delegate: RealAWSCognitoAuth
)
}

suspend fun fetchUserAttributes(): List<AuthUserAttribute> = suspendCoroutine { continuation ->
delegate.fetchUserAttributes(
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun updateUserAttribute(attribute: AuthUserAttribute): AuthUpdateAttributeResult =
suspendCoroutine { continuation ->
delegate.updateUserAttribute(
attribute,
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun updateUserAttribute(
attribute: AuthUserAttribute,
options: AuthUpdateUserAttributeOptions
): AuthUpdateAttributeResult = suspendCoroutine { continuation ->
delegate.updateUserAttribute(
attribute,
options,
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun updateUserAttributes(
attributes: List<AuthUserAttribute>
): Map<AuthUserAttributeKey, AuthUpdateAttributeResult> = suspendCoroutine { continuation ->
delegate.updateUserAttributes(
attributes,
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun updateUserAttributes(
attributes: List<AuthUserAttribute>,
options: AuthUpdateUserAttributesOptions
): Map<AuthUserAttributeKey, AuthUpdateAttributeResult> = suspendCoroutine { continuation ->
delegate.updateUserAttributes(
attributes,
options,
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun resendUserAttributeConfirmationCode(attributeKey: AuthUserAttributeKey): AuthCodeDeliveryDetails =
suspendCoroutine { continuation ->
delegate.resendUserAttributeConfirmationCode(
attributeKey,
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun resendUserAttributeConfirmationCode(
attributeKey: AuthUserAttributeKey,
options: AuthResendUserAttributeConfirmationCodeOptions
): AuthCodeDeliveryDetails = suspendCoroutine { continuation ->
delegate.resendUserAttributeConfirmationCode(
attributeKey,
options,
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun confirmUserAttribute(attributeKey: AuthUserAttributeKey, confirmationCode: String) =
suspendCoroutine { continuation ->
delegate.confirmUserAttribute(
attributeKey,
confirmationCode,
{ continuation.resume(Unit) },
{ continuation.resumeWithException(it) }
)
}

suspend fun signOut(): AuthSignOutResult = suspendCoroutine { continuation ->
delegate.signOut { continuation.resume(it) }
}
Expand Down
Loading

0 comments on commit 7f4e639

Please sign in to comment.