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

import com.l7tech.common.http.HttpCookie;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.credential.CredentialFinderException;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.credential.http.CookieCredentialSourceAssertion;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.credential.ServerCredentialSourceAssertion;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

public class ServerCookieCredentialSourceAssertion extends ServerCredentialSourceAssertion<CookieCredentialSourceAssertion> {
   private static final String d = "\"";
   private final String e;
   private static final String[] g;

   public ServerCookieCredentialSourceAssertion(CookieCredentialSourceAssertion var1) {
      super(var1);
      this.e = var1.getCookieName();
   }

   @Override
   public AssertionStatus checkRequest(PolicyEnforcementContext var1) throws IOException, PolicyAssertionException {
      AssertionStatus var2 = super.checkRequest(var1);

      label45: {
         try {
            if (!AssertionStatus.NONE.equals(var2)) {
               return var2;
            }

            if (!StringUtils.isNotBlank(this.assertion.getVariablePrefix())) {
               break label45;
            }
         } catch (IOException var6) {
            throw a(var6);
         }

         Iterator var3 = var1.getRequest().getHttpCookiesKnob().getCookies().iterator();

         HttpCookie var4;
         do {
            if (!var3.hasNext()) {
               return var2;
            }

            var4 = (HttpCookie)var3.next();
         } while (!var4.getCookieName().equals(this.e));

         String var5 = var4.getCookieValue();
         var5 = this.stripSurroundingQuotationMarks(var5);
         var1.setVariable(this.assertion.getVariablePrefix() + "." + var4.getCookieName(), var5);
         return var2;
      }

      this.logAndAudit(AssertionMessages.ASSERTION_MISCONFIGURED, g[0]);
      return var2;
   }

   protected String stripSurroundingQuotationMarks(String var1) {
      return var1 != null && var1.length() >= 2 && var1.startsWith("\"") && var1.endsWith("\"") ? var1.substring(1, var1.length() - 1) : var1;
   }

   @Override
   protected LoginCredentials findCredentials(Message param1, Map<String, String> param2) throws IOException, CredentialFinderException {
      // $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: aload 1
      // 01: invokevirtual com/l7tech/message/Message.getHttpCookiesKnob ()Lcom/l7tech/message/HttpCookiesKnob;
      // 04: astore 3
      // 05: aload 3
      // 06: invokeinterface com/l7tech/message/HttpCookiesKnob.getCookies ()Ljava/util/Set; 1
      // 0b: invokeinterface java/util/Set.iterator ()Ljava/util/Iterator; 1
      // 10: astore 4
      // 12: aload 4
      // 14: invokeinterface java/util/Iterator.hasNext ()Z 1
      // 19: ifeq 92
      // 1c: aload 4
      // 1e: invokeinterface java/util/Iterator.next ()Ljava/lang/Object; 1
      // 23: checkcast com/l7tech/common/http/HttpCookie
      // 26: astore 5
      // 28: aload 0
      // 29: getfield com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.e Ljava/lang/String;
      // 2c: aload 5
      // 2e: invokevirtual com/l7tech/common/http/HttpCookie.getCookieName ()Ljava/lang/String;
      // 31: invokevirtual java/lang/String.equalsIgnoreCase (Ljava/lang/String;)Z
      // 34: ifeq 8f
      // 37: aload 5
      // 39: invokevirtual com/l7tech/common/http/HttpCookie.getCookieValue ()Ljava/lang/String;
      // 3c: astore 6
      // 3e: aload 6
      // 40: ifnull 7b
      // 43: aload 6
      // 45: invokevirtual java/lang/String.length ()I
      // 48: ifle 7b
      // 4b: goto 52
      // 4e: invokestatic com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 51: athrow
      // 52: aload 0
      // 53: getstatic com/l7tech/gateway/common/audit/AssertionMessages.HTTPCOOKIE_FOUND Lcom/l7tech/gateway/common/audit/Messages$M;
      // 56: bipush 1
      // 57: anewarray 137
      // 5a: dup
      // 5b: bipush 0
      // 5c: aload 0
      // 5d: getfield com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.e Ljava/lang/String;
      // 60: aastore
      // 61: invokevirtual com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.logAndAudit (Lcom/l7tech/gateway/common/audit/AuditDetailMessage;[Ljava/lang/String;)V
      // 64: new com/l7tech/security/token/OpaqueSecurityToken
      // 67: dup
      // 68: aconst_null
      // 69: aload 6
      // 6b: invokevirtual java/lang/String.toCharArray ()[C
      // 6e: invokespecial com/l7tech/security/token/OpaqueSecurityToken.<init> (Ljava/lang/String;[C)V
      // 71: ldc com/l7tech/policy/assertion/credential/http/CookieCredentialSourceAssertion
      // 73: invokestatic com/l7tech/policy/assertion/credential/LoginCredentials.makeLoginCredentials (Lcom/l7tech/security/token/SecurityToken;Ljava/lang/Class;)Lcom/l7tech/policy/assertion/credential/LoginCredentials;
      // 76: areturn
      // 77: invokestatic com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 7a: athrow
      // 7b: aload 0
      // 7c: getstatic com/l7tech/gateway/common/audit/AssertionMessages.HTTPCOOKIE_FOUND_EMPTY Lcom/l7tech/gateway/common/audit/Messages$M;
      // 7f: bipush 1
      // 80: anewarray 137
      // 83: dup
      // 84: bipush 0
      // 85: aload 0
      // 86: getfield com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.e Ljava/lang/String;
      // 89: aastore
      // 8a: invokevirtual com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.logAndAudit (Lcom/l7tech/gateway/common/audit/AuditDetailMessage;[Ljava/lang/String;)V
      // 8d: aconst_null
      // 8e: areturn
      // 8f: goto 12
      // 92: aload 0
      // 93: getstatic com/l7tech/gateway/common/audit/AssertionMessages.HTTPCOOKIE_NOT_FOUND Lcom/l7tech/gateway/common/audit/Messages$M;
      // 96: bipush 1
      // 97: anewarray 137
      // 9a: dup
      // 9b: bipush 0
      // 9c: aload 0
      // 9d: getfield com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.e Ljava/lang/String;
      // a0: aastore
      // a1: invokevirtual com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.logAndAudit (Lcom/l7tech/gateway/common/audit/AuditDetailMessage;[Ljava/lang/String;)V
      // a4: aconst_null
      // a5: areturn
      // try (23 -> 28): 29 java/io/IOException
      // try (25 -> 50): 50 java/io/IOException
   }

