/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.CookieUtils
 *  com.l7tech.common.http.GenericHttpClient
 *  com.l7tech.common.http.GenericHttpClientFactory
 *  com.l7tech.common.http.GenericHttpException
 *  com.l7tech.common.http.GenericHttpRequest
 *  com.l7tech.common.http.GenericHttpRequestParams
 *  com.l7tech.common.http.GenericHttpResponse
 *  com.l7tech.common.http.GenericHttpState
 *  com.l7tech.common.http.HttpConstants
 *  com.l7tech.common.http.HttpCookie
 *  com.l7tech.common.http.HttpHeaders
 *  com.l7tech.common.http.HttpMethod
 *  com.l7tech.common.http.ParameterizedString
 *  com.l7tech.common.io.XmlUtil
 *  com.l7tech.common.mime.ContentTypeHeader
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.Messages$M
 *  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.xmlsec.AuthenticationProperties
 *  com.l7tech.policy.assertion.xmlsec.SamlBrowserArtifact
 *  com.l7tech.util.Charsets
 *  com.l7tech.util.HtmlConstants
 *  com.l7tech.util.IOUtils
 *  com.l7tech.util.ResourceUtils
 *  org.apache.hc.client5.http.cookie.BasicCookieStore
 *  org.apache.hc.client5.http.cookie.Cookie
 *  org.apache.hc.client5.http.cookie.CookieStore
 *  org.apache.hc.client5.http.impl.cookie.BasicClientCookie
 *  org.apache.hc.client5.http.protocol.HttpClientContext
 *  org.cyberneko.html.parsers.DOMParser
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion.xmlsec;

import com.l7tech.common.http.CookieUtils;
import com.l7tech.common.http.GenericHttpClient;
import com.l7tech.common.http.GenericHttpClientFactory;
import com.l7tech.common.http.GenericHttpException;
import com.l7tech.common.http.GenericHttpRequest;
import com.l7tech.common.http.GenericHttpRequestParams;
import com.l7tech.common.http.GenericHttpResponse;
import com.l7tech.common.http.GenericHttpState;
import com.l7tech.common.http.HttpConstants;
import com.l7tech.common.http.HttpCookie;
import com.l7tech.common.http.HttpHeaders;
import com.l7tech.common.http.HttpMethod;
import com.l7tech.common.http.ParameterizedString;
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.gateway.common.audit.Messages;
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.xmlsec.AuthenticationProperties;
import com.l7tech.policy.assertion.xmlsec.SamlBrowserArtifact;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.util.Charsets;
import com.l7tech.util.HtmlConstants;
import com.l7tech.util.IOUtils;
import com.l7tech.util.ResourceUtils;
import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import org.apache.hc.client5.http.cookie.BasicCookieStore;
import org.apache.hc.client5.http.cookie.Cookie;
import org.apache.hc.client5.http.cookie.CookieStore;
import org.apache.hc.client5.http.impl.cookie.BasicClientCookie;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.cyberneko.html.parsers.DOMParser;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

