/*
 * 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.identity.User
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.xmlsec.CancelSecurityContext
 *  com.l7tech.security.token.SecurityContextToken
 *  com.l7tech.security.token.SecurityToken
 *  org.springframework.beans.factory.BeanFactory
 */
package com.l7tech.server.policy.assertion.xmlsec;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.identity.User;
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.xmlsec.CancelSecurityContext;
import com.l7tech.security.token.SecurityContextToken;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.server.identity.AuthenticationResult;
import com.l7tech.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractMessageTargetableServerAssertion;
import com.l7tech.server.policy.assertion.AssertionStatusException;
import com.l7tech.server.policy.assertion.xmlsec.s;
import com.l7tech.server.policy.variable.ExpandVariables;
import com.l7tech.server.secureconversation.InboundSecureConversationContextManager;
import com.l7tech.server.secureconversation.OutboundSecureConversationContextManager;
import com.l7tech.server.secureconversation.SecureConversationSession;
import com.l7tech.server.util.RstSoapMessageProcessor;
import java.io.IOException;
import java.util.Map;
import org.springframework.beans.factory.BeanFactory;

public class ServerCancelSecurityContext
extends AbstractMessageTargetableServerAssertion<CancelSecurityContext> {
    private final InboundSecureConversationContextManager d;
    private final OutboundSecureConversationContextManager e;
    private final String[] f;
    private static final String[] g;

    public ServerCancelSecurityContext(CancelSecurityContext cancelSecurityContext, BeanFactory beanFactory) {
        super(cancelSecurityContext);
        String[] stringArray = g;
        this.d = (InboundSecureConversationContextManager)beanFactory.getBean(stringArray[7], InboundSecureConversationContextManager.class);
        this.e = (OutboundSecureConversationContextManager)beanFactory.getBean(stringArray[8], OutboundSecureConversationContextManager.class);
        this.f = cancelSecurityContext.getVariablesUsed();
    }

    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        try {
            if (((CancelSecurityContext)this.assertion).isCancelInbound()) {
                return this.a(message, authenticationContext);
            }
        }
        catch (IOException iOException) {
            throw ServerCancelSecurityContext.a(iOException);
        }
        return this.a(policyEnforcementContext, authenticationContext);
    }

    private AssertionStatus a(Message message, AuthenticationContext authenticationContext) {
        block13: {
            Map<String, String> map = RstSoapMessageProcessor.getRstParameters(message, false);
            try {
                if (map.containsKey(g[9])) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_INVALID_RST_REQUEST, map.get(g[3]));
                    return AssertionStatus.BAD_REQUEST;
                }
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCancelSecurityContext.a(assertionStatusException);
            }
            String[] stringArray = g;
            String string = map.get(stringArray[10]);
            switch (s.a[((CancelSecurityContext)this.assertion).getRequiredAuthorization().ordinal()]) {
                case 1: {
                    SecureConversationSession secureConversationSession = this.d.getSession(string);
                    try {
                        if (secureConversationSession == null) break;
                        this.a(authenticationContext, secureConversationSession.getUsedBy());
                        break;
                    }
                    catch (AssertionStatusException assertionStatusException) {
                        throw ServerCancelSecurityContext.a(assertionStatusException);
                    }
                }
                case 2: {
                    this.a(authenticationContext, string);
                }
            }
            try {
                try {
                    if (this.d.cancelSession(string) || !((CancelSecurityContext)this.assertion).isFailIfNotExist()) break block13;
                }
                catch (AssertionStatusException assertionStatusException) {
                    throw ServerCancelSecurityContext.a(assertionStatusException);
                }
                this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_EXPIRED_SC_SESSION, g[0] + string + "'");
                return AssertionStatus.BAD_REQUEST;
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCancelSecurityContext.a(assertionStatusException);
            }
        }
        return AssertionStatus.NONE;
    }

    private AssertionStatus a(PolicyEnforcementContext policyEnforcementContext, AuthenticationContext authenticationContext) {
        block9: {
            String string;
            Map<String, Object> map = policyEnforcementContext.getVariableMap(this.f, this.getAudit());
            User user = authenticationContext.getLastAuthenticatedUser();
            try {
                if (user == null) {
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_AUTHENTICATION_FAILURE, g[6]);
                    return AssertionStatus.FALSIFIED;
                }
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCancelSecurityContext.a(assertionStatusException);
            }
            try {
                string = ((CancelSecurityContext)this.assertion).getOutboundServiceUrl() == null ? "" : ((CancelSecurityContext)this.assertion).getOutboundServiceUrl();
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCancelSecurityContext.a(assertionStatusException);
            }
            String string2 = ExpandVariables.process(string, map, this.getAudit());
            try {
                try {
                    if (this.e.cancelSession(OutboundSecureConversationContextManager.newSessionKey(user, string2)) || !((CancelSecurityContext)this.assertion).isFailIfNotExist()) break block9;
                }
                catch (AssertionStatusException assertionStatusException) {
                    throw ServerCancelSecurityContext.a(assertionStatusException);
                }
                String[] stringArray = new String[1];
                stringArray[0] = g[1] + user.getLogin() + g[4] + string2 + "'";
                this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_EXPIRED_SC_SESSION, stringArray);
                return AssertionStatus.FALSIFIED;
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCancelSecurityContext.a(assertionStatusException);
            }
        }
        return AssertionStatus.NONE;
    }

    private void a(AuthenticationContext authenticationContext, User user) {
        boolean bl2 = false;
        for (AuthenticationResult authenticationResult : authenticationContext.getAllAuthenticationResults()) {
            User user2 = authenticationResult.getUser();
            try {
                if (!user2.getProviderId().equals((Object)user.getProviderId()) || !user2.getId().equals(user.getId())) continue;
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCancelSecurityContext.a(assertionStatusException);
            }
            bl2 = true;
            break;
        }
        try {
            if (!bl2) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_AUTHORIZATION_FAILURE, g[2]);
                throw new AssertionStatusException(AssertionStatus.BAD_REQUEST);
            }
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerCancelSecurityContext.a(assertionStatusException);
        }
    }

    private void a(AuthenticationContext authenticationContext, String string) {
        boolean bl2;
        block10: {
            bl2 = false;
            SecurityContextToken securityContextToken = null;
            block6: for (LoginCredentials object : authenticationContext.getCredentials()) {
                for (SecurityToken securityToken : object.getSecurityTokens()) {
                    try {
                        if (!(securityToken instanceof SecurityContextToken) || !string.equals(((SecurityContextToken)securityToken).getContextIdentifier())) continue;
                    }
                    catch (AssertionStatusException assertionStatusException) {
                        throw ServerCancelSecurityContext.a(assertionStatusException);
                    }
                    securityContextToken = (SecurityContextToken)securityToken;
                    break block6;
                }
            }
            try {
                if (securityContextToken == null || !securityContextToken.isPossessionProved()) break block10;
            }
            catch (AssertionStatusException assertionStatusException) {
                throw ServerCancelSecurityContext.a(assertionStatusException);
            }
            for (AuthenticationResult authenticationResult : authenticationContext.getAllAuthenticationResults()) {
                if (!authenticationResult.matchesSecurityToken((SecurityToken)securityContextToken)) continue;
                bl2 = true;
                break;
            }
        }
        try {
            if (!bl2) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_AUTHORIZATION_FAILURE, g[5]);
                throw new AssertionStatusException(AssertionStatus.BAD_REQUEST);
            }
        }
        catch (AssertionStatusException assertionStatusException) {
            throw ServerCancelSecurityContext.a(assertionStatusException);
        }
    }

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

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[11];
                var3_1 = 0;
                var2_2 = "\u0016.z\t1MAe%f\u000exD@0%mZ\u007f\u001c\u0016.z\t1MAe%f\u000exD@0%mZ>M]e>z\u001f*\u0002\b\"\u00108l\bxL@1ky\u001f*OF1?l\u001exV@e(h\u0014;GCe?f\u0011=L\r5*{\t1LH\u001a.{\b7P\u0010bg)\t=PY,(lZ\rpcel\u001a\u0017.x\u000f1PJ!k}\u00153GAe%f\u000exR] 8l\u0014,:\u0011#lZ,C]\".}Z5G\\6*n\u001fxF@ 8)\u00147V\u000f&$g\u000e9KAe*gZ9W[-.g\u000e1AN1.mZ-QJ7e',%k\u0015-LK\u0016.j\u000f*Gl*%\u007f\u001f*QN1\"f\u0014\u001bMA1.q\u000e\u0015CA$,l\b(*>}\u00187WA!\u0018l\u0019-PJ\u0006$g\f=P\\$?`\u00156a@+?l\u0002,oN+*n\u001f*";
                var4_3 = "\u0016.z\t1MAe%f\u000exD@0%mZ\u007f\u001c\u0016.z\t1MAe%f\u000exD@0%mZ>M]e>z\u001f*\u0002\b\"\u00108l\bxL@1ky\u001f*OF1?l\u001exV@e(h\u0014;GCe?f\u0011=L\r5*{\t1LH\u001a.{\b7P\u0010bg)\t=PY,(lZ\rpcel\u001a\u0017.x\u000f1PJ!k}\u00153GAe%f\u000exR] 8l\u0014,:\u0011#lZ,C]\".}Z5G\\6*n\u001fxF@ 8)\u00147V\u000f&$g\u000e9KAe*gZ9W[-.g\u000e1AN1.mZ-QJ7e',%k\u0015-LK\u0016.j\u000f*Gl*%\u007f\u001f*QN1\"f\u0014\u001bMA1.q\u000e\u0015CA$,l\b(*>}\u00187WA!\u0018l\u0019-PJ\u0006$g\f=P\\$?`\u00156a@+?l\u0002,oN+*n\u001f*".length();
                var1_4 = 19;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerCancelSecurityContext.c(v0, ServerCancelSecurityContext.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "4+z\b0MI\u001b/z\t6Q\u00176/n\u001e+F@'/W\u001a-W\\-(}\u000f<|[6#";
                    var4_3 = "4+z\b0MI\u001b/z\t6Q\u00176/n\u001e+F@'/W\u001a-W\\-(}\u000f<|[6#".length();
                    var1_4 = 13;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 119;
                        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++] = ServerCancelSecurityContext.c(v0, ServerCancelSecurityContext.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;
            }
        }
        ServerCancelSecurityContext.g = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x59);
        }
        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 -> 51;
                    case 1 -> 61;
                    case 2 -> 127;
                    case 3 -> 12;
                    case 4 -> 46;
                    case 5 -> 84;
                    default -> 89;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

