Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Login Flow V2 Response #13159

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
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 @@ -35,21 +35,15 @@
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.AndroidRuntimeException;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
Expand Down Expand Up @@ -208,7 +202,6 @@ public class AuthenticatorActivity extends AccountAuthenticatorActivity

public static final int NO_ICON = 0;
public static final String EMPTY_STRING = "";

public static final int REQUEST_CODE_FIRST_RUN = 102;

/// parameters from EXTRAs in starter Intent
Expand Down Expand Up @@ -394,36 +387,50 @@ private void deleteCookies() {
* @param url The URL where the login request is to be anonymously posted.
* This URL should handle the login request and return the login URL.
* It's typically the entry point for the login process.
* Example: "https://example.com/index.php/login/v2"
* Example: "<a href="https://example.com/index.php/login/v2">...</a>"
*/
private void anonymouslyPostLoginRequest(String url) {
baseUrl = url;

Thread thread = new Thread(() -> {
PostMethod post = new PostMethod(baseUrl, false, new FormBody.Builder().build());

PlainClient client = clientFactory.createPlainClient();
post.execute(client);
String response = post.getResponseBodyAsString();
JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
String login = jsonObject.get("login").getAsString();
if (login == null) {
login = getResources().getString(R.string.webview_login_url);
}

String loginUrl = login;
runOnUiThread(() -> {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(loginUrl));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
});
String response = getResponseOfAnonymouslyPostLoginRequest();

token = jsonObject.getAsJsonObject("poll").get("token").getAsString();
try {
JsonObject jsonObject = JsonParser.parseString(response).getAsJsonObject();
String loginUrl = getLoginUrl(jsonObject);
runOnUiThread(() -> launchDefaultWebBrowser(loginUrl));
token = jsonObject.getAsJsonObject("poll").get("token").getAsString();
} catch (Throwable t) {
Log_OC.d(TAG, "Error caught at anonymouslyPostLoginRequest: " + t);
DisplayUtils.showSnackMessage(this, R.string.authenticator_activity_login_error);
}
});

thread.start();
}

private String getResponseOfAnonymouslyPostLoginRequest() {
PostMethod post = new PostMethod(baseUrl, false, new FormBody.Builder().build());
PlainClient client = clientFactory.createPlainClient();
post.execute(client);
return post.getResponseBodyAsString();
}

private String getLoginUrl(JsonObject response) {
String result = response.get("login").getAsString();
if (result == null) {
result = getResources().getString(R.string.webview_login_url);
}

return result;
}

private void launchDefaultWebBrowser(String url) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}

private static String getWebLoginUserAgent() {
return Build.MANUFACTURER.substring(0, 1).toUpperCase(Locale.getDefault()) +
Build.MANUFACTURER.substring(1).toLowerCase(Locale.getDefault()) + " " + Build.MODEL + " (Android)";
Expand Down Expand Up @@ -1585,12 +1592,9 @@ private void doOnResumeAndBound() {
}
}


private void dismissWaitingDialog() {
Fragment frag = getSupportFragmentManager().findFragmentByTag(WAIT_DIALOG_TAG);
if (frag instanceof DialogFragment) {
DialogFragment dialog = (DialogFragment) frag;

if (frag instanceof DialogFragment dialog) {
try {
dialog.dismiss();
} catch (IllegalStateException e) {
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@

<string name="authenticator_activity_cancel_login">Cancel Login</string>
<string name="authenticator_activity_please_complete_login_process">Please complete login process in your browser</string>
<string name="authenticator_activity_login_error">There was an issue processing your login request. Please try again later.</string>

<string name="favorite">Add to favorites</string>
<string name="unset_favorite">Remove from favorites</string>
Expand Down
Loading