/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.XmlUtil
 *  com.l7tech.common.mime.ContentTypeHeader
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.MessageTargetable
 *  com.l7tech.policy.assertion.MessageTargetableSupport
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.xmlsec.BuildRstSoapRequest
 *  com.l7tech.policy.variable.NoSuchVariableException
 *  com.l7tech.policy.variable.Syntax
 *  com.l7tech.security.token.SecurityTokenType
 *  com.l7tech.security.wstrust.TokenServiceClient
 *  com.l7tech.security.wstrust.WsTrustConfig
 *  com.l7tech.security.wstrust.WsTrustConfigException
 *  com.l7tech.security.wstrust.WsTrustConfigFactory
 *  com.l7tech.util.Config
 *  com.l7tech.util.DomUtils
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.HexUtils
 *  com.l7tech.util.SoapConstants
 *  com.l7tech.util.TimeUnit
 *  com.l7tech.util.ValidatedConfig
 *  com.l7tech.xml.WsTrustRequestType
 *  com.l7tech.xml.soap.SoapVersion
 *  org.springframework.beans.factory.BeanFactory
 */
package com.l7tech.server.policy.assertion.xmlsec;

import com.l7tech.common.io.XmlUtil;
import com.l7tech.common.mime.ContentTypeHeader;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.MessageTargetable;
import com.l7tech.policy.assertion.MessageTargetableSupport;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.xmlsec.BuildRstSoapRequest;
import com.l7tech.policy.variable.NoSuchVariableException;
import com.l7tech.policy.variable.Syntax;
import com.l7tech.security.token.SecurityTokenType;
import com.l7tech.security.wstrust.TokenServiceClient;
import com.l7tech.security.wstrust.WsTrustConfig;
import com.l7tech.security.wstrust.WsTrustConfigException;
import com.l7tech.security.wstrust.WsTrustConfigFactory;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.server.secureconversation.SecureConversationSession;
import com.l7tech.util.Config;
import com.l7tech.util.DomUtils;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.HexUtils;
import com.l7tech.util.SoapConstants;
import com.l7tech.util.TimeUnit;
import com.l7tech.util.ValidatedConfig;
import com.l7tech.xml.WsTrustRequestType;
import com.l7tech.xml.soap.SoapVersion;
import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;
import org.springframework.beans.factory.BeanFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;

