/*
 * 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.CredentialFormat
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.credential.WsFederationPassiveTokenRequest
 *  com.l7tech.security.token.HasUsernameAndPassword
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.UsernameToken
 *  com.l7tech.security.token.UsernameTokenImpl
 *  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.CredentialFormat;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.credential.WsFederationPassiveTokenRequest;
import com.l7tech.security.token.HasUsernameAndPassword;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.UsernameToken;
import com.l7tech.security.token.UsernameTokenImpl;
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.AuthenticationContext;
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.PasswordAuthentication;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

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

    public ServerWsFederationPassiveTokenRequest(WsFederationPassiveTokenRequest wsFederationPassiveTokenRequest, ApplicationContext applicationContext) {
        block6: {
            super(wsFederationPassiveTokenRequest, j, applicationContext);
            String[] stringArray = o;
            this.k = ((GenericHttpClientFactory)applicationContext.getBean(stringArray[7], GenericHttpClientFactory.class)).createHttpClient();
            try {
                if (wsFederationPassiveTokenRequest.getIpStsUrl() == null) {
                    this.logger.warning(stringArray[0]);
                    this.l = null;
                    break block6;
                }
                try {
                    this.l = new URL(wsFederationPassiveTokenRequest.getIpStsUrl());
                    if (this.l.getQuery() != null) {
                        throw new IllegalArgumentException(o[3]);
                    }
                }
                catch (MalformedURLException malformedURLException) {
                    throw ServerWsFederationPassiveTokenRequest.b(malformedURLException);
                }
            }
            catch (MalformedURLException malformedURLException) {
                throw new IllegalArgumentException(o[11], malformedURLException);
            }
        }
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        AssertionStatus assertionStatus = AssertionStatus.FAILED;
        try {
            try {
                if (this.l == null) {
                    throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_CONFIG_INVALID);
                }
            }
            catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
                throw ServerWsFederationPassiveTokenRequest.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 UsernameToken a(SecurityKnob securityKnob) throws AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
        UsernameToken usernameToken;
        block9: {
            boolean bl2;
            Logger logger;
            block11: {
                block10: {
                    ProcessorResult processorResult;
                    usernameToken = null;
                    if (securityKnob != null && (processorResult = securityKnob.getProcessorResult()) != null) {
                        XmlSecurityToken[] xmlSecurityTokenArray;
                        for (XmlSecurityToken xmlSecurityToken : xmlSecurityTokenArray = processorResult.getXmlSecurityTokens()) {
                            block8: {
                                try {
                                    if (!(xmlSecurityToken instanceof UsernameToken)) continue;
                                    if (usernameToken != null) break block8;
                                }
                                catch (AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException stopAndAuditException) {
                                    throw ServerWsFederationPassiveTokenRequest.b(stopAndAuditException);
                                }
                                usernameToken = (UsernameToken)xmlSecurityToken;
                                continue;
                            }
                            throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_MULTI_TOKENS);
                        }
                    }
                    try {
                        try {
                            if (!this.logger.isLoggable(Level.FINEST)) break block9;
                            logger = this.logger;
                            if (usernameToken == null) break block10;
                        }
                        catch (AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException stopAndAuditException) {
                            throw ServerWsFederationPassiveTokenRequest.b(stopAndAuditException);
                        }
                        bl2 = true;
                        break block11;
                    }
                    catch (AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException stopAndAuditException) {
                        throw ServerWsFederationPassiveTokenRequest.b(stopAndAuditException);
                    }
                }
                bl2 = false;
            }
            logger.finest(o[10] + bl2);
        }
        return usernameToken;
    }

    private UsernameToken a(AuthenticationContext authenticationContext) {
        UsernameTokenImpl usernameTokenImpl;
        block11: {
            boolean bl2;
            Logger logger;
            block13: {
                block12: {
                    block9: {
                        block10: {
                            usernameTokenImpl = null;
                            LoginCredentials loginCredentials = authenticationContext.getLastCredentials();
                            try {
                                if (loginCredentials == null) break block9;
                                if (loginCredentials.getFormat() != CredentialFormat.CLEARTEXT) break block10;
                            }
                            catch (IllegalArgumentException illegalArgumentException) {
                                throw ServerWsFederationPassiveTokenRequest.b(illegalArgumentException);
                            }
                            usernameTokenImpl = new UsernameTokenImpl(loginCredentials.getLogin(), loginCredentials.getCredentials());
                            break block9;
                        }
                        try {
                            if (this.logger.isLoggable(Level.FINEST)) {
                                this.logger.finest(o[5]);
                            }
                        }
                        catch (IllegalArgumentException illegalArgumentException) {
                            throw ServerWsFederationPassiveTokenRequest.b(illegalArgumentException);
                        }
                    }
                    try {
                        try {
                            if (!this.logger.isLoggable(Level.FINEST)) break block11;
                            logger = this.logger;
                            if (usernameTokenImpl == null) break block12;
                        }
                        catch (IllegalArgumentException illegalArgumentException) {
                            throw ServerWsFederationPassiveTokenRequest.b(illegalArgumentException);
                        }
                        bl2 = true;
                        break block13;
                    }
                    catch (IllegalArgumentException illegalArgumentException) {
                        throw ServerWsFederationPassiveTokenRequest.b(illegalArgumentException);
                    }
                }
                bl2 = false;
            }
            logger.finest(o[9] + bl2);
        }
        return usernameTokenImpl;
    }

    private SamlAssertion a(SecurityToken securityToken) throws AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException, AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
        SamlAssertion samlAssertion;
        try {
            if (!(securityToken instanceof HasUsernameAndPassword)) {
                String[] stringArray = o;
                throw new InvalidTokenException(stringArray[1]);
            }
            HasUsernameAndPassword hasUsernameAndPassword = (HasUsernameAndPassword)securityToken;
            String string = hasUsernameAndPassword.getUsername();
            char[] cArray = hasUsernameAndPassword.getPassword();
            try {
                if (cArray == null) {
                    throw new InvalidTokenException(o[2]);
                }
            }
            catch (ResponseStatusException responseStatusException) {
                throw ServerWsFederationPassiveTokenRequest.b((Exception)((Object)responseStatusException));
            }
            hasUsernameAndPassword = new GenericHttpRequestParams(this.l);
            hasUsernameAndPassword.setPasswordAuthentication(new PasswordAuthentication(string, cArray));
            XmlSecurityToken xmlSecurityToken = FederationPassiveClient.obtainFederationToken((GenericHttpClient)this.k, (GenericHttpRequestParams)hasUsernameAndPassword, (String)((WsFederationPassiveTokenRequest)this.assertion).getRealm(), (String)((WsFederationPassiveTokenRequest)this.assertion).getReplyUrl(), (String)((WsFederationPassiveTokenRequest)this.assertion).getContext(), (boolean)((WsFederationPassiveTokenRequest)this.assertion).isTimestamp());
            if (!(xmlSecurityToken instanceof SamlAssertion)) {
                throw new InvalidTokenException(o[4]);
            }
            samlAssertion = (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 samlAssertion;
    }

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

    private void a(PolicyEnforcementContext policyEnforcementContext, XmlKnob xmlKnob, UsernameToken usernameToken, SamlAssertion samlAssertion, boolean bl2) throws AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException {
        try {
            Element element;
            Document document;
            block8: {
                block7: {
                    document = xmlKnob.getDocumentWritable();
                    try {
                        if (bl2) break block7;
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.WSFEDPASS_ORIGINAL_TOKEN_NOT_XML);
                        break block8;
                    }
                    catch (Exception exception) {
                        throw ServerWsFederationPassiveTokenRequest.b(exception);
                    }
                }
                element = usernameToken.asElement();
                Node node = element.getParentNode();
                try {
                    node.removeChild(element);
                    if (node.getFirstChild() == null) {
                        node.getParentNode().removeChild(node);
                    }
                }
                catch (Exception exception) {
                    throw ServerWsFederationPassiveTokenRequest.b(exception);
                }
            }
            element = policyEnforcementContext.getRequest().getSecurityKnob();
            this.updateRequestXml(policyEnforcementContext, xmlKnob, (SecurityKnob)element, document, samlAssertion, 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 {
        XmlKnob xmlKnob = (XmlKnob)policyEnforcementContext.getRequest().getKnob(XmlKnob.class);
        this.ensureXmlRequest(xmlKnob);
        SecurityKnob securityKnob = (SecurityKnob)policyEnforcementContext.getRequest().getKnob(SecurityKnob.class);
        boolean bl2 = true;
        UsernameToken usernameToken = this.a(securityKnob);
        if (usernameToken == null) {
            bl2 = false;
            usernameToken = this.a(policyEnforcementContext.getAuthenticationContext(policyEnforcementContext.getRequest()));
        }
        try {
            if (usernameToken == null) {
                throw new AbstractServerWsFederationPassiveRequestProfile.StopAndAuditException(AssertionMessages.WSFEDPASS_NO_SUITABLE_CREDENTIALS);
            }
        }
        catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
            throw ServerWsFederationPassiveTokenRequest.b(authRequiredException);
        }
        PolicyContextCache policyContextCache = policyEnforcementContext.getCache();
        boolean bl3 = true;
        SamlAssertion samlAssertion = (SamlAssertion)this.getCachedSecurityToken(policyContextCache);
        if (samlAssertion == null) {
            bl3 = false;
            samlAssertion = this.a((SecurityToken)usernameToken);
            this.setCachedSecurityToken(policyContextCache, (SecurityToken)samlAssertion, this.getSamlAssertionExpiry(samlAssertion));
        } else {
            try {
                if (this.logger.isLoggable(Level.FINE)) {
                    this.logger.fine(o[8]);
                }
            }
            catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
                throw ServerWsFederationPassiveTokenRequest.b(authRequiredException);
            }
        }
        try {
            this.a(policyEnforcementContext, xmlKnob, usernameToken, samlAssertion, bl2);
            if (((WsFederationPassiveTokenRequest)this.assertion).isAuthenticate()) {
                this.a(policyEnforcementContext, samlAssertion, bl3);
            }
        }
        catch (AbstractServerWsFederationPassiveRequestProfile.AuthRequiredException authRequiredException) {
            throw ServerWsFederationPassiveTokenRequest.b(authRequiredException);
        }
        this.addCacheInvalidator(policyEnforcementContext);
        this.addResponseAuthFailureDecorator(policyEnforcementContext);
        return AssertionStatus.NONE;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[12];
                var3_1 = 0;
                var2_2 = "+}r\u000e\u0011[2J[J1\u0011G0)$>\u0003Ba\u0007\u0017|w\r_2\u000b\u0016(p\r_?\u0004\u0010f}\u0016X}\f\u0004d$,fn\u0017E2\u0016\nc{\f\u0011e\u0003\u0016f9\u0016\u0011g\u0011\u0000zp\u0003\\wM\u0015im\u0011F}\u0010\u0001%,fn\u0017E2\u0016\nc{\f\u0011v\u000b\u0001f9\u0016\u0011q\r\u000b|\u007f\u000b_2\u0003Ex\u007f\u0011Be\r\u0017l\u001d,X11eAB0ZRBYs\u0011Ei>\u0013Dw\u0010\u001c(m\u0016C{\f\u0002\u00160fm\u0017Ab\r\u0017|{\u0006\u0011f\r\u000empBEk\u0012\u0000)&z{\u0006T|\u0016\fir\u0011\u0011s\u0010\u0000(p\rE2\u0001\tm\u007f\u0010\u0011f\u0007\u001d|2BXu\f\nzw\fV<\u0005K[_/}\u0018\u0004fq\fd`\u000e-|j\u0012r~\u000b\u0000fj$Pq\u0016\nzg\u001b0{w\fV2\u0001\u0004kv\u0007U21$ERBPa\u0011\u0000zj\u000b^|\u001d\"gjB_}\fHPS.\u0011a\u0007\u0006}l\u000bEkB\u0011gu\u0007_-XE";
                var4_3 = "+}r\u000e\u0011[2J[J1\u0011G0)$>\u0003Ba\u0007\u0017|w\r_2\u000b\u0016(p\r_?\u0004\u0010f}\u0016X}\f\u0004d$,fn\u0017E2\u0016\nc{\f\u0011e\u0003\u0016f9\u0016\u0011g\u0011\u0000zp\u0003\\wM\u0015im\u0011F}\u0010\u0001%,fn\u0017E2\u0016\nc{\f\u0011v\u000b\u0001f9\u0016\u0011q\r\u000b|\u007f\u000b_2\u0003Ex\u007f\u0011Be\r\u0017l\u001d,X11eAB0ZRBYs\u0011Ei>\u0013Dw\u0010\u001c(m\u0016C{\f\u0002\u00160fm\u0017Ab\r\u0017|{\u0006\u0011f\r\u000empBEk\u0012\u0000)&z{\u0006T|\u0016\fir\u0011\u0011s\u0010\u0000(p\rE2\u0001\tm\u007f\u0010\u0011f\u0007\u001d|2BXu\f\nzw\fV<\u0005K[_/}\u0018\u0004fq\fd`\u000e-|j\u0012r~\u000b\u0000fj$Pq\u0016\nzg\u001b0{w\fV2\u0001\u0004kv\u0007U21$ERBPa\u0011\u0000zj\u000b^|\u001d\"gjB_}\fHPS.\u0011a\u0007\u0006}l\u000bEkB\u0011gu\u0007_-XE".length();
                var1_4 = 44;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerWsFederationPassiveTokenRequest.d(v0, ServerWsFederationPassiveTokenRequest.c(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\\\u0019\u0014<\u0017!P;\u0005\u0005\u007f:\u001euo\u000f@h \u0007yuIZ<\u001aN\u0018\u0001~#\t<o\u0019@l.\u001eo~V)L`?HHV5N\u0003";
                    var4_3 = "\\\u0019\u0014<\u0017!P;\u0005\u0005\u007f:\u001euo\u000f@h \u0007yuIZ<\u001aN\u0018\u0001~#\t<o\u0019@l.\u001eo~V)L`?HHV5N\u0003".length();
                    var1_4 = 25;
                    var0_5 = -1;
lbl22:
                    // 2 sources

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

    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] ^ 0xD);
        }
        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 -> 10;
                    case 1 -> 103;
                    case 2 -> 113;
                    case 3 -> 13;
                    case 4 -> 94;
                    case 5 -> 125;
                    default -> 13;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

