Example usage for android.accounts AccountManager invalidateAuthToken

List of usage examples for android.accounts AccountManager invalidateAuthToken

Introduction

In this page you can find the example usage for android.accounts AccountManager invalidateAuthToken.

Prototype

public void invalidateAuthToken(final String accountType, final String authToken) 

Source Link

Document

Removes an auth token from the AccountManager's cache.

Usage

From source file:org.ohmage.app.OhmageErrorHandler.java

@Override
public Throwable handleError(RetrofitError cause) {

    Response r = cause.getResponse();
    if (r != null && r.getStatus() == 401) {
        // invalidate the access token
        AccountManager accountManager = AccountManager.get(Ohmage.app());
        Account[] accounts = accountManager.getAccountsByType(AuthUtil.ACCOUNT_TYPE);
        if (accounts.length != 0) {
            String token = accountManager.peekAuthToken(accounts[0], AuthUtil.AUTHTOKEN_TYPE);
            if (token != null) {
                accountManager.invalidateAuthToken(AuthUtil.ACCOUNT_TYPE, token);
                Log.e(TAG, "Invalidated " + token);
            }//from   w w w  . ja  va 2s. c o m
        }
        return new AuthenticationException("Error authenticating with ohmage", cause);
    }

    return cause;
}

From source file:com.mrcaps.taskswidget.TasksHelper.java

public static void refreshAuthTokenImpl5(final Context context, final boolean invalidate,
        final Runnable continuation) {
    final Handler handl = getHandler();

    new Thread(new Runnable() {
        public void run() {
            AccountManager mgr = AccountManager.get(context);
            Account[] accts = mgr.getAccountsByType(ACCOUNT_TYPE);
            if (accts.length < 1) {
                handl.post(new Runnable() {
                    public void run() {
                        Toast.makeText(context, "Error: could not find a Google account on this phone",
                                Toast.LENGTH_SHORT).show();
                    }//from  ww  w.  j  av a 2 s . co m
                });
                return;
            }

            Account acct = accts[0];

            if (invalidate) {
                long now = System.currentTimeMillis();
                Log.v(TAG, "I5 Auth Token Invalidation Requested");
                if (now - lastInvalidation > invalidationDelta) {
                    Log.v(TAG, "I5 Invalidating Auth Token");
                    try {
                        String token = mgr.blockingGetAuthToken(acct, SERVICE_NAME, true);
                        mgr.invalidateAuthToken(ACCOUNT_TYPE, token);
                    } catch (Exception e) {
                        Log.e(TAG, "I5 couldn't invalidate token", e);
                    }
                }
            }

            try {
                String token = mgr.blockingGetAuthToken(acct, SERVICE_NAME, true);
                Log.v(TAG, "I5 got auth token: " + token);
                authToken = token;
            } catch (Exception e) {
                Log.e(TAG, "I5 couldn't authenticate", e);
                handl.post(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(context, "Error: I5 could not authenticate with Google account",
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }

            continuation.run();
        }
    }).start();
}

From source file:com.jefftharris.passwdsafe.sync.owncloud.OwncloudProvider.java

@Override
public void unlinkAccount() {
    saveCertAlias(null, getContext());// w  ww.j  a  v  a  2s  .  c  o m
    saveAuthData(null, null);
    updateOwncloudAcct();
    AccountManager acctMgr = AccountManager.get(getContext());
    acctMgr.invalidateAuthToken(SyncDb.OWNCLOUD_ACCOUNT_TYPE,
            AccountTypeUtils.getAuthTokenTypePass(SyncDb.OWNCLOUD_ACCOUNT_TYPE));
}

From source file:org.klnusbaum.udj.network.EventCommService.java

private void handleLoginAuthException(Intent intent, AccountManager am, Account account, String authToken,
        boolean attemptReauth) {
    if (attemptReauth) {
        Log.d(TAG, "Soft Authentication exception when joining event");
        am.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
        enterEvent(intent, am, account, false);
    } else {/*  w  w  w . j a  v a2 s.c om*/
        Log.e(TAG, "Hard Authentication exception when joining event");
        doLoginFail(am, account, EventJoinError.AUTHENTICATION_ERROR);
    }
}

From source file:com.github.opengarageapp.activity.MainActivity.java

public void invalidateAuthToken() {
    AccountManager manager = AccountManager.get(this);
    manager.invalidateAuthToken("com.google", application.getAuthToken());
}

From source file:dev.drsoran.moloko.sync.SyncAdapter.java

private void invalidateAccount(String authToken) {
    final AccountManager accountManager = AccountManager.get(context);
    accountManager.invalidateAuthToken(Constants.ACCOUNT_TYPE, authToken);
}

From source file:com.google.sampling.experiential.android.lib.GoogleAccountLoginHelper.java

private String getGoogleAuthToken(AccountManager accountManager, Account account)
        throws OperationCanceledException, IOException, AuthenticatorException {
    String authToken = getNewAuthToken(accountManager, account);
    accountManager.invalidateAuthToken("ah", authToken);
    // There is a bug. Workaround: invalidate the old token, then re-retrieve.
    authToken = getNewAuthToken(accountManager, account);
    return authToken;
}

From source file:com.google.wireless.speed.speedometer.AccountSelector.java

/** Starts an authentication request  */
public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException {
    Log.i(SpeedometerApp.TAG, "AccountSelector.authenticate() running");
    /* We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during
     * which we can reuse the cookie. If authentication fails due to expired
     * authToken, the client of AccountSelector can call authImmedately() to request
     * authenticate() upon the next checkin
     *///  ww w. jav a  2s  .  c  o m
    long authTimeLast = this.getLastAuthTime();
    long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast;
    if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) {
        return;
    }

    Log.i(SpeedometerApp.TAG,
            "Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. ");

    AccountManager accountManager = AccountManager.get(context.getApplicationContext());
    if (this.authToken != null) {
        // There will be no effect on the token if it is still valid
        Log.i(SpeedometerApp.TAG, "Invalidating token");
        accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken);
    }

    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    Log.i(SpeedometerApp.TAG, "Got " + accounts.length + " accounts");

    if (accounts != null && accounts.length > 0) {
        // TODO(mdw): If multiple accounts, need to pick the correct one
        Account accountToUse = accounts[0];
        // We prefer google's corporate account to personal accounts such as somebody@gmail.com
        for (Account account : accounts) {
            if (account.name.toLowerCase().trim().endsWith(ACCOUNT_NAME)) {
                Log.i(SpeedometerApp.TAG, "Using the preferred google.com account: " + account.name);
                accountToUse = account;
                break;
            }
        }

        Log.i(SpeedometerApp.TAG, "Trying to get auth token for " + accountToUse);

        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false,
                new AccountManagerCallback<Bundle>() {
                    @Override
                    public void run(AccountManagerFuture<Bundle> result) {
                        Log.i(SpeedometerApp.TAG, "AccountManagerCallback invoked");
                        try {
                            getAuthToken(result);
                        } catch (RuntimeException e) {
                            Log.e(SpeedometerApp.TAG, "Failed to get authToken", e);
                            /* TODO(Wenjie): May ask the user whether to quit the app nicely here if a number
                             * of trials have been made and failed. Since Speedometer is basically useless 
                             * without checkin
                             */
                        }
                    }
                }, null);
        Log.i(SpeedometerApp.TAG, "AccountManager.getAuthToken returned " + future);
    } else {
        throw new RuntimeException("No google account found");
    }
}

