/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.credential.wss.WssBasic
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.UsernameToken
 *  com.l7tech.security.token.XmlSecurityToken
 *  com.l7tech.security.xml.SecurityTokenResolver
 *  com.l7tech.security.xml.processor.ProcessorResult
 *  com.l7tech.util.CausedIOException
 *  com.l7tech.util.Config
 *  org.springframework.context.ApplicationContext
 */
package com.l7tech.server.policy.assertion.credential.wss;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.credential.wss.WssBasic;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.UsernameToken;
import com.l7tech.security.token.XmlSecurityToken;
import com.l7tech.security.xml.SecurityTokenResolver;
import com.l7tech.security.xml.processor.ProcessorResult;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.util.WSSecurityProcessorUtils;
import com.l7tech.util.CausedIOException;
import com.l7tech.util.Config;
import java.io.IOException;
import org.springframework.context.ApplicationContext;
import org.xml.sax.SAXException;

public class ServerWssBasic
extends AbstractMessageTargetableServerAssertion<WssBasic> {
    private final Config d;
    private final SecurityTokenResolver e;
    private static final String[] f;

    public ServerWssBasic(WssBasic wssBasic, ApplicationContext applicationContext) {
        super(wssBasic);
        String[] stringArray = f;
        this.d = (Config)applicationContext.getBean(stringArray[2], Config.class);
        this.e = (SecurityTokenResolver)applicationContext.getBean(stringArray[0]);
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        try {
            if (!((WssBasic)this.assertion).getRecipientContext().localRecipient()) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.WSS_BASIC_FOR_ANOTHER_RECIPIENT);
                return AssertionStatus.NONE;
            }
        }
        catch (IOException iOException) {
            throw ServerWssBasic.a(iOException);
        }
        return super.checkRequest(policyEnforcementContext);
    }

    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        XmlSecurityToken[] xmlSecurityTokenArray;
        ProcessorResult processorResult;
        block17: {
            block18: {
                block16: {
                    try {
                        if (!message.isSoap()) {
                            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSS_BASIC_NOT_SOAP, string);
                            return AssertionStatus.NOT_APPLICABLE;
                        }
                    }
                    catch (IOException iOException) {
                        throw ServerWssBasic.a(iOException);
                    }
                    try {
                        block15: {
                            try {
                                if (!this.isRequest() || this.d.getBooleanProperty(f[1], true)) break block15;
                            }
                            catch (SAXException sAXException) {
                                throw ServerWssBasic.a(sAXException);
                            }
                            processorResult = message.getSecurityKnob().getProcessorResult();
                            break block16;
                        }
                        processorResult = WSSecurityProcessorUtils.getWssResults(message, string, this.e, this.getAudit());
                    }
                    catch (SAXException sAXException) {
                        String[] stringArray = f;
                        throw new CausedIOException(stringArray[4] + string + stringArray[3], (Throwable)sAXException);
                    }
                }
                try {
                    try {
                        if (processorResult != null) break block17;
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.WSS_BASIC_NO_CREDENTIALS, string);
                        if (!this.isRequest()) break block18;
                    }
                    catch (SAXException sAXException) {
                        throw ServerWssBasic.a(sAXException);
                    }
                    policyEnforcementContext.setAuthenticationMissing();
                    policyEnforcementContext.setRequestPolicyViolated();
                }
                catch (SAXException sAXException) {
                    throw ServerWssBasic.a(sAXException);
                }
            }
            return AssertionStatus.AUTH_REQUIRED;
        }
        for (XmlSecurityToken xmlSecurityToken : xmlSecurityTokenArray = processorResult.getXmlSecurityTokens()) {
            if (!(xmlSecurityToken instanceof UsernameToken)) continue;
            UsernameToken usernameToken = (UsernameToken)xmlSecurityToken;
            LoginCredentials loginCredentials = LoginCredentials.makeLoginCredentials((SecurityToken)usernameToken, WssBasic.class);
            authenticationContext.addCredentials(loginCredentials);
            return AssertionStatus.NONE;
        }
        try {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.WSS_BASIC_CANNOT_FIND_CREDENTIALS);
            if (this.isRequest()) {
                policyEnforcementContext.setRequestPolicyViolated();
            }
        }
        catch (SAXException sAXException) {
            throw ServerWssBasic.a(sAXException);
        }
        return AssertionStatus.AUTH_REQUIRED;
    }

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

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "g\f4W\u0006RXm=8I\u0011U~q\u001a8N\u0002^^-c\u001a$\f\u0004ICw\f$Q\u001bI\u0002q\u00076@\u0018^hq\u000f2P\u0006^HF\f&W\u0011HXD\u001b8A\u0011H_}\u00070\fg\f%T\u0011Io{\u00071K\u0013";
                var4_3 = "g\f4W\u0006RXm=8I\u0011U~q\u001a8N\u0002^^-c\u001a$\f\u0004ICw\f$Q\u001bI\u0002q\u00076@\u0018^hq\u000f2P\u0006^HF\f&W\u0011HXD\u001b8A\u0011H_}\u00070\fg\f%T\u0011Io{\u00071K\u0013".length();
                var1_4 = 21;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerWssBasic.c(v0, ServerWssBasic.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "^$^*z: \u000ba^ox%a!Ivo{#5YmIow95Ys_#u{'\u0016vW*}\t4aI<x1$Y#";
                    var4_3 = "^$^*z: \u000ba^ox%a!Ivo{#5YmIow95Ys_#u{'\u0016vW*}\t4aI<x1$Y#".length();
                    var1_4 = 40;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 49;
                        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++] = ServerWssBasic.c(v0, ServerWssBasic.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;
            }
        }
        ServerWssBasic.f = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x70);
        }
        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 -> 72;
                    case 1 -> 53;
                    case 2 -> 11;
                    case 3 -> 126;
                    case 4 -> 40;
                    case 5 -> 103;
                    default -> 112;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

