/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.NonCloseableOutputStream
 *  com.l7tech.common.io.XmlUtil
 *  com.l7tech.identity.User
 *  com.l7tech.objectmodel.DeleteException
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.objectmodel.SaveException
 *  com.l7tech.util.Config
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.HexUtils
 *  com.l7tech.util.PoolByteArrayOutputStream
 */
package com.l7tech.server.secureconversation;

import com.l7tech.common.io.NonCloseableOutputStream;
import com.l7tech.common.io.XmlUtil;
import com.l7tech.identity.User;
import com.l7tech.objectmodel.DeleteException;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.objectmodel.SaveException;
import com.l7tech.server.secureconversation.SecureConversationContextManager;
import com.l7tech.server.secureconversation.SecureConversationSession;
import com.l7tech.server.secureconversation.SessionCreationException;
import com.l7tech.server.secureconversation.StoredSecureConversationSession;
import com.l7tech.server.secureconversation.StoredSecureConversationSessionManager;
import com.l7tech.util.Config;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.HexUtils;
import com.l7tech.util.PoolByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.logging.Logger;
import org.w3c.dom.Node;

public class OutboundSecureConversationContextManager
extends SecureConversationContextManager<OutboundSessionKey> {
    private static final Logger m;
    private final StoredSecureConversationSessionManager n;
    private static final String[] p;

    public OutboundSecureConversationContextManager(Config config, StoredSecureConversationSessionManager storedSecureConversationSessionManager) {
        super(m, config, false);
        this.n = storedSecureConversationSessionManager;
    }

    public static OutboundSessionKey newSessionKey(User user, String string) {
        return new OutboundSessionKey(user, string);
    }

    public SecureConversationSession createContextForUser(User user, OutboundSessionKey outboundSessionKey, String string, String string2, long l2, long l3, byte[] byArray, byte[] byArray2, byte[] byArray3, int n2) throws SessionCreationException {
        try {
            if (user == null) {
                throw new SessionCreationException(p[3]);
            }
        }
        catch (SessionCreationException sessionCreationException) {
            throw OutboundSecureConversationContextManager.a(sessionCreationException);
        }
        try {
            if (outboundSessionKey == null) {
                throw new SessionCreationException(p[0]);
            }
        }
        catch (SessionCreationException sessionCreationException) {
            throw OutboundSecureConversationContextManager.a(sessionCreationException);
        }
        SecureConversationSession secureConversationSession = this.getSession(outboundSessionKey);
        try {
            if (secureConversationSession != null) {
                this.cancelSession(outboundSessionKey);
                m.warning(p[4] + secureConversationSession.getIdentifier() + p[5]);
            }
        }
        catch (SessionCreationException sessionCreationException) {
            throw OutboundSecureConversationContextManager.a(sessionCreationException);
        }
        return super.createContextForUser(user, outboundSessionKey, string, string2, l2, l3, byArray, byArray2, byArray3, n2, null);
    }

    @Override
    protected void validateSessionKey(OutboundSessionKey outboundSessionKey, User user) throws SessionCreationException {
        block9: {
            try {
                if (!outboundSessionKey.isValid()) {
                    throw new SessionCreationException(p[2]);
                }
            }
            catch (SessionCreationException sessionCreationException) {
                throw OutboundSecureConversationContextManager.a(sessionCreationException);
            }
            try {
                block10: {
                    try {
                        try {
                            if (user == null) break block9;
                            if (!user.getProviderId().equals((Object)outboundSessionKey.providerId)) break block10;
                        }
                        catch (SessionCreationException sessionCreationException) {
                            throw OutboundSecureConversationContextManager.a(sessionCreationException);
                        }
                        if (outboundSessionKey.userId.equals(user.getId())) break block9;
                    }
                    catch (SessionCreationException sessionCreationException) {
                        throw OutboundSecureConversationContextManager.a(sessionCreationException);
                    }
                }
                throw new SessionCreationException(p[1]);
            }
            catch (SessionCreationException sessionCreationException) {
                throw OutboundSecureConversationContextManager.a(sessionCreationException);
            }
        }
    }

    @Override
    protected void storeSession(OutboundSessionKey outboundSessionKey, SecureConversationSession secureConversationSession) throws SaveException {
        this.n.save((PersistentEntity)this.a(outboundSessionKey, secureConversationSession));
    }

    @Override
    protected SecureConversationSession loadSession(OutboundSessionKey outboundSessionKey) throws FindException {
        SecureConversationSession secureConversationSession;
        StoredSecureConversationSession storedSecureConversationSession = this.n.findOutboundSessionByUserAndService(outboundSessionKey.providerId, outboundSessionKey.userId, outboundSessionKey.serviceUrl);
        try {
            secureConversationSession = storedSecureConversationSession == null ? null : this.fromStored(storedSecureConversationSession, this.n.decryptSessionKey(storedSecureConversationSession.getEncryptedKey()));
        }
        catch (FindException findException) {
            throw OutboundSecureConversationContextManager.a((Exception)((Object)findException));
        }
        return secureConversationSession;
    }

    @Override
    protected void deleteSession(OutboundSessionKey outboundSessionKey) throws DeleteException {
        this.n.deleteOutboundSessionByUserAndService(outboundSessionKey.providerId, outboundSessionKey.userId, outboundSessionKey.serviceUrl);
    }

    private StoredSecureConversationSession a(OutboundSessionKey outboundSessionKey, SecureConversationSession secureConversationSession) throws SaveException {
        try {
            String string;
            String string2;
            long l2;
            long l3;
            String string3;
            String string4;
            String string5;
            Goid goid;
            String string6;
            StoredSecureConversationSession storedSecureConversationSession;
            StoredSecureConversationSession storedSecureConversationSession2;
            try {
                StoredSecureConversationSession storedSecureConversationSession3;
                storedSecureConversationSession2 = storedSecureConversationSession3;
                storedSecureConversationSession = storedSecureConversationSession3;
                string6 = outboundSessionKey.serviceUrl;
                goid = secureConversationSession.getUsedBy().getProviderId();
                string5 = secureConversationSession.getUsedBy().getId();
                string4 = secureConversationSession.getUsedBy().getLogin();
                string3 = secureConversationSession.getIdentifier();
                l3 = secureConversationSession.getCreation();
                l2 = secureConversationSession.getExpiration();
                string2 = secureConversationSession.getSCNamespace();
                string = secureConversationSession.getElement() != null ? XmlUtil.nodeToString((Node)secureConversationSession.getElement()) : null;
            }
            catch (IOException iOException) {
                throw OutboundSecureConversationContextManager.a(iOException);
            }
            storedSecureConversationSession2(string6, goid, string5, string4, string3, l3, l2, string2, string, this.n.encryptSessionKey(secureConversationSession.getSecretKey()));
            return storedSecureConversationSession;
        }
        catch (IOException iOException) {
            throw new SaveException(p[6] + ExceptionUtils.getMessage((Throwable)iOException), (Throwable)iOException);
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[7];
                var3_1 = 0;
                var2_2 = "Q{\tl[\u0017H\u0002u\u001ff\u0012\u0011U\u0002l\u001fnG\u0011TGz<wp\u001b}^\u001d\u0006VqZ|@\u001dGV{ZlW\u000bUKq\u0014?T\u0017T\u0002k\tz@X\u000eKp\f~^\u0011B\u0002k\tz@X@MlZlW\u000bUKq\u00146=wp\u001b}^\u001d\u0006VqZ|@\u001dGV{ZlW\u000bUKq\u0014?T\u0017T\u0002k\tz@X\u000eLq\u000e?SXVGl\tvA\fCLjZvV\u001dHVw\u000ef\u001b\u0019Q{\tl[\u0017H\u0002q\rqW\n\u0006KmZmW\tSKl\u001f{9q{\u0019j@\u001d\u0006aq\u0014iW\nUCj\u0013p\\XuGm\tv]\u0016\u0006Uw\u000ew\u0012\fNG>\tzA\u000bOMpZvV\u001dHVw\u001cvW\n\u0006\u0005";
                var4_3 = "Q{\tl[\u0017H\u0002u\u001ff\u0012\u0011U\u0002l\u001fnG\u0011TGz<wp\u001b}^\u001d\u0006VqZ|@\u001dGV{ZlW\u000bUKq\u0014?T\u0017T\u0002k\tz@X\u000eKp\f~^\u0011B\u0002k\tz@X@MlZlW\u000bUKq\u00146=wp\u001b}^\u001d\u0006VqZ|@\u001dGV{ZlW\u000bUKq\u0014?T\u0017T\u0002k\tz@X\u000eLq\u000e?SXVGl\tvA\fCLjZvV\u001dHVw\u000ef\u001b\u0019Q{\tl[\u0017H\u0002q\rqW\n\u0006KmZmW\tSKl\u001f{9q{\u0019j@\u001d\u0006aq\u0014iW\nUCj\u0013p\\XuGm\tv]\u0016\u0006Uw\u000ew\u0012\fNG>\tzA\u000bOMpZvV\u001dHVw\u001cvW\n\u0006\u0005".length();
                var1_4 = 23;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 96;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = OutboundSecureConversationContextManager.b(v0, OutboundSecureConversationContextManager.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "2\t%IvospL#\bj9tg^?Aq;t{\u0007)@G,Ji*1aFm[`=xtE$R`obpZ>Aj!1sF?\bv;~gH*M?o";
                    var4_3 = "2\t%IvospL#\bj9tg^?Aq;t{\u0007)@G,Ji*1aFm[`=xtE$R`obpZ>Aj!1sF?\bv;~gH*M?o".length();
                    var1_4 = 23;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 87;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = OutboundSecureConversationContextManager.b(v0, OutboundSecureConversationContextManager.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        OutboundSecureConversationContextManager.p = var5;
        OutboundSecureConversationContextManager.m = Logger.getLogger(OutboundSecureConversationContextManager.class.getName());
    }

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

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x46);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 66;
                    case 1 -> 126;
                    case 2 -> 26;
                    case 3 -> 127;
                    case 4 -> 82;
                    case 5 -> 24;
                    default -> 70;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    public static final class OutboundSessionKey
    implements Serializable {
        private final Goid providerId;
        private final String userId;
        private final String serviceUrl;
        static final long serialVersionUID = 7165464867192739512L;
        private static final String[] a;

        private OutboundSessionKey(User user, String string) {
            if (user == null) {
                String[] stringArray = a;
                throw new IllegalArgumentException(stringArray[2]);
            }
            try {
                if (string == null) {
                    throw new IllegalArgumentException(a[0]);
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw OutboundSessionKey.a(illegalArgumentException);
            }
            this.providerId = user.getProviderId();
            this.userId = user.getId();
            this.serviceUrl = string;
        }

        public static OutboundSessionKey fromStringIdentifier(String string) {
            try {
                ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(HexUtils.decodeBase64((String)string)));
                Object object = objectInputStream.readObject();
                if (object instanceof OutboundSessionKey) {
                    return (OutboundSessionKey)object;
                }
                String[] stringArray = a;
                throw new IllegalArgumentException(stringArray[3]);
            }
            catch (IOException iOException) {
                String[] stringArray = a;
                throw new IllegalArgumentException(stringArray[1], iOException);
            }
            catch (ClassNotFoundException classNotFoundException) {
                throw new IllegalArgumentException(a[1], classNotFoundException);
            }
        }

        public String toStringIdentifier() {
            String string;
            try (PoolByteArrayOutputStream poolByteArrayOutputStream = new PoolByteArrayOutputStream();){
                ObjectOutputStream objectOutputStream = new ObjectOutputStream((OutputStream)new NonCloseableOutputStream((OutputStream)poolByteArrayOutputStream));
                objectOutputStream.writeObject(this);
                objectOutputStream.close();
                string = HexUtils.encodeBase64((byte[])poolByteArrayOutputStream.toByteArray(), (boolean)true);
            }
            return string;
        }

        boolean isValid() {
            boolean bl2;
            try {
                bl2 = this.userId != null;
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw OutboundSessionKey.a(illegalArgumentException);
            }
            return bl2;
        }

        /*
         * Enabled force condition propagation
         * Lifted jumps to return sites
         */
        public boolean equals(Object object) {
            OutboundSessionKey outboundSessionKey;
            block27: {
                block26: {
                    block25: {
                        block24: {
                            try {
                                if (this == object) {
                                    return true;
                                }
                            }
                            catch (IllegalArgumentException illegalArgumentException) {
                                throw OutboundSessionKey.a(illegalArgumentException);
                            }
                            try {
                                try {
                                    if (object != null && this.getClass() == object.getClass()) break block24;
                                    return false;
                                }
                                catch (IllegalArgumentException illegalArgumentException) {
                                    throw OutboundSessionKey.a(illegalArgumentException);
                                }
                            }
                            catch (IllegalArgumentException illegalArgumentException) {
                                throw OutboundSessionKey.a(illegalArgumentException);
                            }
                        }
                        outboundSessionKey = (OutboundSessionKey)object;
                        try {
                            try {
                                if (this.providerId == null) break block25;
                                if (this.providerId.equals((Object)outboundSessionKey.providerId)) break block26;
                                return false;
                            }
                            catch (IllegalArgumentException illegalArgumentException) {
                                throw OutboundSessionKey.a(illegalArgumentException);
                            }
                        }
                        catch (IllegalArgumentException illegalArgumentException) {
                            throw OutboundSessionKey.a(illegalArgumentException);
                        }
                    }
                    try {
                        if (outboundSessionKey.providerId == null) break block26;
                        return false;
                    }
                    catch (IllegalArgumentException illegalArgumentException) {
                        throw OutboundSessionKey.a(illegalArgumentException);
                    }
                }
                try {
                    if (!this.serviceUrl.equals(outboundSessionKey.serviceUrl)) {
                        return false;
                    }
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw OutboundSessionKey.a(illegalArgumentException);
                }
                try {
                    try {
                        if (this.userId == null) break block27;
                        if (this.userId.equals(outboundSessionKey.userId)) return true;
                        return false;
                    }
                    catch (IllegalArgumentException illegalArgumentException) {
                        throw OutboundSessionKey.a(illegalArgumentException);
                    }
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw OutboundSessionKey.a(illegalArgumentException);
                }
            }
            try {
                if (outboundSessionKey.userId == null) return true;
                return false;
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw OutboundSessionKey.a(illegalArgumentException);
            }
        }

        public int hashCode() {
            int n2;
            int n3;
            int n4;
            try {
                n4 = this.providerId != null ? this.providerId.hashCode() : 0;
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw OutboundSessionKey.a(illegalArgumentException);
            }
            int n5 = n4;
            try {
                n3 = 31 * n5;
                n2 = this.userId != null ? this.userId.hashCode() : 0;
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw OutboundSessionKey.a(illegalArgumentException);
            }
            n5 = n3 + n2;
            n5 = 31 * n5 + this.serviceUrl.hashCode();
            return n5;
        }

        private static IllegalArgumentException a(IllegalArgumentException illegalArgumentException) {
            return illegalArgumentException;
        }

        /*
         * Unable to fully structure code
         */
        static {
            block10: {
                block9: {
                    var5 = new String[4];
                    var3_1 = 0;
                    var2_2 = "Dv\\f \u0010]\u0017f\\|i\u001aK\u0017aKa<\u001aJRw\u0012~}Xq%\u001a\\\u0017zJu'\u0007QQzKb";
                    var4_3 = "Dv\\f \u0010]\u0017f\\|i\u001aK\u0017aKa<\u001aJRw\u0012~}Xq%\u001a\\\u0017zJu'\u0007QQzKb".length();
                    var1_4 = 23;
                    var0_5 = -1;
lbl7:
                    // 2 sources

                    while (true) {
                        v0 = 17;
                        v1 = ++var0_5;
                        v2 = var2_2.substring(v1, v1 + var1_4);
                        v3 = -1;
                        break block9;
                        break;
                    }
lbl13:
                    // 1 sources

                    while (true) {
                        var5[var3_1++] = OutboundSessionKey.a(v0, OutboundSessionKey.a(v2));
                        if ((var0_5 += var1_4) < var4_3) {
                            var1_4 = var2_2.charAt(var0_5);
                            ** continue;
                        }
                        var2_2 = "3\u0011:\u0013\u0018k:f\u0010:\u0010Mk;#\u0006\u0012\u000f\f)\u0000Tk-f\u000b;\u0004Vv  \u000b:\u0013";
                        var4_3 = "3\u0011:\u0013\u0018k:f\u0010:\u0010Mk;#\u0006\u0012\u000f\f)\u0000Tk-f\u000b;\u0004Vv  \u000b:\u0013".length();
                        var1_4 = 16;
                        var0_5 = -1;
lbl22:
                        // 2 sources

                        while (true) {
                            v0 = 96;
                            v4 = ++var0_5;
                            v2 = var2_2.substring(v4, v4 + var1_4);
                            v3 = 0;
                            break block9;
                            break;
                        }
                        break;
                    }
lbl28:
                    // 1 sources

                    while (true) {
                        var5[var3_1++] = OutboundSessionKey.a(v0, OutboundSessionKey.a(v2));
                        if ((var0_5 += var1_4) < var4_3) {
                            var1_4 = var2_2.charAt(var0_5);
                            ** continue;
                        }
                        break block10;
                        break;
                    }
                }
                switch (v3) {
                    default: {
                        ** continue;
                    }
                    ** case 0:
lbl39:
                    // 1 sources

                    ** continue;
                }
            }
            OutboundSessionKey.a = var5;
        }

        private static char[] a(String string) {
            char[] cArray = string.toCharArray();
            if (cArray.length < 2) {
                cArray = cArray;
                cArray[0] = (char)(cArray[0] ^ 0x29);
            }
            return cArray;
        }

        /*
         * Handled impossible loop by duplicating code
         * Enabled aggressive block sorting
         */
        private static String a(int n2, char[] cArray) {
            char[] cArray2;
            block11: {
                int n3;
                int n4;
                int n5;
                block10: {
                    n5 = n2;
                    cArray2 = cArray;
                    n4 = cArray.length;
                    n3 = 0;
                    if (!true) break block10;
                    n4 = n4;
                    if (n4 <= n3) break block11;
                }
                do {
                    n5 = n5;
                    cArray2 = cArray2;
                    int n6 = n3;
                    cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                        case 0 -> 38;
                        case 1 -> 2;
                        case 2 -> 63;
                        case 3 -> 1;
                        case 4 -> 88;
                        case 5 -> 98;
                        default -> 41;
                    })));
                    ++n3;
                    n4 = n4;
                } while (n4 > n3);
            }
            return new String(cArray2).intern();
        }
    }
}