From source file:com.mobiperf.speedometer.AccountSelector.java

/** Starts an authentication request */
public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException {
    Logger.i("AccountSelector.authenticate() running");
    /*// w  ww  . j a v a 2s  .c o m
     * We only need to authenticate every AUTHENTICATE_PERIOD_MILLI milliseconds, during which we
     * can reuse the cookie. If authentication fails due to expired authToken, the client of
     * AccountSelector can call authImmedately() to request authenticate() upon the next checkin
     */
    long authTimeLast = this.getLastAuthTime();
    long timeSinceLastAuth = System.currentTimeMillis() - authTimeLast;
    if (!this.shouldAuthImmediately() && authTimeLast != 0 && (timeSinceLastAuth < AUTHENTICATE_PERIOD_MSEC)) {
        return;
    }

    Logger.i("Authenticating. Last authentication is " + timeSinceLastAuth / 1000 / 60 + " minutes ago. ");

    AccountManager accountManager = AccountManager.get(context.getApplicationContext());
    if (this.authToken != null) {
        // There will be no effect on the token if it is still valid
        Logger.i("Invalidating token");
        accountManager.invalidateAuthToken(ACCOUNT_TYPE, this.authToken);
    }

    Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE);
    Logger.i("Got " + accounts.length + " accounts");

    // get selected account
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context);
    String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null);

    if (accounts != null && accounts.length > 0 && selectedAccount != null) {
        Account accountToUse = null;
        for (Account account : accounts) {
            // if (account.name.toLowerCase().trim().endsWith(ACCOUNT_NAME)) {
            Logger.i("account list: " + account.name + " " + account.type + " " + account.toString());
            // If one of the available accounts is the one selected by user, use that
            if (account.name.equals(selectedAccount)) {
                accountToUse = account;
                Logger.i("selected account: " + account.name + " " + account.type + " " + account.toString());
            }
        }

        Logger.i("Trying to get auth token for " + accountToUse);

        AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false,
                new AccountManagerCallback<Bundle>() {
                    @Override
                    public void run(AccountManagerFuture<Bundle> result) {
                        Logger.i("AccountManagerCallback invoked");
                        try {
                            getAuthToken(result);
                        } catch (RuntimeException e) {
                            Logger.e("Failed to get authToken", e);
                            /*
                             * TODO(Wenjie): May ask the user whether to quit the app nicely here if a number of
                             * trials have been made and failed. Since Speedometer is basically useless without
                             * checkin
                             */
                        }
                    }
                }, null);
        Logger.i("AccountManager.getAuthToken returned " + future);
    } else {
        throw new RuntimeException("No google account found or no google account selected");
    }
}

From source file:mobisocial.musubi.ui.fragments.AccountLinkDialog.java

/**
 * Returns a current token for the given Google account, or
 * null if a token isn't available without user interaction.
 *///from   w w  w .j a  v a  2s.c o m
public static String silentBlockForGoogleToken(Context context, String accountName) throws IOException {
    Account account = new Account(accountName, ACCOUNT_TYPE_GOOGLE);
    AccountManager accountManager = AccountManager.get(context);
    // Need to get cached token, invalidate it, then get the token again
    String token = blockForCachedGoogleToken(context, account, accountManager);
    if (token != null) {
        accountManager.invalidateAuthToken(ACCOUNT_TYPE_GOOGLE, token);
    }
    token = blockForCachedGoogleToken(context, account, accountManager);
    return token;
}