package com.l7tech.server.secureconversation;

import com.l7tech.identity.User;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.security.xml.processor.SecurityContext;
import com.l7tech.security.xml.processor.SecurityContextFinder;
import com.l7tech.util.Config;
import java.util.logging.Logger;

public class InboundSecureConversationContextManager extends SecureConversationContextManager<String> implements SecurityContextFinder {
   private static final Logger m;
   private final StoredSecureConversationSessionManager n;
   private static final String[] p;

   public InboundSecureConversationContextManager(Config var1, StoredSecureConversationSessionManager var2) {
      super(m, var1, true);
      this.n = var2;
   }

   public SecurityContext getSecurityContext(String var1) {
      return this.getSession(var1);
   }

   public SecureConversationSession createContextForUser(User var1, String var2) throws SessionCreationException {
      return this.createContextForUser(var1, var2, this.getDefaultSessionDuration(), null, -1);
   }

   public SecureConversationSession createContextForUser(User param1, String param2, long param3, byte[] param5, int param6) throws SessionCreationException {
      // $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 0
      // 01: invokevirtual com/l7tech/server/secureconversation/InboundSecureConversationContextManager.randomUuid ()Ljava/lang/String;
      // 04: getstatic com/l7tech/server/secureconversation/InboundSecureConversationContextManager.p [Ljava/lang/String;
      // 07: astore 15
      // 09: aload 15
      // 0b: bipush 1
      // 0c: aaload
      // 0d: swap
      // 0e: invokedynamic makeConcatWithConstants (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; bsm=java/lang/invoke/StringConcatFactory.makeConcatWithConstants (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; args=[ "\u0001\u0001" ]
      // 13: astore 7
      // 15: lload 3
      // 16: lconst_0
      // 17: lcmp
      // 18: ifgt 2b
      // 1b: new com/l7tech/server/secureconversation/SessionCreationException
      // 1e: dup
      // 1f: aload 15
      // 21: bipush 2
      // 22: aaload
      // 23: invokespecial com/l7tech/server/secureconversation/SessionCreationException.<init> (Ljava/lang/String;)V
      // 26: athrow
      // 27: invokestatic com/l7tech/server/secureconversation/InboundSecureConversationContextManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 2a: athrow
      // 2b: aload 0
      // 2c: iload 6
      // 2e: aload 2
      // 2f: invokevirtual com/l7tech/server/secureconversation/InboundSecureConversationContextManager.a (ILjava/lang/String;)I
      // 32: istore 8
      // 34: aload 5
      // 36: ifnull af
      // 39: aload 5
      // 3b: arraylength
      // 3c: getstatic com/l7tech/server/secureconversation/InboundSecureConversationContextManager.MIN_CLIENT_ENTROPY_BYTES I
      // 3f: if_icmplt af
      // 42: goto 49
      // 45: invokestatic com/l7tech/server/secureconversation/InboundSecureConversationContextManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 48: athrow
      // 49: aload 5
      // 4b: arraylength
      // 4c: getstatic com/l7tech/server/secureconversation/InboundSecureConversationContextManager.MAX_CLIENT_ENTROPY_BYTES I
      // 4f: if_icmpgt af
      // 52: goto 59
      // 55: invokestatic com/l7tech/server/secureconversation/InboundSecureConversationContextManager.a (Ljava/lang/Exception;)Ljava/lang/Exception;
      // 58: athrow
      // 59: aload 5
      // 5b: astore 9
      // 5d: aload 0
      // 5e: iload 8
      // 60: invokevirtual com/l7tech/server/secureconversation/InboundSecureConversationContextManager.generateNewSecret (I)[B
      // 63: astore 10
      // 65: aload 9
      // 67: aload 10
      // 69: iload 8
      // 6b: invokestatic com/l7tech/security/xml/SecureConversationKeyDeriver.pSHA1 ([B[BI)[B
      // 6e: astore 11
      // 70: goto bd
      // 73: astore 12
      // 75: new com/l7tech/server/secureconversation/SessionCreationException
      // 78: dup
      // 79: aload 12
      // 7b: invokestatic com/l7tech/util/ExceptionUtils.getMessage (Ljava/lang/Throwable;)Ljava/lang/String;
      // 7e: getstatic com/l7tech/server/secureconversation/InboundSecureConversationContextManager.p [Ljava/lang/String;
      // 81: astore 15
      // 83: aload 15
      // 85: bipush 0
      // 86: aaload
      // 87: swap
      // 88: invokedynamic makeConcatWithConstants (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; bsm=java/lang/invoke/StringConcatFactory.makeConcatWithConstants (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; args=[ "\u0001\u0001" ]
      // 8d: aload 12
      // 8f: invokespecial com/l7tech/server/secureconversation/SessionCreationException.<init> (Ljava/lang/String;Ljava/lang/Throwable;)V
      // 92: athrow
      // 93: astore 12
      // 95: new com/l7tech/server/secureconversation/SessionCreationException
      // 98: dup
      // 99: aload 12
      // 9b: invokestatic com/l7tech/util/ExceptionUtils.getMessage (Ljava/lang/Throwable;)Ljava/lang/String;
      // 9e: getstatic com/l7tech/server/secureconversation/InboundSecureConversationContextManager.p [Ljava/lang/String;
      // a1: bipush 3
      // a2: aaload
      // a3: swap
      // a4: invokedynamic makeConcatWithConstants (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; bsm=java/lang/invoke/StringConcatFactory.makeConcatWithConstants (Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite; args=[ "\u0001\u0001" ]
      // a9: aload 12
      // ab: invokespecial com/l7tech/server/secureconversation/SessionCreationException.<init> (Ljava/lang/String;Ljava/lang/Throwable;)V
      // ae: athrow
      // af: aconst_null
      // b0: astore 9
      // b2: aconst_null
      // b3: astore 10
      // b5: aload 0
      // b6: iload 8
      // b8: invokevirtual com/l7tech/server/secureconversation/InboundSecureConversationContextManager.generateNewSecret (I)[B
      // bb: astore 11
      // bd: invokestatic java/lang/System.currentTimeMillis ()J
      // c0: lstore 12
      // c2: new com/l7tech/server/secureconversation/SecureConversationSession
      // c5: dup
      // c6: aload 2
      // c7: aload 7
      // c9: aload 9
      // cb: aload 10
      // cd: aload 11
      // cf: lload 12
      // d1: lload 12
      // d3: lload 3
      // d4: ladd
      // d5: aload 1
      // d6: aconst_null
      // d7: invokespecial com/l7tech/server/secureconversation/SecureConversationSession.<init> (Ljava/lang/String;Ljava/lang/String;[B[B[BJJLcom/l7tech/identity/User;Lorg/w3c/dom/Element;)V
      // da: astore 14
      // dc: aload 0
      // dd: aload 14
      // df: invokevirtual com/l7tech/server/secureconversation/SecureConversationSession.getIdentifier ()Ljava/lang/String;
      // e2: aload 14
      // e4: invokevirtual com/l7tech/server/secureconversation/InboundSecureConversationContextManager.saveSession (Ljava/lang/Object;Lcom/l7tech/server/secureconversation/SecureConversationSession;)V
      // e7: aload 14
      // e9: areturn
      // try (30 -> 41): 42 java/security/NoSuchAlgorithmException
      // try (28 -> 34): 35 java/security/NoSuchAlgorithmException
      // try (10 -> 21): 21 java/security/NoSuchAlgorithmException
      // try (50 -> 55): 56 java/security/NoSuchAlgorithmException
      // try (50 -> 55): 71 java/security/InvalidKeyException
   }

