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); }
@Test public void addAuthToken() { Account account = new Account("name", "type"); shadowOf(am).addAccount(account); am.setAuthToken(account, "token_type_1", "token1"); am.setAuthToken(account, "token_type_2", "token2"); assertThat(am.peekAuthToken(account, "token_type_1")).isEqualTo("token1"); assertThat(am.peekAuthToken(account, "token_type_2")).isEqualTo("token2"); }
@Test public void setAuthToken_shouldNotAddTokenIfAccountNotPresent() { Account account = new Account("name", "type"); am.setAuthToken(account, "token_type_1", "token1"); assertThat(am.peekAuthToken(account, "token_type_1")).isNull(); }
@Test public void invalidateAuthToken_multipleTokenTypesSameToken() { Account account = new Account("name", "type1"); shadowOf(am).addAccount(account); am.setAuthToken(account, "token_type_1", "token1"); am.setAuthToken(account, "token_type_2", "token1"); assertThat(am.peekAuthToken(account, "token_type_1")).isEqualTo("token1"); assertThat(am.peekAuthToken(account, "token_type_2")).isEqualTo("token1"); // invalidate token1 am.invalidateAuthToken("type1", "token1"); assertThat(am.peekAuthToken(account, "token_type_1")).isNull(); assertThat(am.peekAuthToken(account, "token_type_2")).isNull(); }
@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 invalidateAuthToken_multipleTokens() { Account account = new Account("name", "type1"); shadowOf(am).addAccount(account); am.setAuthToken(account, "token_type_1", "token1"); am.setAuthToken(account, "token_type_2", "token2"); assertThat(am.peekAuthToken(account, "token_type_1")).isEqualTo("token1"); assertThat(am.peekAuthToken(account, "token_type_2")).isEqualTo("token2"); // invalidate token1 am.invalidateAuthToken("type1", "token1"); assertThat(am.peekAuthToken(account, "token_type_1")).isNull(); assertThat(am.peekAuthToken(account, "token_type_2")).isEqualTo("token2"); // invalidate token2 am.invalidateAuthToken("type1", "token2"); assertThat(am.peekAuthToken(account, "token_type_1")).isNull(); assertThat(am.peekAuthToken(account, "token_type_2")).isNull(); }
@Test public void getAuthToken_withNotifyAuthFailureSetToFalse_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(), /* notifyAuthFailure= */ false, 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 invalidateAuthToken_multipleAccounts() { Account account1 = new Account("name", "type1"); shadowOf(am).addAccount(account1); Account account2 = new Account("name", "type2"); shadowOf(am).addAccount(account2); am.setAuthToken(account1, "token_type_1", "token1"); am.setAuthToken(account2, "token_type_1", "token1"); assertThat(am.peekAuthToken(account1, "token_type_1")).isEqualTo("token1"); assertThat(am.peekAuthToken(account2, "token_type_1")).isEqualTo("token1"); // invalidate token for type1 account am.invalidateAuthToken("type1", "token1"); assertThat(am.peekAuthToken(account1, "token_type_1")).isNull(); assertThat(am.peekAuthToken(account2, "token_type_1")).isEqualTo("token1"); // invalidate token for type2 account am.invalidateAuthToken("type2", "token1"); assertThat(am.peekAuthToken(account1, "token_type_1")).isNull(); assertThat(am.peekAuthToken(account2, "token_type_1")).isNull(); }
@Test public void testBlockingGetAuthToken() throws AuthenticatorException, OperationCanceledException, IOException { Account account = new Account("name", "type"); shadowOf(am).addAccount(account); am.setAuthToken(account, "token_type_1", "token1"); am.setAuthToken(account, "token_type_2", "token2"); assertThat(am.blockingGetAuthToken(account, "token_type_1", false)).isEqualTo("token1"); assertThat(am.blockingGetAuthToken(account, "token_type_2", false)).isEqualTo("token2"); try { am.blockingGetAuthToken(null, "token_type_1", false); fail("blockingGetAuthToken() should throw an illegal argument exception if the account is null"); } catch (IllegalArgumentException iae) { // Expected } try { am.blockingGetAuthToken(account, null, false); fail("blockingGetAuthToken() should throw an illegal argument exception if the auth token type is null"); } catch (IllegalArgumentException iae) { // Expected } Account account1 = new Account("unknown", "type"); assertThat(am.blockingGetAuthToken(account1, "token_type_1", false)).isNull(); }
private void finishAuthentication(String authenticationToken) { AccountManager accountManager = AccountManager.get(this); Bundle authenticationBundle = new Bundle(); Account account = new AmahiAccount(getUsername()); if (accountManager.addAccountExplicitly(account, getPassword(), null)) { authenticationBundle.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); authenticationBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); authenticationBundle.putString(AccountManager.KEY_AUTHTOKEN, authenticationToken); accountManager.setAuthToken(account, account.type, authenticationToken); } setAccountAuthenticatorResult(authenticationBundle); setResult(Activity.RESULT_OK); finish(); }
@Override public void tokenCreated(String token) { TDUtils.event(R.string.td_event_login_success); String accountName = username.getText().toString(); String accountPassword = password.getText().toString(); PreferenceUtils.putString(this, PreferenceUtils.Key.ACCOUNT, accountName); final Account account = new Account(accountName, accountType); if (getIntent().getBooleanExtra(Authenticator.ARG_IS_ADDING_NEW_ACCOUNT, true)) { mAccountManager.addAccountExplicitly(account, accountPassword, null); mAccountManager.setAuthToken(account, mAuthTokenType, token); } else { mAccountManager.setPassword(account, accountPassword); } //Save token created from loginActivity ((GithubApplication)this.getApplication()).setToken(token); Bundle bundle = new Bundle(); bundle.putString(AccountManager.KEY_ACCOUNT_NAME, accountName); bundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); bundle.putString(AccountManager.KEY_AUTHTOKEN, token); bundle.putString(Authenticator.PARAM_USER_PASS, accountPassword); setAccountAuthenticatorResult(bundle); setResult(RESULT_OK, new Intent().putExtras(bundle)); finish(); }
accountManager.setAuthToken(account, authTokenType, authToken);
public void onLogin(String username, String refreshToken, long refreshTokenExpiresIn, String accessToken, long accessTokenExpiresIn) { Log.d(TAG, "Hatchet user '" + username + "' logged in successfully :)"); if (username != null && !TextUtils.isEmpty(username) && refreshToken != null && !TextUtils.isEmpty(refreshToken)) { Log.d(TAG, "Hatchet auth token is served and yummy"); Account account = new Account(username, ACCOUNT_TYPE); AccountManager am = AccountManager.get(TomahawkApp.getContext()); if (am != null) { am.addAccountExplicitly(account, null, new Bundle()); am.setAuthToken(account, AUTH_TOKEN_HATCHET, refreshToken); am.setUserData(account, AUTH_TOKEN_EXPIRES_IN_HATCHET, String.valueOf(refreshTokenExpiresIn)); am.setUserData(account, MANDELLA_ACCESS_TOKEN_HATCHET, accessToken); am.setUserData(account, MANDELLA_ACCESS_TOKEN_EXPIRATIONTIME_HATCHET, String.valueOf(accessTokenExpiresIn)); ensureAccessTokens(); } } AuthenticatorManager.ConfigTestResultEvent event = new AuthenticatorManager.ConfigTestResultEvent(); event.mComponent = this; event.mType = AuthenticatorManager.CONFIG_TEST_RESULT_TYPE_SUCCESS; EventBus.getDefault().post(event); AuthenticatorManager.showToast(getPrettyName(), event); }
accountManager.peekAuthToken(account.getAccount(), property.getPackageKey())); accountManager.setAuthToken(account.getAccount(), property.getPackageKey(), PropertyUtils.convertToString(object)); break; case PEEK_AUTH_TOKEN: