/**
   * Validates the request by checking for the presence of a pre-configured attribute in the
   * ServletRequest.
   *
   * @param messageInfo {@inheritDoc}
   * @param clientSubject {@inheritDoc}
   * @param serviceSubject {@inheritDoc}
   * @return {@inheritDoc}
   */
  @Override
  public Promise<AuthStatus, AuthenticationException> validateRequest(
      MessageInfoContext messageInfo, Subject clientSubject, Subject serviceSubject) {

    SecurityContextMapper securityContextMapper =
        SecurityContextMapper.fromMessageInfo(messageInfo);
    final JsonValue attributes =
        json(messageInfo.asContext(AttributesContext.class).getAttributes());

    if (attributes.isDefined(authenticationIdAttribute)
        && attributes.get(authenticationIdAttribute).isString()) {
      final String authenticationId = attributes.get(authenticationIdAttribute).asString();
      securityContextMapper.setAuthenticationId(authenticationId);
      clientSubject
          .getPrincipals()
          .add(
              new Principal() {
                public String getName() {
                  return authenticationId;
                }
              });
      return newResultPromise(SUCCESS);
    } else {
      return newResultPromise(SEND_FAILURE);
    }
  }
  /**
   * Validates the client's request by passing through the request to be authenticated against a
   * OpenICF Connector.
   *
   * @param messageInfo {@inheritDoc}
   * @param clientSubject {@inheritDoc}
   * @param serviceSubject {@inheritDoc}
   * @return {@inheritDoc}
   * @throws AuthException If there is a problem performing the authentication.
   */
  @Override
  public AuthStatus validateRequest(
      MessageInfo messageInfo, Subject clientSubject, Subject serviceSubject) throws AuthException {

    logger.debug("DelegatedAuthModule: validateRequest START");

    SecurityContextMapper securityContextMapper =
        SecurityContextMapper.fromMessageInfo(messageInfo);
    HttpServletRequest request = (HttpServletRequest) messageInfo.getRequestMessage();

    try {
      logger.debug("DelegatedAuthModule: Delegating call to remote authentication");

      if (authenticate(HEADER_AUTH_CRED_HELPER.getCredential(request), securityContextMapper)
          || authenticate(BASIC_AUTH_CRED_HELPER.getCredential(request), securityContextMapper)) {

        logger.debug("DelegatedAuthModule: Authentication successful");

        final String authcid = securityContextMapper.getAuthenticationId();
        clientSubject
            .getPrincipals()
            .add(
                new Principal() {
                  public String getName() {
                    return authcid;
                  }
                });

        // Auth success will be logged in IDMJaspiModuleWrapper
        return AuthStatus.SUCCESS;
      } else {
        logger.debug("DelegatedAuthModule: Authentication failed");
        return AuthStatus.SEND_FAILURE;
      }
    } finally {
      logger.debug("DelegatedAuthModule: validateRequest END");
    }
  }