Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Verified Commit 45ee4903 authored by Marvin W.'s avatar Marvin W. 🐿️
Browse files

Adapt auth implementation to better reflect current play services

parent 6174997c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -197,6 +197,7 @@ public class AuthManager {
            }
        }
        AuthRequest request = new AuthRequest().fromContext(context)
                .source("android")
                .app(packageName, getPackageSignature())
                .email(accountName)
                .token(getAccountManager().getPassword(account))
+11 −6
Original line number Diff line number Diff line
@@ -30,8 +30,8 @@ import static org.microg.gms.common.HttpFormClient.RequestContent;
import static org.microg.gms.common.HttpFormClient.RequestHeader;

public class AuthRequest extends HttpFormClient.Request {
    private static final String SERVICE_URL = "https://android.clients.google.com/auth";
    private static final String USER_AGENT = "GoogleAuth/1.4 (%s %s)";
    private static final String SERVICE_URL = "https://android.googleapis.com/auth";
    private static final String USER_AGENT = "GoogleAuth/1.4 (%s %s); gzip";

    @RequestHeader("User-Agent")
    private String userAgent;
@@ -59,13 +59,13 @@ public class AuthRequest extends HttpFormClient.Request {
    @RequestContent("google_play_services_version")
    public int gmsVersion = Constants.MAX_REFERENCE_VERSION;
    @RequestContent("accountType")
    public String accountType = "HOSTED_OR_GOOGLE";
    public String accountType;
    @RequestContent("Email")
    public String email;
    @RequestContent("service")
    public String service;
    @RequestContent("source")
    public String source = "android";
    public String source;
    @RequestContent({"is_called_from_account_manager", "_opt_is_called_from_account_manager"})
    public boolean isCalledFromAccountManager;
    @RequestContent("Token")
@@ -97,10 +97,15 @@ public class AuthRequest extends HttpFormClient.Request {
        return this;
    }

    public AuthRequest source(String source) {
        this.source = source;
        return this;
    }

    public AuthRequest locale(Locale locale) {
        this.locale = locale.toString();
        this.countryCode = locale.getCountry();
        this.operatorCountryCode = locale.getCountry();
        this.countryCode = locale.getCountry().toLowerCase();
        this.operatorCountryCode = locale.getCountry().toLowerCase();
        return this;
    }

+68 −61
Original line number Diff line number Diff line
@@ -21,19 +21,17 @@ import android.accounts.AccountManager;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.StringRes;
import android.support.v4.view.LayoutInflaterCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
@@ -73,7 +71,6 @@ import static android.view.View.VISIBLE;
import static android.view.inputmethod.InputMethodManager.SHOW_IMPLICIT;
import static org.microg.gms.common.Constants.GMS_PACKAGE_NAME;
import static org.microg.gms.common.Constants.MAX_REFERENCE_VERSION;
import android.os.Build;

public class LoginActivity extends AssistantActivity {
    public static final String TMPL_NEW_ACCOUNT = "new_account";
@@ -110,18 +107,13 @@ public class LoginActivity extends AssistantActivity {
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                Log.d(TAG, "pageFinished: " + url);
                Uri uri = Uri.parse(url);
                Log.d(TAG, "pageFinished: " + view.getUrl());
                Uri uri = Uri.parse(view.getUrl());

                // Begin login.
                // UNUSED: uri=...#identifier never happens. if commented out, webView still appears!
                if ("identifier".equals(uri.getFragment()))
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            webView.setVisibility(VISIBLE);
                        }
                    });
                // Only required if client code does not invoke showView() via JSBridge
                if ("identifier".equals(uri.getFragment()) || uri.getPath().endsWith("/identifier"))
                    runOnUiThread(() -> webView.setVisibility(VISIBLE));

