@@ -19,30 +19,10 @@ import androidx.annotation.WorkerThread
1919import com.amplifyframework.AmplifyException
2020import com.amplifyframework.annotations.InternalAmplifyApi
2121import com.amplifyframework.auth.AWSCognitoAuthMetadataType
22- import com.amplifyframework.auth.AuthChannelEventName
23- import com.amplifyframework.auth.AuthException
24- import com.amplifyframework.auth.AuthSession
25- import com.amplifyframework.auth.cognito.exceptions.service.InvalidAccountTypeException
26- import com.amplifyframework.auth.exceptions.ConfigurationException
27- import com.amplifyframework.auth.exceptions.InvalidStateException
28- import com.amplifyframework.auth.exceptions.NotAuthorizedException
29- import com.amplifyframework.auth.exceptions.ServiceException
30- import com.amplifyframework.auth.exceptions.SessionExpiredException
31- import com.amplifyframework.auth.exceptions.SignedOutException
32- import com.amplifyframework.auth.exceptions.UnknownException
33- import com.amplifyframework.auth.options.AuthFetchSessionOptions
34- import com.amplifyframework.core.Amplify
35- import com.amplifyframework.core.Consumer
36- import com.amplifyframework.hub.HubChannel
37- import com.amplifyframework.hub.HubEvent
3822import com.amplifyframework.logging.Logger
3923import com.amplifyframework.statemachine.StateChangeListenerToken
40- import com.amplifyframework.statemachine.codegen.data.AmplifyCredential
41- import com.amplifyframework.statemachine.codegen.errors.SessionError
4224import com.amplifyframework.statemachine.codegen.events.AuthEvent
43- import com.amplifyframework.statemachine.codegen.events.AuthorizationEvent
4425import com.amplifyframework.statemachine.codegen.states.AuthState
45- import com.amplifyframework.statemachine.codegen.states.AuthorizationState
4626import java.util.concurrent.CountDownLatch
4727import java.util.concurrent.TimeUnit
4828import kotlinx.coroutines.flow.collect
@@ -96,125 +76,6 @@ internal class RealAWSCognitoAuthPlugin(
9676 authStateMachine.state.takeWhile { it !is AuthState .Configured && it !is AuthState .Error }.collect()
9777 }
9878
99- fun fetchAuthSession (onSuccess : Consumer <AuthSession >, onError : Consumer <AuthException >) {
100- fetchAuthSession(AuthFetchSessionOptions .defaults(), onSuccess, onError)
101- }
102-
103- fun fetchAuthSession (
104- options : AuthFetchSessionOptions ,
105- onSuccess : Consumer <AuthSession >,
106- onError : Consumer <AuthException >
107- ) {
108- val forceRefresh = options.forceRefresh
109- authStateMachine.getCurrentState { authState ->
110- when (val authZState = authState.authZState) {
111- is AuthorizationState .Configured -> {
112- authStateMachine.send(AuthorizationEvent (AuthorizationEvent .EventType .FetchUnAuthSession ))
113- _fetchAuthSession (onSuccess)
114- }
115- is AuthorizationState .SessionEstablished -> {
116- val credential = authZState.amplifyCredential
117- if (! credential.isValid() || forceRefresh) {
118- if (credential is AmplifyCredential .IdentityPoolFederated ) {
119- authStateMachine.send(
120- AuthorizationEvent (
121- AuthorizationEvent .EventType .StartFederationToIdentityPool (
122- credential.federatedToken,
123- credential.identityId,
124- credential
125- )
126- )
127- )
128- } else {
129- authStateMachine.send(
130- AuthorizationEvent (AuthorizationEvent .EventType .RefreshSession (credential))
131- )
132- }
133- _fetchAuthSession (onSuccess)
134- } else {
135- onSuccess.accept(credential.getCognitoSession())
136- }
137- }
138- is AuthorizationState .Error -> {
139- val error = authZState.exception
140- if (error is SessionError ) {
141- val amplifyCredential = error.amplifyCredential
142- if (amplifyCredential is AmplifyCredential .IdentityPoolFederated ) {
143- authStateMachine.send(
144- AuthorizationEvent (
145- AuthorizationEvent .EventType .StartFederationToIdentityPool (
146- amplifyCredential.federatedToken,
147- amplifyCredential.identityId,
148- amplifyCredential
149- )
150- )
151- )
152- } else {
153- authStateMachine.send(
154- AuthorizationEvent (AuthorizationEvent .EventType .RefreshSession (amplifyCredential))
155- )
156- }
157- _fetchAuthSession (onSuccess)
158- } else {
159- onError.accept(InvalidStateException ())
160- }
161- }
162- else -> onError.accept(InvalidStateException ())
163- }
164- }
165- }
166-
167- private fun _fetchAuthSession (onSuccess : Consumer <AuthSession >) {
168- val token = StateChangeListenerToken ()
169- authStateMachine.listen(
170- token,
171- { authState ->
172- when (val authZState = authState.authZState) {
173- is AuthorizationState .SessionEstablished -> {
174- authStateMachine.cancel(token)
175- onSuccess.accept(authZState.amplifyCredential.getCognitoSession())
176- }
177- is AuthorizationState .Error -> {
178- authStateMachine.cancel(token)
179- when (val error = authZState.exception) {
180- is SessionError -> {
181- when (val innerException = error.exception) {
182- is SignedOutException -> {
183- onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
184- }
185- is SessionExpiredException -> {
186- onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
187- sendHubEvent(AuthChannelEventName .SESSION_EXPIRED .toString())
188- }
189- is ServiceException -> {
190- onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
191- }
192- is NotAuthorizedException -> {
193- onSuccess.accept(error.amplifyCredential.getCognitoSession(innerException))
194- }
195- else -> {
196- val errorResult = UnknownException (" Fetch auth session failed." , innerException)
197- onSuccess.accept(error.amplifyCredential.getCognitoSession(errorResult))
198- }
199- }
200- }
201- is ConfigurationException -> {
202- val errorResult = InvalidAccountTypeException (error)
203- onSuccess.accept(AmplifyCredential .Empty .getCognitoSession(errorResult))
204- }
205- else -> {
206- val errorResult = UnknownException (" Fetch auth session failed." , error)
207- onSuccess.accept(AmplifyCredential .Empty .getCognitoSession(errorResult))
208- }
209- }
210- }
211- else -> Unit
212- }
213- },
214- null
215- )
216- }
217-
21879 private fun addAuthStateChangeListener () {
21980 authStateMachine.listen(
22081 StateChangeListenerToken (),
@@ -240,8 +101,4 @@ internal class RealAWSCognitoAuthPlugin(
240101 }
241102 )
242103 }
243-
244- private fun sendHubEvent (eventName : String ) {
245- Amplify .Hub .publish(HubChannel .AUTH , HubEvent .create(eventName))
246- }
247104}
0 commit comments