package com.l7tech.server.policy.assertion.credential;

import com.l7tech.common.http.GenericHttpClient;
import com.l7tech.common.http.GenericHttpRequestParams;
import com.l7tech.common.http.HttpCookie;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.Messages.M;
import com.l7tech.message.SecurityKnob;
import com.l7tech.message.XmlKnob;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.security.wsfederation.FederationPassiveClient;
import com.l7tech.security.wsfederation.ResponseStatusException;
import com.l7tech.security.xml.SecurityTokenResolver;
import com.l7tech.security.xml.processor.WssProcessor;
import com.l7tech.security.xml.processor.WssProcessorImpl;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.xml.saml.SamlAssertion;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Document;

public abstract class AbstractServerWsFederationPassiveRequestProfile<AT extends Assertion> extends AbstractServerCachedSecurityTokenAssertion<AT> {
   private final WssProcessor g;
   private final SecurityTokenResolver h;
   private final Set<String> i = new CopyOnWriteArraySet<>();
   private static final String[] n;

   protected AbstractServerWsFederationPassiveRequestProfile(AT var1, String var2, ApplicationContext var3) {
      super((AT)var1, var2);
      this.g = new WssProcessorImpl();
      this.h = (SecurityTokenResolver)var3.getBean(n[1]);
   }