   protected void storeSession(String var1, SecureConversationSession var2) throws SaveException {
      this.n.save(this.a(var2));
   }

   protected SecureConversationSession loadSession(String var1) throws FindException {
      StoredSecureConversationSession var2 = this.n.findInboundSessionByIdentifier(var1);

      try {
         if (var2 == null) {
            return null;
         }
      } catch (FindException var3) {
         throw a(var3);
      }

      return this.fromStored(var2, this.n.decryptSessionKey(var2.getEncryptedKey()));
   }

   protected void deleteSession(String var1) throws DeleteException {
      this.n.deleteInboundSessionByIdentifer(var1);
   }

   private StoredSecureConversationSession a(SecureConversationSession var1) {
      return new StoredSecureConversationSession(
         var1.getIdentifier(),
         var1.getCreation(),
         var1.getExpiration(),
         var1.getSCNamespace(),
         this.n.encryptSessionKey(var1.getSecretKey()),
         var1.getUsedBy().getProviderId(),
         var1.getUsedBy().getId(),
         var1.getUsedBy().getLogin()
      );
   }

   private int a(int var1, String var2) {
      int var4 = this.getDefaultKeySize(var2);
      int var3;
      if (var1 > 0) {
         int var5 = var1 / 8 + (var1 % 8 > 0 ? 1 : 0);
         if (var5 < MIN_KEY_SIZE) {
            var3 = MIN_KEY_SIZE;
         } else if (var5 > MAX_KEY_SIZE) {
            var3 = MAX_KEY_SIZE;
         } else {
            var3 = var5;
         }
      } else {
         var3 = var4;
      }

      return var3;
   }

