Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
import com.nextcloud.client.onboarding.OnboardingService;
import com.nextcloud.client.preferences.AppPreferences;
import com.nextcloud.common.PlainClient;
import com.nextcloud.model.HTTPStatusCodes;
import com.nextcloud.operations.PostMethod;
import com.nextcloud.utils.SnackbarUtil;
import com.nextcloud.utils.extensions.BundleExtensionsKt;
Expand Down Expand Up @@ -117,6 +118,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;
Expand Down Expand Up @@ -401,17 +403,30 @@ private void deleteCookies() {
}

// region LoginFlow
private static final long LOGIN_FLOW_POLL_INTERVAL_SECONDS = 30;

private final ScheduledExecutorService loginFlowExecutorService = Executors.newSingleThreadScheduledExecutor();
private ScheduledFuture<?> loginFlowPollTask;
private boolean isLoginProcessCompleted = false;
private boolean isRedirectedToTheDefaultBrowser = false;
private String baseUrl;

private void poolLogin() {
loginFlowExecutorService.scheduleWithFixedDelay(() -> {
if (!isLoginProcessCompleted) {
if (loginFlowPollTask != null && !loginFlowPollTask.isDone()) {
return;
}

loginFlowPollTask = loginFlowExecutorService.scheduleWithFixedDelay(() -> {
if (isLoginProcessCompleted) {
return;
}

try {
performLoginFlowV2();
} catch (Exception e) {
Log_OC.e(TAG, "Error polling login flow: " + e);
}
}, 0, 30, TimeUnit.SECONDS);
}, 0, LOGIN_FLOW_POLL_INTERVAL_SECONDS, TimeUnit.SECONDS);
}

/**
Expand Down Expand Up @@ -553,36 +568,38 @@ private void performLoginFlowV2() {
String response = post.getResponseBodyAsString();

Log_OC.d(TAG, "performLoginFlowV2 status: " + status);
Log_OC.d(TAG, "performLoginFlowV2 response: " + response);

if (!response.isEmpty()) {
runOnUiThread(() -> completeLoginFlow(response, status));
if (status != HTTPStatusCodes.SUCCESS.getCode() || TextUtils.isEmpty(response)) {
return;
}

runOnUiThread(() -> completeLoginFlow(response, status));
}

private void completeLoginFlow(String response, int status) {
final LoginUrlInfo loginUrlInfo;

try {
LoginUrlInfo loginUrlInfo = gson.fromJson(response, LoginUrlInfo.class);
if (loginUrlInfo == null) {
Log_OC.e(TAG, "cannot complete login flow loginUrl is null");
loginUrlInfo = gson.fromJson(response, LoginUrlInfo.class);
if (loginUrlInfo == null || !loginUrlInfo.isValid(status)) {
Log_OC.d(TAG, "Login flow not granted yet, keep polling");
return;
}
isLoginProcessCompleted = loginUrlInfo.isValid(status);
} catch (Exception e) {
Log_OC.e(TAG, "Error completeLoginFlow: " + e);
return;
}

if (accountSetupBinding != null) {
accountSetupBinding.hostUrlInput.setText("");
}
isLoginProcessCompleted = true;

mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(loginUrlInfo.getServer());
webViewUser = loginUrlInfo.getLoginName();
webViewPassword = loginUrlInfo.getAppPassword();
} catch (Exception e) {
Log_OC.d(TAG, "Error completeLoginFlow: " + e);
mServerStatusIcon = R.drawable.ic_alert;
mServerStatusText = getString(R.string.qr_could_not_be_read);
showServerStatus();
if (accountSetupBinding != null) {
accountSetupBinding.hostUrlInput.setText("");
}

mServerInfo.mBaseUrl = AuthenticatorUrlUtils.INSTANCE.normalizeUrlSuffix(loginUrlInfo.getServer());
webViewUser = loginUrlInfo.getLoginName();
webViewPassword = loginUrlInfo.getAppPassword();

checkOcServer();
loginFlowExecutorService.shutdown();
ProcessLifecycleOwner.get().getLifecycle().removeObserver(lifecycleEventObserver);
Expand Down
Loading