Skip to content

Commit d4cbe2c

Browse files
authored
chore(auth): Revert fetchAuthSession changes (#3033)
1 parent bddf064 commit d4cbe2c

File tree

13 files changed

+212
-393
lines changed

13 files changed

+212
-393
lines changed

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthPlugin.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
176176
logger
177177
)
178178

179-
useCaseFactory = AuthUseCaseFactory(authEnvironment, authStateMachine)
179+
useCaseFactory = AuthUseCaseFactory(realPlugin, authEnvironment, authStateMachine)
180180

181181
blockQueueChannelWhileConfiguring()
182182
}
@@ -291,10 +291,10 @@ class AWSCognitoAuthPlugin : AuthPlugin<AWSCognitoAuthService>() {
291291
options: AuthFetchSessionOptions,
292292
onSuccess: Consumer<AuthSession>,
293293
onError: Consumer<AuthException>
294-
) = enqueue(onSuccess, onError) { useCaseFactory.fetchAuthSession().execute(options) }
294+
) = enqueue(onSuccess, onError) { queueFacade.fetchAuthSession(options) }
295295

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

299299
override fun rememberDevice(onSuccess: Action, onError: Consumer<AuthException>) =
300300
enqueue(onSuccess, onError) { useCaseFactory.rememberDevice().execute() }

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/AWSCognitoAuthSession.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ internal fun AmplifyCredential.isValid(): Boolean = when (this) {
6666
else -> false
6767
}
6868

