Search in sources :

Example 1 with AccessTokenModel

use of com.fastaccess.data.dao.AccessTokenModel in project FastHub by k0shk0sh.

the class LoginPresenter method login.

@Override
public void login(@NonNull String username, @NonNull String password, @Nullable String twoFactorCode, boolean isBasicAuth, @Nullable String endpoint) {
    boolean usernameIsEmpty = InputHelper.isEmpty(username);
    boolean passwordIsEmpty = InputHelper.isEmpty(password);
    boolean endpointIsEmpty = InputHelper.isEmpty(endpoint) && isEnterprise();
    if (getView() == null)
        return;
    getView().onEmptyUserName(usernameIsEmpty);
    getView().onEmptyPassword(passwordIsEmpty);
    getView().onEmptyEndpoint(endpointIsEmpty);
    if ((!usernameIsEmpty && !passwordIsEmpty)) {
        try {
            String authToken = Credentials.basic(username, password);
            if (isBasicAuth && !isEnterprise()) {
                AuthModel authModel = new AuthModel();
                authModel.setScopes(Arrays.asList("user", "repo", "gist", "notifications", "read:org"));
                authModel.setNote(BuildConfig.APPLICATION_ID);
                authModel.setClientSecret(GithubConfigHelper.getSecret());
                authModel.setClientId(GithubConfigHelper.getClientId());
                authModel.setNoteUrl(GithubConfigHelper.getRedirectUrl());
                if (!InputHelper.isEmpty(twoFactorCode)) {
                    authModel.setOtpCode(twoFactorCode);
                }
                makeRestCall(LoginProvider.getLoginRestService(authToken, twoFactorCode, null).login(authModel), accessTokenModel -> {
                    if (!InputHelper.isEmpty(twoFactorCode)) {
                        PrefGetter.setOtpCode(twoFactorCode);
                    }
                    onTokenResponse(accessTokenModel);
                });
            } else {
                accessTokenLogin(password, endpoint, twoFactorCode, authToken);
            }
        } catch (Exception e) {
            sendToView(view -> view.showMessage("Error", "The app was about to crash!!(" + e.getMessage() + ")"));
        }
    }
}
Also used : LoginProvider(com.fastaccess.provider.rest.LoginProvider) Arrays(java.util.Arrays) Login(com.fastaccess.data.dao.model.Login) RestProvider(com.fastaccess.provider.rest.RestProvider) Uri(android.net.Uri) InputHelper(com.fastaccess.helper.InputHelper) HttpException(retrofit2.HttpException) Intent(android.content.Intent) NonNull(android.support.annotation.NonNull) Credentials(okhttp3.Credentials) PrefGetter(com.fastaccess.helper.PrefGetter) R(com.fastaccess.R) BuildConfig(com.fastaccess.BuildConfig) AccessTokenModel(com.fastaccess.data.dao.AccessTokenModel) AuthModel(com.fastaccess.data.dao.AuthModel) Nullable(android.support.annotation.Nullable) GithubConfigHelper(com.fastaccess.helper.GithubConfigHelper) BasePresenter(com.fastaccess.ui.base.mvp.presenter.BasePresenter) AuthModel(com.fastaccess.data.dao.AuthModel) HttpException(retrofit2.HttpException)

Aggregations

Intent (android.content.Intent)1 Uri (android.net.Uri)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 BuildConfig (com.fastaccess.BuildConfig)1 R (com.fastaccess.R)1 AccessTokenModel (com.fastaccess.data.dao.AccessTokenModel)1 AuthModel (com.fastaccess.data.dao.AuthModel)1 Login (com.fastaccess.data.dao.model.Login)1 GithubConfigHelper (com.fastaccess.helper.GithubConfigHelper)1 InputHelper (com.fastaccess.helper.InputHelper)1 PrefGetter (com.fastaccess.helper.PrefGetter)1 LoginProvider (com.fastaccess.provider.rest.LoginProvider)1 RestProvider (com.fastaccess.provider.rest.RestProvider)1 BasePresenter (com.fastaccess.ui.base.mvp.presenter.BasePresenter)1 Arrays (java.util.Arrays)1 Credentials (okhttp3.Credentials)1 HttpException (retrofit2.HttpException)1