diff --git a/app/src/main/java/app/fedilab/android/activities/LoginActivity.java b/app/src/main/java/app/fedilab/android/activities/LoginActivity.java index 481089020..c6446b8ed 100644 --- a/app/src/main/java/app/fedilab/android/activities/LoginActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/LoginActivity.java @@ -259,9 +259,14 @@ public class LoginActivity extends BaseActivity { connect_button.setEnabled(true); if (instanceNodeInfo != null && instanceNodeInfo.getName() != null) { socialNetwork = Helper.setSoftware(instanceNodeInfo.getName(), false); - if (instanceNodeInfo.getName().equals("PLEROMA") || instanceNodeInfo.getName().equals("MASTODON") || instanceNodeInfo.getName().equals("PIXELFED")) { + if (instanceNodeInfo.getName().equals("PLEROMA") || instanceNodeInfo.getName().equals("MASTODON")) { client_id_for_webview = true; retrievesClientId(); + } else if (instanceNodeInfo.getName().equals("PIXELFED")) { + Intent i = new Intent(LoginActivity.this, WebviewConnectActivity.class); + i.putExtra("social", socialNetwork); + i.putExtra("instance", instance); + startActivity(i); } else { client_id_for_webview = false; if (instanceNodeInfo.getName().equals("PEERTUBE")) { diff --git a/app/src/main/java/app/fedilab/android/activities/WebviewConnectActivity.java b/app/src/main/java/app/fedilab/android/activities/WebviewConnectActivity.java index 2217ecabf..82ffec338 100644 --- a/app/src/main/java/app/fedilab/android/activities/WebviewConnectActivity.java +++ b/app/src/main/java/app/fedilab/android/activities/WebviewConnectActivity.java @@ -23,12 +23,17 @@ import android.content.SharedPreferences; import android.graphics.drawable.ColorDrawable; import android.os.Build; import android.os.Bundle; +import android.os.Handler; +import android.os.Looper; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.webkit.CookieManager; import android.webkit.CookieSyncManager; import android.webkit.WebChromeClient; +import android.webkit.WebResourceRequest; +import android.webkit.WebResourceResponse; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ImageView; @@ -45,6 +50,8 @@ import org.json.JSONException; import org.json.JSONObject; import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; import app.fedilab.android.R; import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask; @@ -65,6 +72,7 @@ public class WebviewConnectActivity extends BaseActivity { private String clientId, clientSecret; private String instance; private UpdateAccountInfoAsyncTask.SOCIAL social; + private String pixelfedToken; @SuppressWarnings("deprecation") public static void clearCookies(Context context) { @@ -99,6 +107,7 @@ public class WebviewConnectActivity extends BaseActivity { setTheme(R.style.AppThemeDark); } + pixelfedToken = null; setContentView(R.layout.activity_webview_connect); Bundle b = getIntent().getExtras(); if (b != null) { @@ -152,9 +161,45 @@ public class WebviewConnectActivity extends BaseActivity { finish(); } webView.setWebViewClient(new WebViewClient() { + + @Override + public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) { + + if (request.getUrl().toString().contains("accounts/verify_credentials")) { + Log.v(Helper.TAG, "-> " + request.getUrl()); + Map requestHeaders = request.getRequestHeaders(); + Iterator> it = requestHeaders.entrySet().iterator(); + while (it.hasNext()) { + Map.Entry pair = it.next(); + Log.v(Helper.TAG, "pair.getKey() -> " + pair.getKey()); + if (pair.getKey().compareTo("X-XSRF-TOKEN") == 0) { + new Handler(Looper.getMainLooper()).post(() -> { + pixelfedToken = pair.getValue(); + Log.v(Helper.TAG, "pixelfedToken -> " + pixelfedToken); + view.stopLoading(); + SharedPreferences sharedpreferences1 = getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedpreferences1.edit(); + String token = "X-XSRF-TOKEN: " + pixelfedToken; + editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, token); + editor.commit(); + view.setVisibility(View.GONE); + //Update the account with the token; + new UpdateAccountInfoAsyncTask(WebviewConnectActivity.this, token, clientId, clientSecret, null, instance, social); + finish(); + }); + + } + it.remove(); + } + } + return super.shouldInterceptRequest(view, request); + } + + @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { super.shouldOverrideUrlLoading(view, url); + Log.v(Helper.TAG, "pixelfedToken: " + pixelfedToken); if (url.contains(Helper.REDIRECT_CONTENT_WEB)) { String[] val = url.split("code="); if (val.length < 2) { @@ -199,7 +244,11 @@ public class WebviewConnectActivity extends BaseActivity { } }); - webView.loadUrl(LoginActivity.redirectUserToAuthorizeAndLogin(WebviewConnectActivity.this, clientId, instance)); + if (social == UpdateAccountInfoAsyncTask.SOCIAL.PIXELFED) { + webView.loadUrl("https://" + instance + "/login"); + } else { + webView.loadUrl(LoginActivity.redirectUserToAuthorizeAndLogin(WebviewConnectActivity.this, clientId, instance)); + } } diff --git a/app/src/main/java/app/fedilab/android/client/HttpsConnection.java b/app/src/main/java/app/fedilab/android/client/HttpsConnection.java index 4f5f60991..ebe974b0d 100644 --- a/app/src/main/java/app/fedilab/android/client/HttpsConnection.java +++ b/app/src/main/java/app/fedilab/android/client/HttpsConnection.java @@ -128,6 +128,28 @@ public class HttpsConnection { } + private void setToken(String token) { + if (token != null) { + if (token.startsWith("Basic ")) + httpURLConnection.setRequestProperty("Authorization", token); + else if (token.startsWith("X-XSRF-TOKEN")) + httpURLConnection.setRequestProperty("Authorization", token); + else + httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); + } + } + + private String getToken(String token) { + if (token != null) { + if (token.startsWith("Basic ")) + return token; + else if (token.startsWith("X-XSRF-TOKEN")) + return token; + else + return "Bearer " + token; + } else return null; + } + /** * Get calls * @@ -180,10 +202,7 @@ public class HttpsConnection { if (httpURLConnection instanceof HttpsURLConnection) { ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(new TLSSocketFactory(this.instance)); } - if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", token); + setToken(token); httpURLConnection.setRequestMethod("GET"); String response; if (httpURLConnection.getResponseCode() >= 200 && httpURLConnection.getResponseCode() < 400) { @@ -347,10 +366,7 @@ public class HttpsConnection { ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(new TLSSocketFactory(this.instance)); } httpURLConnection.setRequestMethod("POST"); - if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", token); + setToken(token); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); @@ -406,10 +422,7 @@ public class HttpsConnection { httpURLConnection.setRequestProperty("Content-Type", "application/json"); httpURLConnection.setRequestProperty("Accept", "application/json"); httpURLConnection.setRequestMethod("POST"); - if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", token); + setToken(token); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); @@ -463,10 +476,7 @@ public class HttpsConnection { ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(new TLSSocketFactory(this.instance)); } httpURLConnection.setRequestMethod("POST"); - if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); - else if (token != null && token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", token); + setToken(token); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); @@ -643,7 +653,7 @@ public class HttpsConnection { m.addFileToUpload(file.getPath(), "header"); } m.addParameter("name", filename) - .addHeader("Authorization", "Bearer " + token) + .addHeader("Authorization", getToken(token)) .setNotificationConfig(uploadConfig) .setDelegate(new UploadStatusDelegate() { @Override @@ -709,7 +719,7 @@ public class HttpsConnection { } httpURLConnection.setRequestMethod("PATCH"); if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); + httpURLConnection.setRequestProperty("Authorization", getToken(token)); else if (token != null && token.startsWith("Basic ")) httpURLConnection.setRequestProperty("Authorization", token); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); @@ -790,7 +800,7 @@ public class HttpsConnection { ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(new TLSSocketFactory(this.instance)); } if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); + httpURLConnection.setRequestProperty("Authorization", getToken(token)); else if (token != null && token.startsWith("Basic ")) httpURLConnection.setRequestProperty("Authorization", token); httpURLConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length)); @@ -863,7 +873,7 @@ public class HttpsConnection { ((HttpsURLConnection) httpURLConnection).setSSLSocketFactory(new TLSSocketFactory(this.instance)); } if (token != null && !token.startsWith("Basic ")) - httpURLConnection.setRequestProperty("Authorization", "Bearer " + token); + httpURLConnection.setRequestProperty("Authorization", getToken(token)); else if (token != null && token.startsWith("Basic ")) httpURLConnection.setRequestProperty("Authorization", token); httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");