1515
1616package com.amplifyframework.auth.cognito
1717
18- import android.app.Activity
1918import androidx.annotation.WorkerThread
2019import com.amplifyframework.AmplifyException
2120import com.amplifyframework.annotations.InternalAmplifyApi
2221import com.amplifyframework.auth.AWSCognitoAuthMetadataType
2322import com.amplifyframework.auth.AuthChannelEventName
2423import com.amplifyframework.auth.AuthException
25- import com.amplifyframework.auth.AuthProvider
2624import com.amplifyframework.auth.AuthSession
27- import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidOauthConfigurationException
28- import com.amplifyframework.auth.cognito.exceptions.configuration.InvalidUserPoolConfigurationException
29- import com.amplifyframework.auth.cognito.exceptions.invalidstate.SignedInException
3025import com.amplifyframework.auth.cognito.exceptions.service.InvalidAccountTypeException
31- import com.amplifyframework.auth.cognito.helpers.HostedUIHelper
32- import com.amplifyframework.auth.cognito.options.AWSCognitoAuthWebUISignInOptions
3326import com.amplifyframework.auth.exceptions.ConfigurationException
3427import com.amplifyframework.auth.exceptions.InvalidStateException
3528import com.amplifyframework.auth.exceptions.NotAuthorizedException
@@ -38,26 +31,18 @@ import com.amplifyframework.auth.exceptions.SessionExpiredException
3831import com.amplifyframework.auth.exceptions.SignedOutException
3932import com.amplifyframework.auth.exceptions.UnknownException
4033import com.amplifyframework.auth.options.AuthFetchSessionOptions
41- import com.amplifyframework.auth.options.AuthWebUISignInOptions
42- import com.amplifyframework.auth.result.AuthSignInResult
43- import com.amplifyframework.auth.result.step.AuthNextSignInStep
44- import com.amplifyframework.auth.result.step.AuthSignInStep
4534import com.amplifyframework.core.Amplify
4635import com.amplifyframework.core.Consumer
4736import com.amplifyframework.hub.HubChannel
4837import com.amplifyframework.hub.HubEvent
4938import com.amplifyframework.logging.Logger
5039import com.amplifyframework.statemachine.StateChangeListenerToken
5140import com.amplifyframework.statemachine.codegen.data.AmplifyCredential
52- import com.amplifyframework.statemachine.codegen.data.SignInData
5341import com.amplifyframework.statemachine.codegen.errors.SessionError
5442import com.amplifyframework.statemachine.codegen.events.AuthEvent
55- import com.amplifyframework.statemachine.codegen.events.AuthenticationEvent
5643import com.amplifyframework.statemachine.codegen.events.AuthorizationEvent
5744import com.amplifyframework.statemachine.codegen.states.AuthState
58- import com.amplifyframework.statemachine.codegen.states.AuthenticationState
5945import com.amplifyframework.statemachine.codegen.states.AuthorizationState
60- import com.amplifyframework.statemachine.codegen.states.HostedUISignInState
6146import java.util.concurrent.CountDownLatch
6247import java.util.concurrent.TimeUnit
6348import kotlinx.coroutines.flow.collect
@@ -111,172 +96,6 @@ internal class RealAWSCognitoAuthPlugin(
11196 authStateMachine.state.takeWhile { it !is AuthState .Configured && it !is AuthState .Error }.collect()
11297 }
11398
114- fun signInWithSocialWebUI (
115- provider : AuthProvider ,
116- callingActivity : Activity ,
117- onSuccess : Consumer <AuthSignInResult >,
118- onError : Consumer <AuthException >
119- ) {
120- signInWithSocialWebUI(
121- provider,
122- callingActivity,
123- AWSCognitoAuthWebUISignInOptions .builder().build(),
124- onSuccess,
125- onError
126- )
127- }
128-
129- fun signInWithSocialWebUI (
130- provider : AuthProvider ,
131- callingActivity : Activity ,
132- options : AuthWebUISignInOptions ,
133- onSuccess : Consumer <AuthSignInResult >,
134- onError : Consumer <AuthException >
135- ) {
136- signInWithHostedUI(
137- provider = provider,
138- callingActivity = callingActivity,
139- options = options,
140- onSuccess = onSuccess,
141- onError = onError
142- )
143- }
144-
145- fun signInWithWebUI (
146- callingActivity : Activity ,
147- onSuccess : Consumer <AuthSignInResult >,
148- onError : Consumer <AuthException >
149- ) {
150- signInWithWebUI(callingActivity, AuthWebUISignInOptions .builder().build(), onSuccess, onError)
151- }
152-
153- fun signInWithWebUI (
154- callingActivity : Activity ,
155- options : AuthWebUISignInOptions ,
156- onSuccess : Consumer <AuthSignInResult >,
157- onError : Consumer <AuthException >
158- ) {
159- signInWithHostedUI(
160- callingActivity = callingActivity,
161- options = options,
162- onSuccess = onSuccess,
163- onError = onError
164- )
165- }
166-
167- private fun signInWithHostedUI (
168- provider : AuthProvider ? = null,
169- callingActivity : Activity ,
170- options : AuthWebUISignInOptions ,
171- onSuccess : Consumer <AuthSignInResult >,
172- onError : Consumer <AuthException >
173- ) {
174- authStateMachine.getCurrentState { authState ->
175- when (authState.authNState) {
176- is AuthenticationState .NotConfigured -> onError.accept(
177- InvalidUserPoolConfigurationException ()
178- )
179- // Continue sign in
180- is AuthenticationState .SignedOut -> {
181- if (configuration.oauth == null ) {
182- onError.accept(InvalidOauthConfigurationException ())
183- return @getCurrentState
184- }
185-
186- _signInWithHostedUI (
187- callingActivity = callingActivity,
188- options = options,
189- onSuccess = onSuccess,
190- onError = onError,
191- provider = provider
192- )
193- }
194- is AuthenticationState .SignedIn -> onError.accept(SignedInException ())
195- is AuthenticationState .SigningIn -> {
196- val token = StateChangeListenerToken ()
197- authStateMachine.listen(
198- token,
199- { authState ->
200- when (authState.authNState) {
201- is AuthenticationState .SignedOut -> {
202- authStateMachine.cancel(token)
203- _signInWithHostedUI (
204- callingActivity = callingActivity,
205- options = options,
206- onSuccess = onSuccess,
207- onError = onError,
208- provider = provider
209- )
210- }
211- else -> Unit
212- }
213- },
214- {
215- authStateMachine.send(AuthenticationEvent (AuthenticationEvent .EventType .CancelSignIn ()))
216- }
217- )
218- }
219- else -> onError.accept(InvalidStateException ())
220- }
221- }
222- }
223-
224- private fun _signInWithHostedUI (
225- callingActivity : Activity ,
226- options : AuthWebUISignInOptions ,
227- onSuccess : Consumer <AuthSignInResult >,
228- onError : Consumer <AuthException >,
229- provider : AuthProvider ? = null
230- ) {
231- val token = StateChangeListenerToken ()
232- authStateMachine.listen(
233- token,
234- { authState ->
235- val authNState = authState.authNState
236- val authZState = authState.authZState
237- when {
238- authNState is AuthenticationState .SigningIn -> {
239- val hostedUISignInState = authNState.signInState.hostedUISignInState
240- if (hostedUISignInState is HostedUISignInState .Error ) {
241- authStateMachine.cancel(token)
242- val exception = hostedUISignInState.exception
243- onError.accept(
244- if (exception is AuthException ) {
245- exception
246- } else {
247- UnknownException (" Sign in failed" , exception)
248- }
249- )
250- authStateMachine.send(AuthenticationEvent (AuthenticationEvent .EventType .CancelSignIn ()))
251- }
252- }
253- authNState is AuthenticationState .SignedIn &&
254- authZState is AuthorizationState .SessionEstablished -> {
255- authStateMachine.cancel(token)
256- val authSignInResult =
257- AuthSignInResult (
258- true ,
259- AuthNextSignInStep (AuthSignInStep .DONE , mapOf (), null , null , null , null )
260- )
261- onSuccess.accept(authSignInResult)
262- sendHubEvent(AuthChannelEventName .SIGNED_IN .toString())
263- }
264- else -> Unit
265- }
266- },
267- {
268- val hostedUIOptions = HostedUIHelper .createHostedUIOptions(callingActivity, provider, options)
269- authStateMachine.send(
270- AuthenticationEvent (
271- AuthenticationEvent .EventType .SignInRequested (
272- SignInData .HostedUISignInData (hostedUIOptions)
273- )
274- )
275- )
276- }
277- )
278- }
279-
28099 fun fetchAuthSession (onSuccess : Consumer <AuthSession >, onError : Consumer <AuthException >) {
281100 fetchAuthSession(AuthFetchSessionOptions .defaults(), onSuccess, onError)
282101 }
0 commit comments