69-
internal fun AmplifyCredential.getCognitoSession(exception: AuthException? = null): AWSCognitoAuthSession {
69+
internal fun AmplifyCredential.getCognitoSession(
70+
exception: AuthException? = null
71+
): AWSAuthSessionBehavior<AWSCognitoUserPoolTokens> {
7072
fun getCredentialsResult(
7173
awsCredentials: CognitoCredentials,
7274
exception: AuthException?

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/KotlinAuthFacadeInternal.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ package com.amplifyframework.auth.cognito
1818
import android.app.Activity
1919
import android.content.Intent
2020
import com.amplifyframework.auth.AuthProvider
21+
import com.amplifyframework.auth.AuthSession
2122
import com.amplifyframework.auth.cognito.options.FederateToIdentityPoolOptions
2223
import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
2324
import com.amplifyframework.auth.options.AuthConfirmSignInOptions
25+
import com.amplifyframework.auth.options.AuthFetchSessionOptions
2426
import com.amplifyframework.auth.options.AuthSignInOptions
2527
import com.amplifyframework.auth.options.AuthSignOutOptions
2628
import com.amplifyframework.auth.options.AuthWebUISignInOptions
@@ -116,6 +118,21 @@ internal class KotlinAuthFacadeInternal(private val delegate: RealAWSCognitoAuth
116118
delegate.handleWebUISignInResponse(intent)
117119
}
118120

121+
suspend fun fetchAuthSession(): AuthSession = suspendCoroutine { continuation ->
122+
delegate.fetchAuthSession(
123+
{ continuation.resume(it) },
124+
{ continuation.resumeWithException(it) }
125+
)
126+
}
127+
128+
suspend fun fetchAuthSession(options: AuthFetchSessionOptions): AuthSession = suspendCoroutine { continuation ->
129+
delegate.fetchAuthSession(
130+
options,
131+
{ continuation.resume(it) },
132+
{ continuation.resumeWithException(it) }
133+
)
134+
}
135+
119136
suspend fun signOut(): AuthSignOutResult = suspendCoroutine { continuation ->
120137
delegate.signOut { continuation.resume(it) }
121138
}

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/RealAWSCognitoAuthPlugin.kt

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ import com.amplifyframework.auth.AuthCodeDeliveryDetails
2929
import com.amplifyframework.auth.AuthException
3030
import com.amplifyframework.auth.AuthFactorType
3131
import com.amplifyframework.auth.AuthProvider
32+
import com.amplifyframework.auth.AuthSession
3233
import com.amplifyframework.auth.MFAType
3334
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidOauthConfigurationException
3435
import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
3536
import com.amplifyframework.auth.cognito.exceptions.invalidstate.SignedInException
3637
import com.amplifyframework.auth.cognito.exceptions.service.HostedUISignOutException
38+
import com.amplifyframework.auth.cognito.exceptions.service.InvalidAccountTypeException
3739
import com.amplifyframework.auth.cognito.exceptions.service.InvalidParameterException
3840
import com.amplifyframework.auth.cognito.exceptions.service.UserCancelledException
3941
import com.amplifyframework.auth.cognito.helpers.HostedUIHelper
@@ -55,9 +57,15 @@ import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
5557
import com.amplifyframework.auth.cognito.result.GlobalSignOutError
5658
import com.amplifyframework.auth.cognito.result.HostedUIError
5759
import com.amplifyframework.auth.cognito.result.RevokeTokenError
60+
import com.amplifyframework.auth.exceptions.ConfigurationException
5861
import com.amplifyframework.auth.exceptions.InvalidStateException
62+
import com.amplifyframework.auth.exceptions.NotAuthorizedException
63+
import com.amplifyframework.auth.exceptions.ServiceException
64+
import com.amplifyframework.auth.exceptions.SessionExpiredException
65+
import com.amplifyframework.auth.exceptions.SignedOutException
5966
import com.amplifyframework.auth.exceptions.UnknownException
6067
import com.amplifyframework.auth.options.AuthConfirmSignInOptions
68+
import com.amplifyframework.auth.options.AuthFetchSessionOptions
6169
import com.amplifyframework.auth.options.AuthSignInOptions
6270
import com.amplifyframework.auth.options.AuthSignOutOptions
6371
import com.amplifyframework.auth.options.AuthWebUISignInOptions
@@ -104,6 +112,9 @@ import java.lang.ref.WeakReference
104112
import java.util.concurrent.CountDownLatch
105113
import java.util.concurrent.TimeUnit
106114
import java.util.concurrent.atomic.AtomicReference
115+
import kotlin.coroutines.resume
116+
import kotlin.coroutines.resumeWithException
117+
import kotlin.coroutines.suspendCoroutine
107118
import kotlinx.coroutines.flow.collect
108119
import kotlinx.coroutines.flow.takeWhile
109120

@@ -980,6 +991,142 @@ internal class RealAWSCognitoAuthPlugin(
980991
}
981992
}
982993

994+
private suspend fun getSession(): AWSCognitoAuthSession = suspendCoroutine { continuation ->
995+
fetchAuthSession(
996+
{ authSession ->
997+
if (authSession is AWSCognitoAuthSession) {
998+
continuation.resume(authSession)
999+
} else {
1000+
continuation.resumeWithException(
1001+
UnknownException(
1002+
message = "fetchAuthSession did not return a type of AWSCognitoAuthSession"
1003+
)
1004+
)
1005+
}
1006+
},
1007+
{ continuation.resumeWithException(it) }
1008+
)
1009+
}
1010+
1011+
fun fetchAuthSession(onSuccess: Consumer<AuthSession>, onError: Consumer<AuthException>) {
1012+
fetchAuthSession(AuthFetchSessionOptions.defaults(), onSuccess, onError)
1013+
}
1014+
1015+
fun fetchAuthSession(
1016+
options: AuthFetchSessionOptions,
1017+
onSuccess: Consumer<AuthSession>,
1018+
onError: Consumer<AuthException>
1019+
) {
1020+
val forceRefresh = options.forceRefresh
1021+
authStateMachine.getCurrentState { authState ->
1022+
when (val authZState = authState.authZState) {
1023+
is AuthorizationState.Configured -> {
1024+
authStateMachine.send(AuthorizationEvent(AuthorizationEvent.EventType.FetchUnAuthSession))
1025+
_fetchAuthSession(onSuccess)
1026+
}
1027+
is AuthorizationState.SessionEstablished -> {
1028+
val credential = authZState.amplifyCredential
1029+
if (!credential.isValid() || forceRefresh) {
1030+
if (credential is AmplifyCredential.IdentityPoolFederated) {
1031+
authStateMachine.send(
1032+
AuthorizationEvent(
1033+
AuthorizationEvent.EventType.StartFederationToIdentityPool(
1034+
credential.federatedToken,
1035+
credential.identityId,
1036+
credential
1037+
)
1038+
)
1039+
)
1040+
} else {
1041+
authStateMachine.send(
1042+
AuthorizationEvent(AuthorizationEvent.EventType.RefreshSession(credential))
1043+
)
1044+
}
1045+
_fetchAuthSession(onSuccess)
1046+
} else {
1047+
onSuccess.accept(credential.getCognitoSession())
1048+
}
1049+
}
1050+
is AuthorizationState.Error -> {
1051+
val error = authZState.exception
1052+
if (error is SessionError) {
1053+
val amplifyCredential = error.amplifyCredential
1054+
if (amplifyCredential is AmplifyCredential.IdentityPoolFederated) {
1055+
authStateMachine.send(
1056+
AuthorizationEvent(
1057+
AuthorizationEvent.EventType.StartFederationToIdentityPool(
1058+
amplifyCredential.federatedToken,
1059+
amplifyCredential.identityId,
1060+
amplifyCredential
1061+
)
1062+
)
1063+
)
1064+
} else {
1065+
authStateMachine.send(
1066+
AuthorizationEvent(AuthorizationEvent.EventType.RefreshSession(amplifyCredential))
1067+
)
1068+
}
1069+
_fetchAuthSession(onSuccess)
1070+
} else {
1071+
onError.accept(InvalidStateException())
1072+
}
1073+
}
1074+
else -> onError.accept(InvalidStateException())
1075+
}
1076+
}
1077+
}
1078+
1079+
private fun _fetchAuthSession(onSuccess: Consumer<AuthSession>) {
1080+
val token = StateChangeListenerToken()
1081+
authStateMachine.listen(
1082+
token,
1083+
{ authState ->
1084+
when (val authZState = authState.authZState) {
1085+
is AuthorizationState.SessionEstablished -> {
1086+
authStateMachine.cancel(token)
1087+
onSuccess.accept(authZState.amplifyCredential.getCognitoSession())
1088+
}
1089+
is AuthorizationState.Error -> {
1090+
authStateMachine.cancel(token)
1091+
when (val error = authZState.exception) {
1092+
is SessionError -> {
1093+
when (val innerException = error.exception) {
1094+
is SignedOutException -> {
1095+
onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
1096+
}
1097+
is SessionExpiredException -> {
1098+
onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
1099+
sendHubEvent(AuthChannelEventName.SESSION_EXPIRED.toString())
1100+
}
1101+
is ServiceException -> {
1102+
onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
1103+
}
1104+
is NotAuthorizedException -> {
1105+
onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
1106+
}
1107+
else -> {
1108+
val errorResult = UnknownException("Fetch auth session failed.", innerException)
1109+
onSuccess.accept(error.amplifyCredential.getCognitoSession(errorResult))
1110+
}
1111+
}
1112+
}
1113+
is ConfigurationException -> {
1114+
val errorResult = InvalidAccountTypeException(error)
1115+
onSuccess.accept(AmplifyCredential.Empty.getCognitoSession(errorResult))
1116+
}
1117+
else -> {
1118+
val errorResult = UnknownException("Fetch auth session failed.", error)
1119+
onSuccess.accept(AmplifyCredential.Empty.getCognitoSession(errorResult))
1120+
}
1121+
}
1122+
}
1123+
else -> Unit
1124+
}
1125+
},
1126+
null
1127+
)
1128+
}
1129+
9831130
fun signOut(onComplete: Consumer<AuthSignOutResult>) {
9841131
signOut(AuthSignOutOptions.builder().build(), onComplete)
9851132
}

aws-auth-cognito/src/main/java/com/amplifyframework/auth/cognito/usecases/AuthUseCaseFactory.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ package com.amplifyframework.auth.cognito.usecases
1717

1818
import com.amplifyframework.auth.cognito.AuthEnvironment
1919
import com.amplifyframework.auth.cognito.AuthStateMachine
20+
import com.amplifyframework.auth.cognito.RealAWSCognitoAuthPlugin
2021
import com.amplifyframework.auth.cognito.helpers.WebAuthnHelper
2122
import com.amplifyframework.auth.cognito.requireIdentityProviderClient
2223
import com.amplifyframework.auth.plugins.core.AuthHubEventEmitter
2324

2425
internal class AuthUseCaseFactory(
26+
private val plugin: RealAWSCognitoAuthPlugin,
2527
private val authEnvironment: AuthEnvironment,
2628
private val stateMachine: AuthStateMachine,
2729
private val hubEmitter: AuthHubEventEmitter = AuthHubEventEmitter()
2830
) {
2931

30-
fun fetchAuthSession() = FetchAuthSessionUseCase(
31-
stateMachine = stateMachine
32-
)
32+
fun fetchAuthSession() = FetchAuthSessionUseCase(plugin)
3333

3434
fun associateWebAuthnCredential() = AssociateWebAuthnCredentialUseCase(
3535
client = authEnvironment.requireIdentityProviderClient(),

0 commit comments

Comments
 (0)