public class ServerSamlBrowserArtifact
extends AbstractServerAssertion<SamlBrowserArtifact> {
    private static final String d;
    private static final String e;
    private static final Short f;
    private final URL g;
    private final GenericHttpClient h;
    private static final String[] i;

    public ServerSamlBrowserArtifact(SamlBrowserArtifact samlBrowserArtifact, ApplicationContext applicationContext) {
        super(samlBrowserArtifact);
        try {
            this.g = new URL(samlBrowserArtifact.getSsoEndpointUrl());
        }
        catch (MalformedURLException malformedURLException) {
            String[] stringArray = i;
            throw (IllegalArgumentException)new IllegalArgumentException(stringArray[18] + samlBrowserArtifact.getSsoEndpointUrl()).initCause(malformedURLException);
        }
        this.h = ((GenericHttpClientFactory)applicationContext.getBean(i[19], GenericHttpClientFactory.class)).createHttpClient();
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        return this.a(policyEnforcementContext, true);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     */
    private AssertionStatus a(PolicyEnforcementContext policyEnforcementContext, boolean bl2) throws PolicyAssertionException {
        int n2;
        GenericHttpResponse genericHttpResponse;
        GenericHttpRequest genericHttpRequest;
        block64: {
            Object object;
            block65: {
                boolean bl3;
                block62: {
                    String[] stringArray;
                    HashSet<Cookie> hashSet;
                    GenericHttpState genericHttpState;
                    AuthenticationProperties authenticationProperties;
                    block63: {
                        Map map;
                        Object object2;
                        Object object3;
                        Object object4;
                        block57: {
                            LoginCredentials loginCredentials;
                            GenericHttpRequestParams genericHttpRequestParams;
                            block59: {
                                block60: {
                                    block58: {
                                        block66: {
                                            authenticationProperties = ((SamlBrowserArtifact)this.assertion).getAuthenticationProperties();
                                            genericHttpState = new GenericHttpState();
                                            HttpClientContext httpClientContext = new HttpClientContext();
                                            genericHttpState.setStateObject((Object)httpClientContext);
                                            genericHttpRequestParams = new GenericHttpRequestParams(this.g, genericHttpState);
                                            genericHttpRequestParams.setFollowRedirects(false);
                                            genericHttpRequest = null;
                                            genericHttpResponse = null;
                                            loginCredentials = policyEnforcementContext.getAuthenticationContext(policyEnforcementContext.getRequest()).getLastCredentials();
                                            try {
                                                if (loginCredentials == null) {
                                                    throw new AssertionException(AssertionStatus.AUTH_REQUIRED, AssertionMessages.SAMLBROWSER_CREDENTIALS_NOCREDS);
                                                }
                                            }
                                            catch (Exception exception) {
                                                throw ServerSamlBrowserArtifact.a(exception);
                                            }
                                            try {
                                                if (loginCredentials.getFormat() != CredentialFormat.CLEARTEXT) {
                                                    throw new AssertionException(AssertionStatus.AUTH_REQUIRED, AssertionMessages.SAMLBROWSER_CREDENTIALS_CREDS_NOT_PASSWORD);
                                                }
                                            }
                                            catch (Exception exception) {
                                                throw ServerSamlBrowserArtifact.a(exception);
                                            }
                                            bl3 = false;
                                            hashSet = new HashSet<Cookie>();
                                            if (!AuthenticationProperties.METHOD_BASIC.equals((Object)authenticationProperties.getMethod())) break block66;
                                            genericHttpRequestParams.setPasswordAuthentication(new PasswordAuthentication(loginCredentials.getLogin(), loginCredentials.getCredentials()));
                                            genericHttpRequest = this.h.createRequest(HttpMethod.GET, genericHttpRequestParams);
                                            break block57;
                                        }
                                        if (!AuthenticationProperties.METHOD_FORM.equals((Object)authenticationProperties.getMethod())) break block57;
                                        try {
                                            block67: {
                                                if (!authenticationProperties.isEnableCookies()) break block58;
                                                break block67;
                                                catch (Exception exception) {
                                                    throw ServerSamlBrowserArtifact.a(exception);
                                                }
                                            }
                                            if (!bl2) break block58;
                                        }
                                        catch (Exception exception) {
                                            throw ServerSamlBrowserArtifact.a(exception);
                                        }
                                        bl3 = this.a(policyEnforcementContext, (HttpClientContext)genericHttpState.getStateObject(), this.g.getHost(), hashSet);
                                    }
                                    if (!bl3) break block59;
                                    try {
                                        block68: {
                                            if (!this.logger.isLoggable(Level.FINER)) break block60;
                                            break block68;
                                            catch (Exception exception) {
                                                throw ServerSamlBrowserArtifact.a(exception);
                                            }
                                        }
                                        this.logger.finer(i[10] + String.valueOf(policyEnforcementContext.getRequestId()) + i[3]);
                                    }
                                    catch (Exception exception) {
                                        throw ServerSamlBrowserArtifact.a(exception);
                                    }
                                }
                                genericHttpRequestParams.setEnableCookieManagement(true);
                                genericHttpRequest = this.h.createRequest(HttpMethod.GET, genericHttpRequestParams);
                                break block57;
                            }
                            object4 = authenticationProperties.getUsernameFieldname();
                            String string = authenticationProperties.getPasswordFieldname();
                            object = new HashMap();
                            if (authenticationProperties.isRequestForm()) {
                                block61: {
                                    object3 = new AuthenticationProperties();
                                    try {
                                        this.a(genericHttpState, genericHttpRequestParams, (AuthenticationProperties)object3);
                                        if (object4 != null && ((String)object4).length() != 0) break block61;
                                    }
                                    catch (Exception exception) {
                                        throw ServerSamlBrowserArtifact.a(exception);
                                    }
                                    object4 = object3.getUsernameFieldname();
                                    string = object3.getPasswordFieldname();
                                }
                                object.putAll(object3.getAdditionalFields());
                            }
                            try {
                                object.putAll(authenticationProperties.getAdditionalFields());
                                object.put(object4, loginCredentials.getLogin());
                                object.put(string, new String(loginCredentials.getCredentials()));
                                if (this.logger.isLoggable(Level.FINER)) {
                                    this.logger.finer(i[14] + String.valueOf(policyEnforcementContext.getRequestId()) + i[6] + String.valueOf(object));
                                }
                            }
                            catch (Exception exception) {
                                throw ServerSamlBrowserArtifact.a(exception);
                            }
                            genericHttpRequestParams.setContentType(ContentTypeHeader.APPLICATION_X_WWW_FORM_URLENCODED);
                            genericHttpRequest = this.h.createRequest(HttpMethod.POST, genericHttpRequestParams);
                            genericHttpRequest.setInputStream((InputStream)new ByteArrayInputStream(this.a((Map<String, String>)object).getBytes(Charsets.UTF8)));
                            if (authenticationProperties.isRedirectAfterSubmit()) {
                                object3 = genericHttpRequest;
                                genericHttpRequest = null;
                                try {
                                    object2 = this.a(genericHttpRequestParams, (GenericHttpRequest)object3);
                                    genericHttpRequestParams.setTargetUrl((URL)object2);
                                    genericHttpRequestParams.setContentType(null);
                                    genericHttpRequest = this.h.createRequest(HttpMethod.GET, genericHttpRequestParams);
                                }
                                finally {
                                    ResourceUtils.closeQuietly((Closeable)object3);
                                }
                            }
                        }
                        genericHttpResponse = genericHttpRequest.getResponse();
                        object4 = genericHttpResponse.getHeaders();
                        n2 = genericHttpResponse.getStatus();
                        try {
                            if (this.logger.isLoggable(Level.FINE)) {
                                this.logger.log(Level.FINE, i[12], new Object[]{this.g, n2});
                            }
                        }
                        catch (Exception exception) {
                            throw ServerSamlBrowserArtifact.a(exception);
                        }
                        try {
                            if (n2 != 302 && n2 != 303) break block62;
                        }
                        catch (Exception exception) {
                            throw ServerSamlBrowserArtifact.a(exception);
                        }
                        object = object4.getOnlyOneValue(HttpConstants.HEADER_LOCATION);
                        try {
                            if (object == null) {
                                throw new GenericHttpException(i[11] + HttpConstants.HEADER_LOCATION);
                            }
                        }
                        catch (Exception exception) {
                            throw ServerSamlBrowserArtifact.a(exception);
                        }
                        object3 = new URL(this.g, (String)object);
                        policyEnforcementContext.setVariable(SamlBrowserArtifact.VAR_REDIRECT_URL, object3);
                        object2 = ((URL)object3).getQuery();
                        try {
                            if (object2 == null) {
                                throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSERARTIFACT_REDIRECT_NO_QUERY);
                            }
                        }
                        catch (Exception exception) {
                            throw ServerSamlBrowserArtifact.a(exception);
                        }
                        try {
                            map = ParameterizedString.parseQueryString((String)object2);
                        }
                        catch (Exception exception) {
                            throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSERARTIFACT_REDIRECT_BAD_QUERY, exception);
                        }
                        stringArray = (String[])map.get(((SamlBrowserArtifact)this.assertion).getArtifactQueryParameter());
                        try {
                            if (stringArray != null && stringArray.length != 0) break block63;
                        }
                        catch (Exception exception) {
                            throw ServerSamlBrowserArtifact.a(exception);
                        }
                        byte[] byArray = IOUtils.slurpStream((InputStream)genericHttpResponse.getInputStream());
                        this.logger.info(object4.toString());
                        this.logger.info(new String(byArray, HttpConstants.ENCODING_UTF8));
                        throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSERARTIFACT_REDIRECT_NO_ARTIFACT);
                    }
                    try {
                        policyEnforcementContext.setVariable(SamlBrowserArtifact.VAR_ARTIFACT, URLEncoder.encode(stringArray[0], HttpConstants.ENCODING_UTF8));
                        if (authenticationProperties.isEnableCookies()) {
                            this.a(policyEnforcementContext, (HttpClientContext)genericHttpState.getStateObject(), hashSet);
                        }
                    }
                    catch (Exception exception) {
                        throw ServerSamlBrowserArtifact.a(exception);
                    }
                    AssertionStatus assertionStatus = AssertionStatus.NONE;
                    ResourceUtils.closeQuietly((Closeable[])new Closeable[]{genericHttpRequest, genericHttpResponse});
                    return assertionStatus;
                }
                if (!bl3) break block64;
                try {
                    block69: {
                        if (!this.logger.isLoggable(Level.FINER)) break block65;
                        break block69;
                        catch (Exception exception) {
                            throw ServerSamlBrowserArtifact.a(exception);
                        }
                    }
                    this.logger.finer(i[20] + String.valueOf(policyEnforcementContext.getRequestId()) + i[3]);
                }
                catch (Exception exception) {
                    throw ServerSamlBrowserArtifact.a(exception);
                }
            }
            object = this.a(policyEnforcementContext, false);
            ResourceUtils.closeQuietly((Closeable[])new Closeable[]{genericHttpRequest, genericHttpResponse});
            return object;
        }
        try {
            try {
                if (this.logger.isLoggable(Level.FINEST)) {
                    this.logger.log(Level.FINEST, i[0], new Object[]{n2, new String(IOUtils.slurpStream((InputStream)genericHttpResponse.getInputStream()))});
                }
            }
            catch (Exception exception) {
                throw ServerSamlBrowserArtifact.a(exception);
            }
            throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSERARTIFACT_RESPONSE_NON_302);
        }
        catch (IOException iOException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.SAMLBROWSER_LOGINFORM_IOEXCEPTION, null, iOException);
            AssertionStatus assertionStatus = AssertionStatus.FAILED;
            ResourceUtils.closeQuietly((Closeable[])new Closeable[]{genericHttpRequest, genericHttpResponse});
            return assertionStatus;
        }
        catch (AssertionException assertionException) {
            AssertionStatus assertionStatus;
            try {
                this.logAndAudit((AuditDetailMessage)assertionException.getAssertionMessage(), null, assertionException.getCause());
                assertionStatus = assertionException.getAssertionStatus();
            }
            catch (Throwable throwable) {
                ResourceUtils.closeQuietly((Closeable[])new Closeable[]{genericHttpRequest, genericHttpResponse});
                throw throwable;
            }
            ResourceUtils.closeQuietly((Closeable[])new Closeable[]{genericHttpRequest, genericHttpResponse});
            return assertionStatus;
        }
    }

    private boolean a(PolicyEnforcementContext policyEnforcementContext, HttpClientContext httpClientContext, String string, Collection<Cookie> collection) {
        boolean bl2 = false;
        Set set = policyEnforcementContext.getRequest().getHttpCookiesKnob().getCookies();
        CookieStore cookieStore = httpClientContext.getCookieStore();
        if (cookieStore == null) {
            cookieStore = new BasicCookieStore();
            httpClientContext.setCookieStore(cookieStore);
        }
        for (HttpCookie httpCookie : set) {
            if (!httpCookie.getCookieName().startsWith(d)) continue;
            BasicClientCookie basicClientCookie = (BasicClientCookie)CookieUtils.toHttpClientCookie((HttpCookie)httpCookie, (String)d);
            String string2 = httpCookie.getPath();
            if (string2 == null) {
                string2 = "/";
            }
            try {
                basicClientCookie.setPath(string2);
                basicClientCookie.setDomain(string);
                if (this.logger.isLoggable(Level.FINE)) {
                    this.logger.fine(i[13] + basicClientCookie.getName() + i[3]);
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ServerSamlBrowserArtifact.a(illegalArgumentException);
            }
            cookieStore.addCookie((Cookie)basicClientCookie);
            collection.add((Cookie)basicClientCookie);
            bl2 = true;
        }
        return bl2;
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, HttpClientContext httpClientContext, Collection collection) {
        CookieStore cookieStore = httpClientContext.getCookieStore();
        LinkedHashSet linkedHashSet = new LinkedHashSet(cookieStore.getCookies());
        linkedHashSet.removeAll(collection);
        for (Cookie cookie : linkedHashSet) {
            try {
                if (this.logger.isLoggable(Level.FINE)) {
                    this.logger.log(Level.FINE, i[2] + cookie.getName() + i[9]);
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ServerSamlBrowserArtifact.a(illegalArgumentException);
            }
            BasicClientCookie basicClientCookie = new BasicClientCookie(d + cookie.getName(), cookie.getValue());
            basicClientCookie.setDomain(null);
            basicClientCookie.setExpiryDate(cookie.getExpiryDate());
            basicClientCookie.setPath(null);
            basicClientCookie.setSecure(cookie.isSecure());
            policyEnforcementContext.getResponse().getHttpCookiesKnob().addCookie(CookieUtils.fromHttpClientCookie((Cookie)basicClientCookie, (boolean)true));
        }
    }

    /*
     * Unable to fully structure code
     */
    private void a(GenericHttpState var1_1, GenericHttpRequestParams var2_2, AuthenticationProperties var3_3) throws AssertionException {
        var4_4 = ((SamlBrowserArtifact)this.assertion).getAuthenticationProperties();
        var5_5 = null;
        var6_6 = null;
        try {
            block30: {
                block29: {
                    block28: {
                        block27: {
                            block26: {
                                block25: {
                                    var7_7 = new GenericHttpRequestParams(this.g, var1_1);
                                    var7_7.setFollowRedirects(false);
                                    var5_5 = this.h.createRequest(HttpMethod.GET, var7_7);
                                    var6_6 = var5_5.getResponse();
                                    var8_9 = var6_6.getStatus();
                                    try {
                                        if (var8_9 != 200) {
                                            throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_NON_200);
                                        }
                                    }
                                    catch (IOException v0) {
                                        throw ServerSamlBrowserArtifact.a(v0);
                                    }
                                    if (var4_4.getUsernameFieldname() == null) ** GOTO lbl23
                                    try {
                                        block32: {
                                            if (var4_4.getUsernameFieldname().length() != 0) break block25;
                                            break block32;
                                            catch (IOException v1) {
                                                throw ServerSamlBrowserArtifact.a(v1);
                                            }
                                        }
                                        v2 = true;
                                        break block26;
                                    }
                                    catch (IOException v3) {
                                        throw ServerSamlBrowserArtifact.a(v3);
                                    }
                                }
                                v2 = false;
                            }
                            var9_10 = v2;
                            if (var4_4.getFormTarget() == null) ** GOTO lbl39
                            try {
                                block33: {
                                    if (var4_4.getFormTarget().length() != 0) break block27;
                                    break block33;
                                    catch (IOException v4) {
                                        throw ServerSamlBrowserArtifact.a(v4);
                                    }
                                }
                                v5 = true;
                                break block28;
                            }
                            catch (IOException v6) {
                                throw ServerSamlBrowserArtifact.a(v6);
                            }
                        }
                        v5 = false;
                    }
                    var10_11 = v5;
                    if (var9_10) break block29;
                    try {
                        block34: {
                            if (var4_4.isCopyFormFields()) break block29;
                            break block34;
                            catch (IOException v7) {
                                throw ServerSamlBrowserArtifact.a(v7);
                            }
                        }
                        if (!var10_11) break block30;
                    }
                    catch (IOException v8) {
                        throw ServerSamlBrowserArtifact.a(v8);
                    }
                }
                var11_12 = this.a(var6_6.getInputStream(), var9_10, var4_4.isCopyFormFields(), var3_3);
                try {
                    if (var10_11) {
                        var2_2.setTargetUrl(var11_12);
                    }
                }
                catch (IOException v9) {
                    throw ServerSamlBrowserArtifact.a(v9);
                }
            }
            try {
                if (!var10_11) {
                    var2_2.setTargetUrl(new URL(var4_4.getFormTarget()));
                }
            }
            catch (IOException v10) {
                throw ServerSamlBrowserArtifact.a(v10);
            }
        }
        catch (IOException var7_8) {
            try {
                throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_IOEXCEPTION, var7_8);
            }
            catch (Throwable var12_13) {
                ResourceUtils.closeQuietly((Closeable[])new Closeable[]{var5_5, var6_6});
                throw var12_13;
            }
        }
        ResourceUtils.closeQuietly((Closeable[])new Closeable[]{var5_5, var6_6});
    }

    private URL a(InputStream inputStream, boolean bl2, boolean bl3, AuthenticationProperties authenticationProperties) throws IOException, AssertionException {
        Object object;
        HashMap<String, String> hashMap;
        String string;
        block67: {
            String string2;
            block65: {
                boolean bl4;
                boolean bl5;
                block56: {
                    NodeList nodeList;
                    String[] stringArray;
                    block55: {
                        DOMParser dOMParser = new DOMParser();
                        try {
                            stringArray = i;
                            dOMParser.setProperty(stringArray[1], (Object)f);
                            dOMParser.setEntityResolver(XmlUtil.getSafeEntityResolver());
                            dOMParser.parse(new InputSource(inputStream));
                        }
                        catch (SAXException sAXException) {
                            throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_IOEXCEPTION);
                        }
                        Element element = dOMParser.getDocument().getDocumentElement();
                        nodeList = element.getElementsByTagName(HtmlConstants.ELE_FORM);
                        try {
                            try {
                                if (nodeList != null && nodeList.getLength() != 0) break block55;
                            }
                            catch (SAXException sAXException) {
                                throw ServerSamlBrowserArtifact.a(sAXException);
                            }
                            throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_NO_FORM);
                        }
                        catch (SAXException sAXException) {
                            throw ServerSamlBrowserArtifact.a(sAXException);
                        }
                    }
                    try {
                        if (nodeList.getLength() > 1) {
                            throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_MULTIPLE_FORMS);
                        }
                    }
                    catch (SAXException sAXException) {
                        throw ServerSamlBrowserArtifact.a(sAXException);
                    }
                    Element element = (Element)nodeList.item(0);
                    string = element.getAttribute(HtmlConstants.ATTR_ACTION);
                    string2 = element.getAttribute(HtmlConstants.ATTR_METHOD);
                    bl5 = false;
                    bl4 = false;
                    hashMap = new HashMap<String, String>();
                    try {
                        if (!bl2 && !bl3) break block56;
                    }
                    catch (SAXException sAXException) {
                        throw ServerSamlBrowserArtifact.a(sAXException);
                    }
                    object = element.getElementsByTagName(HtmlConstants.ELE_INPUT);
                    for (int i2 = 0; i2 < object.getLength(); ++i2) {
                        String string3;
                        String string4;
                        HashMap<String, String> hashMap2;
                        block64: {
                            String string5;
                            block63: {
                                String string6;
                                String string7;
                                block60: {
                                    block61: {
                                        block58: {
                                            block59: {
                                                block57: {
                                                    string7 = ((Element)object.item(i2)).getAttribute(HtmlConstants.ATTR_NAME);
                                                    string6 = ((Element)object.item(i2)).getAttribute(HtmlConstants.ATTR_TYPE);
                                                    string5 = ((Element)object.item(i2)).getAttribute(HtmlConstants.ATTR_VALUE);
                                                    if (this.logger.isLoggable(Level.FINEST)) {
                                                        String string8 = string5;
                                                        String string9 = string7;
                                                        String string10 = string6;
                                                        stringArray = i;
                                                        this.logger.finest(stringArray[7] + string10 + stringArray[8] + string9 + stringArray[17] + string8 + "'");
                                                    }
                                                    try {
                                                        if (string6 != null && string6.length() != 0) break block57;
                                                    }
                                                    catch (SAXException sAXException) {
                                                        throw ServerSamlBrowserArtifact.a(sAXException);
                                                    }
                                                    string6 = HtmlConstants.VALUE_TEXT;
                                                }
                                                try {
                                                    try {
                                                        try {
                                                            if (!HtmlConstants.VALUE_TEXT.equalsIgnoreCase(string6) || !bl2) break block58;
                                                        }
                                                        catch (SAXException sAXException) {
                                                            throw ServerSamlBrowserArtifact.a(sAXException);
                                                        }
                                                        if (!bl5) break block59;
                                                    }
                                                    catch (SAXException sAXException) {
                                                        throw ServerSamlBrowserArtifact.a(sAXException);
                                                    }
                                                    throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_MULTIPLE_FIELDS);
                                                }
                                                catch (SAXException sAXException) {
                                                    throw ServerSamlBrowserArtifact.a(sAXException);
                                                }
                                            }
                                            bl5 = true;
                                            authenticationProperties.setUsernameFieldname(string7);
                                            continue;
                                        }
                                        try {
                                            try {
                                                try {
                                                    if (!HtmlConstants.VALUE_PASSWORD.equalsIgnoreCase(string6) || !bl2) break block60;
                                                }
                                                catch (SAXException sAXException) {
                                                    throw ServerSamlBrowserArtifact.a(sAXException);
                                                }
                                                if (!bl4) break block61;
                                            }
                                            catch (SAXException sAXException) {
                                                throw ServerSamlBrowserArtifact.a(sAXException);
                                            }
                                            throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_MULTIPLE_FIELDS);
                                        }
                                        catch (SAXException sAXException) {
                                            throw ServerSamlBrowserArtifact.a(sAXException);
                                        }
                                    }
                                    bl4 = true;
                                    authenticationProperties.setPasswordFieldname(string7);
                                    continue;
                                }
                                try {
                                    try {
                                        try {
                                            try {
                                                block62: {
                                                    try {
                                                        try {
                                                            if (!bl3) continue;
                                                            if (HtmlConstants.VALUE_HIDDEN.equalsIgnoreCase(string6)) break block62;
                                                        }
                                                        catch (SAXException sAXException) {
                                                            throw ServerSamlBrowserArtifact.a(sAXException);
                                                        }
                                                        if (!HtmlConstants.VALUE_SUBMIT.equalsIgnoreCase(string6)) continue;
                                                    }
                                                    catch (SAXException sAXException) {
                                                        throw ServerSamlBrowserArtifact.a(sAXException);
                                                    }
                                                }
                                                if (string7 == null) continue;
                                            }
                                            catch (SAXException sAXException) {
                                                throw ServerSamlBrowserArtifact.a(sAXException);
                                            }
                                            if (string7.length() <= 0) continue;
                                        }
                                        catch (SAXException sAXException) {
                                            throw ServerSamlBrowserArtifact.a(sAXException);
                                        }
                                        hashMap2 = hashMap;
                                        string4 = string7;
                                        if (string5 != null) break block63;
                                    }
                                    catch (SAXException sAXException) {
                                        throw ServerSamlBrowserArtifact.a(sAXException);
                                    }
                                    string3 = "";
                                    break block64;
                                }
                                catch (SAXException sAXException) {
                                    throw ServerSamlBrowserArtifact.a(sAXException);
                                }
                            }
                            string3 = string5;
                        }
                        hashMap2.put(string4, string3);
                    }
                }
                try {
                    block66: {
                        try {
                            try {
                                if (!bl2) break block65;
                                if (!bl5) break block66;
                            }
                            catch (SAXException sAXException) {
                                throw ServerSamlBrowserArtifact.a(sAXException);
                            }
                            if (bl4) break block65;
                        }
                        catch (SAXException sAXException) {
                            throw ServerSamlBrowserArtifact.a(sAXException);
                        }
                    }
                    throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_CANT_FIND_FIELDS);
                }
                catch (SAXException sAXException) {
                    throw ServerSamlBrowserArtifact.a(sAXException);
                }
            }
            try {
                try {
                    if (string2 != null && string2.trim().equalsIgnoreCase(HtmlConstants.VALUE_POST)) break block67;
                }
                catch (SAXException sAXException) {
                    throw ServerSamlBrowserArtifact.a(sAXException);
                }
                throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_INVALID);
            }
            catch (SAXException sAXException) {
                throw ServerSamlBrowserArtifact.a(sAXException);
            }
        }
        authenticationProperties.setAdditionalFields(hashMap);
        object = new URL(this.g, string);
        try {
            if (!this.g.getHost().equals(((URL)object).getHost())) {
                throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_INVALID);
            }
        }
        catch (SAXException sAXException) {
            throw ServerSamlBrowserArtifact.a(sAXException);
        }
        try {
            if (this.logger.isLoggable(Level.FINEST)) {
                this.logger.finest(i[15] + ((URL)object).toExternalForm());
            }
        }
        catch (SAXException sAXException) {
            throw ServerSamlBrowserArtifact.a(sAXException);
        }
        return object;
    }

    private URL a(GenericHttpRequestParams genericHttpRequestParams, GenericHttpRequest genericHttpRequest) throws IOException, AssertionException {
        GenericHttpResponse genericHttpResponse = null;
        try {
            block12: {
                genericHttpResponse = genericHttpRequest.getResponse();
                HttpHeaders httpHeaders = genericHttpResponse.getHeaders();
                int n2 = genericHttpResponse.getStatus();
                try {
                    if (n2 != 302 && n2 != 303) break block12;
                }
                catch (IOException iOException) {
                    throw ServerSamlBrowserArtifact.a(iOException);
                }
                String string = httpHeaders.getOnlyOneValue(HttpConstants.HEADER_LOCATION);
                try {
                    if (string == null) {
                        throw new GenericHttpException(i[16] + HttpConstants.HEADER_LOCATION);
                    }
                }
                catch (IOException iOException) {
                    throw ServerSamlBrowserArtifact.a(iOException);
                }
                URL uRL = genericHttpRequestParams.getTargetUrl();
                URL uRL2 = new URL(uRL, string);
                try {
                    if (!uRL.getHost().equals(uRL2.getHost())) {
                        throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSER_LOGINFORM_REDIRECT_INVALID);
                    }
                }
                catch (IOException iOException) {
                    throw ServerSamlBrowserArtifact.a(iOException);
                }
                URL uRL3 = uRL2;
                return uRL3;
            }
            throw new AssertionException(AssertionStatus.FAILED, AssertionMessages.SAMLBROWSERARTIFACT_RESPONSE_NON_302);
        }
        finally {
            ResourceUtils.closeQuietly((Closeable)genericHttpResponse);
        }
    }

    private String a(Map<String, String> map) throws UnsupportedEncodingException {
        StringBuffer stringBuffer = new StringBuffer();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String string = entry.getKey();
            String string2 = entry.getValue();
            this.a(stringBuffer, string, string2);
        }
        return stringBuffer.toString();
    }

    private void a(StringBuffer stringBuffer, String string, String string2) throws UnsupportedEncodingException {
        block6: {
            String string3;
            StringBuffer stringBuffer2;
            block7: {
                try {
                    try {
                        if (string == null) break block6;
                        if (stringBuffer.length() <= 0) break block7;
                    }
                    catch (UnsupportedEncodingException unsupportedEncodingException) {
                        throw ServerSamlBrowserArtifact.a(unsupportedEncodingException);
                    }
                    stringBuffer.append("&");
                }
                catch (UnsupportedEncodingException unsupportedEncodingException) {
                    throw ServerSamlBrowserArtifact.a(unsupportedEncodingException);
                }
            }
            try {
                stringBuffer.append(URLEncoder.encode(string, HttpConstants.ENCODING_UTF8));
                stringBuffer.append('=');
                stringBuffer2 = stringBuffer;
                string3 = string2 == null ? "" : string2;
            }
            catch (UnsupportedEncodingException unsupportedEncodingException) {
                throw ServerSamlBrowserArtifact.a(unsupportedEncodingException);
            }
            stringBuffer2.append(URLEncoder.encode(string3, HttpConstants.ENCODING_UTF8));
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[21];
                var3_1 = 0;
                var2_2 = "0\u0014Ya\u0016hn\u0011\u001fX9.YY5ZOm\u0007yx\u0016Z_v\u0002h-M\u0001\fdO!-\u0017\u001fOi\tc~\u0000Z^v\u0002t-\f\t\u00069lv<\u00180\r\u000eHi\\\"\"\u0006\u0003^|\u0014ch\u000e\u0015\u0012v\u0014j\"\r\u000eQuI}\u007f\n\nYk\u0012dh\u0016URx\u000bh~J\u001fP|\u000b~$+\u001fK9\u0005bb\u000e\u0013Y9\u0000\u007fb\bZoJ)-h\u000b\u001eLv\u000fcyIZRx\u000bh0B\u0002BT\u0005\u0016\tOvK0\r\u000eHi\\\"\"\u0006\u0003^|\u0014ch\u000e\u0015\u0012v\u0014j\"\r\u000eQuI}\u007f\n\nYk\u0012dh\u0016URx\u000bh~J\u001fP|\u000b~\u0007BZ]k\u00037-\u001c#\u0015Iw\u0002-k\n\bQ9\u0003ah\b\u001fRmFbkE\u000eEi\u0003-*\tBV\u001cw\u0007`hX]\u0002BT(0\tUw\u0001-n\u0004\u0019T|\u0002-E\u0011\u000eLJ\u0012ly\u0000ZKq\u0003c-\u0015\bSz\u0003~~\f\u0014[9A\u0010+\u0015\u001cj\u0013neE\u0012Yx\u0002h\u007f_Z@$\bHp\u0000ln\u0011ZN|\u0017xh\u0016\u000e\u001cm\t-*B\u0001\fdA*!E\u0019St\u0016ah\u0011\u001fX9\u0011dy\rZtM2]-\u0016\u000e]m\u0013~-\u0006\u0015X|Fv<\u0018'$\u001eXp\bj-\u0004Z_v\tfd\u0000ZHvFAb\u0002\u0013R9 b\u007f\bZN|\u0017xh\u0016\u000e\u001c>\u001e#\u000fPuF}b\u0016\u000e\u001ci\u0007\u007fl\b\t\u001c\u007f\t\u007f-\u0017\u001fMl\u0003~yE]\u0014#\u0015NtFyl\u0017\u001dYmFx\u007f\tZUj\\-\u0010+\u0015\u001cj\u0013neE\u0012Yx\u0002h\u007f_Z\nBV\u001co\u0007ax\u0000G\u001b\",\u0014Jx\ndiE)}T*-o\u0017\u0015Kj\u0003\u007f-\u0015\bS\u007f\u000fahE/nU\\-";
                var4_3 = "0\u0014Ya\u0016hn\u0011\u001fX9.YY5ZOm\u0007yx\u0016Z_v\u0002h-M\u0001\fdO!-\u0017\u001fOi\tc~\u0000Z^v\u0002t-\f\t\u00069lv<\u00180\r\u000eHi\\\"\"\u0006\u0003^|\u0014ch\u000e\u0015\u0012v\u0014j\"\r\u000eQuI}\u007f\n\nYk\u0012dh\u0016URx\u000bh~J\u001fP|\u000b~$+\u001fK9\u0005bb\u000e\u0013Y9\u0000\u007fb\bZoJ)-h\u000b\u001eLv\u000fcyIZRx\u000bh0B\u0002BT\u0005\u0016\tOvK0\r\u000eHi\\\"\"\u0006\u0003^|\u0014ch\u000e\u0015\u0012v\u0014j\"\r\u000eQuI}\u007f\n\nYk\u0012dh\u0016URx\u000bh~J\u001fP|\u000b~\u0007BZ]k\u00037-\u001c#\u0015Iw\u0002-k\n\bQ9\u0003ah\b\u001fRmFbkE\u000eEi\u0003-*\tBV\u001cw\u0007`hX]\u0002BT(0\tUw\u0001-n\u0004\u0019T|\u0002-E\u0011\u000eLJ\u0012ly\u0000ZKq\u0003c-\u0015\bSz\u0003~~\f\u0014[9A\u0010+\u0015\u001cj\u0013neE\u0012Yx\u0002h\u007f_Z@$\bHp\u0000ln\u0011ZN|\u0017xh\u0016\u000e\u001cm\t-*B\u0001\fdA*!E\u0019St\u0016ah\u0011\u001fX9\u0011dy\rZtM2]-\u0016\u000e]m\u0013~-\u0006\u0015X|Fv<\u0018'$\u001eXp\bj-\u0004Z_v\tfd\u0000ZHvFAb\u0002\u0013R9 b\u007f\bZN|\u0017xh\u0016\u000e\u001c>\u001e#\u000fPuF}b\u0016\u000e\u001ci\u0007\u007fl\b\t\u001c\u007f\t\u007f-\u0017\u001fMl\u0003~yE]\u0014#\u0015NtFyl\u0017\u001dYmFx\u007f\tZUj\\-\u0010+\u0015\u001cj\u0013neE\u0012Yx\u0002h\u007f_Z\nBV\u001co\u0007ax\u0000G\u001b\",\u0014Jx\ndiE)}T*-o\u0017\u0015Kj\u0003\u007f-\u0015\bS\u007f\u000fahE/nU\\-".length();
                var1_4 = 57;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerSamlBrowserArtifact.b(v0, ServerSamlBrowserArtifact.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "vf!\u0005\\\u000b\u000bgK\"\u0002q\u0011\u000bQi-\u001f{\r\u0006\u001dDm=\u0018}\u0010\u00117m6\u001b}\r\u001as((\u0004f_\rry;\u000eg\u000b_0";
                    var4_3 = "vf!\u0005\\\u000b\u000bgK\"\u0002q\u0011\u000bQi-\u001f{\r\u0006\u001dDm=\u0018}\u0010\u00117m6\u001b}\r\u001as((\u0004f_\rry;\u000eg\u000b_0".length();
                    var1_4 = 21;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 54;
                        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++] = ServerSamlBrowserArtifact.b(v0, ServerSamlBrowserArtifact.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;
            }
        }
        ServerSamlBrowserArtifact.i = var5;
        ServerSamlBrowserArtifact.e = ServerSamlBrowserArtifact.i[5];
        ServerSamlBrowserArtifact.d = CookieUtils.PREFIX_GATEWAY_MANAGED + ServerSamlBrowserArtifact.i[4];
        ServerSamlBrowserArtifact.f = (short)2;
    }

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

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x49);
        }
        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 -> 33;
                    case 1 -> 62;
                    case 2 -> 120;
                    case 3 -> 93;
                    case 4 -> 34;
                    case 5 -> 73;
                    default -> 73;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    private static class AssertionException
    extends Exception {
        private final AssertionStatus status;
        private final Messages.M message;
        static final long serialVersionUID = 5687169706914293680L;

        AssertionException(AssertionStatus assertionStatus, Messages.M m2) {
            super(m2.getMessage());
            this.status = assertionStatus;
            this.message = m2;
        }

        AssertionException(AssertionStatus assertionStatus, Messages.M m2, Throwable throwable) {
            super(m2.getMessage(), throwable);
            this.status = assertionStatus;
            this.message = m2;
        }

        public AssertionStatus getAssertionStatus() {
            return this.status;
        }

        public Messages.M getAssertionMessage() {
            return this.message;
        }
    }
}

