/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.XmlUtil
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.message.XmlKnob
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.credential.XpathCredentialSource
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.SecurityTokenType
 *  com.l7tech.security.token.UsernamePasswordSecurityToken
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.xml.xpath.XpathExpression
 *  com.l7tech.xml.xpath.XpathUtil
 *  com.l7tech.xml.xpath.XpathVariableContext
 *  com.l7tech.xml.xpath.XpathVariableFinder
 *  com.l7tech.xml.xpath.XpathVariableFinderVariableContext
 *  com.l7tech.xml.xpath.XpathVersion
 *  org.jaxen.FunctionContext
 *  org.jaxen.JaxenException
 *  org.jaxen.VariableContext
 *  org.jaxen.XPathFunctionContext
 *  org.jaxen.dom.DOMXPath
 */
package com.l7tech.server.policy.assertion.credential;

import com.l7tech.common.io.XmlUtil;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.message.XmlKnob;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.credential.XpathCredentialSource;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.SecurityTokenType;
import com.l7tech.security.token.UsernamePasswordSecurityToken;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.credential.e;
import com.l7tech.server.util.xml.PolicyEnforcementContextXpathVariableFinder;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.xml.xpath.XpathExpression;
import com.l7tech.xml.xpath.XpathUtil;
import com.l7tech.xml.xpath.XpathVariableContext;
import com.l7tech.xml.xpath.XpathVariableFinder;
import com.l7tech.xml.xpath.XpathVariableFinderVariableContext;
import com.l7tech.xml.xpath.XpathVersion;
import java.io.IOException;
import java.util.concurrent.Callable;
import org.jaxen.FunctionContext;
import org.jaxen.JaxenException;
import org.jaxen.VariableContext;
import org.jaxen.XPathFunctionContext;
import org.jaxen.dom.DOMXPath;
import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class ServerXpathCredentialSource
extends AbstractMessageTargetableServerAssertion<XpathCredentialSource> {
    private static final FunctionContext d;
    private final DOMXPath e;
    private final DOMXPath f;
    private final boolean g;
    private static final String[] h;

    public ServerXpathCredentialSource(XpathCredentialSource xpathCredentialSource) {
        boolean bl2;
        block21: {
            block20: {
                boolean bl3;
                boolean bl4;
                block19: {
                    block17: {
                        super(xpathCredentialSource);
                        bl4 = true;
                        XpathExpression xpathExpression = null;
                        try {
                            block16: {
                                xpathExpression = xpathCredentialSource.getXpathExpression();
                                try {
                                    if (xpathExpression != null) break block16;
                                    this.logger.warning(h[1]);
                                    this.e = null;
                                    break block17;
                                }
                                catch (JaxenException jaxenException) {
                                    throw ServerXpathCredentialSource.a((Exception)((Object)jaxenException));
                                }
                            }
                            String string = xpathExpression.getExpression();
                            if (!XpathUtil.usesTargetDocument((String)string, (XpathVersion)xpathExpression.getXpathVersion())) {
                                bl4 = false;
                            }
                            this.e = new DOMXPath(string);
                            this.e.setFunctionContext(d);
                            this.e.setVariableContext((VariableContext)new XpathVariableFinderVariableContext(null));
                            for (String string2 : xpathExpression.getNamespaces().keySet()) {
                                String string3 = (String)xpathExpression.getNamespaces().get(string2);
                                this.e.addNamespace(string2, string3);
                            }
                        }
                        catch (JaxenException jaxenException) {
                            String[] stringArray = h;
                            throw new RuntimeException(stringArray[0] + String.valueOf(xpathExpression) + "'", jaxenException);
                        }
                    }
                    bl3 = true;
                    Object object = null;
                    try {
                        String string2;
                        block18: {
                            object = xpathCredentialSource.getPasswordExpression();
                            try {
                                if (object != null) break block18;
                                this.logger.warning(h[3]);
                                this.f = null;
                                break block19;
                            }
                            catch (JaxenException jaxenException) {
                                throw ServerXpathCredentialSource.a((Exception)((Object)jaxenException));
                            }
                        }
                        string2 = object.getExpression();
                        if (!XpathUtil.usesTargetDocument((String)string2, (XpathVersion)object.getXpathVersion())) {
                            bl3 = false;
                        }
                        this.f = new DOMXPath(string2);
                        this.f.setFunctionContext(d);
                        this.f.setVariableContext((VariableContext)new XpathVariableFinderVariableContext(null));
                        for (String string : object.getNamespaces().keySet()) {
                            String string4 = (String)object.getNamespaces().get(string);
                            this.f.addNamespace(string, string4);
                        }
                    }
                    catch (JaxenException jaxenException) {
                        throw new RuntimeException(h[2] + String.valueOf(object) + "'", jaxenException);
                    }
                }
                try {
                    try {
                        ServerXpathCredentialSource serverXpathCredentialSource = this;
                        if (!bl4 && !bl3) break block20;
                    }
                    catch (JaxenException jaxenException) {
                        throw ServerXpathCredentialSource.a((Exception)((Object)jaxenException));
                    }
                    bl2 = true;
                    break block21;
                }
                catch (JaxenException jaxenException) {
                    throw ServerXpathCredentialSource.a((Exception)((Object)jaxenException));
                }
            }
            bl2 = false;
        }
        serverXpathCredentialSource.g = bl2;
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        try {
            return (AssertionStatus)XpathVariableContext.doWithVariableFinder((XpathVariableFinder)new PolicyEnforcementContextXpathVariableFinder(policyEnforcementContext), (Callable)new e(this, policyEnforcementContext));
        }
        catch (IOException iOException) {
            throw iOException;
        }
        catch (PolicyAssertionException policyAssertionException) {
            throw policyAssertionException;
        }
        catch (AssertionStatusException assertionStatusException) {
            return assertionStatusException.getAssertionStatus();
        }
        catch (Exception exception) {
            throw new PolicyAssertionException(this.assertion, (Throwable)exception);
        }
    }

    protected AssertionStatus checkTargetRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        return super.checkRequest(policyEnforcementContext);
    }

    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        String string2;
        String string3;
        block11: {
            Document document = this.a(message);
            try {
                string3 = this.a(document, this.e, (AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_LOGIN_XPATH_NOT_FOUND, (AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_LOGIN_FOUND_MULTI, (AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_LOGIN_PARENT_NOT_ELEMENT, ((XpathCredentialSource)this.assertion).isRemoveLoginElement(), false);
            }
            catch (Exception exception) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_LOGIN_XPATH_FAILED, null, ExceptionUtils.getDebugException((Throwable)exception));
                return AssertionStatus.FAILED;
            }
            try {
                string2 = this.a(document, this.f, (AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_PASS_XPATH_NOT_FOUND, (AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_PASS_FOUND_MULTI, (AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_PASS_PARENT_NOT_ELEMENT, ((XpathCredentialSource)this.assertion).isRemovePasswordElement(), true);
            }
            catch (Exception exception) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_PASS_XPATH_FAILED, null, exception);
                return AssertionStatus.FAILED;
            }
            try {
                block10: {
                    try {
                        try {
                            if (string3 == null || string3.length() == 0) break block10;
                        }
                        catch (Exception exception) {
                            throw ServerXpathCredentialSource.a(exception);
                        }
                        if (string2 != null) break block11;
                    }
                    catch (Exception exception) {
                        throw ServerXpathCredentialSource.a(exception);
                    }
                }
                return AssertionStatus.AUTH_REQUIRED;
            }
            catch (Exception exception) {
                throw ServerXpathCredentialSource.a(exception);
            }
        }
        LoginCredentials loginCredentials = LoginCredentials.makeLoginCredentials((SecurityToken)new UsernamePasswordSecurityToken(SecurityTokenType.XPATH_CREDENTIALS, string3, string2.toCharArray()), XpathCredentialSource.class);
        policyEnforcementContext.getAuthenticationContext(message).addCredentials(loginCredentials);
        return AssertionStatus.NONE;
    }

    /*
     * Unable to fully structure code
     */
    private Document a(Message var1_1) throws IOException {
        block13: {
            block14: {
                block12: {
                    block11: {
                        if (((XpathCredentialSource)this.assertion).isRemoveLoginElement()) ** GOTO lbl8
                        try {
                            block15: {
                                if (!((XpathCredentialSource)this.assertion).isRemovePasswordElement()) break block11;
                                break block15;
                                catch (SAXException v0) {
                                    throw ServerXpathCredentialSource.a(v0);
                                }
                            }
                            v1 = true;
                            break block12;
                        }
                        catch (SAXException v2) {
                            throw ServerXpathCredentialSource.a(v2);
                        }
                    }
                    v1 = false;
                }
                var2_2 = v1;
                var3_3 = null;
                var4_4 = null;
                if (var1_1.isXml() && (var5_5 = (XmlKnob)var1_1.getKnob(XmlKnob.class)) != null) {
                    try {
                        var3_3 = var2_2 ? var5_5.getDocumentWritable() : var5_5.getDocumentReadOnly();
                    }
                    catch (SAXException var6_6) {
                        var4_4 = var6_6;
                    }
                }
                try {
                    try {
                        if (var3_3 != null) break block13;
                        if (!this.g) break block14;
                    }
                    catch (SAXException v3) {
                        throw ServerXpathCredentialSource.a(v3);
                    }
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.XPATHCREDENTIAL_REQUEST_NOT_XML, null, ExceptionUtils.getDebugException((Throwable)var4_4));
                    throw new AssertionStatusException(AssertionStatus.FAILED);
                }
                catch (SAXException v4) {
                    throw ServerXpathCredentialSource.a(v4);
                }
            }
            var3_3 = XmlUtil.createEmptyDocument();
        }
        return var3_3;
    }

    /*
     * Unable to fully structure code
     */
    private String a(Document var1_1, DOMXPath var2_2, AuditDetailMessage var3_3, AuditDetailMessage var4_4, AuditDetailMessage var5_5, boolean var6_6, boolean var7_7) throws JaxenException {
        block38: {
            block39: {
                block41: {
                    block40: {
                        block37: {
                            var8_8 = null;
                            var9_9 = null;
                            try {
                                if (var2_2 == null) {
                                    this.logAndAudit(var3_3);
                                    return null;
                                }
                            }
                            catch (JaxenException v0) {
                                throw ServerXpathCredentialSource.a((Exception)v0);
                            }
                            var10_10 = var2_2.selectNodes((Object)var1_1);
                            try {
                                try {
                                    if (var10_10 != null && var10_10.size() != 0) break block37;
                                }
                                catch (JaxenException v1) {
                                    throw ServerXpathCredentialSource.a((Exception)v1);
                                }
                                this.logAndAudit(var3_3);
                                return null;
                            }
                            catch (JaxenException v2) {
                                throw ServerXpathCredentialSource.a((Exception)v2);
                            }
                        }
                        try {
                            if (var10_10.size() > 1) {
                                this.logAndAudit(var4_4);
                                return null;
                            }
                        }
                        catch (JaxenException v3) {
                            throw ServerXpathCredentialSource.a((Exception)v3);
                        }
                        var11_11 = var10_10.get(0);
                        if (!(var11_11 instanceof Node)) break block40;
                        var12_12 = (Node)var11_11;
                        var13_13 = var12_12.getNodeType();
                        switch (var13_13) {
                            case 2: {
                                var8_8 = var12_12.getNodeValue();
                                var9_9 = var12_12;
                                break;
                            }
                            case 3: {
                                var8_8 = var12_12.getNodeValue();
                                var9_9 = var12_12;
                                break;
                            }
                            case 1: {
                                var14_16 = var12_12.getFirstChild();
                                while (true) {
                                    try {
                                        try {
                                            try {
                                                if (var14_16 == null) break;
                                                if (var14_16.getNodeType() == 3) {
                                                }
                                                ** GOTO lbl65
                                            }
                                            catch (JaxenException v4) {
                                                throw ServerXpathCredentialSource.a((Exception)v4);
                                            }
                                            if (var8_8 != null) {
                                            }
                                            ** GOTO lbl63
                                        }
                                        catch (JaxenException v5) {
                                            throw ServerXpathCredentialSource.a((Exception)v5);
                                        }
                                        this.logAndAudit(var4_4);
                                        return null;
                                    }
                                    catch (JaxenException v6) {
                                        throw ServerXpathCredentialSource.a((Exception)v6);
                                    }
lbl63:
                                    // 1 sources

                                    var8_8 = var14_16.getNodeValue();
                                    var9_9 = var14_16;
lbl65:
                                    // 2 sources

                                    var14_16 = var14_16.getNextSibling();
                                }
                                try {
                                    if (!var7_7 || var8_8 != null) break;
                                }
                                catch (JaxenException v7) {
                                    throw ServerXpathCredentialSource.a((Exception)v7);
                                }
                                var8_8 = "";
                            }
                        }
                        break block41;
                    }
                    if (var11_11 instanceof String) {
                        var8_8 = (String)var11_11;
                    }
                }
                try {
                    try {
                        try {
                            if (var8_8 == null || !var6_6) break block38;
                        }
                        catch (JaxenException v8) {
                            throw ServerXpathCredentialSource.a((Exception)v8);
                        }
                        if (var9_9 != null) break block39;
                    }
                    catch (JaxenException v9) {
                        throw ServerXpathCredentialSource.a((Exception)v9);
                    }
                    this.logAndAudit(var5_5);
                    break block38;
                }
                catch (JaxenException v10) {
                    throw ServerXpathCredentialSource.a((Exception)v10);
                }
            }
            if (var9_9 instanceof Attr) {
                var12_12 = (Attr)var9_9;
                var13_14 = var12_12.getOwnerElement();
                var13_14.removeAttributeNode((Attr)var12_12);
            } else {
                var12_12 = var9_9.getParentNode();
                if (var12_12.getNodeType() == 1) {
                    var13_15 = (Element)var12_12;
                    var13_15.getParentNode().removeChild(var13_15);
                } else {
                    this.logAndAudit(var5_5);
                }
            }
        }
        return var8_8;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[4];
                var3_1 = 0;
                var2_2 = "\u0011E}1}/\txGd7x(M [j$yf\b [y5b5\u00047E+w0\u0014Dl9\u007ff5\bJ\u007f81/\u001exE~<}}M9Xx5c2\u00047E+9bf\u00037E&6d(\u000e,Bd>p*";
                var4_3 = "\u0011E}1}/\txGd7x(M [j$yf\b [y5b5\u00047E+w0\u0014Dl9\u007ff5\bJ\u007f81/\u001exE~<}}M9Xx5c2\u00047E+9bf\u00037E&6d(\u000e,Bd>p*".length();
                var1_4 = 32;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerXpathCredentialSource.c(v0, ServerXpathCredentialSource.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = ",x@\f@\u00124EfW\u001e_\f?\u0017r\u0016\u0015\\\u001a$\r6S\u0015\\\t5\u0016e_\u0002B[w35wE\u001e[\u0014\"\u00016n=M\u000f8E\u007fEMB\u000e<\t-\u0016\f_\b5\u0017b_\u0002B[9\u00166X\u0002BV6\u0010xU\u0019E\u0014>\u0004z";
                    var4_3 = ",x@\f@\u00124EfW\u001e_\f?\u0017r\u0016\u0015\\\u001a$\r6S\u0015\\\t5\u0016e_\u0002B[w35wE\u001e[\u0014\"\u00016n=M\u000f8E\u007fEMB\u000e<\t-\u0016\f_\b5\u0017b_\u0002B[9\u00166X\u0002BV6\u0010xU\u0019E\u0014>\u0004z".length();
                    var1_4 = 35;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 114;
                        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++] = ServerXpathCredentialSource.c(v0, ServerXpathCredentialSource.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;
            }
        }
        ServerXpathCredentialSource.h = var5;
        ServerXpathCredentialSource.d = new XPathFunctionContext(false);
    }

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

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String c(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 -> 23;
                    case 1 -> 100;
                    case 2 -> 68;
                    case 3 -> 31;
                    case 4 -> 94;
                    case 5 -> 9;
                    default -> 34;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