   @Override
   protected AssertionStatus checkCredentials(LoginCredentials param1, Map<String, String> param2) throws CredentialFinderException {
      // $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: aload 1
      // 01: ifnonnull 0c
      // 04: getstatic com/l7tech/policy/assertion/AssertionStatus.AUTH_REQUIRED Lcom/l7tech/policy/assertion/AssertionStatus;
      // 07: areturn
      // 08: invokestatic com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 0b: athrow
      // 0c: aload 1
      // 0d: invokevirtual com/l7tech/policy/assertion/credential/LoginCredentials.getCredentials ()[C
      // 10: astore 3
      // 11: aload 3
      // 12: ifnull 22
      // 15: aload 3
      // 16: arraylength
      // 17: bipush 1
      // 18: if_icmpge 33
      // 1b: goto 22
      // 1e: invokestatic com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 21: athrow
      // 22: new com/l7tech/policy/assertion/credential/CredentialFinderException
      // 25: dup
      // 26: getstatic com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.g [Ljava/lang/String;
      // 29: bipush 1
      // 2a: aaload
      // 2b: invokespecial com/l7tech/policy/assertion/credential/CredentialFinderException.<init> (Ljava/lang/String;)V
      // 2e: athrow
      // 2f: invokestatic com/l7tech/server/policy/assertion/credential/http/ServerCookieCredentialSourceAssertion.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 32: athrow
      // 33: getstatic com/l7tech/policy/assertion/AssertionStatus.NONE Lcom/l7tech/policy/assertion/AssertionStatus;
      // 36: areturn
      // try (0 -> 4): 4 com/l7tech/policy/assertion/credential/CredentialFinderException
      // try (9 -> 15): 16 com/l7tech/policy/assertion/credential/CredentialFinderException
      // try (11 -> 25): 25 com/l7tech/policy/assertion/credential/CredentialFinderException
   }

   @Override
   protected void challenge(PolicyEnforcementContext var1, Map<String, String> var2) {
   }

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

   static {
      String[] var5 = new String[2];
      int var3 = 0;
      String var2 = "=\u001bfHQ5[\u0012\u0016*[\u00107@\u0016\u0012/F\u00104B\u0016\u0017/XY\"V]T\u0005_^)]\u0007T5[DgQ\u001c\u001b-WUgQ\u001c\u001a2[H3\u0012\u0005\u00154WQ%^\u0016Z\" \u00115MY(\\S\u0017)Q[.WS\u001d5\u001e].A\u0000\u001d(Y\u0010(@S\u0011+ND>";
      int var4 = "=\u001bfHQ5[\u0012\u0016*[\u00107@\u0016\u0012/F\u00104B\u0016\u0017/XY\"V]T\u0005_^)]\u0007T5[DgQ\u001c\u001b-WUgQ\u001c\u001a2[H3\u0012\u0005\u00154WQ%^\u0016Z\" \u00115MY(\\S\u0017)Q[.WS\u001d5\u001e].A\u0000\u001d(Y\u0010(@S\u0011+ND>"
         .length();
      char var1 = 'A';
      int var0 = -1;

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

         var1 = var2.charAt(var0);
      }
   }

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

      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 -> 70;
            case 1 -> 65;
            case 2 -> 115;
            case 3 -> 11;
            case 4 -> 5;
            case 5 -> 114;
            default -> 7;
         });
      }

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