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

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

Auth: Update API implementation

parent 5d50ae37
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -11,4 +11,7 @@ interface IAuthManagerService {
    Bundle clearToken(String token, in Bundle extras) = 1;
    AccountChangeEventsResponse getChangeEvents(in AccountChangeEventsRequest request) = 2;
    Bundle getTokenWithAccount(in Account account, String scope, in Bundle extras) = 4;
    Bundle getAccounts(in Bundle extras) = 5;
    Bundle removeAccount(in Account account) = 6;
    Bundle requestGoogleAccountsAccess(String packageName) = 7;
}
+62 −24
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package org.microg.gms.auth;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.accounts.AuthenticatorException;
import android.accounts.OperationCanceledException;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
@@ -43,7 +45,9 @@ import org.microg.gms.common.PackageUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import static android.accounts.AccountManager.KEY_ACCOUNTS;
import static android.accounts.AccountManager.KEY_ACCOUNT_NAME;
import static android.accounts.AccountManager.KEY_ACCOUNT_TYPE;
import static android.accounts.AccountManager.KEY_AUTHTOKEN;
@@ -53,6 +57,7 @@ import static org.microg.gms.auth.AskPermissionActivity.EXTRA_CONSENT_DATA;
public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
    private static final String TAG = "GmsAuthManagerSvc";

    public static final String KEY_ACCOUNT_FEATURES = "account_features";
    public static final String KEY_AUTHORITY = "authority";
    public static final String KEY_CALLBACK_INTENT = "callback_intent";
    public static final String KEY_CALLER_UID = "callerUid";
@@ -74,14 +79,42 @@ public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
    }

    @Override
    public Bundle getToken(String accountName, String scope, Bundle extras) throws RemoteException {
    public Bundle getToken(String accountName, String scope, Bundle extras) {
        return getTokenWithAccount(new Account(accountName, AuthConstants.DEFAULT_ACCOUNT_TYPE), scope, extras);
    }

    private List<Scope> getScopes(String scope) {
        if (!scope.startsWith("oauth2:")) return null;
        String[] strings = scope.substring(7).split(" ");
        List<Scope> res = new ArrayList<Scope>();
        for (String string : strings) {
            res.add(new Scope(string));
        }
        return res;
    }

    private static CharSequence getPackageLabel(String packageName, PackageManager pm) {
        try {
            return pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0));
        } catch (PackageManager.NameNotFoundException e) {
            return packageName;
        }
    }

    @Override
    public AccountChangeEventsResponse getChangeEvents(AccountChangeEventsRequest request) {
        return new AccountChangeEventsResponse();
    }

    @Override
    public Bundle getTokenWithAccount(Account account, String scope, Bundle extras) {
        String packageName = extras.getString(KEY_ANDROID_PACKAGE_NAME);
        if (packageName == null || packageName.isEmpty())
            packageName = extras.getString(KEY_CLIENT_PACKAGE_NAME);
        packageName = PackageUtils.getAndCheckCallingPackage(context, packageName, extras.getInt(KEY_CALLER_UID, 0), extras.getInt(KEY_CALLER_PID, 0));
        boolean notify = extras.getBoolean(KEY_HANDLE_NOTIFICATION, false);

        Log.d(TAG, "getToken: account:" + accountName + " scope:" + scope + " extras:" + extras + ", notify: " + notify);
        Log.d(TAG, "getToken: account:" + account.name + " scope:" + scope + " extras:" + extras + ", notify: " + notify);

        /*
         * TODO: This scope seems to be invalid (according to https://developers.google.com/oauthplayground/),
@@ -89,9 +122,9 @@ public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
         */
        scope = scope.replace("https://www.googleapis.com/auth/identity.plus.page.impersonation ", "");

        AuthManager authManager = new AuthManager(context, accountName, packageName, scope);
        AuthManager authManager = new AuthManager(context, account.name, packageName, scope);
        Bundle result = new Bundle();
        result.putString(KEY_ACCOUNT_NAME, accountName);
        result.putString(KEY_ACCOUNT_NAME, account.name);
        result.putString(KEY_ACCOUNT_TYPE, authManager.getAccountType());
        if (!authManager.accountExists()) {
            result.putString(KEY_ERROR, "NetworkError");
@@ -112,7 +145,7 @@ public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
                i.putExtras(extras);
                i.putExtra(KEY_ANDROID_PACKAGE_NAME, packageName);
                i.putExtra(KEY_ACCOUNT_TYPE, authManager.getAccountType());
                i.putExtra(KEY_ACCOUNT_NAME, accountName);
                i.putExtra(KEY_ACCOUNT_NAME, account.name);
                i.putExtra(KEY_AUTHTOKEN, scope);
                try {
                    if (res.consentDataBase64 != null)
@@ -138,36 +171,41 @@ public class AuthManagerServiceImpl extends IAuthManagerService.Stub {
        return result;
    }

    private List<Scope> getScopes(String scope) {
        if (!scope.startsWith("oauth2:")) return null;
        String[] strings = scope.substring(7).split(" ");
        List<Scope> res = new ArrayList<Scope>();
        for (String string : strings) {
            res.add(new Scope(string));
        }
        return res;
    }

    private static CharSequence getPackageLabel(String packageName, PackageManager pm) {
    @Override
    public Bundle getAccounts(Bundle extras) {
        PackageUtils.assertExtendedAccess(context);
        String[] accountFeatures = extras.getStringArray(KEY_ACCOUNT_FEATURES);
        String accountType = extras.getString(KEY_ACCOUNT_TYPE);
        Account[] accounts;
        if (accountFeatures != null) {
            try {
            return pm.getApplicationLabel(pm.getApplicationInfo(packageName, 0));
        } catch (PackageManager.NameNotFoundException e) {
            return packageName;
                accounts = AccountManager.get(context).getAccountsByTypeAndFeatures(accountType, accountFeatures, null, null).getResult(5, TimeUnit.SECONDS);
            } catch (Exception e) {
                Log.w(TAG, e);
                return null;
            }
        } else {
            accounts = AccountManager.get(context).getAccountsByType(accountType);
        }
        Bundle res = new Bundle();
        res.putParcelableArray(KEY_ACCOUNTS, accounts);
        return res;
    }

    @Override
    public AccountChangeEventsResponse getChangeEvents(AccountChangeEventsRequest request) {
        return new AccountChangeEventsResponse();
    public Bundle removeAccount(Account account) {
        Log.w(TAG, "Not implemented: removeAccount(" + account + ")");
        return null;
    }

    @Override
    public Bundle getTokenWithAccount(Account account, String scope, Bundle extras) throws RemoteException {
        return getToken(account.name, scope, extras);
    public Bundle requestGoogleAccountsAccess(String packageName) throws RemoteException {
        Log.w(TAG, "Not implemented: requestGoogleAccountsAccess(" + packageName + ")");
        return null;
    }

    @Override
    public Bundle clearToken(String token, Bundle extras) throws RemoteException {
    public Bundle clearToken(String token, Bundle extras) {
        String packageName = extras.getString(KEY_ANDROID_PACKAGE_NAME);
        if (packageName == null) packageName = extras.getString(KEY_CLIENT_PACKAGE_NAME);
        packageName = PackageUtils.getAndCheckCallingPackage(context, packageName, extras.getInt(KEY_CALLER_UID, 0), extras.getInt(KEY_CALLER_PID, 0));