2121import android .content .SharedPreferences ;
2222import android .os .Handler ;
2323
24+ import com .amazonaws .internal .ReturningRunnable ;
2425import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .AuthenticationContinuation ;
2526import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .AuthenticationDetails ;
2627import com .amazonaws .mobileconnectors .cognitoidentityprovider .continuations .ChallengeContinuation ;
@@ -335,13 +336,13 @@ public void confirmSignUp(String confirmationCode,
335336 * contentions
336337 */
337338 private void confirmSignUpInternal (String confirmationCode , boolean forcedAliasCreation ) {
338- final ConfirmSignUpRequest confirmUserRegistrationRequest = new ConfirmSignUpRequest ();
339- confirmUserRegistrationRequest . setClientId (clientId );
340- confirmUserRegistrationRequest . setSecretHash (secretHash );
341- confirmUserRegistrationRequest . setUsername (userId );
342- confirmUserRegistrationRequest . setConfirmationCode (confirmationCode );
343- confirmUserRegistrationRequest . setForceAliasCreation (forcedAliasCreation );
344- confirmUserRegistrationRequest . setUserContextData (getUserContextData ());
339+ final ConfirmSignUpRequest confirmUserRegistrationRequest = new ConfirmSignUpRequest ()
340+ . withClientId (clientId )
341+ . withSecretHash (secretHash )
342+ . withUsername (userId )
343+ . withConfirmationCode (confirmationCode )
344+ . withForceAliasCreation (forcedAliasCreation )
345+ . withUserContextData (getUserContextData ());
345346 final String pinpointEndpointId = pool .getPinpointEndpointId ();
346347 if (pinpointEndpointId != null ) {
347348 final AnalyticsMetadataType amd = new AnalyticsMetadataType ();
@@ -416,10 +417,11 @@ public void resendConfirmationCode(final VerificationHandler callback) {
416417 * Internal method to request registration code resend.
417418 */
418419 private ResendConfirmationCodeResult resendConfirmationCodeInternal () {
419- final ResendConfirmationCodeRequest resendConfirmationCodeRequest = new ResendConfirmationCodeRequest ();
420- resendConfirmationCodeRequest .setUsername (userId );
421- resendConfirmationCodeRequest .setClientId (clientId );
422- resendConfirmationCodeRequest .setSecretHash (secretHash );
420+ final ResendConfirmationCodeRequest resendConfirmationCodeRequest =
421+ new ResendConfirmationCodeRequest ()
422+ .withUsername (userId )
423+ .withClientId (clientId )
424+ .withSecretHash (secretHash );
423425 final String pinpointEndpointId = pool .getPinpointEndpointId ();
424426 resendConfirmationCodeRequest .setUserContextData (getUserContextData ());
425427 if (pinpointEndpointId != null ) {
@@ -753,6 +755,9 @@ public void getSession(final AuthenticationHandler callback) {
753755 }
754756
755757 /**
758+ * Note: Please use {@link #getSession(AuthenticationHandler)} or
759+ * {@link #getSessionInBackground(AuthenticationHandler)} instead.
760+ *
756761 * Initiates user authentication through the generic auth flow (also called
757762 * as Enhanced or Custom authentication). This is the first step in user
758763 * authentication. The response to this step from the service will contain
@@ -767,6 +772,26 @@ public void getSession(final AuthenticationHandler callback) {
767772 */
768773 public Runnable initiateUserAuthentication (final AuthenticationDetails authenticationDetails ,
769774 final AuthenticationHandler callback , final boolean runInBackground ) {
775+ final Runnable task = _initiateUserAuthentication (authenticationDetails , callback , runInBackground );
776+ if (runInBackground ) {
777+ return new Runnable () {
778+ @ Override
779+ public void run () {
780+ new Thread (new Runnable () {
781+ @ Override
782+ public void run () {
783+ task .run ();
784+ }
785+ }).start ();
786+ }
787+ };
788+ } else {
789+ return task ;
790+ }
791+ }
792+
793+ Runnable _initiateUserAuthentication (final AuthenticationDetails authenticationDetails ,
794+ final AuthenticationHandler callback , final boolean runInBackground ) {
770795 if (CognitoServiceConstants .CHLG_TYPE_USER_PASSWORD_VERIFIER .equals (
771796 authenticationDetails .getAuthenticationType ())) {
772797 return startWithUserSrpAuth (authenticationDetails , callback , runInBackground );
@@ -2316,58 +2341,48 @@ public void run() {
23162341 */
23172342 private Runnable startWithUserSrpAuth (final AuthenticationDetails authenticationDetails ,
23182343 final AuthenticationHandler callback , final boolean runInBackground ) {
2319- final AuthenticationHelper authenticationHelper = new AuthenticationHelper (
2320- pool . getUserPoolId ());
2321- final InitiateAuthRequest initiateAuthRequest = initiateUserSrpAuthRequest (
2322- authenticationDetails , authenticationHelper );
2323- try {
2324- final InitiateAuthResult initiateAuthResult = cognitoIdentityProviderClient
2325- . initiateAuth ( initiateAuthRequest );
2326- updateInternalUsername ( initiateAuthResult . getChallengeParameters ());
2327- if ( CognitoServiceConstants . CHLG_TYPE_USER_PASSWORD_VERIFIER
2328- . equals ( initiateAuthResult . getChallengeName ())) {
2329- if ( authenticationDetails . getPassword () != null ) {
2330- final RespondToAuthChallengeRequest challengeRequest = userSrpAuthRequest (
2331- initiateAuthResult .getChallengeParameters (),
2332- authenticationDetails .getPassword (),
2333- initiateAuthResult . getChallengeName (),
2334- initiateAuthResult .getSession (),
2335- authenticationHelper
2336- );
2337- return respondToChallenge ( challengeRequest , callback , runInBackground );
2338- }
2339- }
2340- return handleChallenge ( initiateAuthResult , authenticationDetails , callback , runInBackground );
2341- } catch ( final ResourceNotFoundException rna ) {
2342- final CognitoUser cognitoUser = this ;
2343- if ( rna . getMessage (). contains ( "Device" )) {
2344- CognitoDeviceHelper . clearCachedDevice ( usernameInternal , pool . getUserPoolId (),
2345- context ) ;
2346- return new Runnable ( ) {
2347- @ Override
2348- public void run () {
2344+ return new Runnable () {
2345+ @ Override
2346+ public void run () {
2347+ final AuthenticationHelper authenticationHelper = new AuthenticationHelper (
2348+ pool . getUserPoolId ());
2349+ final InitiateAuthRequest initiateAuthRequest = initiateUserSrpAuthRequest (
2350+ authenticationDetails , authenticationHelper );
2351+ try {
2352+ final InitiateAuthResult initiateAuthResult = cognitoIdentityProviderClient
2353+ . initiateAuth ( initiateAuthRequest );
2354+ updateInternalUsername ( initiateAuthResult . getChallengeParameters ());
2355+ if ( CognitoServiceConstants . CHLG_TYPE_USER_PASSWORD_VERIFIER
2356+ . equals ( initiateAuthResult .getChallengeName ())) {
2357+ if ( authenticationDetails .getPassword () != null ) {
2358+ final RespondToAuthChallengeRequest challengeRequest = userSrpAuthRequest (
2359+ initiateAuthResult .getChallengeParameters (),
2360+ authenticationDetails . getPassword (),
2361+ initiateAuthResult . getChallengeName (),
2362+ initiateAuthResult . getSession (),
2363+ authenticationHelper
2364+ );
2365+ respondToChallenge ( challengeRequest , callback , runInBackground ). run ( );
2366+ }
2367+ }
2368+ handleChallenge ( initiateAuthResult , authenticationDetails , callback , runInBackground ). run ();
2369+ } catch ( final ResourceNotFoundException rna ) {
2370+ final CognitoUser cognitoUser = CognitoUser . this ;
2371+ if ( rna . getMessage (). contains ( "Device" ) ) {
2372+ CognitoDeviceHelper . clearCachedDevice ( usernameInternal , pool . getUserPoolId (),
2373+ context );
23492374 final AuthenticationContinuation authenticationContinuation = new AuthenticationContinuation (
23502375 cognitoUser , context , runInBackground , callback );
23512376 callback .getAuthenticationDetails (authenticationContinuation ,
23522377 cognitoUser .getUserId ());
2353- }
2354- };
2355- } else {
2356- return new Runnable () {
2357- @ Override
2358- public void run () {
2378+ } else {
23592379 callback .onFailure (rna );
23602380 }
2361- };
2362- }
2363- } catch (final Exception e ) {
2364- return new Runnable () {
2365- @ Override
2366- public void run () {
2381+ } catch (final Exception e ) {
23672382 callback .onFailure (e );
23682383 }
2369- };
2370- }
2384+ }
2385+ };
23712386 }
23722387
23732388 /**
@@ -2383,38 +2398,38 @@ public void run() {
23832398 */
23842399 private Runnable startWithCustomAuth (final AuthenticationDetails authenticationDetails ,
23852400 final AuthenticationHandler callback , final boolean runInBackground ) {
2386- try {
2387- final AuthenticationHelper authenticationHelper = new AuthenticationHelper ( this . getUserPoolId ());
2388- final InitiateAuthRequest initiateAuthRequest = initiateCustomAuthRequest (
2389- authenticationDetails ,
2390- authenticationHelper );
2391- final InitiateAuthResult initiateAuthResult = cognitoIdentityProviderClient
2392- . initiateAuth ( initiateAuthRequest );
2393- updateInternalUsername ( initiateAuthResult . getChallengeParameters () );
2394- if ( CognitoServiceConstants . CHLG_TYPE_USER_PASSWORD_VERIFIER
2395- . equals ( initiateAuthResult . getChallengeName ())) {
2396- if ( authenticationDetails . getPassword () == null ) {
2397- throw new IllegalStateException ( "Failed to find password in " +
2398- "authentication details to response to PASSWORD_VERIFIER challenge" );
2399- }
2400- final RespondToAuthChallengeRequest challengeRequest = userSrpAuthRequest (
2401- initiateAuthResult . getChallengeParameters (),
2402- authenticationDetails . getPassword (),
2403- initiateAuthResult . getChallengeName (),
2404- initiateAuthResult .getSession (),
2405- authenticationHelper
2406- );
2407- return respondToChallenge ( challengeRequest , callback , runInBackground );
2408- }
2409- return handleChallenge ( initiateAuthResult , authenticationDetails , callback , runInBackground );
2410- } catch ( final Exception e ) {
2411- return new Runnable () {
2412- @ Override
2413- public void run ( ) {
2401+ return new Runnable () {
2402+ @ Override
2403+ public void run () {
2404+ try {
2405+ final AuthenticationHelper authenticationHelper = new AuthenticationHelper ( CognitoUser . this . getUserPoolId () );
2406+ final InitiateAuthRequest initiateAuthRequest = initiateCustomAuthRequest (
2407+ authenticationDetails ,
2408+ authenticationHelper );
2409+ final InitiateAuthResult initiateAuthResult = cognitoIdentityProviderClient
2410+ . initiateAuth ( initiateAuthRequest );
2411+ updateInternalUsername ( initiateAuthResult . getChallengeParameters ());
2412+ if ( CognitoServiceConstants . CHLG_TYPE_USER_PASSWORD_VERIFIER
2413+ . equals ( initiateAuthResult . getChallengeName ())) {
2414+ if ( authenticationDetails . getPassword () == null ) {
2415+ throw new IllegalStateException ( "Failed to find password in " +
2416+ "authentication details to response to PASSWORD_VERIFIER challenge" );
2417+ }
2418+ final RespondToAuthChallengeRequest challengeRequest = userSrpAuthRequest (
2419+ initiateAuthResult .getChallengeParameters (),
2420+ authenticationDetails . getPassword (),
2421+ initiateAuthResult . getChallengeName (),
2422+ initiateAuthResult . getSession (),
2423+ authenticationHelper
2424+ );
2425+ respondToChallenge ( challengeRequest , callback , runInBackground ). run ();
2426+ }
2427+ handleChallenge ( initiateAuthResult , authenticationDetails , callback , runInBackground ). run ();
2428+ } catch ( final Exception e ) {
24142429 callback .onFailure (e );
24152430 }
2416- };
2417- }
2431+ }
2432+ };
24182433 }
24192434
24202435 /**
@@ -2605,22 +2620,22 @@ public void run() {
26052620 */
26062621 private Runnable startWithUserPasswordAuth (final AuthenticationDetails authenticationDetails ,
26072622 final AuthenticationHandler callback , final boolean runInBackground ) {
2608- try {
2609- final InitiateAuthRequest initiateAuthRequest = initiateUserPasswordAuthRequest (
2610- authenticationDetails );
2611- final InitiateAuthResult initiateAuthResult = cognitoIdentityProviderClient
2612- . initiateAuth ( initiateAuthRequest );
2613- this . usernameInternal = initiateAuthResult . getChallengeParameters ()
2614- . get ( CognitoServiceConstants . CHLG_PARAM_USER_ID_FOR_SRP );
2615- return handleChallenge ( initiateAuthResult , authenticationDetails , callback , runInBackground );
2616- } catch ( final Exception e ) {
2617- return new Runnable () {
2618- @ Override
2619- public void run ( ) {
2623+ return new Runnable () {
2624+ @ Override
2625+ public void run () {
2626+ try {
2627+ final InitiateAuthRequest initiateAuthRequest = initiateUserPasswordAuthRequest (
2628+ authenticationDetails );
2629+ final InitiateAuthResult initiateAuthResult = cognitoIdentityProviderClient
2630+ . initiateAuth ( initiateAuthRequest );
2631+ CognitoUser . this . usernameInternal = initiateAuthResult . getChallengeParameters ()
2632+ . get ( CognitoServiceConstants . CHLG_PARAM_USER_ID_FOR_SRP );
2633+ handleChallenge ( initiateAuthResult , authenticationDetails , callback , runInBackground ). run ();
2634+ } catch ( final Exception e ) {
26202635 callback .onFailure (e );
26212636 }
2622- };
2623- }
2637+ }
2638+ };
26242639 }
26252640
26262641 /**
0 commit comments