private FilterDirector authenticate(HttpServletRequest request) {
    final FilterDirector filterDirector = new FilterDirectorImpl();
    filterDirector.setResponseStatus(HttpStatusCode.UNAUTHORIZED);
    filterDirector.setFilterAction(FilterAction.RETURN);

    final String authToken = request.getHeader(CommonHttpHeader.AUTH_TOKEN.toString());
    ExtractorResult<String> account = null;
    AuthToken token = null;

    if (tenanted) {
      account = extractAccountIdentification(request);
    }

    final boolean allow = allowAccount(account);

    if ((!StringUtilities.isBlank(authToken) && allow)) {
      token = checkToken(account, authToken);

      if (token == null) {
        try {
          token = validateToken(account, StringUriUtilities.encodeUri(authToken));
          cacheUserInfo(token);
        } catch (ClientHandlerException ex) {
          LOG.error("Failure communicating with the auth service: " + ex.getMessage(), ex);
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        } catch (AuthServiceException ex) {
          LOG.error("Failure in Auth-N: " + ex.getMessage());
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        } catch (IllegalArgumentException ex) {
          LOG.error("Failure in Auth-N: " + ex.getMessage());
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        } catch (Exception ex) {
          LOG.error("Failure in auth: " + ex.getMessage(), ex);
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        }
      }
    }

    List<AuthGroup> groups = getAuthGroups(token);

    setFilterDirectorValues(
        authToken,
        token,
        delegable,
        filterDirector,
        account == null ? "" : account.getResult(),
        groups);

    return filterDirector;
  }
 private AuthToken checkToken(ExtractorResult<String> account, String authToken) {
   if (tenanted) {
     return checkUserCache(account.getResult(), authToken);
   } else {
     return checkUserCache("", authToken);
   }
 }
  @Override
  public FilterDirector authenticate(HttpServletRequest request) {
    final FilterDirector filterDirector = new FilterDirectorImpl();
    filterDirector.setResponseStatus(HttpStatusCode.UNAUTHORIZED);
    filterDirector.setFilterAction(FilterAction.RETURN);

    final String authToken = request.getHeader(CommonHttpHeader.AUTH_TOKEN.toString());
    final ExtractorResult<Object> account = keyedRegexExtractor.extract(request.getRequestURI());
    CachableUserInfo user = null;

    if ((!StringUtilities.isBlank(authToken) && account != null)) {
      user = checkUserCache(account.getResult(), authToken);

      if (user == null) {
        try {
          user = authenticationService.validateToken(account.getResult(), authToken);
          cacheUserInfo(user);
        } catch (Exception ex) {
          LOG.error("Failure in auth: " + ex.getMessage(), ex);
          filterDirector.setResponseStatus(HttpStatusCode.INTERNAL_SERVER_ERROR);
        }
      }
    }

    Groups groups = null;
    if (user != null) {
      groups = authenticationService.getGroups(user.getUserId());
    }

    final AuthenticationHeaderManager headerManager =
        new AuthenticationHeaderManager(
            authToken,
            user,
            delegatable,
            filterDirector,
            account == null ? "" : account.getResult(),
            groups,
            request);
    headerManager.setFilterDirectorValues();

    return filterDirector;
  }