   protected void ensureXmlRequest(XmlKnob var1) throws AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
      try {
         if (var1 == null) {
            throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_NON_XML_MESSAGE);
         }
      } catch (AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException var2) {
         throw a(var2);
      }
   }

   protected void ensureSecurityKnob(SecurityKnob var1) throws AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
      try {
         if (var1 == null) {
            throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_NO_SUITABLE_CREDENTIALS);
         }
      } catch (AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException var2) {
         throw a(var2);
      }
   }

   protected Throwable causeOrSelf(Throwable var1) {
      Throwable var2 = var1.getCause();
      if (var2 == null) {
         var2 = var1;
      }

      return var2;
   }

   protected void updateRequestXml(
      PolicyEnforcementContext param1, XmlKnob param2, SecurityKnob param3, Document param4, SamlAssertion param5, Assertion param6
   ) throws Exception {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: new com/l7tech/security/xml/decorator/WssDecoratorImpl
      // 03: dup
      // 04: invokespecial com/l7tech/security/xml/decorator/WssDecoratorImpl.<init> ()V
      // 07: astore 7
      // 09: aload 1
      // 0a: aload 1
      // 0b: invokeinterface com/l7tech/server/message/PolicyEnforcementContext.getRequest ()Lcom/l7tech/message/Message; 1
      // 10: invokeinterface com/l7tech/server/message/PolicyEnforcementContext.getAuthenticationContext (Lcom/l7tech/message/Message;)Lcom/l7tech/server/message/AuthenticationContext; 2
      // 15: aload 5
      // 17: aload 6
      // 19: invokevirtual java/lang/Object.getClass ()Ljava/lang/Class;
      // 1c: invokestatic com/l7tech/policy/assertion/credential/LoginCredentials.makeLoginCredentials (Lcom/l7tech/security/token/SecurityToken;Ljava/lang/Class;)Lcom/l7tech/policy/assertion/credential/LoginCredentials;
      // 1f: invokevirtual com/l7tech/server/message/AuthenticationContext.addCredentials (Lcom/l7tech/policy/assertion/credential/LoginCredentials;)V
      // 22: aload 3
      // 23: invokeinterface com/l7tech/message/SecurityKnob.getProcessorResult ()Lcom/l7tech/security/xml/processor/ProcessorResult; 1
      // 28: astore 8
      // 2a: new com/l7tech/security/xml/decorator/DecorationRequirements
      // 2d: dup
      // 2e: invokespecial com/l7tech/security/xml/decorator/DecorationRequirements.<init> ()V
      // 31: astore 9
      // 33: aload 9
      // 35: bipush 1
      // 36: invokevirtual com/l7tech/security/xml/decorator/DecorationRequirements.setSecurityHeaderReusable (Z)V
      // 39: aload 8
      // 3b: ifnull 5c
      // 3e: aload 8
      // 40: invokeinterface com/l7tech/security/xml/processor/ProcessorResult.getTimestamp ()Lcom/l7tech/security/xml/processor/WssTimestamp; 1
      // 45: ifnull 5c
      // 48: goto 4f
      // 4b: invokestatic com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 4e: athrow
      // 4f: aload 9
      // 51: bipush 0
      // 52: invokevirtual com/l7tech/security/xml/decorator/DecorationRequirements.setIncludeTimestamp (Z)V
      // 55: goto 5c
      // 58: invokestatic com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 5b: athrow
      // 5c: aload 9
      // 5e: aload 5
      // 60: bipush 0
      // 61: invokevirtual com/l7tech/security/xml/decorator/DecorationRequirements.setSenderSamlToken (Lcom/l7tech/xml/saml/SamlAssertion;Z)V
      // 64: aload 7
      // 66: new com/l7tech/message/Message
      // 69: dup
      // 6a: aload 4
      // 6c: invokespecial com/l7tech/message/Message.<init> (Lorg/w3c/dom/Document;)V
      // 6f: aload 9
      // 71: invokeinterface com/l7tech/security/xml/decorator/WssDecorator.decorateMessage (Lcom/l7tech/message/Message;Lcom/l7tech/security/xml/decorator/DecorationRequirements;)Lcom/l7tech/security/xml/decorator/WssDecorator$DecorationResult; 3
      // 76: pop
      // 77: aload 2
      // 78: aload 4
      // 7a: invokeinterface com/l7tech/message/XmlKnob.setDocument (Lorg/w3c/dom/Document;)V 2
      // 7f: aload 3
      // 80: aload 0
      // 81: getfield com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.g Lcom/l7tech/security/xml/processor/WssProcessor;
      // 84: aload 1
      // 85: invokeinterface com/l7tech/server/message/PolicyEnforcementContext.getRequest ()Lcom/l7tech/message/Message; 1
      // 8a: aconst_null
      // 8b: aload 0
      // 8c: getfield com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.h Lcom/l7tech/security/xml/SecurityTokenResolver;
      // 8f: invokeinterface com/l7tech/security/xml/processor/WssProcessor.undecorateMessage (Lcom/l7tech/message/Message;Lcom/l7tech/security/xml/processor/SecurityContextFinder;Lcom/l7tech/security/xml/SecurityTokenResolver;)Lcom/l7tech/security/xml/processor/ProcessorResult; 4
      // 94: invokeinterface com/l7tech/message/SecurityKnob.setProcessorResult (Lcom/l7tech/security/xml/processor/ProcessorResult;)V 2
      // 99: return
      // try (20 -> 28): 29 java/lang/Exception
      // try (25 -> 34): 35 java/lang/Exception
   }

   protected void doAuth(PolicyEnforcementContext param1, GenericHttpClient param2, SamlAssertion param3, String param4, String param5, boolean param6) throws AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException, AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
      // $VF: Couldn't be decompiled
      // Please report this to the Vineflower issue tracker, at https://github.com/Vineflower/vineflower/issues with a copy of the class file (if you have the rights to distribute it!)
      // java.lang.RuntimeException: parsing failure!
      //   at org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper.parseGraph(DomHelper.java:211)
      //   at org.jetbrains.java.decompiler.main.rels.MethodProcessor.codeToJava(MethodProcessor.java:174)
      //
      // Bytecode:
      // 00: iload 6
      // 02: ifeq 4c
      // 05: aload 0
      // 06: aload 1
      // 07: invokevirtual com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.a (Lcom/l7tech/server/message/PolicyEnforcementContext;)Z
      // 0a: ifeq 4c
      // 0d: goto 14
      // 10: invokestatic com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 13: athrow
      // 14: aload 0
      // 15: getfield com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.logger Ljava/util/logging/Logger;
      // 18: getstatic java/util/logging/Level.FINER Ljava/util/logging/Level;
      // 1b: invokevirtual java/util/logging/Logger.isLoggable (Ljava/util/logging/Level;)Z
      // 1e: ifeq 3e
      // 21: goto 28
      // 24: invokestatic com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 27: athrow
      // 28: aload 0
      // 29: getfield com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.logger Ljava/util/logging/Logger;
      // 2c: getstatic java/util/logging/Level.FINER Ljava/util/logging/Level;
      // 2f: getstatic com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.n [Ljava/lang/String;
      // 32: bipush 2
      // 33: aaload
      // 34: invokevirtual java/util/logging/Logger.log (Ljava/util/logging/Level;Ljava/lang/String;)V
      // 37: goto 3e
      // 3a: invokestatic com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 3d: athrow
      // 3e: aload 0
      // 3f: aload 1
      // 40: aload 2
      // 41: aload 3
      // 42: aload 4
      // 44: aload 5
      // 46: invokevirtual com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.b (Lcom/l7tech/server/message/PolicyEnforcementContext;Lcom/l7tech/common/http/GenericHttpClient;Lcom/l7tech/xml/saml/SamlAssertion;Ljava/lang/String;Ljava/lang/String;)V
      // 49: goto 7a
      // 4c: aload 0
      // 4d: getfield com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.logger Ljava/util/logging/Logger;
      // 50: getstatic java/util/logging/Level.FINER Ljava/util/logging/Level;
      // 53: invokevirtual java/util/logging/Logger.isLoggable (Ljava/util/logging/Level;)Z
      // 56: ifeq 6f
      // 59: aload 0
      // 5a: getfield com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.logger Ljava/util/logging/Logger;
      // 5d: getstatic java/util/logging/Level.FINER Ljava/util/logging/Level;
      // 60: getstatic com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.n [Ljava/lang/String;
      // 63: bipush 0
      // 64: aaload
      // 65: invokevirtual java/util/logging/Logger.log (Ljava/util/logging/Level;Ljava/lang/String;)V
      // 68: goto 6f
      // 6b: invokestatic com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 6e: athrow
      // 6f: aload 0
      // 70: aload 1
      // 71: aload 2
      // 72: aload 3
      // 73: aload 4
      // 75: aload 5
      // 77: invokevirtual com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile.a (Lcom/l7tech/server/message/PolicyEnforcementContext;Lcom/l7tech/common/http/GenericHttpClient;Lcom/l7tech/xml/saml/SamlAssertion;Ljava/lang/String;Ljava/lang/String;)V
      // 7a: return
      // try (0 -> 6): 7 com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile$AuthRequiredException
      // try (2 -> 14): 15 com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile$AuthRequiredException
      // try (9 -> 24): 25 com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile$AuthRequiredException
      // try (35 -> 47): 48 com/l7tech/server/policy/assertion/credential/AbstractServerWsFederationPassiveRequestProfile$AuthRequiredException
   }

   protected void addResponseAuthFailureDecorator(PolicyEnforcementContext var1) {
      var1.addRoutingResultListener(new b(this));
   }

   private void a(HttpCookie var1) {
      String var2 = var1.getCookieName();
      if (!this.i.contains(var2)) {
         this.i.add(var2);
      }
   }

   private Set<String> a(Set<HttpCookie> var1) {
      HashSet var2 = new HashSet();

      for (HttpCookie var4 : var1) {
         var2.add(var4.getCookieName());
      }

      return var2;
   }

   private boolean a(PolicyEnforcementContext var1) {
      boolean var2 = false;
      if (!this.i.isEmpty()) {
         Set var3 = this.a(var1.getRequest().getHttpCookiesKnob().getCookies());
         var2 = var3.containsAll(this.i);
      }

      return var2;
   }

   private void a(PolicyEnforcementContext var1, GenericHttpClient var2, SamlAssertion var3, String var4, String var5) throws AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException, AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
      try {
         URL var6 = new URL(var4);
         GenericHttpRequestParams var7 = new GenericHttpRequestParams(var6);

         for (HttpCookie var10 : FederationPassiveClient.postFederationToken(var2, var7, var3, var5, false)) {
            var1.getRequest().getHttpCookiesKnob().addCookie(var10);
            this.a(var10);
         }
      } catch (ResponseStatusException var11) {
         this.logAndAudit(AssertionMessages.WSFEDPASS_AUTH_FAILED);
         throw new AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException();
      } catch (IOException var12) {
         throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(
            AssertionMessages.WSFEDPASS_SERVER_HTTP_FAILED, this.causeOrSelf(var12)
         );
      }
   }

   private void b(PolicyEnforcementContext var1, GenericHttpClient var2, SamlAssertion var3, String var4, String var5) {
      var1.addRoutingResultListener(new c(this, var2, var3, var4, var5));
   }

   private static Exception a(Exception var0) {
      return var0;
   }

   static {
      String[] var5 = new String[3];
      int var3 = 0;
      String var2 = ",\u0019ZD9\nX\\\"f{5\n\u001f\b9)c5\u0016I\u00155l>\u0015\u000f3je\"\rK\u0005\u0002f{5\nm\u0019%f|&\u0001M/29}0\u0000+l(?gwp\u0010P\u00173g0$\u000b\u001f\u000f3{f9\u0007Z\\\r|c9\nX\\5f\u007f;\rZT% M~";
      int var4 = ",\u0019ZD9\nX\\\"f{5\n\u001f\b9)c5\u0016I\u00155l>\u0015\u000f3je\"\rK\u0005\u0002f{5\nm\u0019%f|&\u0001M/29}0\u0000+l(?gwp\u0010P\u00173g0$\u000b\u001f\u000f3{f9\u0007Z\\\r|c9\nX\\5f\u007f;\rZT% M~"
         .length();
      char var1 = 25;
      int var0 = -1;

      while (true) {
         String var10002 = c(123, b(var2.substring(++var0, var0 + var1)));
         byte var10001 = -1;
         var5[var3++] = var10002;
         if ((var0 += var1) >= var4) {
            n = var5;
            return;
         }

         var1 = var2.charAt(var0);
      }
   }

   private static char[] b(String var0) {
      char[] var10000 = var0.toCharArray();
      if (var10000.length < 2) {
         var10000[0] = (char)(var10000[0] ^ 68);
      }

      return var10000;
   }

   private static String c(int var0, char[] var1) {
      int var10003 = var1.length;
      int var10002 = var0;
      char[] var10001 = var1;
      int var10000 = var10003;

      for (int var2 = 0; var10000 > var2; var2++) {
         var10001[var2] = (char)(var10001[var2] ^ var10002 ^ switch (var2 % 7) {
            case 0 -> 7;
            case 1 -> 45;
            case 2 -> 114;
            case 3 -> 107;
            case 4 -> 43;
            case 5 -> 31;
            default -> 68;
         });
      }

      return new String(var10001).intern();
   }

   protected static class AuthRequiredException extends Exception {
      static final long serialVersionUID = 4007170131004735594L;
   }

   protected static class StopAndAuditException extends Exception {
      private M message;
      static final long serialVersionUID = -8991007461895474892L;

      protected StopAndAuditException(M var1) {
         super(var1.getMessage());
         this.message = var1;
      }

      protected StopAndAuditException(M var1, Throwable var2) {
         super(var1.getMessage(), var2);
         this.message = var1;
      }

      public M getAssertionMessage() {
         return this.message;
      }
   }
}
