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

Commit 82d01782 authored by Simranjit Singh Kohli's avatar Simranjit Singh Kohli
Browse files

[Fix confirmCredntials flow]

This CL contains 2 fixes:

1. On a successfult confirm credential, lastAuthenticatedTime was
   not getting updated. This gets fixed.

2. Historically, confirmCredential used to work for accounts NOT on
   the device. Setting lastAuthenticated time only for existing
   accounts and preventing crash if account is not on the device.

Bug: 20141809
Change-Id: I301d49cbc4a594af6110366ef6f55350f74d250d
parent d4925af8
Loading
Loading
Loading
Loading
+80 −13
Original line number Original line Diff line number Diff line
@@ -734,12 +734,15 @@ public class AccountManagerService
            throw new IllegalArgumentException("account is null");
            throw new IllegalArgumentException("account is null");
        }
        }
        checkAuthenticateAccountsPermission(account);
        checkAuthenticateAccountsPermission(account);

        final UserAccounts accounts = getUserAccountsForCaller();
        int userId = Binder.getCallingUserHandle().getIdentifier();
        int userId = Binder.getCallingUserHandle().getIdentifier();
        if (!canUserModifyAccounts(userId) || !canUserModifyAccountsForType(userId, account.type)) {
        if (!canUserModifyAccounts(userId) || !canUserModifyAccountsForType(userId, account.type)) {
            return false;
            return false;
        }
        }
        return updateLastAuthenticatedTime(account);
    }

    private boolean updateLastAuthenticatedTime(Account account) {
        final UserAccounts accounts = getUserAccountsForCaller();
        synchronized (accounts.cacheLock) {
        synchronized (accounts.cacheLock) {
            final ContentValues values = new ContentValues();
            final ContentValues values = new ContentValues();
            values.put(ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS, System.currentTimeMillis());
            values.put(ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS, System.currentTimeMillis());
@@ -2022,7 +2025,7 @@ public class AccountManagerService
        try {
        try {
            new Session(accounts, response, account.type, expectActivityLaunch,
            new Session(accounts, response, account.type, expectActivityLaunch,
                    true /* stripAuthTokenFromResult */, account.name,
                    true /* stripAuthTokenFromResult */, account.name,
                    true /* authDetailsRequired */) {
                    true /* authDetailsRequired */, true /* updateLastAuthenticatedTime */) {
                @Override
                @Override
                public void run() throws RemoteException {
                public void run() throws RemoteException {
                    mAuthenticator.confirmCredentials(this, account, options);
                    mAuthenticator.confirmCredentials(this, account, options);
@@ -2059,7 +2062,7 @@ public class AccountManagerService
        try {
        try {
            new Session(accounts, response, account.type, expectActivityLaunch,
            new Session(accounts, response, account.type, expectActivityLaunch,
                    true /* stripAuthTokenFromResult */, account.name,
                    true /* stripAuthTokenFromResult */, account.name,
                    false /* authDetailsRequired */) {
                    false /* authDetailsRequired */, true /* updateLastCredentialTime */) {
                @Override
                @Override
                public void run() throws RemoteException {
                public void run() throws RemoteException {
                    mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions);
                    mAuthenticator.updateCredentials(this, account, authTokenType, loginOptions);
@@ -2492,6 +2495,11 @@ public class AccountManagerService
        final String mAccountName;
        final String mAccountName;
        // Indicates if we need to add auth details(like last credential time)
        // Indicates if we need to add auth details(like last credential time)
        final boolean mAuthDetailsRequired;
        final boolean mAuthDetailsRequired;
        // If set, we need to update the last authenticated time. This is
        // currently
        // used on
        // successful confirming credentials.
        final boolean mUpdateLastAuthenticatedTime;


        public int mNumResults = 0;
        public int mNumResults = 0;
        private int mNumRequestContinued = 0;
        private int mNumRequestContinued = 0;
@@ -2505,6 +2513,13 @@ public class AccountManagerService
        public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType,
        public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType,
                boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName,
                boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName,
                boolean authDetailsRequired) {
                boolean authDetailsRequired) {
            this(accounts, response, accountType, expectActivityLaunch, stripAuthTokenFromResult,
                    accountName, authDetailsRequired, false /* updateLastAuthenticatedTime */);
        }

        public Session(UserAccounts accounts, IAccountManagerResponse response, String accountType,
                boolean expectActivityLaunch, boolean stripAuthTokenFromResult, String accountName,
                boolean authDetailsRequired, boolean updateLastAuthenticatedTime) {
            super();
            super();
            //if (response == null) throw new IllegalArgumentException("response is null");
            //if (response == null) throw new IllegalArgumentException("response is null");
            if (accountType == null) throw new IllegalArgumentException("accountType is null");
            if (accountType == null) throw new IllegalArgumentException("accountType is null");
@@ -2516,6 +2531,7 @@ public class AccountManagerService
            mCreationTime = SystemClock.elapsedRealtime();
            mCreationTime = SystemClock.elapsedRealtime();
            mAccountName = accountName;
            mAccountName = accountName;
            mAuthDetailsRequired = authDetailsRequired;
            mAuthDetailsRequired = authDetailsRequired;
            mUpdateLastAuthenticatedTime = updateLastAuthenticatedTime;


            synchronized (mSessions) {
            synchronized (mSessions) {
                mSessions.put(toString(), this);
                mSessions.put(toString(), this);
@@ -2651,16 +2667,56 @@ public class AccountManagerService
        public void onResult(Bundle result) {
        public void onResult(Bundle result) {
            mNumResults++;
            mNumResults++;
            Intent intent = null;
            Intent intent = null;
            if (result != null && mAuthDetailsRequired) {
            if (result != null) {
                long lastAuthenticatedTime = DatabaseUtils.longForQuery(
                boolean isSuccessfulConfirmCreds = result.getBoolean(
                        AccountManager.KEY_BOOLEAN_RESULT, false);
                boolean isSuccessfulUpdateCreds = 
                        result.containsKey(AccountManager.KEY_ACCOUNT_NAME)
                        && result.containsKey(AccountManager.KEY_ACCOUNT_TYPE);
                // We should only update lastAuthenticated time, if 
                // mUpdateLastAuthenticatedTime is true and the confirmRequest
                // or updateRequest was successful
                boolean needUpdate = mUpdateLastAuthenticatedTime 
                        && (isSuccessfulConfirmCreds || isSuccessfulUpdateCreds);
                if (needUpdate || mAuthDetailsRequired) {
                    boolean accountPresent = isAccountPresentForCaller(mAccountName, mAccountType);
                    if (needUpdate && accountPresent) {
                        updateLastAuthenticatedTime(new Account(mAccountName, mAccountType));
                    }
                    if (mAuthDetailsRequired) {
                        long lastAuthenticatedTime = -1;
                        if (accountPresent) {
                            lastAuthenticatedTime = DatabaseUtils.longForQuery(
                                    mAccounts.openHelper.getReadableDatabase(),
                                    "select " + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS
                                            + " from " +
                                            TABLE_ACCOUNTS + " WHERE " + ACCOUNTS_NAME + "=? AND "
                                            + ACCOUNTS_TYPE + "=?",
                                    new String[] {
                                            mAccountName, mAccountType
                                    });
                        }
                        result.putLong(AccountManager.KEY_LAST_AUTHENTICATE_TIME_MILLIS_EPOCH,
                                lastAuthenticatedTime);
                    }
                }
                if (mAuthDetailsRequired) {
                    long lastAuthenticatedTime = -1;
                    if (isAccountPresentForCaller(mAccountName, mAccountType)) {
                        lastAuthenticatedTime = DatabaseUtils.longForQuery(
                                mAccounts.openHelper.getReadableDatabase(),
                                mAccounts.openHelper.getReadableDatabase(),
                        "select " + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS + " from " +
                                "select " + ACCOUNTS_LAST_AUTHENTICATE_TIME_EPOCH_MILLIS + " from "
                                        +
                                        TABLE_ACCOUNTS + " WHERE " + ACCOUNTS_NAME + "=? AND "
                                        TABLE_ACCOUNTS + " WHERE " + ACCOUNTS_NAME + "=? AND "
                                        + ACCOUNTS_TYPE + "=?",
                                        + ACCOUNTS_TYPE + "=?",
                        new String[]{mAccountName, mAccountType});
                                new String[] {
                                        mAccountName, mAccountType
                                });
                    }
                    result.putLong(AccountManager.KEY_LAST_AUTHENTICATE_TIME_MILLIS_EPOCH,
                    result.putLong(AccountManager.KEY_LAST_AUTHENTICATE_TIME_MILLIS_EPOCH,
                            lastAuthenticatedTime);
                            lastAuthenticatedTime);
                }
                }
            }
            if (result != null
            if (result != null
                    && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) {
                    && (intent = result.getParcelable(AccountManager.KEY_INTENT)) != null) {
                /*
                /*
@@ -3202,6 +3258,17 @@ public class AccountManagerService
        return false;
        return false;
    }
    }


    private boolean isAccountPresentForCaller(String accountName, String accountType) {
        if (getUserAccountsForCaller().accountCache.containsKey(accountType)) {
            for (Account account : getUserAccountsForCaller().accountCache.get(accountType)) {
                if (account.name.equals(accountName)) {
                    return true;
                }
            }
        }
        return false;
    }

    private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType,
    private boolean hasExplicitlyGrantedPermission(Account account, String authTokenType,
            int callerUid) {
            int callerUid) {
        if (callerUid == Process.SYSTEM_UID) {
        if (callerUid == Process.SYSTEM_UID) {