package com.airlock.iam.authentication.application.configuration.idpropagation;

import com.airlock.iam.authentication.api.application.configuration.idpropagation.ResponseAdderConfig;
import com.airlock.iam.plugin.api.annotation.config.Plugin;
import com.airlock.iam.plugin.api.annotation.property.BooleanProperty;
import com.airlock.iam.plugin.api.annotation.property.StringProperty;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

@Plugin(description = "Adds a ticket string as a response cookie.")
public final class CookieTicketAdderConfig implements ResponseAdderConfig {
   @StringProperty(
      description = "<p>The name of the cookie used to transport the ticket.</p><p>Only one cookie per cookie path and name can exist, therefore the name of this cookie must be distinct from all other cookie names used by this applications (such as \"JSESSIONID\").</p>",
      exampleValues = "AUTH_TICKET",
      optional = false
   )
   private String cookieName;
   @StringProperty(
      description = "<p>The path for which the cookie is set. This determines with which future requests the cookie will be sent to the server.</p><p>To add the cookie to all requests to a given domain, the value \"/\" can be used. If the cookie should be limited to a certain backend, the corresponding context path should be used.</p><p>Only one cookie per cookie path and name can exist, therefore the name of this cookie must be distinct from all other cookie for the same path (such as \"JSESSIONID\").</p><p>When using an Airlock Gateway (WAF), the Gateway configuration flag <em>Interpret Cookie Domains</em> must be set. Otherwise the cookie path is ignored and cookies in the cookie store are sent with back-end HTTP requests of the same session.</p>",
      exampleValues = {"/", "/appl1", "/appl2"},
      optional = true,
      defaultValue = "/"
   )
   private String cookiePath;
   @StringProperty(
      description = "The domain for which the cookie is set. This determines with which future requests the cookie will be sent to the server.<p>Because of security restrictions in browsers (same origin policy) it is usually not possible to set a cookie for a different domain (except subdomains).</p><p>Airlock Gateway (WAF) handle cookies differently and allow setting cookies for other domains within the protected infrastructure while not exposing them to the internet. The Gateway configuration flag <em>Interpret Cookie Domains</em> needs to be enabled for this feature. If this flag is enabled, also the following special domain names are supported: <ul> <li>An empty value results in the cookie only being sent to the origin server that set the cookie.</li> <li>The value <code>.*</code> results in cookies being sent to all back-end servers.</li> <li>Setting a different hostname results in the cookie being sent to the back-end host with that hostname.</li></ul>Consult the Airlock Gataway documentation for more information on cookie handling.</p>",
      exampleValues = {".*", "@www.test.com", "ergon.ch"},
      optional = true
   )
   private String cookieDomain;
   @BooleanProperty(
      description = "If enabled, the \"secure\"-flag of the cookie is set. <p>If the cookie is marked as secure, the browser (and any HTTP proxy behaving like a browser) should send the cookie only over secure connections.<br/>",
      optional = true,
      defaultValue = "true"
   )
   private boolean secureFlag;

   public String getCookieName() {
      return this.cookieName;
   }

   public String getCookiePath() {
      return this.cookiePath;
   }

   public String getCookieDomain() {
      return this.cookieDomain;
   }

   public boolean isSecureFlag() {
      return this.secureFlag;
   }

   @SuppressFBWarnings(justification = "generated code")
   public static CookieTicketAdderConfig.CookieTicketAdderConfigBuilder builder() {
      return new CookieTicketAdderConfig.CookieTicketAdderConfigBuilder();
   }

   @SuppressFBWarnings(justification = "generated code")
   public CookieTicketAdderConfig(String cookieName, String cookiePath, String cookieDomain, boolean secureFlag) {
      this.cookieName = cookieName;
      this.cookiePath = cookiePath;
      this.cookieDomain = cookieDomain;
      this.secureFlag = secureFlag;
   }

   @SuppressFBWarnings(justification = "generated code")
   public CookieTicketAdderConfig() {
   }

   @SuppressFBWarnings(justification = "generated code")
   public static class CookieTicketAdderConfigBuilder {
      @SuppressFBWarnings(justification = "generated code")
      private String cookieName;
      @SuppressFBWarnings(justification = "generated code")
      private String cookiePath;
      @SuppressFBWarnings(justification = "generated code")
      private String cookieDomain;
      @SuppressFBWarnings(justification = "generated code")
      private boolean secureFlag;

      @SuppressFBWarnings(justification = "generated code")
      CookieTicketAdderConfigBuilder() {
      }

      @SuppressFBWarnings(justification = "generated code")
      public CookieTicketAdderConfig.CookieTicketAdderConfigBuilder cookieName(String cookieName) {
         this.cookieName = cookieName;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CookieTicketAdderConfig.CookieTicketAdderConfigBuilder cookiePath(String cookiePath) {
         this.cookiePath = cookiePath;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CookieTicketAdderConfig.CookieTicketAdderConfigBuilder cookieDomain(String cookieDomain) {
         this.cookieDomain = cookieDomain;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CookieTicketAdderConfig.CookieTicketAdderConfigBuilder secureFlag(boolean secureFlag) {
         this.secureFlag = secureFlag;
         return this;
      }

      @SuppressFBWarnings(justification = "generated code")
      public CookieTicketAdderConfig build() {
         return new CookieTicketAdderConfig(this.cookieName, this.cookiePath, this.cookieDomain, this.secureFlag);
      }

      @SuppressFBWarnings(justification = "generated code")
      @Override
      public String toString() {
         return "CookieTicketAdderConfig.CookieTicketAdderConfigBuilder(cookieName="
            + this.cookieName
            + ", cookiePath="
            + this.cookiePath
            + ", cookieDomain="
            + this.cookieDomain
            + ", secureFlag="
            + this.secureFlag
            + ")";
      }
   }
}