public class ServerBuildRstSoapRequest
extends AbstractServerAssertion<BuildRstSoapRequest> {
    private static final String d;
    private static final long e = 60000L;
    private static final long f = 86400000L;
    private final Config g;
    private final String[] h;
    private static final String[] i;

    public ServerBuildRstSoapRequest(BuildRstSoapRequest buildRstSoapRequest, BeanFactory beanFactory) {
        super(buildRstSoapRequest);
        this.g = ServerBuildRstSoapRequest.a((Config)beanFactory.getBean(i[6], Config.class), this.logger);
        this.h = buildRstSoapRequest.getVariablesUsed();
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        int n2;
        byte[] byArray;
        String string;
        String string2;
        Element element;
        WsTrustRequestType wsTrustRequestType;
        SecurityTokenType securityTokenType;
        TokenServiceClient tokenServiceClient;
        Element element2;
        Object object;
        Map<String, Object> map;
        long l2;
        byte[] byArray2;
        TokenServiceClient tokenServiceClient2;
        WsTrustConfig wsTrustConfig;
        WsTrustRequestType wsTrustRequestType2;
        SoapVersion soapVersion;
        SecurityTokenType securityTokenType2;
        block27: {
            block25: {
                block28: {
                    block26: {
                        long l3;
                        block24: {
                            block23: {
                                byte[] byArray3;
                                int n3;
                                securityTokenType2 = ((BuildRstSoapRequest)this.assertion).getTokenType();
                                soapVersion = ((BuildRstSoapRequest)this.assertion).getSoapVersion();
                                String string3 = ((BuildRstSoapRequest)this.assertion).getWsTrustNamespace();
                                wsTrustRequestType2 = ((BuildRstSoapRequest)this.assertion).getRequestType();
                                wsTrustConfig = this.a(string3);
                                tokenServiceClient2 = new TokenServiceClient(wsTrustConfig, null, soapVersion);
                                try {
                                    n3 = ((BuildRstSoapRequest)this.assertion).getKeySize() != null ? ((BuildRstSoapRequest)this.assertion).getKeySize() / 8 : 32;
                                }
                                catch (SAXException sAXException) {
                                    throw ServerBuildRstSoapRequest.a(sAXException);
                                }
                                int n4 = n3;
                                try {
                                    byArray3 = ((BuildRstSoapRequest)this.assertion).isIncludeClientEntropy() ? HexUtils.randomBytes((int)n4) : null;
                                }
                                catch (SAXException sAXException) {
                                    throw ServerBuildRstSoapRequest.a(sAXException);
                                }
                                byArray2 = byArray3;
                                try {
                                    if (((BuildRstSoapRequest)this.assertion).getLifetime() != null) break block23;
                                    l3 = 0L;
                                    break block24;
                                }
                                catch (SAXException sAXException) {
                                    throw ServerBuildRstSoapRequest.a(sAXException);
                                }
                            }
                            try {
                                l3 = ((BuildRstSoapRequest)this.assertion).getLifetime() == -1L ? this.g.getTimeUnitProperty(i[1], TimeUnit.HOURS.toMillis(2L)) : ((BuildRstSoapRequest)this.assertion).getLifetime().longValue();
                            }
                            catch (SAXException sAXException) {
                                throw ServerBuildRstSoapRequest.a(sAXException);
                            }
                        }
                        l2 = l3;
                        map = policyEnforcementContext.getVariableMap(this.h, this.getAudit());
                        if (((BuildRstSoapRequest)this.assertion).getTargetTokenVariable() == null) break block25;
                        object = ExpandVariables.processSingleVariableAsObject(Syntax.getVariableExpression((String)((BuildRstSoapRequest)this.assertion).getTargetTokenVariable()), map, this.getAudit());
                        if (!(object instanceof Element)) break block26;
                        element2 = (Element)object;
                        break block27;
                    }
                    if (!(object instanceof Document)) break block28;
                    element2 = ((Document)object).getDocumentElement();
                    break block27;
                }
                if (object instanceof SecureConversationSession) {
                    element2 = this.a((SecureConversationSession)object);
                    break block27;
                } else if (object instanceof String) {
                    try {
                        element2 = XmlUtil.parse((String)((String)object)).getDocumentElement();
                    }
                    catch (SAXException sAXException) {
                        String[] stringArray = new String[1];
                        String[] stringArray2 = i;
                        stringArray[0] = stringArray2[3] + ExceptionUtils.getMessage((Throwable)sAXException);
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.RST_BUILDER_ERROR, stringArray);
                        return AssertionStatus.FAILED;
                    }
                } else {
                    if (object != null) {
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.RST_BUILDER_ERROR, i[2] + String.valueOf(object.getClass()));
                        return AssertionStatus.FAILED;
                    }
                    element2 = null;
                }
                break block27;
            }
            element2 = null;
        }
        try {
            tokenServiceClient = tokenServiceClient2;
            securityTokenType = securityTokenType2;
            wsTrustRequestType = wsTrustRequestType2;
            element = element2;
            string2 = this.a(((BuildRstSoapRequest)this.assertion).getAppliesToAddress(), map);
            string = this.a(((BuildRstSoapRequest)this.assertion).getIssuerAddress(), map);
            byArray = byArray2;
            n2 = ((BuildRstSoapRequest)this.assertion).getKeySize() == null ? 0 : ((BuildRstSoapRequest)this.assertion).getKeySize();
        }
        catch (SAXException sAXException) {
            throw ServerBuildRstSoapRequest.a(sAXException);
        }
        object = tokenServiceClient.createRequestSecurityTokenMessage(securityTokenType, wsTrustRequestType, element, string2, string, byArray, n2, l2);
        try {
            ContentTypeHeader contentTypeHeader;
            Object object2;
            Message message;
            Message message2 = policyEnforcementContext.getOrCreateTargetMessage((MessageTargetable)new MessageTargetableSupport(this.b(BuildRstSoapRequest.VARIABLE_RST_REQUEST)), false);
            try {
                message = message2;
                object2 = object;
                contentTypeHeader = soapVersion == SoapVersion.SOAP_1_1 ? ContentTypeHeader.XML_DEFAULT : ContentTypeHeader.SOAP_1_2_DEFAULT;
            }
            catch (SAXException sAXException) {
                throw ServerBuildRstSoapRequest.a(sAXException);
            }
            message.initialize((Document)object2, contentTypeHeader);
            message2.getSecurityKnob().getOrMakeDecorationRequirements().getNamespaceFactory().setWsscNs(wsTrustConfig.getWsscNs());
        }
        catch (NoSuchVariableException noSuchVariableException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.RST_BUILDER_OUTPUT, noSuchVariableException.getVariable());
            return AssertionStatus.FAILED;
        }
        try {
            if (byArray2 == null) return AssertionStatus.NONE;
            policyEnforcementContext.setVariable(this.b(BuildRstSoapRequest.VARIABLE_CLIENT_ENTROPY), HexUtils.encodeBase64((byte[])byArray2));
            return AssertionStatus.NONE;
        }
        catch (SAXException sAXException) {
            throw ServerBuildRstSoapRequest.a(sAXException);
        }
    }

    private WsTrustConfig a(String string) {
        try {
            return WsTrustConfigFactory.getWsTrustConfigForNamespaceUri((String)string);
        }
        catch (WsTrustConfigException wsTrustConfigException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.RST_BUILDER_ERROR, ExceptionUtils.getMessage((Throwable)wsTrustConfigException));
            throw new AssertionStatusException(AssertionStatus.FAILED);
        }
    }

    private Element a(SecureConversationSession secureConversationSession) {
        Object object;
        Document document = XmlUtil.createEmptyDocument((String)SoapConstants.SECURITYTOKENREFERENCE_EL_NAME, (String)SoapConstants.SECURITY_NAMESPACE_PREFIX, (String)SoapConstants.SECURITY_NAMESPACE);
        Element element = document.getDocumentElement();
        Element element2 = DomUtils.createAndAppendElementNS((Node)element, (String)SoapConstants.REFERENCE_EL_NAME, (String)SoapConstants.SECURITY_NAMESPACE, (String)SoapConstants.SECURITY_NAMESPACE_PREFIX);
        String[] stringArray = i;
        element2.setAttributeNS(null, stringArray[4], secureConversationSession.getIdentifier());
        if (SoapConstants.WSSC_NAMESPACE.equals(secureConversationSession.getSCNamespace())) {
            object = SoapConstants.WSC_RST_SCT_TOKEN_TYPE;
        } else {
            stringArray = i;
            object = secureConversationSession.getSCNamespace() + stringArray[0];
        }
        element2.setAttributeNS(null, i[5], (String)object);
        return element;
    }

    private String a(String string, Map<String, Object> map) {
        String string2 = null;
        if (string != null) {
            string2 = ExpandVariables.process(string, map, this.getAudit());
        }
        return string2;
    }

    private String b(String string) {
        Object object = string;
        if (((BuildRstSoapRequest)this.assertion).getVariablePrefix() != null) {
            object = ((BuildRstSoapRequest)this.assertion).getVariablePrefix() + "." + string;
        }
        return object;
    }

    private static Config a(Config config, Logger logger) {
        ValidatedConfig validatedConfig = new ValidatedConfig(config, logger);
        String[] stringArray = i;
        validatedConfig.setMinimumValue(stringArray[7], (Number)60000L);
        validatedConfig.setMaximumValue(stringArray[7], (Number)86400000L);
        return validatedConfig;
    }

    private static SAXException a(SAXException sAXException) {
        return sAXException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[8];
                var3_1 = 0;
                var2_2 = "\u001d:k\u00142]<|\u0002v\u001a\u001bVg{\u0005z\u001a\u0007W\ng\u000eo\n\u0007A(|\tv\u0001[V,n\u0001l\u0003\u0001a,{\u0013p\u0000\u001bv<z\u0001m\u0006\u001a\\!g'{\u0015i\u001f\u001a@=m\u00049\u001b\u001aY,f@o\u000e\u0007[(j\f|O\u0001K9mZ9\u0015{'~\u0001u\u0006\u0011\u0012=g\u000b|\u0001UD(d\u0015|UU\u0003g\u001bA\td(d\u0015|;\fB,";
                var4_3 = "\u001d:k\u00142]<|\u0002v\u001a\u001bVg{\u0005z\u001a\u0007W\ng\u000eo\n\u0007A(|\tv\u0001[V,n\u0001l\u0003\u0001a,{\u0013p\u0000\u001bv<z\u0001m\u0006\u001a\\!g'{\u0015i\u001f\u001a@=m\u00049\u001b\u001aY,f@o\u000e\u0007[(j\f|O\u0001K9mZ9\u0015{'~\u0001u\u0006\u0011\u0012=g\u000b|\u0001UD(d\u0015|UU\u0003g\u001bA\td(d\u0015|;\fB,".length();
                var1_4 = 4;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerBuildRstSoapRequest.b(v0, ServerBuildRstSoapRequest.c(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0010}+G-Lg\fv?X/2\fm-S'KJ\u00076*T+KV\u0006[6_>[V\u0010y-X'P\n\u0007}?P=RP0}*B!QJ'm+P<WK\r";
                    var4_3 = "\u0010}+G-Lg\fv?X/2\fm-S'KJ\u00076*T+KV\u0006[6_>[V\u0010y-X'P\n\u0007}?P=RP0}*B!QJ'm+P<WK\r".length();
                    var1_4 = 12;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 86;
                        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++] = ServerBuildRstSoapRequest.b(v0, ServerBuildRstSoapRequest.c(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;
            }
        }
        ServerBuildRstSoapRequest.i = var5;
        ServerBuildRstSoapRequest.d = ServerBuildRstSoapRequest.i[7];
    }

    private static char[] c(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x72);
        }
        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 -> 53;
                    case 1 -> 78;
                    case 2 -> 15;
                    case 3 -> 103;
                    case 4 -> 30;
                    case 5 -> 104;
                    default -> 114;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

