/*
 * 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.gateway.common.service.PublishedService
 *  com.l7tech.identity.User
 *  com.l7tech.message.HttpRequestKnob
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.xmlsec.SecureConversation
 *  com.l7tech.policy.assertion.xmlsec.SecurityHeaderAddressableSupport
 *  com.l7tech.security.token.SecurityContextToken
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.XmlSecurityToken
 *  com.l7tech.security.xml.SecurityTokenResolver
 *  com.l7tech.security.xml.processor.ProcessorResult
 *  com.l7tech.security.xml.processor.SecurityContextFinder
 *  com.l7tech.util.CausedIOException
 *  com.l7tech.util.Config
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.InvalidDocumentFormatException
 *  com.l7tech.xml.SoapFaultLevel
 *  com.l7tech.xml.soap.SoapFaultUtils
 *  com.l7tech.xml.soap.SoapUtil
 *  com.l7tech.xml.soap.SoapVersion
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion.xmlsec;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.identity.User;
import com.l7tech.message.HttpRequestKnob;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.xmlsec.SecureConversation;
import com.l7tech.policy.assertion.xmlsec.SecurityHeaderAddressableSupport;
import com.l7tech.security.token.SecurityContextToken;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.XmlSecurityToken;
import com.l7tech.security.xml.SecurityTokenResolver;
import com.l7tech.security.xml.processor.ProcessorResult;
import com.l7tech.security.xml.processor.SecurityContextFinder;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.server.policy.assertion.ServerAssertion;
import com.l7tech.server.policy.assertion.xmlsec.c;
import com.l7tech.server.policy.assertion.xmlsec.v;
import com.l7tech.server.secureconversation.InboundSecureConversationContextManager;
import com.l7tech.server.secureconversation.SecureConversationSession;
import com.l7tech.server.util.WSSecurityProcessorUtils;
import com.l7tech.util.CausedIOException;
import com.l7tech.util.Config;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.InvalidDocumentFormatException;
import com.l7tech.xml.SoapFaultLevel;
import com.l7tech.xml.soap.SoapFaultUtils;
import com.l7tech.xml.soap.SoapUtil;
import com.l7tech.xml.soap.SoapVersion;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

public class ServerSecureConversation
extends AbstractServerAssertion<SecureConversation> {
    private final Config d;
    private final SecurityTokenResolver e;
    private final SecurityContextFinder f;
    private final InboundSecureConversationContextManager g;
    private static final String[] h;

    public ServerSecureConversation(SecureConversation secureConversation, ApplicationContext applicationContext) {
        super(secureConversation);
        String[] stringArray = h;
        this.d = (Config)applicationContext.getBean(stringArray[2], Config.class);
        this.e = (SecurityTokenResolver)applicationContext.getBean(stringArray[5], SecurityTokenResolver.class);
        this.f = (SecurityContextFinder)applicationContext.getBean(stringArray[0], SecurityContextFinder.class);
        this.g = (InboundSecureConversationContextManager)applicationContext.getBean(stringArray[1], InboundSecureConversationContextManager.class);
    }

    private String a(PolicyEnforcementContext policyEnforcementContext) {
        HttpRequestKnob httpRequestKnob = (HttpRequestKnob)policyEnforcementContext.getRequest().getKnob(HttpRequestKnob.class);
        if (httpRequestKnob == null) {
            this.logger.warning(h[4]);
            return "";
        }
        return httpRequestKnob.getQueryString() == null ? httpRequestKnob.getRequestUrl() : httpRequestKnob.getRequestUrl() + "?" + httpRequestKnob.getQueryString();
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        XmlSecurityToken[] xmlSecurityTokenArray;
        ProcessorResult processorResult;
        String[] stringArray;
        try {
            if (!SecurityHeaderAddressableSupport.isLocalRecipient((Assertion)this.assertion)) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUESTWSS_NOT_FOR_US);
                return AssertionStatus.NONE;
            }
        }
        catch (SAXException sAXException) {
            throw ServerSecureConversation.a(sAXException);
        }
        try {
            if (!policyEnforcementContext.getRequest().isSoap()) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.SC_REQUEST_NOT_SOAP);
                return AssertionStatus.NOT_APPLICABLE;
            }
        }
        catch (InvalidDocumentFormatException invalidDocumentFormatException) {
            throw ServerSecureConversation.a((Exception)((Object)invalidDocumentFormatException));
        }
        try {
            stringArray = h;
            if (!this.d.getBooleanProperty(stringArray[8], true)) {
                processorResult = policyEnforcementContext.getRequest().getSecurityKnob().getProcessorResult();
            } else {
                stringArray = h;
                processorResult = WSSecurityProcessorUtils.getWssResults(policyEnforcementContext.getRequest(), stringArray[6], this.e, this.f, this.getAudit(), new v(this, policyEnforcementContext));
            }
        }
        catch (SAXException sAXException) {
            throw new CausedIOException((Throwable)sAXException);
        }
        try {
            if (processorResult == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.REQUESTWSS_NO_SECURITY);
                policyEnforcementContext.setAuthenticationMissing();
                policyEnforcementContext.setRequestPolicyViolated();
                return AssertionStatus.FALSIFIED;
            }
        }
        catch (SAXException sAXException) {
            throw ServerSecureConversation.a(sAXException);
        }
        for (XmlSecurityToken xmlSecurityToken : xmlSecurityTokenArray = processorResult.getXmlSecurityTokens()) {
            if (!(xmlSecurityToken instanceof SecurityContextToken)) continue;
            SecurityContextToken securityContextToken = (SecurityContextToken)xmlSecurityToken;
            try {
                if (!securityContextToken.isPossessionProved()) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.SC_NO_PROOF_OF_POSSESSION);
                    continue;
                }
            }
            catch (SAXException sAXException) {
                throw ServerSecureConversation.a(sAXException);
            }
            String string = securityContextToken.getContextIdentifier();
            SecureConversationSession secureConversationSession = this.g.getSession(string);
            try {
                if (secureConversationSession == null) {
                    this.a(policyEnforcementContext, securityContextToken.getWsscNamespace());
                    return AssertionStatus.AUTH_FAILED;
                }
            }
            catch (SAXException sAXException) {
                throw ServerSecureConversation.a(sAXException);
            }
            User user = secureConversationSession.getUsedBy();
            AuthenticationContext authenticationContext = policyEnforcementContext.getAuthenticationContext(policyEnforcementContext.getRequest());
            LoginCredentials loginCredentials = LoginCredentials.makeLoginCredentials((SecurityToken)secureConversationSession.getCredentialSecurityToken(), (boolean)false, SecureConversation.class, (SecurityToken[])new SecurityToken[]{securityContextToken});
            authenticationContext.addCredentials(loginCredentials);
            policyEnforcementContext.addDeferredAssertion(this, this.a(secureConversationSession));
            this.logAndAudit((AuditDetailMessage)AssertionMessages.SC_SESSION_FOR_USER, user.getLogin());
            try {
                String string2;
                PolicyEnforcementContext policyEnforcementContext2;
                String string3 = SoapUtil.getL7aMessageId((Document)policyEnforcementContext.getRequest().getXmlKnob().getDocumentReadOnly());
                try {
                    policyEnforcementContext2 = policyEnforcementContext;
                    string2 = string3 == null ? "" : string3;
                }
                catch (SAXException sAXException) {
                    throw ServerSecureConversation.a(sAXException);
                }
                policyEnforcementContext2.setSavedRequestL7aMessageId(string2);
            }
            catch (InvalidDocumentFormatException invalidDocumentFormatException) {
                String[] stringArray2 = new String[1];
                stringArray = h;
                stringArray2[0] = stringArray[7] + ExceptionUtils.getMessage((Throwable)invalidDocumentFormatException);
                this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_INFO_WITH_MORE_INFO, stringArray2);
                return AssertionStatus.BAD_REQUEST;
            }
            catch (SAXException sAXException) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_INFO_WITH_MORE_INFO, h[3] + ExceptionUtils.getMessage((Throwable)sAXException));
                return AssertionStatus.BAD_REQUEST;
            }
            policyEnforcementContext.setVariable(SecureConversation.VARIABLE_INBOUND_SC_SESSION, secureConversationSession);
            return AssertionStatus.NONE;
        }
        this.logAndAudit((AuditDetailMessage)AssertionMessages.SC_REQUEST_NOT_REFER_TO_SC_TOKEN);
        policyEnforcementContext.setAuthenticationMissing();
        policyEnforcementContext.setRequestPolicyViolated();
        return AssertionStatus.AUTH_REQUIRED;
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, String string) {
        this.logAndAudit((AuditDetailMessage)AssertionMessages.SC_TOKEN_INVALID);
        policyEnforcementContext.setRequestPolicyViolated();
        SoapFaultLevel soapFaultLevel = new SoapFaultLevel();
        soapFaultLevel.setLevel(1);
        soapFaultLevel.setFaultTemplate(SoapFaultUtils.badContextTokenFault((SoapVersion)ServerSecureConversation.b(policyEnforcementContext), (String)string, (String)this.a(policyEnforcementContext)));
        policyEnforcementContext.setFaultlevel(soapFaultLevel);
    }

    private static SoapVersion b(PolicyEnforcementContext policyEnforcementContext) {
        PublishedService publishedService = policyEnforcementContext.getService();
        return publishedService != null ? publishedService.getSoapVersion() : SoapVersion.getDefaultSoapVersion();
    }

    private ServerAssertion a(SecureConversationSession secureConversationSession) {
        return new c(this, (SecureConversation)this.assertion, secureConversationSession);
    }

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

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[9];
                var3_1 = 0;
                var2_2 = "-\u000evvaGL'(zmgK@*-|mwKJ'7\u0005wlf@\\\r\u000evvaK{1\u0005cfa]Y*\u0002zmPAV*\u000emw^OV?\fpq\f-\u000eguv\\{1\u0005sjt\u0014\u0017\u0005cb\u007fG\\~\u0019prfKK*KMN_\u0014\u001c=\n{m|Z\u00189\u000e{faOL;K|mpAU7\u0005r#F|t\u0015-\u000evvaGL'?zhv@j;\u0018zoeKJ\u0007\f\u000edvv]L";
                var4_3 = "-\u000evvaGL'(zmgK@*-|mwKJ'7\u0005wlf@\\\r\u000evvaK{1\u0005cfa]Y*\u0002zmPAV*\u000emw^OV?\fpq\f-\u000eguv\\{1\u0005sjt\u0014\u0017\u0005cb\u007fG\\~\u0019prfKK*KMN_\u0014\u001c=\n{m|Z\u00189\u000e{faOL;K|mpAU7\u0005r#F|t\u0015-\u000evvaGL'?zhv@j;\u0018zoeKJ\u0007\f\u000edvv]L".length();
                var1_4 = 21;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerSecureConversation.b(v0, ServerSecureConversation.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "]O)(5\r\u00164S:8,\u0001\u0001`\u0001\f\u0006\u00184H-cR,g)\u0016\u001dwD,:6\u0016\\qO>+5\u00016qG:;+\u0001\u0016FD.<<\u0017\u0006DS0*<\u0017\u0001}O8";
                    var4_3 = "]O)(5\r\u00164S:8,\u0001\u0001`\u0001\f\u0006\u00184H-cR,g)\u0016\u001dwD,:6\u0016\\qO>+5\u00016qG:;+\u0001\u0016FD.<<\u0017\u0006DS0*<\u0017\u0001}O8".length();
                    var1_4 = 21;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 98;
                        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++] = ServerSecureConversation.b(v0, ServerSecureConversation.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;
            }
        }
        ServerSecureConversation.h = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x10);
        }
        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 -> 118;
                    case 1 -> 67;
                    case 2 -> 61;
                    case 3 -> 43;
                    case 4 -> 59;
                    case 5 -> 6;
                    default -> 16;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

