/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  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.PolicyAssertionException
 *  com.l7tech.policy.assertion.xmlsec.ProcessRstrSoapResponse
 *  com.l7tech.security.token.SecurityTokenType
 *  com.l7tech.security.wstrust.RstrInfo
 *  com.l7tech.security.xml.SecurityTokenResolver
 *  com.l7tech.util.ArrayUtils
 *  com.l7tech.util.DomUtils
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.InvalidDocumentFormatException
 *  com.l7tech.util.SoapConstants
 *  com.l7tech.util.TooManyChildElementsException
 *  com.l7tech.xml.soap.SoapUtil
 *  org.springframework.beans.factory.BeanFactory
 */
package com.l7tech.server.policy.assertion.xmlsec;

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.PolicyAssertionException;
import com.l7tech.policy.assertion.xmlsec.ProcessRstrSoapResponse;
import com.l7tech.security.token.SecurityTokenType;
import com.l7tech.security.wstrust.RstrInfo;
import com.l7tech.security.xml.SecurityTokenResolver;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.util.ArrayUtils;
import com.l7tech.util.DomUtils;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.InvalidDocumentFormatException;
import com.l7tech.util.SoapConstants;
import com.l7tech.util.TooManyChildElementsException;
import com.l7tech.xml.soap.SoapUtil;
import java.io.IOException;
import javax.xml.namespace.QName;
import org.springframework.beans.factory.BeanFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class ServerProcessRstrSoapResponse
extends AbstractMessageTargetableServerAssertion<ProcessRstrSoapResponse> {
    private final SecurityTokenResolver d;
    private static final String[] e;

    public ServerProcessRstrSoapResponse(ProcessRstrSoapResponse processRstrSoapResponse, BeanFactory beanFactory) {
        super(processRstrSoapResponse);
        this.d = (SecurityTokenResolver)beanFactory.getBean(e[0], SecurityTokenResolver.class);
    }

    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        RstrInfo rstrInfo;
        Element element;
        try {
            if (!message.isSoap()) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.RSTR_PROCESSOR_NOT_SOAP, string);
                return AssertionStatus.NOT_APPLICABLE;
            }
        }
        catch (IOException iOException) {
            throw ServerProcessRstrSoapResponse.a(iOException);
        }
        try {
            element = SoapUtil.getBodyElement((Document)message.getXmlKnob().getDocumentReadOnly());
        }
        catch (SAXException sAXException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.RSTR_PROCESSOR_BAD_XML, string, ExceptionUtils.getMessage((Throwable)sAXException));
            return AssertionStatus.FALSIFIED;
        }
        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.RSTR_PROCESSOR_NOT_SOAP, string);
            return AssertionStatus.NOT_APPLICABLE;
        }
        try {
            Element element2 = DomUtils.findOnlyOneChildElement((Element)element);
            rstrInfo = RstrInfo.parse((Element)element2, (SecurityTokenResolver)this.d);
        }
        catch (TooManyChildElementsException tooManyChildElementsException) {
            String[] stringArray = new String[1];
            String[] stringArray2 = e;
            stringArray[0] = stringArray2[2];
            this.logAndAudit((AuditDetailMessage)AssertionMessages.RSTR_PROCESSOR_INVALID, stringArray);
            return AssertionStatus.FALSIFIED;
        }
        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.RSTR_PROCESSOR_INVALID, ExceptionUtils.getMessage((Throwable)invalidDocumentFormatException));
            return AssertionStatus.FALSIFIED;
        }
        try {
            if (rstrInfo.getToken() == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.RSTR_PROCESSOR_INVALID, e[1]);
                return AssertionStatus.FALSIFIED;
            }
        }
        catch (SAXException sAXException) {
            throw ServerProcessRstrSoapResponse.a(sAXException);
        }
        try {
            if (!this.a(rstrInfo.getToken(), ((ProcessRstrSoapResponse)this.assertion).getTokenType())) {
                return AssertionStatus.FALSIFIED;
            }
        }
        catch (SAXException sAXException) {
            throw ServerProcessRstrSoapResponse.a(sAXException);
        }
        policyEnforcementContext.setVariable(this.a(ProcessRstrSoapResponse.VARIABLE_TOKEN), rstrInfo.getToken());
        policyEnforcementContext.setVariable(this.a(ProcessRstrSoapResponse.VARIABLE_CREATE_TIME), rstrInfo.getCreatedText());
        policyEnforcementContext.setVariable(this.a(ProcessRstrSoapResponse.VARIABLE_EXPIRY_TIME), rstrInfo.getExpiresText());
        policyEnforcementContext.setVariable(this.a(ProcessRstrSoapResponse.VARIABLE_SERVER_ENTROPY), rstrInfo.getEntropyBase64());
        policyEnforcementContext.setVariable(this.a(ProcessRstrSoapResponse.VARIABLE_KEY_SIZE), rstrInfo.getKeySize());
        policyEnforcementContext.setVariable(this.a(ProcessRstrSoapResponse.VARIABLE_FULL_KEY), rstrInfo.getSecretBase64());
        return AssertionStatus.NONE;
    }

    private boolean a(Element element, SecurityTokenType securityTokenType) {
        boolean bl2 = false;
        if (securityTokenType == SecurityTokenType.SAML_ASSERTION && !SoapConstants.SAML_NAMESPACE.equals(element.getNamespaceURI()) || securityTokenType == SecurityTokenType.SAML2_ASSERTION && !SoapConstants.SAML_NAMESPACE2.equals(element.getNamespaceURI()) || securityTokenType == SecurityTokenType.WSSC_CONTEXT && !ArrayUtils.contains((Object[])SoapConstants.WSSC_NAMESPACE_ARRAY, (Object)element.getNamespaceURI())) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.RSTR_PROCESSOR_UNEXPECTED_TOKEN, securityTokenType.getName(), this.a(element));
        } else {
            bl2 = true;
        }
        return bl2;
    }

    private String a(Element element) {
        return new QName(element.getNamespaceURI(), element.getLocalName()).toString();
    }

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

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

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[3];
        int n2 = 0;
        String string = "A*'\n\u007f%{K\u001b+\u0014h\"]W<+\u0013{)}\u0014\\ d\u000bb'j\\o-\u0011->jA?+\u0011~)\u0013\\ 0_l\"/`\u001c\u0010--!jA<%\u0018h";
        int n3 = "A*'\n\u007f%{K\u001b+\u0014h\"]W<+\u0013{)}\u0014\\ d\u000bb'j\\o-\u0011->jA?+\u0011~)\u0013\\ 0_l\"/`\u001c\u0010--!jA<%\u0018h".length();
        int n4 = 21;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = ServerProcessRstrSoapResponse.c(47, ServerProcessRstrSoapResponse.b(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                e = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x20);
        }
        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 -> 29;
                    case 1 -> 96;
                    case 2 -> 107;
                    case 3 -> 80;
                    case 4 -> 34;
                    case 5 -> 99;
                    default -> 32;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

