Skip to content

chore(auth): Revert fetchAuthSession changes #3033

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

Merged
merged 3 commits into from
Apr 29, 2025
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 @@ -176,7 +176,7 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
logger
)

useCaseFactory = AuthUseCaseFactory(authEnvironment, authStateMachine)
useCaseFactory = AuthUseCaseFactory(realPlugin, authEnvironment, authStateMachine)

blockQueueChannelWhileConfiguring()
}
Expand Down Expand Up @@ -291,10 +291,10 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
options: AuthFetchSessionOptions,
onSuccess: Consumer<AuthSession>,
onError: Consumer<AuthException>
) = enqueue(onSuccess, onError) { useCaseFactory.fetchAuthSession().execute(options) }
) = enqueue(onSuccess, onError) { queueFacade.fetchAuthSession(options) }

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

override fun rememberDevice(onSuccess: Action, onError: Consumer<AuthException>) =
enqueue(onSuccess, onError) { useCaseFactory.rememberDevice().execute() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ internal fun AmplifyCredential.isValid(): Boolean = when (this) {
else -> false
}

internal fun AmplifyCredential.getCognitoSession(exception: AuthException? = null): AWSCognitoAuthSession {
internal fun AmplifyCredential.getCognitoSession(
exception: AuthException? = null
): AWSAuthSessionBehavior<AWSCognitoUserPoolTokens> {
fun getCredentialsResult(
awsCredentials: CognitoCredentials,
exception: AuthException?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ package com.amplifyframework.auth.cognito
import android.app.Activity
import android.content.Intent
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.AuthConfirmSignInOptions
import com.amplifyframework.auth.options.AuthFetchSessionOptions
import com.amplifyframework.auth.options.AuthSignInOptions
import com.amplifyframework.auth.options.AuthSignOutOptions
import com.amplifyframework.auth.options.AuthWebUISignInOptions
Expand Down Expand Up @@ -116,6 +118,21 @@ internal class KotlinAuthFacadeInternal(private val delegate: RealAWSCognitoAuth
delegate.handleWebUISignInResponse(intent)
}

suspend fun fetchAuthSession(): AuthSession = suspendCoroutine { continuation ->
delegate.fetchAuthSession(
{ continuation.resume(it) },
{ continuation.resumeWithException(it) }
)
}

suspend fun fetchAuthSession(options: AuthFetchSessionOptions): AuthSession = suspendCoroutine { continuation ->
delegate.fetchAuthSession(
options,
{ continuation.resume(it) },
{ 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 @@ -29,11 +29,13 @@ import com.amplifyframework.auth.AuthCodeDeliveryDetails
import com.amplifyframework.auth.AuthException
import com.amplifyframework.auth.AuthFactorType
import com.amplifyframework.auth.AuthProvider
import com.amplifyframework.auth.AuthSession
import com.amplifyframework.auth.MFAType
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidOauthConfigurationException
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
import com.amplifyframework.auth.cognito.exceptions.invalidstate.SignedInException
import com.amplifyframework.auth.cognito.exceptions.service.HostedUISignOutException
import com.amplifyframework.auth.cognito.exceptions.service.InvalidAccountTypeException
import com.amplifyframework.auth.cognito.exceptions.service.InvalidParameterException
import com.amplifyframework.auth.cognito.exceptions.service.UserCancelledException
import com.amplifyframework.auth.cognito.helpers.HostedUIHelper
Expand All @@ -55,9 +57,15 @@ 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.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.AuthConfirmSignInOptions
import com.amplifyframework.auth.options.AuthFetchSessionOptions
import com.amplifyframework.auth.options.AuthSignInOptions
import com.amplifyframework.auth.options.AuthSignOutOptions
import com.amplifyframework.auth.options.AuthWebUISignInOptions
Expand Down Expand Up @@ -104,6 +112,9 @@ import java.lang.ref.WeakReference
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicReference
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.takeWhile

Expand Down Expand Up @@ -980,6 +991,142 @@ internal class RealAWSCognitoAuthPlugin(
}
}

private suspend fun getSession(): AWSCognitoAuthSession = suspendCoroutine { continuation ->
fetchAuthSession(
{ authSession ->
if (authSession is AWSCognitoAuthSession) {
continuation.resume(authSession)
} else {
continuation.resumeWithException(
UnknownException(
message = "fetchAuthSession did not return a type of AWSCognitoAuthSession"
)
)
}
},
{ continuation.resumeWithException(it) }
)
}

fun fetchAuthSession(onSuccess: Consumer<AuthSession>, onError: Consumer<AuthException>) {
fetchAuthSession(AuthFetchSessionOptions.defaults(), onSuccess, onError)
}

fun fetchAuthSession(
options: AuthFetchSessionOptions,
onSuccess: Consumer<AuthSession>,
onError: Consumer<AuthException>
) {
val forceRefresh = options.forceRefresh
authStateMachine.getCurrentState { authState ->
when (val authZState = authState.authZState) {
is AuthorizationState.Configured -> {
authStateMachine.send(AuthorizationEvent(AuthorizationEvent.EventType.FetchUnAuthSession))
_fetchAuthSession(onSuccess)
}
is AuthorizationState.SessionEstablished -> {
val credential = authZState.amplifyCredential
if (!credential.isValid() || forceRefresh) {
if (credential is AmplifyCredential.IdentityPoolFederated) {
authStateMachine.send(
AuthorizationEvent(
AuthorizationEvent.EventType.StartFederationToIdentityPool(
credential.federatedToken,
credential.identityId,
credential
)
)
)
} else {
authStateMachine.send(
AuthorizationEvent(AuthorizationEvent.EventType.RefreshSession(credential))
)
}
_fetchAuthSession(onSuccess)
} else {
onSuccess.accept(credential.getCognitoSession())
}
}
is AuthorizationState.Error -> {
val error = authZState.exception
if (error is SessionError) {
val amplifyCredential = error.amplifyCredential
if (amplifyCredential is AmplifyCredential.IdentityPoolFederated) {
authStateMachine.send(
AuthorizationEvent(
AuthorizationEvent.EventType.StartFederationToIdentityPool(
amplifyCredential.federatedToken,
amplifyCredential.identityId,
amplifyCredential
)
)
)
} else {
authStateMachine.send(
AuthorizationEvent(AuthorizationEvent.EventType.RefreshSession(amplifyCredential))
)
}
_fetchAuthSession(onSuccess)
} else {
onError.accept(InvalidStateException())
}
}
else -> onError.accept(InvalidStateException())
}
}
}

private fun _fetchAuthSession(onSuccess: Consumer<AuthSession>) {
val token = StateChangeListenerToken()
authStateMachine.listen(
token,
{ authState ->
when (val authZState = authState.authZState) {
is AuthorizationState.SessionEstablished -> {
authStateMachine.cancel(token)
onSuccess.accept(authZState.amplifyCredential.getCognitoSession())
}
is AuthorizationState.Error -> {
authStateMachine.cancel(token)
when (val error = authZState.exception) {
is SessionError -> {
when (val innerException = error.exception) {
is SignedOutException -> {
onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
}
is SessionExpiredException -> {
onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
sendHubEvent(AuthChannelEventName.SESSION_EXPIRED.toString())
}
is ServiceException -> {
onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
}
is NotAuthorizedException -> {
onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
}
else -> {
val errorResult = UnknownException("Fetch auth session failed.", innerException)
onSuccess.accept(error.amplifyCredential.getCognitoSession(errorResult))
}
}
}
is ConfigurationException -> {
val errorResult = InvalidAccountTypeException(error)
onSuccess.accept(AmplifyCredential.Empty.getCognitoSession(errorResult))
}
else -> {
val errorResult = UnknownException("Fetch auth session failed.", error)
onSuccess.accept(AmplifyCredential.Empty.getCognitoSession(errorResult))
}
}
}
else -> Unit
}
},
null
)
}

fun signOut(onComplete: Consumer<AuthSignOutResult>) {
signOut(AuthSignOutOptions.builder().build(), onComplete)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ package com.amplifyframework.auth.cognito.usecases

import com.amplifyframework.auth.cognito.AuthEnvironment
import com.amplifyframework.auth.cognito.AuthStateMachine
import com.amplifyframework.auth.cognito.RealAWSCognitoAuthPlugin
import com.amplifyframework.auth.cognito.helpers.WebAuthnHelper
import com.amplifyframework.auth.cognito.requireIdentityProviderClient
import com.amplifyframework.auth.plugins.core.AuthHubEventEmitter

internal class AuthUseCaseFactory(
private val plugin: RealAWSCognitoAuthPlugin,
private val authEnvironment: AuthEnvironment,
private val stateMachine: AuthStateMachine,
private val hubEmitter: AuthHubEventEmitter = AuthHubEventEmitter()
) {

fun fetchAuthSession() = FetchAuthSessionUseCase(
stateMachine = stateMachine
)
fun fetchAuthSession() = FetchAuthSessionUseCase(plugin)

fun associateWebAuthnCredential() = AssociateWebAuthnCredentialUseCase(
client = authEnvironment.requireIdentityProviderClient(),
Expand Down
Loading