ProfileManager: Allow overriding User-Agent in WebView

This commit is contained in:
Marvin W 2022-02-01 13:24:54 +01:00
parent af28974d97
commit 61957c33cd
No known key found for this signature in database
GPG Key ID: 072E9235DB996F2A
5 changed files with 32 additions and 15 deletions

View File

@ -18,6 +18,8 @@ import android.webkit.WebSettings
import android.webkit.WebView import android.webkit.WebView
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import org.microg.gms.firebase.auth.core.R import org.microg.gms.firebase.auth.core.R
import org.microg.gms.profile.Build
import org.microg.gms.profile.ProfileManager
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine import kotlin.coroutines.suspendCoroutine
@ -47,6 +49,8 @@ class ReCaptchaActivity : AppCompatActivity() {
settings.setSupportZoom(false) settings.setSupportZoom(false)
settings.displayZoomControls = false settings.displayZoomControls = false
settings.cacheMode = WebSettings.LOAD_NO_CACHE settings.cacheMode = WebSettings.LOAD_NO_CACHE
ProfileManager.ensureInitialized(this)
settings.userAgentString = Build.generateWebViewUserAgentString(settings.userAgentString)
view.addJavascriptInterface(object : Any() { view.addJavascriptInterface(object : Any() {
@JavascriptInterface @JavascriptInterface
fun onReCaptchaToken(token: String) { fun onReCaptchaToken(token: String) {

View File

@ -7,7 +7,6 @@ package org.microg.gms.firebase.auth
import android.content.Context import android.content.Context
import android.graphics.PixelFormat import android.graphics.PixelFormat
import android.os.Build
import android.provider.Settings import android.provider.Settings
import android.util.DisplayMetrics import android.util.DisplayMetrics
import android.util.Log import android.util.Log
@ -17,6 +16,8 @@ import android.webkit.WebSettings
import android.webkit.WebView import android.webkit.WebView
import android.widget.FrameLayout import android.widget.FrameLayout
import org.microg.gms.firebase.auth.core.R import org.microg.gms.firebase.auth.core.R
import org.microg.gms.profile.Build
import org.microg.gms.profile.ProfileManager
import kotlin.coroutines.Continuation import kotlin.coroutines.Continuation
import kotlin.coroutines.resume import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException import kotlin.coroutines.resumeWithException
@ -32,7 +33,7 @@ class ReCaptchaOverlay(val context: Context, val apiKey: String, val hostname: S
var container: View? = null var container: View? = null
private fun show() { private fun show() {
val layoutParamsType = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val layoutParamsType = if (android.os.Build.VERSION.SDK_INT >= 26) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else { } else {
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT WindowManager.LayoutParams.TYPE_SYSTEM_ALERT
@ -80,6 +81,8 @@ class ReCaptchaOverlay(val context: Context, val apiKey: String, val hostname: S
settings.setSupportZoom(false) settings.setSupportZoom(false)
settings.displayZoomControls = false settings.displayZoomControls = false
settings.cacheMode = WebSettings.LOAD_NO_CACHE settings.cacheMode = WebSettings.LOAD_NO_CACHE
ProfileManager.ensureInitialized(context)
settings.userAgentString = Build.generateWebViewUserAgentString(settings.userAgentString)
view.addJavascriptInterface(object : Any() { view.addJavascriptInterface(object : Any() {
@JavascriptInterface @JavascriptInterface
fun onReCaptchaToken(token: String) { fun onReCaptchaToken(token: String) {
@ -110,7 +113,7 @@ class ReCaptchaOverlay(val context: Context, val apiKey: String, val hostname: S
} }
companion object { companion object {
fun isSupported(context: Context): Boolean = Build.VERSION.SDK_INT < Build.VERSION_CODES.M || Settings.canDrawOverlays(context) fun isSupported(context: Context): Boolean = android.os.Build.VERSION.SDK_INT < 23 || Settings.canDrawOverlays(context)
suspend fun awaitToken(context: Context, apiKey: String, hostname: String? = null) = suspendCoroutine<String> { continuation -> suspend fun awaitToken(context: Context, apiKey: String, hostname: String? = null) = suspendCoroutine<String> { continuation ->
ReCaptchaOverlay(context, apiKey, hostname ?: "localhost:5000", continuation).show() ReCaptchaOverlay(context, apiKey, hostname ?: "localhost:5000", continuation).show()

View File

@ -1,14 +1,11 @@
/* /*
* SPDX-FileCopyrightText: 2021, microG Project Team * SPDX-FileCopyrightText: 2021 microG Project Team
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
package org.microg.gms.profile package org.microg.gms.profile
import android.annotation.TargetApi import android.annotation.TargetApi
import android.content.Context
import android.os.Build
import kotlin.random.Random
object Build { object Build {
@JvmField @JvmField
@ -94,4 +91,11 @@ object Build {
@JvmField @JvmField
var SECURITY_PATCH: String? = null var SECURITY_PATCH: String? = null
} }
fun generateWebViewUserAgentString(original: String): String {
if (!original.startsWith("Mozilla/5.0 (")) return original
val closeParen: Int = original.indexOf(')')
return "Mozilla/5.0 (Linux; Android ${VERSION.RELEASE}; $MODEL Build/$ID; wv)${original.substring(closeParen + 1)}"
}
} }

View File

@ -25,7 +25,6 @@ import android.graphics.Color;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
@ -37,11 +36,11 @@ import android.webkit.CookieManager;
import android.webkit.JavascriptInterface; import android.webkit.JavascriptInterface;
import android.webkit.WebSettings; import android.webkit.WebSettings;
import android.webkit.WebView; import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.RelativeLayout; import android.widget.RelativeLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.StringRes; import androidx.annotation.StringRes;
import androidx.webkit.WebViewClientCompat;
import com.google.android.gms.R; import com.google.android.gms.R;
@ -55,6 +54,8 @@ import org.microg.gms.checkin.LastCheckinInfo;
import org.microg.gms.common.HttpFormClient; import org.microg.gms.common.HttpFormClient;
import org.microg.gms.common.Utils; import org.microg.gms.common.Utils;
import org.microg.gms.people.PeopleManager; import org.microg.gms.people.PeopleManager;
import org.microg.gms.profile.Build;
import org.microg.gms.profile.ProfileManager;
import java.io.IOException; import java.io.IOException;
import java.util.Locale; import java.util.Locale;
@ -106,7 +107,7 @@ public class LoginActivity extends AssistantActivity {
webView.addJavascriptInterface(new JsBridge(), "mm"); webView.addJavascriptInterface(new JsBridge(), "mm");
authContent = (ViewGroup) findViewById(R.id.auth_content); authContent = (ViewGroup) findViewById(R.id.auth_content);
((ViewGroup) findViewById(R.id.auth_root)).addView(webView); ((ViewGroup) findViewById(R.id.auth_root)).addView(webView);
webView.setWebViewClient(new WebViewClient() { webView.setWebViewClient(new WebViewClientCompat() {
@Override @Override
public void onPageFinished(WebView view, String url) { public void onPageFinished(WebView view, String url) {
Log.d(TAG, "pageFinished: " + view.getUrl()); Log.d(TAG, "pageFinished: " + view.getUrl());
@ -135,14 +136,14 @@ public class LoginActivity extends AssistantActivity {
AccountManager accountManager = AccountManager.get(this); AccountManager accountManager = AccountManager.get(this);
Account account = new Account(getIntent().getStringExtra(EXTRA_EMAIL), accountType); Account account = new Account(getIntent().getStringExtra(EXTRA_EMAIL), accountType);
accountManager.addAccountExplicitly(account, getIntent().getStringExtra(EXTRA_TOKEN), null); accountManager.addAccountExplicitly(account, getIntent().getStringExtra(EXTRA_TOKEN), null);
if (isAuthVisible(this) && SDK_INT >= Build.VERSION_CODES.O) { if (isAuthVisible(this) && SDK_INT >= 26) {
accountManager.setAccountVisibility(account, PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, VISIBILITY_USER_MANAGED_VISIBLE); accountManager.setAccountVisibility(account, PACKAGE_NAME_KEY_LEGACY_NOT_VISIBLE, VISIBILITY_USER_MANAGED_VISIBLE);
} }
retrieveGmsToken(account); retrieveGmsToken(account);
} else { } else {
retrieveRtToken(getIntent().getStringExtra(EXTRA_TOKEN)); retrieveRtToken(getIntent().getStringExtra(EXTRA_TOKEN));
} }
} else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { } else if (android.os.Build.VERSION.SDK_INT < 21) {
init(); init();
} else { } else {
setMessage(R.string.auth_before_connect); setMessage(R.string.auth_before_connect);
@ -200,13 +201,14 @@ public class LoginActivity extends AssistantActivity {
webView.setLayoutParams(new RelativeLayout.LayoutParams( webView.setLayoutParams(new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
webView.setBackgroundColor(Color.TRANSPARENT); webView.setBackgroundColor(Color.TRANSPARENT);
prepareWebViewSettings(webView.getSettings()); prepareWebViewSettings(context, webView.getSettings());
return webView; return webView;
} }
@SuppressLint("SetJavaScriptEnabled") @SuppressLint("SetJavaScriptEnabled")
private static void prepareWebViewSettings(WebSettings settings) { private static void prepareWebViewSettings(Context context, WebSettings settings) {
settings.setUserAgentString(settings.getUserAgentString() + MAGIC_USER_AGENT); ProfileManager.ensureInitialized(context);
settings.setUserAgentString(Build.INSTANCE.generateWebViewUserAgentString(settings.getUserAgentString()) + MAGIC_USER_AGENT);
settings.setJavaScriptEnabled(true); settings.setJavaScriptEnabled(true);
settings.setSupportMultipleWindows(false); settings.setSupportMultipleWindows(false);
settings.setSaveFormData(false); settings.setSaveFormData(false);

View File

@ -22,6 +22,8 @@ import androidx.lifecycle.lifecycleScope
import androidx.webkit.WebViewClientCompat import androidx.webkit.WebViewClientCompat
import com.google.android.gms.safetynet.SafetyNetStatusCodes.* import com.google.android.gms.safetynet.SafetyNetStatusCodes.*
import org.microg.gms.droidguard.core.DroidGuardResultCreator import org.microg.gms.droidguard.core.DroidGuardResultCreator
import org.microg.gms.profile.Build
import org.microg.gms.profile.ProfileManager
import org.microg.gms.safetynet.core.ui.R import org.microg.gms.safetynet.core.ui.R
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
import java.net.URLEncoder import java.net.URLEncoder
@ -92,6 +94,8 @@ class ReCaptchaActivity : AppCompatActivity() {
displayZoomControls = false displayZoomControls = false
setSupportZoom(false) setSupportZoom(false)
cacheMode = WebSettings.LOAD_NO_CACHE cacheMode = WebSettings.LOAD_NO_CACHE
ProfileManager.ensureInitialized(this@ReCaptchaActivity)
userAgentString = Build.generateWebViewUserAgentString(userAgentString)
} }
addJavascriptInterface(object { addJavascriptInterface(object {
@JavascriptInterface @JavascriptInterface