   static {
      String[] var5 = new String[4];
      int var3 = 0;
      String var2 = "jbW\u000f\fL/Kc\u0016\n\u0005GjMmB\b@ZjL\u007f_\u0002\u000e\tdZu\fM\u001fWxB\u001dZ\u0006 H{AC\fHvZ~\u0001\u0019\u0005Jg\u0011oY\u0000O\\zVh\u0019";
      int var4 = "jbW\u000f\fL/Kc\u0016\n\u0005GjMmB\b@ZjL\u007f_\u0002\u000e\tdZu\fM\u001fWxB\u001dZ\u0006 H{AC\fHvZ~\u0001\u0019\u0005Jg\u0011oY\u0000O\\zVh\u0019"
         .length();
      char var1 = ' ';
      int var7 = -1;

      label24:
      while (true) {
         byte var10000 = 115;
         String var10001 = var2.substring(++var7, var7 + var1);
         byte var10002 = -1;

         while (true) {
            String var8 = b(var10000, b(var10001));
            switch (var10002) {
               case 0:
                  var5[var3++] = var8;
                  if ((var7 += var1) >= var4) {
                     p = var5;
                     m = Logger.getLogger(InboundSecureConversationContextManager.class.getName());
                     return;
                  }

                  var1 = var2.charAt(var7);
                  break;
               default:
                  var5[var3++] = var8;
                  if ((var7 += var1) < var4) {
                     var1 = var2.charAt(var7);
                     continue label24;
                  }

                  var2 = "\u001c\u00195ny6\u0011o\u00183oq-\u0016 \u0012fpe*\u000bo\u001e#=w+\u001a.\b#o0-\u0017.\u0012fgu+\u0010a \u001a\u0012'\u007f|<_;\u0013fzu7\u001a=\u001d2x0*\u001a<\u000f/r~y\u0014*\u0005|=";
                  var4 = "\u001c\u00195ny6\u0011o\u00183oq-\u0016 \u0012fpe*\u000bo\u001e#=w+\u001a.\b#o0-\u0017.\u0012fgu+\u0010a \u001a\u0012'\u007f|<_;\u0013fzu7\u001a=\u001d2x0*\u001a<\u000f/r~y\u0014*\u0005|="
                     .length();
                  var1 = '+';
                  var7 = -1;
            }

            var10000 = 3;
            var10001 = var2.substring(++var7, var7 + var1);
            var10002 = 0;
         }
      }
   }

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

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

      return var10000;
   }

   private static String b(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 -> 76;
            case 1 -> 127;
            case 2 -> 69;
            case 3 -> 30;
            case 4 -> 19;
            case 5 -> 90;
            default -> 124;
         });
      }

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