List of usage examples for android.accounts AccountManager invalidateAuthToken
public void invalidateAuthToken(final String accountType, final String authToken)
From source file:com.mobiperf.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Logger.i("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.j a v a 2 s .co m*/ 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"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null); final String defaultUserName = context.getString(R.string.defaultUser); isAnonymous = true; if (selectedAccount != null && selectedAccount.equals(defaultUserName)) { return; } if (accounts != null && accounts.length > 0) { // Default account should be the Anonymous account Account accountToUse = accounts[accounts.length - 1]; if (!accounts[accounts.length - 1].name.equals(defaultUserName)) { for (Account account : accounts) { if (account.name.equals(defaultUserName)) { accountToUse = account; break; } } } if (selectedAccount != null) { for (Account account : accounts) { if (account.name.equals(selectedAccount)) { accountToUse = account; break; } } } isAnonymous = accountToUse.name.equals(defaultUserName); if (isAnonymous) { Logger.d("Skipping authentication as account is " + defaultUserName); return; } 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"); } }
From source file:com.mobiperf_library.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Logger.i("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 *//*w ww .j ava 2 s.c om*/ 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"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null); final String defaultUserName = Config.DEFAULT_USER; isAnonymous = true; if (selectedAccount != null && selectedAccount.equals(defaultUserName)) { return; } if (accounts != null && accounts.length > 0) { // Default account should be the Anonymous account Account accountToUse = accounts[accounts.length - 1]; if (!accounts[accounts.length - 1].name.equals(defaultUserName)) { for (Account account : accounts) { if (account.name.equals(defaultUserName)) { accountToUse = account; break; } } } if (selectedAccount != null) { for (Account account : accounts) { if (account.name.equals(selectedAccount)) { accountToUse = account; break; } } } isAnonymous = accountToUse.name.equals(defaultUserName); if (isAnonymous) { Logger.d("Skipping authentication as account is " + defaultUserName); return; } 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"); } }
From source file:net.vleu.par.android.rpc.Transceiver.java
/** * Invalidates the {@link GoogleAuthToken} stored in the * {@link AccountManager}// ww w . ja va2 s . co m */ private void invalidatesGoogleAuthToken(final GoogleAuthToken token) { if (token != null) { final AccountManager am = AccountManager.get(this.context); if (Log.isLoggable(TAG, Log.DEBUG)) Log.d(TAG, "Invalidating GoogleAuthToken : " + token.value); am.invalidateAuthToken(GOOGLE_ACCOUNT_TYPE, token.value); } }
From source file:com.num.mobiperf.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { //Logger.i("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 */// w w w. j a v a2 s .c om 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"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this.context); String selectedAccount = prefs.getString("PREF_KEY_SELECTED_ACCOUNT", null); final String defaultUserName = "Anonymous"; isAnonymous = true; if (selectedAccount != null && selectedAccount.equals(defaultUserName)) { return; } if (accounts != null && accounts.length > 0) { // Default account should be the Anonymous account Account accountToUse = accounts[accounts.length - 1]; if (!accounts[accounts.length - 1].name.equals(defaultUserName)) { for (Account account : accounts) { if (account.name.equals(defaultUserName)) { accountToUse = account; break; } } } if (selectedAccount != null) { for (Account account : accounts) { if (account.name.equals(selectedAccount)) { accountToUse = account; break; } } } isAnonymous = accountToUse.name.equals(defaultUserName); if (isAnonymous) { //Logger.d("Skipping authentication as account is " + defaultUserName); return; } // WHERE YOU GET TOKEN!!!!!! //Logger.i("Trying to get auth token for " + accountToUse); AccountManagerFuture<Bundle> future = accountManager.getAuthToken(accountToUse, "ah", false, new AccountManagerCallback<Bundle>() { public void run(AccountManagerFuture<Bundle> result) { //Logger.i("AccountManagerCallback invoked"); try { getAuthToken(result); } catch (RuntimeException e) { System.out.println("Failed to get authToken" + e.getLocalizedMessage()); //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"); } }
From source file:com.mobilyzer.AccountSelector.java
/** Starts an authentication request */ public void authenticate() throws OperationCanceledException, AuthenticatorException, IOException { Logger.i("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 *//*from w w w . j a v a 2s . com*/ 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); } if (!hasGetAccountsPermission(context)) { isAnonymous = true; return; } Account[] accounts = accountManager.getAccountsByType(ACCOUNT_TYPE); Logger.i("Got " + accounts.length + " accounts"); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext()); String selectedAccount = prefs.getString(Config.PREF_KEY_SELECTED_ACCOUNT, null); Logger.i("Selected account = " + selectedAccount); final String defaultUserName = Config.DEFAULT_USER; isAnonymous = true; if (selectedAccount != null && selectedAccount.equals(defaultUserName)) { return; } if (accounts != null && accounts.length > 0) { // Default account should be the Anonymous account Account accountToUse = new Account(Config.DEFAULT_USER, ACCOUNT_TYPE); if (!accounts[accounts.length - 1].name.equals(defaultUserName)) { for (Account account : accounts) { if (account.name.equals(defaultUserName)) { accountToUse = account; break; } } } if (selectedAccount != null) { for (Account account : accounts) { if (account.name.equals(selectedAccount)) { accountToUse = account; break; } } } isAnonymous = accountToUse.name.equals(defaultUserName); if (isAnonymous) { Logger.d("Skipping authentication as account is " + defaultUserName); return; } 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"); } }
From source file:com.xiaomi.account.utils.SysHelper.java
public static boolean deleteBindedDevice(Context context, String devId) { boolean z = false; Account account = ExtraAccountManager.getXiaomiAccount(context); if (account != null) { AccountManager am = AccountManager.get(context); int count = 0; while (count < 2) { String authToken = getAuthToken(am, account, com.xiaomi.account.Constants.DEVICE_INFO_SID); ExtendedAuthToken extendedAuthToken = ExtendedAuthToken.parse(authToken); if (extendedAuthToken == null) { break; }/*from w w w .ja v a 2s .c o m*/ String serviceToken = extendedAuthToken.authToken; String security = extendedAuthToken.security; if (serviceToken == null || security == null) { break; } try { z = CloudHelper.deleteBindedDevice(account.name, am.getUserData(account, Constants.KEY_ENCRYPTED_USER_ID), devId, serviceToken, security); break; } catch (AuthenticationFailureException e) { Log.e(TAG, "auth failure when delete device", e); am.invalidateAuthToken(account.type, authToken); count++; } } } else { Log.w(TAG, "no Xiaomi account"); } return z; }
From source file:com.owncloud.android.oc_framework.operations.RemoteOperation.java
/** * Asynchronous execution of the operation * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}, * and result posting.//from ww w. j a v a2s . c om * * TODO refactor && clean the code; now it's a mess */ @Override public final void run() { RemoteOperationResult result = null; boolean repeat = false; do { try { if (mClient == null) { if (mAccount != null && mContext != null) { if (mCallerActivity != null) { mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext, mCallerActivity); } else { mClient = OwnCloudClientFactory.createOwnCloudClient(mAccount, mContext); } } else { throw new IllegalStateException( "Trying to run a remote operation asynchronously with no client instance or account"); } } } catch (IOException e) { Log.e(TAG, "Error while trying to access to " + mAccount.name, new AccountsException("I/O exception while trying to authorize the account", e)); result = new RemoteOperationResult(e); } catch (AccountsException e) { Log.e(TAG, "Error while trying to access to " + mAccount.name, e); result = new RemoteOperationResult(e); } if (result == null) result = run(mClient); repeat = false; if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() && // (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) { (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) { /// possible fail due to lack of authorization in an operation performed in foreground Credentials cred = mClient.getCredentials(); String ssoSessionCookie = mClient.getSsoSessionCookie(); if (cred != null || ssoSessionCookie != null) { /// confirmed : unauthorized operation AccountManager am = AccountManager.get(mContext); boolean bearerAuthorization = (cred != null && cred instanceof BearerCredentials); boolean samlBasedSsoAuthorization = (cred == null && ssoSessionCookie != null); if (bearerAuthorization) { am.invalidateAuthToken(mAccount.type, ((BearerCredentials) cred).getAccessToken()); } else if (samlBasedSsoAuthorization) { am.invalidateAuthToken(mAccount.type, ssoSessionCookie); } else { am.clearPassword(mAccount); } mClient = null; repeat = true; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity result = null; } } } while (repeat); final RemoteOperationResult resultToSend = result; if (mListenerHandler != null && mListener != null) { mListenerHandler.post(new Runnable() { @Override public void run() { mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend); } }); } }
From source file:com.owncloud.android.operations.RemoteOperation.java
/** * Asynchronous execution of the operation * started by {@link RemoteOperation#execute(WebdavClient, OnRemoteOperationListener, Handler)}, * and result posting.// ww w.j a v a2s.c o m * * TODO refactor && clean the code; now it's a mess */ @Override public final void run() { RemoteOperationResult result = null; boolean repeat = false; do { try { if (mClient == null) { if (mAccount != null && mContext != null) { if (mCallerActivity != null) { mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext, mCallerActivity); } else { mClient = OwnCloudClientUtils.createOwnCloudClient(mAccount, mContext); } } else { throw new IllegalStateException( "Trying to run a remote operation asynchronously with no client instance or account"); } } } catch (IOException e) { Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, new AccountsException("I/O exception while trying to authorize the account", e)); result = new RemoteOperationResult(e); } catch (AccountsException e) { Log_OC.e(TAG, "Error while trying to access to " + mAccount.name, e); result = new RemoteOperationResult(e); } if (result == null) result = run(mClient); repeat = false; if (mCallerActivity != null && mAccount != null && mContext != null && !result.isSuccess() && // (result.getCode() == ResultCode.UNAUTHORIZED || (result.isTemporalRedirection() && result.isIdPRedirection()))) { (result.getCode() == ResultCode.UNAUTHORIZED || result.isIdPRedirection())) { /// possible fail due to lack of authorization in an operation performed in foreground Credentials cred = mClient.getCredentials(); String ssoSessionCookie = mClient.getSsoSessionCookie(); if (cred != null || ssoSessionCookie != null) { /// confirmed : unauthorized operation AccountManager am = AccountManager.get(mContext); boolean bearerAuthorization = (cred != null && cred instanceof BearerCredentials); boolean samlBasedSsoAuthorization = (cred == null && ssoSessionCookie != null); if (bearerAuthorization) { am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, ((BearerCredentials) cred).getAccessToken()); } else if (samlBasedSsoAuthorization) { am.invalidateAuthToken(AccountAuthenticator.ACCOUNT_TYPE, ssoSessionCookie); } else { am.clearPassword(mAccount); } mClient = null; repeat = true; // when repeated, the creation of a new OwnCloudClient after erasing the saved credentials will trigger the login activity result = null; } } } while (repeat); final RemoteOperationResult resultToSend = result; if (mListenerHandler != null && mListener != null) { mListenerHandler.post(new Runnable() { @Override public void run() { mListener.onRemoteOperationFinish(RemoteOperation.this, resultToSend); } }); } }
From source file:de.unclenet.dehabewe.CalendarActivity.java
private void gotAccount(boolean tokenExpired) { SharedPreferences settings = getSharedPreferences(PREF, 0); String accountName = settings.getString("accountName", null); if (accountName != null) { AccountManager manager = AccountManager.get(this); Account[] accounts = manager.getAccountsByType("com.google"); int size = accounts.length; for (int i = 0; i < size; i++) { Account account = accounts[i]; if (accountName.equals(account.name)) { if (tokenExpired) { manager.invalidateAuthToken("com.google", this.authToken); }/* ww w. j av a2s.c o m*/ gotAccount(manager, account); return; } } } showDialog(DIALOG_ACCOUNTS); }
From source file:org.hfoss.posit.android.sync.Communicator.java
/** * Removes an account. This should be called when, e.g., the user changes * to a new server.//ww w . j ava2s.c o m * @param context * @param accountType */ public static void removeAccount(Context context, String accountType) { AccountManager am = AccountManager.get(context); am.invalidateAuthToken(accountType, SyncAdapter.AUTHTOKEN_TYPE); Account[] accounts = am.getAccountsByType(accountType); if (accounts.length != 0) am.removeAccount(accounts[0], null, null); //String authkey = getAuthKey(context); //return authkey == null; }