                // Normal login.
                if ("close".equals(uri.getFragment()))
@@ -185,12 +177,7 @@ public class LoginActivity extends AssistantActivity {
        setMessage(R.string.auth_connecting);
        CookieManager.getInstance().setAcceptCookie(true);
        if (SDK_INT >= LOLLIPOP) {
            CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
                @Override
                public void onReceiveValue(Boolean value) {
                    start();
                }
            });
            CookieManager.getInstance().removeAllCookies(value -> start());
        } else {
            //noinspection deprecation
            CookieManager.getInstance().removeAllCookie();
@@ -231,23 +218,10 @@ public class LoginActivity extends AssistantActivity {
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (LastCheckinInfo.read(this).androidId == 0) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                new Thread(() -> {
                    Runnable next;
                        next = checkin(false) ? new Runnable() {
                            @Override
                            public void run() {
                                loadLoginPage();
                            }
                        } : new Runnable() {
                            @Override
                            public void run() {
                                showError(R.string.auth_general_error_desc);
                            }
                        };
                    next = checkin(false) ? this::loadLoginPage : () -> showError(R.string.auth_general_error_desc);
                    LoginActivity.this.runOnUiThread(next);
                    }
                }).start();
            } else {
                loadLoginPage();
@@ -278,12 +252,7 @@ public class LoginActivity extends AssistantActivity {

    private void closeWeb(boolean programmaticAuth) {
        setMessage(R.string.auth_finalize);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                webView.setVisibility(INVISIBLE);
            }
        });
        runOnUiThread(() -> webView.setVisibility(INVISIBLE));
        String cookies = CookieManager.getInstance().getCookie(programmaticAuth ? PROGRAMMATIC_AUTH_URL : EMBEDDED_SETUP_URL);
        String[] temp = cookies.split(";");
        for (String ar1 : temp) {
@@ -299,12 +268,11 @@ public class LoginActivity extends AssistantActivity {
    private void retrieveRtToken(String oAuthToken) {
        new AuthRequest().fromContext(this)
                .appIsGms()
                .callerIsGms()
                .service("ac2dm")
                .token(oAuthToken).isAccessToken()
                .addAccount()
                .getAccountId()
                .systemPartition()
                .hasPermission()
                .droidguardResults(null /*TODO*/)
                .getResponseAsync(new HttpFormClient.Callback<AuthResponse>() {
                    @Override
@@ -325,12 +293,9 @@ public class LoginActivity extends AssistantActivity {
                            setResult(RESULT_OK);
                        } else {
                            Log.w(TAG, "Account NOT created!");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                            runOnUiThread(() -> {
                                showError(R.string.auth_general_error_desc);
                                setNextButtonText(android.R.string.ok);
                                }
                            });
                            state = -2;
                        }
@@ -348,6 +313,7 @@ public class LoginActivity extends AssistantActivity {
        authManager.setPermitted(true);
        new AuthRequest().fromContext(this)
                .appIsGms()
                .callerIsGms()
                .service(authManager.getService())
                .email(account.name)
                .token(AccountManager.get(this).getPassword(account))
@@ -405,9 +371,28 @@ public class LoginActivity extends AssistantActivity {
    }

    private class JsBridge {

        @JavascriptInterface
        public final void addAccount(String json) {
            Log.d(TAG, "JSBridge: addAccount " + json);
        }

        @JavascriptInterface
        public final void closeView() {
            Log.d(TAG, "JSBridge: closeView");
            closeWeb(false);
        }

        @JavascriptInterface
        public final String fetchVerifiedPhoneNumber() {
            Log.d(TAG, "JSBridge: fetchVerifiedPhoneNumber");
            return null;
        }

        @SuppressWarnings("MissingPermission")
        @JavascriptInterface
        public final String getAccounts() {
            Log.d(TAG, "JSBridge: getAccounts");
            Account[] accountsByType = accountManager.getAccountsByType(accountType);
            JSONArray json = new JSONArray();
            for (Account account : accountsByType) {
@@ -418,16 +403,23 @@ public class LoginActivity extends AssistantActivity {

        @JavascriptInterface
        public final String getAllowedDomains() {
            Log.d(TAG, "JSBridge: getAllowedDomains");
            return new JSONArray().toString();
        }

        @JavascriptInterface
        public final String getAndroidId() {
            long androidId = LastCheckinInfo.read(LoginActivity.this).androidId;
            Log.d(TAG, "JSBridge: getAndroidId " + androidId);
            if (androidId == 0 || androidId == -1) return null;
            return Long.toHexString(androidId);
        }

        @JavascriptInterface
        public final int getAuthModuleVersionCode() {
            return 1;
        }

        @JavascriptInterface
        public final int getBuildVersionSdk() {
            return SDK_INT;
@@ -438,6 +430,11 @@ public class LoginActivity extends AssistantActivity {
            Log.d(TAG, "JSBridge: getDroidGuardResult: " + s);
        }

        @JavascriptInterface
        public final int getDeviceDataVersionInfo() {
            return 1;
        }

        @JavascriptInterface
        public final String getFactoryResetChallenges() {
            return new JSONArray().toString();
@@ -484,14 +481,29 @@ public class LoginActivity extends AssistantActivity {
            inputMethodManager.hideSoftInputFromWindow(webView.getWindowToken(), 0);
        }

        @JavascriptInterface
        public final boolean isUserOwner() {
            return true;
        }

        @JavascriptInterface
        public final void launchEmergencyDialer() {
            Log.d(TAG, "JSBridge: launchEmergencyDialer");
        }

        @JavascriptInterface
        public final void log(String s) {
            Log.d(TAG, "JSBridge: log " + s);
        }

        @JavascriptInterface
        public final void notifyOnTermsOfServiceAccepted() {
            Log.d(TAG, "Terms of service accepted. (who cares?)");
            Log.d(TAG, "JSBridge: notifyOnTermsOfServiceAccepted");
        }

        @JavascriptInterface
        public final void setAccountIdentifier(String accountIdentifier) {
            Log.d(TAG, "JSBridge: setAccountIdentifier " + accountIdentifier);
        }

        @TargetApi(HONEYCOMB)
@@ -510,7 +522,7 @@ public class LoginActivity extends AssistantActivity {

        @JavascriptInterface
        public final void setNewAccountCreated() {
            Log.d(TAG, "New account created. (who cares?)");
            Log.d(TAG, "JSBridge: setNewAccountCreated");
        }

        @JavascriptInterface
@@ -522,12 +534,7 @@ public class LoginActivity extends AssistantActivity {
        @JavascriptInterface
        public final void showView() {
            Log.d(TAG, "JSBridge: showView");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    webView.setVisibility(VISIBLE);
                }
            });
            runOnUiThread(() -> webView.setVisibility(VISIBLE));
        }

        @JavascriptInterface