protected boolean authenticate(HttpServletRequest request) {
    if (provider.isAuthenticated(request.getSession())) return true;

    String user = null, pass = null;
    String authorization = request.getHeader("Authorization");
    if (authorization != null) {
      String userpass = Base64.base64Decode(authorization.substring(6));
      user = userpass.substring(0, userpass.indexOf(":"));
      pass = userpass.substring(userpass.indexOf(":") + 1);
    }

    if (provider.authenticate(request.getSession(), user, pass)) {
      log.info("Web API authenticated " + request.getSession() + " for user " + user);
      if (user != null) {
        request.getSession().setAttribute(AUTHENTICATED_USER_SESSION_ATTRIBUTE, user);
      }
      return true;
    }

    return false;
  }