001package com.gigya.android.sdk.interruption.tfa;
002
003import com.gigya.android.sdk.GigyaLogger;
004import com.gigya.android.sdk.GigyaLoginCallback;
005import com.gigya.android.sdk.account.models.GigyaAccount;
006import com.gigya.android.sdk.api.GigyaApiResponse;
007import com.gigya.android.sdk.api.IBusinessApiService;
008import com.gigya.android.sdk.containers.IoCContainer;
009import com.gigya.android.sdk.interruption.Resolver;
010import com.gigya.android.sdk.interruption.tfa.models.TFAProviderModel;
011import com.gigya.android.sdk.interruption.tfa.models.TFAProvidersModel;
012import com.gigya.android.sdk.network.GigyaError;
013
014import java.util.List;
015
016public class TFAProviderResolver<A extends GigyaAccount> extends Resolver {
017
018    private static final String LOG_TAG = "TfaProviderResolver";
019
020    private final IoCContainer _container;
021
022    public TFAProviderResolver(IoCContainer container,
023                               GigyaLoginCallback<A> loginCallback,
024                               GigyaApiResponse interruption,
025                               IBusinessApiService<A> businessApiService) {
026        super(loginCallback, interruption, businessApiService);
027        _container = container.clone();
028    }
029
030    public void init() {
031        GigyaLogger.debug(LOG_TAG, "getProviders: ");
032        final String regToken = getRegToken();
033
034        try {
035            _businessApiService.getTFAProviders(regToken, new GigyaLoginCallback<TFAProvidersModel>() {
036                @Override
037                public void onSuccess(TFAProvidersModel model) {
038                    // Get provider lists.
039                    final List<TFAProviderModel> activeProviders = model.getActiveProviders();
040                    final List<TFAProviderModel> inactiveProviders = model.getInactiveProviders();
041
042                    // Instantiate the TFA factory and forward initial interruption.
043                    _container.bind(GigyaApiResponse.class, _interruption)
044                            .bind(GigyaLoginCallback.class, _loginCallback);
045                    try {
046                        // Create instance of TFA resolver factory the can create any instance of a TFAResolver class.
047                        TFAResolverFactory factory = _container.createInstance(TFAResolverFactory.class);
048
049                        if (_interruption.getErrorCode() == GigyaError.Codes.ERROR_PENDING_TWO_FACTOR_REGISTRATION) {
050
051                            _loginCallback.onPendingTwoFactorRegistration(_interruption, inactiveProviders, factory);
052                        } else if (_interruption.getErrorCode() == GigyaError.Codes.ERROR_PENDING_TWO_FACTOR_VERIFICATION) {
053
054                            _loginCallback.onPendingTwoFactorVerification(_interruption, activeProviders, factory);
055                        }
056                    } catch (Exception ex) {
057                        ex.printStackTrace();
058                        // TODO: 2019-06-16 General error?
059                        _loginCallback.onError(GigyaError.generalError());
060                    } finally {
061                        _container.dispose();
062                    }
063                }
064
065                @Override
066                public void onError(GigyaError error) {
067                    _loginCallback.onError(error);
068                }
069            });
070        } catch (Exception ex) {
071            ex.printStackTrace();
072            // TODO: 2019-06-16 General error?
073            _loginCallback.onError(GigyaError.generalError());
074        }
075    }
076}