/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.GenericHttpClient
 *  com.l7tech.common.http.GenericHttpRequestParams
 *  com.l7tech.common.http.HttpCookie
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.Messages$M
 *  com.l7tech.message.Message
 *  com.l7tech.message.SecurityKnob
 *  com.l7tech.message.XmlKnob
 *  com.l7tech.policy.assertion.Assertion
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.XmlSecurityToken
 *  com.l7tech.security.wsfederation.FederationPassiveClient
 *  com.l7tech.security.wsfederation.ResponseStatusException
 *  com.l7tech.security.xml.SecurityTokenResolver
 *  com.l7tech.security.xml.decorator.DecorationRequirements
 *  com.l7tech.security.xml.decorator.WssDecoratorImpl
 *  com.l7tech.security.xml.processor.ProcessorResult
 *  com.l7tech.security.xml.processor.WssProcessor
 *  com.l7tech.security.xml.processor.WssProcessorImpl
 *  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.GenericHttpRequestParams;
import com.l7tech.common.http.HttpCookie;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.Messages;
import com.l7tech.message.Message;
import com.l7tech.message.SecurityKnob;
import com.l7tech.message.XmlKnob;
import com.l7tech.policy.assertion.Assertion;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.XmlSecurityToken;
import com.l7tech.security.wsfederation.FederationPassiveClient;
import com.l7tech.security.wsfederation.ResponseStatusException;
import com.l7tech.security.xml.SecurityTokenResolver;
import com.l7tech.security.xml.decorator.DecorationRequirements;
import com.l7tech.security.xml.decorator.WssDecoratorImpl;
import com.l7tech.security.xml.processor.ProcessorResult;
import com.l7tech.security.xml.processor.WssProcessor;
import com.l7tech.security.xml.processor.WssProcessorImpl;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.credential.AbstractServerCachedSecurityTokenAssertion;
import com.l7tech.server.policy.assertion.credential.b;
import com.l7tech.server.policy.assertion.credential.c;
import com.l7tech.xml.saml.SamlAssertion;
import java.io.IOException;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationContext;
import org.w3c.dom.Document;

