Refine search
/** * Get configured account * * @param context * @return account or null if none */ public static Account getAccount(final Context context) { final Account[] accounts = AccountManager.get(context) .getAccountsByType(ACCOUNT_TYPE); return accounts.length > 0 ? accounts[0] : null; }
@Override public void invalidateAuthToken(String authToken) { mAccountManager.invalidateAuthToken(mAccount.type, authToken); } }
AccountManager am = AccountManager.get(activity); am.getAuthToken(am.getAccounts())[0], "oauth2:" + DriveScopes.DRIVE, new Bundle(), true, new OnTokenAcquired(), null);
public static String getUserToken(Context context, Account account) { AccountManager manager = AccountManager.get(context); return manager.getUserData(account, AccountManager.KEY_AUTHTOKEN); } }
@Test public void getAuthToken_withActivity_returnsAuthIntent() throws Exception { Account account = new Account("name", "google.com"); shadowOf(am).addAccount(account); shadowOf(am).addAuthenticator("google.com"); TestAccountManagerCallback<Bundle> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Bundle> future = am.getAuthToken(account, "auth_token_type", new Bundle(), activity, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_NAME)) .isEqualTo(account.name); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_TYPE)) .isEqualTo(account.type); assertThat(future.getResult().getString(AccountManager.KEY_AUTHTOKEN)).isNull(); assertThat((Intent) future.getResult().getParcelable(AccountManager.KEY_INTENT)).isNotNull(); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void getAuthToken_withActivity_returnsCorrectToken() throws Exception { Account account = new Account("name", "google.com"); shadowOf(am).addAccount(account); shadowOf(am).addAuthenticator("google.com"); am.setAuthToken(account, "auth_token_type", "token1"); TestAccountManagerCallback<Bundle> callback = new TestAccountManagerCallback<>(); AccountManagerFuture<Bundle> future = am.getAuthToken(account, "auth_token_type", new Bundle(), activity, callback, new Handler()); assertThat(future.isDone()).isTrue(); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_NAME)).isEqualTo(account.name); assertThat(future.getResult().getString(AccountManager.KEY_ACCOUNT_TYPE)).isEqualTo(account.type); assertThat(future.getResult().getString(AccountManager.KEY_AUTHTOKEN)).isEqualTo("token1"); assertThat(callback.hasBeenCalled()).isTrue(); }
@Test public void goodToken() throws Exception { Bundle bundle = new Bundle(); bundle.putString(AccountManager.KEY_AUTHTOKEN, "monkey"); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); Assert.assertEquals("monkey", mAuthenticator.getAuthToken()); }
@SuppressWarnings("deprecation") @Override public String getAuthToken() throws AuthFailureError { AccountManagerFuture<Bundle> future = mAccountManager.getAuthToken(mAccount, mAuthTokenType, mNotifyAuthFailure, null, null); Bundle result; try { result = future.getResult(); } catch (Exception e) { throw new AuthFailureError("Error while retrieving auth token", e); } String authToken = null; if (future.isDone() && !future.isCancelled()) { if (result.containsKey(AccountManager.KEY_INTENT)) { Intent intent = result.getParcelable(AccountManager.KEY_INTENT); throw new AuthFailureError(intent); } authToken = result.getString(AccountManager.KEY_AUTHTOKEN); } if (authToken == null) { throw new AuthFailureError("Got null auth token for type: " + mAuthTokenType); } return authToken; }
private void addAccount(String username, String password) { Account account = new Account(username, BuildConfig.APPLICATION_ID); mAccountManager.addAccountExplicitly(account, password, null); mAccountManager.setPassword(account, password); // for re-login with updated password Bundle bundle = new Bundle(); bundle.putString(AccountManager.KEY_ACCOUNT_NAME, username); bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, BuildConfig.APPLICATION_ID); setAccountAuthenticatorResult(bundle); Preferences.setUsername(this, username); finish(); }
@Test(expected = AuthFailureError.class) public void resultContainsIntent() throws Exception { Intent intent = new Intent(); Bundle bundle = new Bundle(); bundle.putParcelable(AccountManager.KEY_INTENT, intent); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); mAuthenticator.getAuthToken(); }
@Test public void testAddAccountExplicitly_withExtras() { Account account = new Account("name", "type"); Bundle extras = new Bundle(); extras.putString("key123", "value123"); boolean accountAdded = am.addAccountExplicitly(account, null, extras); assertThat(accountAdded).isTrue(); assertThat(am.getUserData(account, "key123")).isEqualTo("value123"); assertThat(am.getUserData(account, "key456")).isNull(); }
private void endAuth(final String accessToken, final String scope) { progressDialog.setContent(getString(R.string.loading_user)); TokenStore.getInstance(this).saveToken(accessToken); userService .getUser() .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .as(AutoDisposeUtils.bindToLifecycle(this)) .subscribe(response -> { User user = response.body(); Account account = new Account(user.login(), getString(R.string.account_type)); Bundle userData = AccountsHelper.buildBundle(user.name(), user.email(), user.avatarUrl(), scope); userData.putString(AccountManager.KEY_AUTHTOKEN, accessToken); accountManager.addAccountExplicitly(account, null, userData); accountManager.setAuthToken(account, getString(R.string.account_type), accessToken); Bundle result = new Bundle(); result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); result.putString(AccountManager.KEY_AUTHTOKEN, accessToken); setAccountAuthenticatorResult(result); finish(); }, Throwable::printStackTrace); }
AccountManager am = AccountManager.get(context); Account account = null; am.getAccountsByType(mytype); for(Account a : accounts) { if (am.getUserData(account, key)) { account = a; break; } } Bundle extras = new Bundle(); extras.putString(EXTRA_mystuff, myvalue); ContentResolver.requestSync(account, authority, extras)
@Test(expected = AuthFailureError.class) public void missingAuthToken() throws Exception { Bundle bundle = new Bundle(); when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenReturn(bundle); when(mFuture.isDone()).thenReturn(true); when(mFuture.isCancelled()).thenReturn(false); mAuthenticator.getAuthToken(); }
private AccountManager getAccountManager() { return AccountManager.get(this); } }
@SuppressLint("MissingPermission") public static Pair<String, String> getCredentials(Context context) { String username = Preferences.getUsername(context); if (TextUtils.isEmpty(username)) { return null; } AccountManager accountManager = AccountManager.get(context); Account[] accounts = accountManager.getAccountsByType(BuildConfig.APPLICATION_ID); for (Account account : accounts) { if (TextUtils.equals(username, account.name)) { return Pair.create(username, accountManager.getPassword(account)); } } return null; }
@Config(sdk = 21) @Test public void testRemoveAccount() { AccountManager.get(activity).addAccountExplicitly(new Account("existing", BuildConfig.APPLICATION_ID), "password", null); Preferences.setUsername(activity, "existing"); drawerAccount.performClick(); AlertDialog alertDialog = ShadowAlertDialog.getLatestAlertDialog(); assertNotNull(alertDialog); assertThat(alertDialog.getListView().getAdapter()).hasCount(1); alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL).performClick(); assertThat(alertDialog).isNotShowing(); assertThat(AccountManager.get(activity).getAccounts()).isEmpty(); }
@Test public void shouldInvalidateTokenOnRefresh() throws Exception { when(authPreferences.getOauth2Token()).thenReturn("token"); when(authPreferences.getOauth2Username()).thenReturn("username"); when(accountManager.getAuthToken(notNull(Account.class), anyString(), isNull(Bundle.class), anyBoolean(), any(AccountManagerCallback.class), any(Handler.class))).thenReturn(mock(AccountManagerFuture.class)); try { refresher.refreshOAuth2Token(); fail("expected error "); } catch (TokenRefreshException e) { assertThat(e.getMessage()).isEqualTo("no bundle received from accountmanager"); } verify(accountManager).invalidateAuthToken(GOOGLE_TYPE, "token"); }
private void logout() { // Remove cookies so that the login is clean if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { CookieManager.getInstance().removeAllCookies(null); } else { CookieManager.getInstance().removeAllCookie(); } // Clear all of the cached data CacheHelper helper = new CacheHelper(this); helper.getWritableDatabase().delete("orgs", null, null); helper.getWritableDatabase().delete("users", null, null); helper.getWritableDatabase().delete("repos", null, null); // Remove the account AccountManager accountManager = getAccountManager(); String accountType = getString(R.string.account_type); Account[] allGitHubAccounts = accountManager.getAccountsByType(accountType); for (Account account : allGitHubAccounts) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) { accountManager.removeAccount(account, this, bool -> startLoginActivity(), null); } else { accountManager.removeAccount(account, bundle -> startLoginActivity(), null); } } }
@Test public void testGetSetUserData_remove() { Account account = new Account("name", "type"); boolean accountAdded = am.addAccountExplicitly(account, null, null); assertThat(accountAdded).isTrue(); am.setUserData(account, "key123", "value123"); assertThat(am.getUserData(account, "key123")).isEqualTo("value123"); am.setUserData(account, "key123", null); assertThat(am.getUserData(account, "key123")).isNull(); }