/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.GenericHttpClient
 *  com.l7tech.common.http.GenericHttpClientFactory
 *  com.l7tech.common.http.GenericHttpRequestParams
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.SecurityKnob
 *  com.l7tech.message.XmlKnob
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.WsFederationPassiveTokenExchange
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.XmlSecurityToken
 *  com.l7tech.security.wsfederation.FederationPassiveClient
 *  com.l7tech.security.wsfederation.InvalidHtmlException
 *  com.l7tech.security.wsfederation.InvalidTokenException
 *  com.l7tech.security.wsfederation.ResponseStatusException
 *  com.l7tech.security.xml.processor.ProcessorResult
 *  com.l7tech.xml.saml.SamlAssertion
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion.credential;

import com.l7tech.common.http.GenericHttpClient;
import com.l7tech.common.http.GenericHttpClientFactory;
import com.l7tech.common.http.GenericHttpRequestParams;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.SecurityKnob;
import com.l7tech.message.XmlKnob;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.credential.WsFederationPassiveTokenExchange;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.XmlSecurityToken;
import com.l7tech.security.wsfederation.FederationPassiveClient;
import com.l7tech.security.wsfederation.InvalidHtmlException;
import com.l7tech.security.wsfederation.InvalidTokenException;
import com.l7tech.security.wsfederation.ResponseStatusException;
import com.l7tech.security.xml.processor.ProcessorResult;
import com.l7tech.server.message.PolicyContextCache;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.credential.AbstractServerWsFederationPassiveRequestProfile;
import com.l7tech.xml.saml.SamlAssertion;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ServerWsFederationPassiveTokenExchange
extends AbstractServerWsFederationPassiveRequestProfile<WsFederationPassiveTokenExchange> {
    private static final String j;
    private final GenericHttpClient k;
    private final URL l;
    private static final String[] o;

    /*
     * Unable to fully structure code
     */
    public ServerWsFederationPassiveTokenExchange(WsFederationPassiveTokenExchange var1_1, ApplicationContext var2_2) {
        block11: {
            super(var1_1, ServerWsFederationPassiveTokenExchange.j, var2_2);
            var4_3 = ServerWsFederationPassiveTokenExchange.o;
            this.k = ((GenericHttpClientFactory)var2_2.getBean(var4_3[2], GenericHttpClientFactory.class)).createHttpClient();
            try {
                block9: {
                    block10: {
                        if (var1_1.getIpStsUrl() == null) ** GOTO lbl8
                        if (var1_1.getIpStsUrl().length() != 0) break block9;
lbl8:
                        // 2 sources

                        try {
                            block12: {
                                if (this.a(var1_1)) break block10;
                                break block12;
                                catch (MalformedURLException v0) {
                                    throw ServerWsFederationPassiveTokenExchange.b(v0);
                                }
                            }
                            this.logger.warning(ServerWsFederationPassiveTokenExchange.o[3]);
                        }
                        catch (MalformedURLException v1) {
                            throw ServerWsFederationPassiveTokenExchange.b(v1);
                        }
                    }
                    this.l = null;
                    break block11;
                }
                try {
                    this.l = new URL(var1_1.getIpStsUrl());
                    if (this.l.getQuery() != null) {
                        throw new IllegalArgumentException(ServerWsFederationPassiveTokenExchange.o[1]);
                    }
                }
                catch (MalformedURLException v2) {
                    throw ServerWsFederationPassiveTokenExchange.b(v2);
                }
            }
            catch (MalformedURLException var3_4) {
                throw new IllegalArgumentException(ServerWsFederationPassiveTokenExchange.o[5], var3_4);
            }
        }
    }

    /*
     * Loose catch block
     */
    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        AssertionStatus assertionStatus = AssertionStatus.FAILED;
        try {
            block7: {
                if (this.l != null) break block7;
                try {
                    block8: {
                        if (this.a((WsFederationPassiveTokenExchange)this.assertion)) break block7;
                        break block8;
                        catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
                            throw ServerWsFederationPassiveTokenExchange.b(authRequiredException);
                        }
                    }
                    throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_CONFIG_INVALID);
                }
                catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
                    throw ServerWsFederationPassiveTokenExchange.b(authRequiredException);
                }
            }
            assertionStatus = this.b(policyEnforcementContext);
        }
        catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
            assertionStatus = AssertionStatus.AUTH_REQUIRED;
        }
        catch (AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException stopAndAuditException) {
            this.logAndAudit((AuditDetailMessage)stopAndAuditException.getAssertionMessage(), null, stopAndAuditException.getCause());
        }
        return assertionStatus;
    }

    private boolean a(WsFederationPassiveTokenExchange wsFederationPassiveTokenExchange) {
        boolean bl2;
        block5: {
            block4: {
                try {
                    try {
                        if (!wsFederationPassiveTokenExchange.isAuthenticate() || wsFederationPassiveTokenExchange.getReplyUrl() == null) break block4;
                    }
                    catch (IllegalArgumentException illegalArgumentException) {
                        throw ServerWsFederationPassiveTokenExchange.b(illegalArgumentException);
                    }
                    bl2 = true;
                    break block5;
                }
                catch (IllegalArgumentException illegalArgumentException) {
                    throw ServerWsFederationPassiveTokenExchange.b(illegalArgumentException);
                }
            }
            bl2 = false;
        }
        return bl2;
    }

    private SamlAssertion a(SecurityKnob securityKnob) throws AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
        SamlAssertion samlAssertion = null;
        ProcessorResult processorResult = securityKnob.getProcessorResult();
        if (processorResult != null) {
            XmlSecurityToken[] xmlSecurityTokenArray;
            for (XmlSecurityToken xmlSecurityToken : xmlSecurityTokenArray = processorResult.getXmlSecurityTokens()) {
                block4: {
                    try {
                        if (!(xmlSecurityToken instanceof SamlAssertion)) continue;
                        if (samlAssertion != null) break block4;
                    }
                    catch (AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException stopAndAuditException) {
                        throw ServerWsFederationPassiveTokenExchange.b(stopAndAuditException);
                    }
                    samlAssertion = (SamlAssertion)xmlSecurityToken;
                    continue;
                }
                throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_MULTI_TOKENS);
            }
        }
        return samlAssertion;
    }

    private SamlAssertion a(SamlAssertion samlAssertion) throws AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException, AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
        SamlAssertion samlAssertion2;
        try {
            GenericHttpRequestParams genericHttpRequestParams = new GenericHttpRequestParams(this.l);
            XmlSecurityToken xmlSecurityToken = FederationPassiveClient.exchangeFederationToken((GenericHttpClient)this.k, (GenericHttpRequestParams)genericHttpRequestParams, (XmlSecurityToken)samlAssertion, (String)((WsFederationPassiveTokenExchange)this.assertion).getContext(), (boolean)false);
            if (!(xmlSecurityToken instanceof SamlAssertion)) {
                throw new InvalidTokenException(o[4]);
            }
            samlAssertion2 = (SamlAssertion)xmlSecurityToken;
        }
        catch (ResponseStatusException responseStatusException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSFEDPASS_RSTR_STATUS_NON_200);
            throw new AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException();
        }
        catch (InvalidHtmlException invalidHtmlException) {
            throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_SERVER_HTML_INVALID, this.causeOrSelf(invalidHtmlException));
        }
        catch (InvalidTokenException invalidTokenException) {
            throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_RSTR_BAD_TYPE, this.causeOrSelf(invalidTokenException));
        }
        catch (IOException iOException) {
            throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_SERVER_HTTP_FAILED, this.causeOrSelf(iOException));
        }
        return samlAssertion2;
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, SamlAssertion samlAssertion, boolean bl2) throws AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException, AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
        this.doAuth(policyEnforcementContext, this.k, samlAssertion, ((WsFederationPassiveTokenExchange)this.assertion).getReplyUrl(), ((WsFederationPassiveTokenExchange)this.assertion).getContext(), bl2);
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, XmlKnob xmlKnob, SecurityKnob securityKnob, SamlAssertion samlAssertion, SamlAssertion samlAssertion2) throws AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
        try {
            Document document = xmlKnob.getDocumentWritable();
            Element element = samlAssertion.asElement();
            Node node = element.getParentNode();
            try {
                node.removeChild(element);
                if (node.getFirstChild() == null) {
                    node.getParentNode().removeChild(node);
                }
            }
            catch (Exception exception) {
                throw ServerWsFederationPassiveTokenExchange.b(exception);
            }
            this.updateRequestXml(policyEnforcementContext, xmlKnob, securityKnob, document, samlAssertion2, this.assertion);
        }
        catch (Exception exception) {
            throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_DECORATION_FAILED, (Throwable)exception);
        }
    }

    private AssertionStatus b(PolicyEnforcementContext policyEnforcementContext) throws AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException, AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
        SamlAssertion samlAssertion;
        XmlKnob xmlKnob = (XmlKnob)policyEnforcementContext.getRequest().getKnob(XmlKnob.class);
        this.ensureXmlRequest(xmlKnob);
        SecurityKnob securityKnob = (SecurityKnob)policyEnforcementContext.getRequest().getKnob(SecurityKnob.class);
        this.ensureSecurityKnob(securityKnob);
        SamlAssertion samlAssertion2 = this.a(securityKnob);
        try {
            if (samlAssertion2 == null) {
                throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_NO_SUITABLE_CREDENTIALS);
            }
        }
        catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
            throw ServerWsFederationPassiveTokenExchange.b(authRequiredException);
        }
        boolean bl2 = true;
        if (this.l != null) {
            PolicyContextCache policyContextCache = policyEnforcementContext.getCache();
            samlAssertion = (SamlAssertion)this.getCachedSecurityToken(policyContextCache);
            if (samlAssertion == null) {
                bl2 = false;
                samlAssertion = this.a(samlAssertion2);
                this.setCachedSecurityToken(policyContextCache, (SecurityToken)samlAssertion, this.getSamlAssertionExpiry(samlAssertion));
            } else {
                try {
                    if (this.logger.isLoggable(Level.FINE)) {
                        this.logger.fine(o[6]);
                    }
                }
                catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
                    throw ServerWsFederationPassiveTokenExchange.b(authRequiredException);
                }
            }
            this.a(policyEnforcementContext, xmlKnob, securityKnob, samlAssertion2, samlAssertion);
            this.addCacheInvalidator(policyEnforcementContext);
        } else {
            samlAssertion = samlAssertion2;
        }
        try {
            if (((WsFederationPassiveTokenExchange)this.assertion).isAuthenticate()) {
                this.a(policyEnforcementContext, samlAssertion, bl2);
            }
        }
        catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
            throw ServerWsFederationPassiveTokenExchange.b(authRequiredException);
        }
        this.addResponseAuthFailureDecorator(policyEnforcementContext);
        return AssertionStatus.NONE;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[7];
                var3_1 = 0;
                var2_2 = ",k1)\u000f\u001dKh_7\u0017Y\tWj<D+kZ\"YP\u00156o[{\u0018\u0003\u00101cGe\u0018cV\u001f\n\u0016xEJL\u0004\u0014\u0000f@gV\u0004\"\"i]mJ\t,LM\u001c\bcCy-k$7c_{N\u0014P\u00050yLpL\u0019\u000b-*@q\u0018\u001e\u000b-'OwV\u0013\u0010*eGcT\u0016WV\u0003\u00113zFpL\u0015\u0000c~Fi]\u001eD7sYg";
                var4_3 = ",k1)\u000f\u001dKh_7\u0017Y\tWj<D+kZ\"YP\u00156o[{\u0018\u0003\u00101cGe\u0018cV\u001f\n\u0016xEJL\u0004\u0014\u0000f@gV\u0004\"\"i]mJ\t,LM\u001c\bcCy-k$7c_{N\u0014P\u00050yLpL\u0019\u000b-*@q\u0018\u001e\u000b-'OwV\u0013\u0010*eGcT\u0016WV\u0003\u00113zFpL\u0015\u0000c~Fi]\u001eD7sYg".length();
                var1_4 = 5;
                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++] = ServerWsFederationPassiveTokenExchange.d(v0, ServerWsFederationPassiveTokenExchange.c(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "GF\u0001\u0016?\u007f\u0019fG@\u00042hJw\b)$|ImA\b5&\u001f\u001bG[\t\u001a4:ZsK\b\u00117:jSe,T2iJwZ\u0014\u001d<t";
                    var4_3 = "GF\u0001\u0016?\u007f\u0019fG@\u00042hJw\b)$|ImA\b5&\u001f\u001bG[\t\u001a4:ZsK\b\u00117:jSe,T2iJwZ\u0014\u001d<t".length();
                    var1_4 = 26;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 23;
                        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++] = ServerWsFederationPassiveTokenExchange.d(v0, ServerWsFederationPassiveTokenExchange.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;
            }
        }
        ServerWsFederationPassiveTokenExchange.o = var5;
        ServerWsFederationPassiveTokenExchange.j = ServerWsFederationPassiveTokenExchange.class.getName() + ServerWsFederationPassiveTokenExchange.o[0];
    }

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

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String d(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 -> 5;
                    case 1 -> 63;
                    case 2 -> 119;
                    case 3 -> 99;
                    case 4 -> 68;
                    case 5 -> 13;
                    default -> 46;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

