private QueryFilter<CoreTokenField> getQueryFilter(String userId, String realm) {
   return and(
       equalTo(USER_NAME.getField(), userId),
       equalTo(REALM.getField(), realm),
       or(
           equalTo(TOKEN_NAME.getField(), OAUTH_ACCESS_TOKEN),
           equalTo(TOKEN_NAME.getField(), OAUTH_REFRESH_TOKEN)));
 }
  private String calculateExpiryDateTime(
      Iterable<JsonValue> tokens, OAuth2ProviderSettings oAuth2ProviderSettings)
      throws ServerException {
    long maxExpiryMilliseconds = 0L;

    for (JsonValue token : tokens) {
      long tokenExpiryMilliseconds =
          Long.parseLong(getAttributeValue(token, EXPIRY_TIME.getOAuthField()));

      if (tokenExpiryMilliseconds == -1) {
        return null;
      }

      if (OAUTH_REFRESH_TOKEN.equals(getAttributeValue(token, TOKEN_NAME.getOAuthField()))) {
        if (oAuth2ProviderSettings.issueRefreshTokensOnRefreshingToken()) {
          return null;
        }
        tokenExpiryMilliseconds += oAuth2ProviderSettings.getAccessTokenLifetime() * 1000;
      }

      if (tokenExpiryMilliseconds > maxExpiryMilliseconds) {
        maxExpiryMilliseconds = tokenExpiryMilliseconds;
      }
    }

    return ISODateTimeFormat.dateTime().print(maxExpiryMilliseconds);
  }