@@ -29,11 +29,13 @@ 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
3233import com.amplifyframework.auth.MFAType
3334import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidOauthConfigurationException
3435import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
3536import com.amplifyframework.auth.cognito.exceptions.invalidstate.SignedInException
3637import com.amplifyframework.auth.cognito.exceptions.service.HostedUISignOutException
38+ import com.amplifyframework.auth.cognito.exceptions.service.InvalidAccountTypeException
3739import com.amplifyframework.auth.cognito.exceptions.service.InvalidParameterException
3840import com.amplifyframework.auth.cognito.exceptions.service.UserCancelledException
3941import com.amplifyframework.auth.cognito.helpers.HostedUIHelper
@@ -55,9 +57,15 @@ import com.amplifyframework.auth.cognito.result.FederateToIdentityPoolResult
5557import com.amplifyframework.auth.cognito.result.GlobalSignOutError
5658import com.amplifyframework.auth.cognito.result.HostedUIError
5759import com.amplifyframework.auth.cognito.result.RevokeTokenError
60+ import com.amplifyframework.auth.exceptions.ConfigurationException
5861import 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
5966import com.amplifyframework.auth.exceptions.UnknownException
6067import com.amplifyframework.auth.options.AuthConfirmSignInOptions
68+ import com.amplifyframework.auth.options.AuthFetchSessionOptions
6169import com.amplifyframework.auth.options.AuthSignInOptions
6270import com.amplifyframework.auth.options.AuthSignOutOptions
6371import com.amplifyframework.auth.options.AuthWebUISignInOptions
@@ -104,6 +112,9 @@ import java.lang.ref.WeakReference
104112import java.util.concurrent.CountDownLatch
105113import java.util.concurrent.TimeUnit
106114import java.util.concurrent.atomic.AtomicReference
115+ import kotlin.coroutines.resume
116+ import kotlin.coroutines.resumeWithException
117+ import kotlin.coroutines.suspendCoroutine
107118import kotlinx.coroutines.flow.collect
108119import 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 }
0 commit comments