@@ -29,13 +29,11 @@ import com.amplifyframework.auth.AuthCodeDeliveryDetails
2929import com.amplifyframework.auth.AuthException
3030import com.amplifyframework.auth.AuthFactorType
3131import com.amplifyframework.auth.AuthProvider
32- import com.amplifyframework.auth.AuthSession
3332import com.amplifyframework.auth.MFAType
3433import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidOauthConfigurationException
3534import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
3635import com.amplifyframework.auth.cognito.exceptions.invalidstate.SignedInException
3736import com.amplifyframework.auth.cognito.exceptions.service.HostedUISignOutException
38- import com.amplifyframework.auth.cognito.exceptions.service.InvalidAccountTypeException
3937import com.amplifyframework.auth.cognito.exceptions.service.InvalidParameterException
4038import com.amplifyframework.auth.cognito.exceptions.service.UserCancelledException
4139import com.amplifyframework.auth.cognito.helpers.HostedUIHelper
@@ -57,15 +55,9 @@ import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
5755import com.amplifyframework.auth.cognito.result.GlobalSignOutError
5856import com.amplifyframework.auth.cognito.result.HostedUIError
5957import com.amplifyframework.auth.cognito.result.RevokeTokenError
60- import com.amplifyframework.auth.exceptions.ConfigurationException
6158import 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
6659import com.amplifyframework.auth.exceptions.UnknownException
6760import com.amplifyframework.auth.options.AuthConfirmSignInOptions
68- import com.amplifyframework.auth.options.AuthFetchSessionOptions
6961import com.amplifyframework.auth.options.AuthSignInOptions
7062import com.amplifyframework.auth.options.AuthSignOutOptions
7163import com.amplifyframework.auth.options.AuthWebUISignInOptions
@@ -112,9 +104,6 @@ import java.lang.ref.WeakReference
112104import java.util.concurrent.CountDownLatch
113105import java.util.concurrent.TimeUnit
114106import java.util.concurrent.atomic.AtomicReference
115- import kotlin.coroutines.resume
116- import kotlin.coroutines.resumeWithException
117- import kotlin.coroutines.suspendCoroutine
118107import kotlinx.coroutines.flow.collect
119108import kotlinx.coroutines.flow.takeWhile
120109
@@ -990,142 +979,6 @@ internal class RealAWSCognitoAuthPlugin(
990979 }
991980 }
992981
993- private suspend fun getSession (): AWSCognitoAuthSession = suspendCoroutine { continuation ->
994- fetchAuthSession(
995- { authSession ->
996- if (authSession is AWSCognitoAuthSession ) {
997- continuation.resume(authSession)
998- } else {
999- continuation.resumeWithException(
1000- UnknownException (
1001- message = " fetchAuthSession did not return a type of AWSCognitoAuthSession"
1002- )
1003- )
1004- }
1005- },
1006- { continuation.resumeWithException(it) }
1007- )
1008- }
1009-
1010- fun fetchAuthSession (onSuccess : Consumer <AuthSession >, onError : Consumer <AuthException >) {
1011- fetchAuthSession(AuthFetchSessionOptions .defaults(), onSuccess, onError)
1012- }
1013-
1014- fun fetchAuthSession (
1015- options : AuthFetchSessionOptions ,
1016- onSuccess : Consumer <AuthSession >,
1017- onError : Consumer <AuthException >
1018- ) {
1019- val forceRefresh = options.forceRefresh
1020- authStateMachine.getCurrentState { authState ->
1021- when (val authZState = authState.authZState) {
1022- is AuthorizationState .Configured -> {
1023- authStateMachine.send(AuthorizationEvent (AuthorizationEvent .EventType .FetchUnAuthSession ))
1024- _fetchAuthSession (onSuccess)
1025- }
1026- is AuthorizationState .SessionEstablished -> {
1027- val credential = authZState.amplifyCredential
1028- if (! credential.isValid() || forceRefresh) {
1029- if (credential is AmplifyCredential .IdentityPoolFederated ) {
1030- authStateMachine.send(
1031- AuthorizationEvent (
1032- AuthorizationEvent .EventType .StartFederationToIdentityPool (
1033- credential.federatedToken,
1034- credential.identityId,
1035- credential
1036- )
1037- )
1038- )
1039- } else {
1040- authStateMachine.send(
1041- AuthorizationEvent (AuthorizationEvent .EventType .RefreshSession (credential))
1042- )
1043- }
1044- _fetchAuthSession (onSuccess)
1045- } else {
1046- onSuccess.accept(credential.getCognitoSession())
1047- }
1048- }
1049- is AuthorizationState .Error -> {
1050- val error = authZState.exception
1051- if (error is SessionError ) {
1052- val amplifyCredential = error.amplifyCredential
1053- if (amplifyCredential is AmplifyCredential .IdentityPoolFederated ) {
1054- authStateMachine.send(
1055- AuthorizationEvent (
1056- AuthorizationEvent .EventType .StartFederationToIdentityPool (
1057- amplifyCredential.federatedToken,
1058- amplifyCredential.identityId,
1059- amplifyCredential
1060- )
1061- )
1062- )
1063- } else {
1064- authStateMachine.send(
1065- AuthorizationEvent (AuthorizationEvent .EventType .RefreshSession (amplifyCredential))
1066- )
1067- }
1068- _fetchAuthSession (onSuccess)
1069- } else {
1070- onError.accept(InvalidStateException ())
1071- }
1072- }
1073- else -> onError.accept(InvalidStateException ())
1074- }
1075- }
1076- }
1077-
1078- private fun _fetchAuthSession (onSuccess : Consumer <AuthSession >) {
1079- val token = StateChangeListenerToken ()
1080- authStateMachine.listen(
1081- token,
1082- { authState ->
1083- when (val authZState = authState.authZState) {
1084- is AuthorizationState .SessionEstablished -> {
1085- authStateMachine.cancel(token)
1086- onSuccess.accept(authZState.amplifyCredential.getCognitoSession())
1087- }
1088- is AuthorizationState .Error -> {
1089- authStateMachine.cancel(token)
1090- when (val error = authZState.exception) {
1091- is SessionError -> {
1092- when (val innerException = error.exception) {
1093- is SignedOutException -> {
1094- onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
1095- }
1096- is SessionExpiredException -> {
1097- onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
1098- sendHubEvent(AuthChannelEventName .SESSION_EXPIRED .toString())
1099- }
1100- is ServiceException -> {
1101- onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
1102- }
1103- is NotAuthorizedException -> {
1104- onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
1105- }
1106- else -> {
1107- val errorResult = UnknownException (" Fetch auth session failed." , innerException)
1108- onSuccess.accept(error.amplifyCredential.getCognitoSession(errorResult))
1109- }
1110- }
1111- }
1112- is ConfigurationException -> {
1113- val errorResult = InvalidAccountTypeException (error)
1114- onSuccess.accept(AmplifyCredential .Empty .getCognitoSession(errorResult))
1115- }
1116- else -> {
1117- val errorResult = UnknownException (" Fetch auth session failed." , error)
1118- onSuccess.accept(AmplifyCredential .Empty .getCognitoSession(errorResult))
1119- }
1120- }
1121- }
1122- else -> Unit
1123- }
1124- },
1125- null
1126- )
1127- }
1128-
1129982 fun signOut (onComplete : Consumer <AuthSignOutResult >) {
1130983 signOut(AuthSignOutOptions .builder().build(), onComplete)
1131984 }
0 commit comments