package com.airlock.iam.common.application.configuration;

import com.airlock.iam.common.api.application.configuration.credential.RequestAuthenticationConfig;
import com.airlock.iam.core.misc.authen.UsernameTransformer;
import com.airlock.iam.plugin.api.config.ConfigurationContext;
import com.airlock.iam.plugin.api.config.ConfigurationEnvironment;
import com.airlock.iam.plugin.api.config.PluginConfiguration;
import com.airlock.iam.plugin.api.exception.ConfigurationException;
import com.airlock.iam.plugin.api.exception.PropertyConstraintViolationException;
import com.airlock.iam.plugin.api.valuereader.BooleanPropertyValueReader;
import com.airlock.iam.plugin.api.valuereader.StringPropertyValueReader;
import java.util.List;
import java.util.Optional;
import org.apache.commons.lang3.StringUtils;

public interface RestConfigSpec {
   String ENABLE_LINK_REWRITING = "Enables rewriting of links in REST responses. If rewriting is disabled or cannot be done correctly due to missing information, the internal URI is written to the response. <p>If a 'Base URI' is configured, links are rewritten according to the configured value. Otherwise, links are rewritten according to the external view provided by the WAF (if configured an a WAF environment cookie is present).</p>";
   String BASE_URI_PLUGIN_DESCRIPTION = "Allows to change the base URI for all links in REST responses.<p>This property is useful in test environments where you want links contained in REST responses to be relative to the configured base URI. Note that configuring this property will take precedence over link rewriting based on the WAF environment cookie.</p>";
   String BASE_URI_NAME = "baseUri";
   String LINK_REWRITING_NAME = "linkResponseRewritingEnabled";
   String CSRF_PROTECTION_GENERAL_PROPERTY_DESRIPTION = "<p>If enabled, REST endpoints are protected against CSRF attacks.</p><p>With this protection, the REST API only accepts requests that contain the custom header <tt>X-Same-Domain</tt> with an arbitrary non-empty value. In cross-origin resource sharing (CORS), such requests are not considered <em>simple requests</em> and thus must always be preceded by a preflight request, which prevents cross-site request forgery (CSRF) attacks.</p><p><b>Security warning:</b> Disabling this feature may allow CSRF attacks. Only do so if the REST client is unable to comply with the aforementioned restrictions.</p>";
   String MAX_PAGE_SIZE_PROPERTY = "maxPageSize";

   Optional<RequestAuthenticationConfig> getRequestAuthenticationConfig();

   List<UsernameTransformer> getUsernameTransformers();

   Optional<String> getBaseUri();

   boolean isLinkResponseRewritingEnabled();

   static void validateBaseUriAndLinkRewriting(PluginConfiguration config, ConfigurationEnvironment env, ConfigurationContext context) throws ConfigurationException {
      Boolean linkRewritingEnabled = BooleanPropertyValueReader.effectiveBooleanPropertyValue(config, "linkResponseRewritingEnabled", env, context);
      String baseUri = StringPropertyValueReader.effectiveStringConfigValue(config, "baseUri", env, context);
      if (!linkRewritingEnabled && !StringUtils.isBlank(baseUri)) {
         throw new PropertyConstraintViolationException("baseUri", config, env, context, "If link rewriting is disabled, no base URI must be configured.");
      }
   }
}