public abstract class AbstractServerWsFederationPassiveRequestProfile<AT extends Assertion>
extends AbstractServerCachedSecurityTokenAssertion<AT> {
    private final WssProcessor g;
    private final SecurityTokenResolver h;
    private final Set<String> i = new CopyOnWriteArraySet<String>();
    private static final String[] n;

    protected AbstractServerWsFederationPassiveRequestProfile(AT AT, String string, ApplicationContext applicationContext) {
        super(AT, string);
        this.g = new WssProcessorImpl();
        this.h = (SecurityTokenResolver)applicationContext.getBean(n[1]);
    }

    protected void ensureXmlRequest(XmlKnob xmlKnob) throws StopAndAuditException {
        try {
            if (xmlKnob == null) {
                throw new StopAndAuditException(AssertionMessages.WSFEDPASS_NON_XML_MESSAGE);
            }
        }
        catch (StopAndAuditException stopAndAuditException) {
            throw AbstractServerWsFederationPassiveRequestProfile.a(stopAndAuditException);
        }
    }

    protected void ensureSecurityKnob(SecurityKnob securityKnob) throws StopAndAuditException {
        try {
            if (securityKnob == null) {
                throw new StopAndAuditException(AssertionMessages.WSFEDPASS_NO_SUITABLE_CREDENTIALS);
            }
        }
        catch (StopAndAuditException stopAndAuditException) {
            throw AbstractServerWsFederationPassiveRequestProfile.a(stopAndAuditException);
        }
    }

    protected Throwable causeOrSelf(Throwable throwable) {
        Throwable throwable2 = throwable.getCause();
        if (throwable2 == null) {
            throwable2 = throwable;
        }
        return throwable2;
    }

    protected void updateRequestXml(PolicyEnforcementContext policyEnforcementContext, XmlKnob xmlKnob, SecurityKnob securityKnob, Document document, SamlAssertion samlAssertion, Assertion assertion) throws Exception {
        DecorationRequirements decorationRequirements;
        WssDecoratorImpl wssDecoratorImpl;
        block4: {
            wssDecoratorImpl = new WssDecoratorImpl();
            policyEnforcementContext.getAuthenticationContext(policyEnforcementContext.getRequest()).addCredentials(LoginCredentials.makeLoginCredentials((SecurityToken)samlAssertion, assertion.getClass()));
            ProcessorResult processorResult = securityKnob.getProcessorResult();
            decorationRequirements = new DecorationRequirements();
            try {
                try {
                    decorationRequirements.setSecurityHeaderReusable(true);
                    if (processorResult == null || processorResult.getTimestamp() == null) break block4;
                }
                catch (Exception exception) {
                    throw AbstractServerWsFederationPassiveRequestProfile.a(exception);
                }
                decorationRequirements.setIncludeTimestamp(false);
            }
            catch (Exception exception) {
                throw AbstractServerWsFederationPassiveRequestProfile.a(exception);
            }
        }
        decorationRequirements.setSenderSamlToken(samlAssertion, false);
        wssDecoratorImpl.decorateMessage(new Message(document), decorationRequirements);
        xmlKnob.setDocument(document);
        securityKnob.setProcessorResult(this.g.undecorateMessage(policyEnforcementContext.getRequest(), null, this.h));
    }

    protected void doAuth(PolicyEnforcementContext policyEnforcementContext, GenericHttpClient genericHttpClient, SamlAssertion samlAssertion, String string, String string2, boolean bl2) throws AuthRequiredException, StopAndAuditException {
        block11: {
            block9: {
                block10: {
                    try {
                        try {
                            try {
                                if (!bl2 || !this.a(policyEnforcementContext)) break block9;
                            }
                            catch (AuthRequiredException authRequiredException) {
                                throw AbstractServerWsFederationPassiveRequestProfile.a(authRequiredException);
                            }
                            if (!this.logger.isLoggable(Level.FINER)) break block10;
                        }
                        catch (AuthRequiredException authRequiredException) {
                            throw AbstractServerWsFederationPassiveRequestProfile.a(authRequiredException);
                        }
                        this.logger.log(Level.FINER, n[2]);
                    }
                    catch (AuthRequiredException authRequiredException) {
                        throw AbstractServerWsFederationPassiveRequestProfile.a(authRequiredException);
                    }
                }
                this.b(policyEnforcementContext, genericHttpClient, samlAssertion, string, string2);
                break block11;
            }
            try {
                if (this.logger.isLoggable(Level.FINER)) {
                    this.logger.log(Level.FINER, n[0]);
                }
            }
            catch (AuthRequiredException authRequiredException) {
                throw AbstractServerWsFederationPassiveRequestProfile.a(authRequiredException);
            }
            this.a(policyEnforcementContext, genericHttpClient, samlAssertion, string, string2);
        }
    }

    protected void addResponseAuthFailureDecorator(PolicyEnforcementContext policyEnforcementContext) {
        policyEnforcementContext.addRoutingResultListener(new b(this));
    }

    private void a(HttpCookie httpCookie) {
        String string = httpCookie.getCookieName();
        if (!this.i.contains(string)) {
            this.i.add(string);
        }
    }

    private Set<String> a(Set<HttpCookie> set) {
        HashSet<String> hashSet = new HashSet<String>();
        for (HttpCookie httpCookie : set) {
            hashSet.add(httpCookie.getCookieName());
        }
        return hashSet;
    }

    private boolean a(PolicyEnforcementContext policyEnforcementContext) {
        boolean bl2 = false;
        if (!this.i.isEmpty()) {
            Set<String> set = this.a(policyEnforcementContext.getRequest().getHttpCookiesKnob().getCookies());
            bl2 = set.containsAll(this.i);
        }
        return bl2;
    }

    private void a(PolicyEnforcementContext policyEnforcementContext, GenericHttpClient genericHttpClient, SamlAssertion samlAssertion, String string, String string2) throws AuthRequiredException, StopAndAuditException {
        try {
            URL uRL = new URL(string);
            GenericHttpRequestParams genericHttpRequestParams = new GenericHttpRequestParams(uRL);
            Set set = FederationPassiveClient.postFederationToken((GenericHttpClient)genericHttpClient, (GenericHttpRequestParams)genericHttpRequestParams, (XmlSecurityToken)samlAssertion, (String)string2, (boolean)false);
            for (HttpCookie httpCookie : set) {
                policyEnforcementContext.getRequest().getHttpCookiesKnob().addCookie(httpCookie);
                this.a(httpCookie);
            }
        }
        catch (ResponseStatusException responseStatusException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSFEDPASS_AUTH_FAILED);
            throw new AuthRequiredException();
        }
        catch (IOException iOException) {
            throw new StopAndAuditException(AssertionMessages.WSFEDPASS_SERVER_HTTP_FAILED, this.causeOrSelf(iOException));
        }
    }

    private void b(PolicyEnforcementContext policyEnforcementContext, GenericHttpClient genericHttpClient, SamlAssertion samlAssertion, String string, String string2) {
        policyEnforcementContext.addRoutingResultListener(new c(this, genericHttpClient, samlAssertion, string, string2));
    }

    static /* synthetic */ Logger access$000(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile) {
        return abstractServerWsFederationPassiveRequestProfile.logger;
    }

    static /* synthetic */ void access$100(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile, AuditDetailMessage auditDetailMessage) {
        abstractServerWsFederationPassiveRequestProfile.logAndAudit(auditDetailMessage);
    }

    static /* synthetic */ Logger access$200(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile) {
        return abstractServerWsFederationPassiveRequestProfile.logger;
    }

    static /* synthetic */ Logger access$300(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile) {
        return abstractServerWsFederationPassiveRequestProfile.logger;
    }

    static /* synthetic */ Logger access$400(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile) {
        return abstractServerWsFederationPassiveRequestProfile.logger;
    }

    static /* synthetic */ Logger access$500(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile) {
        return abstractServerWsFederationPassiveRequestProfile.logger;
    }

    static /* synthetic */ Logger access$600(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile) {
        return abstractServerWsFederationPassiveRequestProfile.logger;
    }

    static /* synthetic */ void access$700(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile, AuditDetailMessage auditDetailMessage, String[] stringArray, Throwable throwable) {
        abstractServerWsFederationPassiveRequestProfile.logAndAudit(auditDetailMessage, stringArray, throwable);
    }

    static /* synthetic */ Logger access$800(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile) {
        return abstractServerWsFederationPassiveRequestProfile.logger;
    }

    static /* synthetic */ Logger access$900(AbstractServerWsFederationPassiveRequestProfile abstractServerWsFederationPassiveRequestProfile) {
        return abstractServerWsFederationPassiveRequestProfile.logger;
    }

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

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[3];
        int n2 = 0;
        String string = ",\u0019ZD9\nX\\\"f{5\n\u001f\b9)c5\u0016I\u00155l>\u0015\u000f3je\"\rK\u0005\u0002f{5\nm\u0019%f|&\u0001M/29}0\u0000+l(?gwp\u0010P\u00173g0$\u000b\u001f\u000f3{f9\u0007Z\\\r|c9\nX\\5f\u007f;\rZT% M~";
        int n3 = ",\u0019ZD9\nX\\\"f{5\n\u001f\b9)c5\u0016I\u00155l>\u0015\u000f3je\"\rK\u0005\u0002f{5\nm\u0019%f|&\u0001M/29}0\u0000+l(?gwp\u0010P\u00173g0$\u000b\u001f\u000f3{f9\u0007Z\\\r|c9\nX\\5f\u007f;\rZT% M~".length();
        int n4 = 25;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = AbstractServerWsFederationPassiveRequestProfile.c(123, AbstractServerWsFederationPassiveRequestProfile.b(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                n = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String c(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 7;
                    case 1 -> 45;
                    case 2 -> 114;
                    case 3 -> 107;
                    case 4 -> 43;
                    case 5 -> 31;
                    default -> 68;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    protected static class StopAndAuditException
    extends Exception {
        private Messages.M message;
        static final long serialVersionUID = -8991007461895474892L;

        protected StopAndAuditException(Messages.M m2) {
            super(m2.getMessage());
            this.message = m2;
        }

        protected StopAndAuditException(Messages.M m2, Throwable throwable) {
            super(m2.getMessage(), throwable);
            this.message = m2;
        }

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

    protected static class AuthRequiredException
    extends Exception {
        static final long serialVersionUID = 4007170131004735594L;

        protected AuthRequiredException() {
        }
    }
}

