/*
 * 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.xmlsec.CreateSecurityContextToken
 *  com.l7tech.security.wstrust.WsTrustConfig
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.HexUtils
 *  com.l7tech.util.SoapConstants
 *  com.l7tech.xml.soap.SoapUtil
 *  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.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.CreateSecurityContextToken;
import com.l7tech.security.wstrust.WsTrustConfig;
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.secureconversation.InboundSecureConversationContextManager;
import com.l7tech.server.secureconversation.SecureConversationSession;
import com.l7tech.server.secureconversation.SessionCreationException;
import com.l7tech.server.util.RstSoapMessageProcessor;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.HexUtils;
import com.l7tech.util.SoapConstants;
import com.l7tech.xml.soap.SoapUtil;
import java.io.IOException;
import java.util.Map;
import org.springframework.beans.factory.BeanFactory;

public class ServerCreateSecurityContextToken
extends AbstractMessageTargetableServerAssertion<CreateSecurityContextToken> {
    private final InboundSecureConversationContextManager d;
    private static final String[] e;

    public ServerCreateSecurityContextToken(CreateSecurityContextToken createSecurityContextToken, BeanFactory beanFactory) {
        super(createSecurityContextToken);
        this.d = (InboundSecureConversationContextManager)beanFactory.getBean(e[16], InboundSecureConversationContextManager.class);
    }

    @Override
    protected AssertionStatus doCheckRequest(PolicyEnforcementContext policyEnforcementContext, Message message, String string, AuthenticationContext authenticationContext) throws IOException, PolicyAssertionException {
        Object object;
        byte[] byArray;
        Object object2;
        String[] stringArray;
        Object object32;
        AuthenticationResult authenticationResult;
        Map<String, String> map;
        block26: {
            block25: {
                block24: {
                    map = RstSoapMessageProcessor.getRstParameters(message, true);
                    try {
                        if (map.containsKey(e[22])) {
                            this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_INVALID_RST_REQUEST, map.get(e[3]));
                            return AssertionStatus.BAD_REQUEST;
                        }
                    }
                    catch (NumberFormatException numberFormatException) {
                        throw ServerCreateSecurityContextToken.a(numberFormatException);
                    }
                    authenticationResult = authenticationContext.getLastAuthenticationResult();
                    try {
                        if (authenticationResult == null) {
                            this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_AUTHENTICATION_FAILURE, e[13]);
                            return AssertionStatus.AUTH_FAILED;
                        }
                    }
                    catch (NumberFormatException numberFormatException) {
                        throw ServerCreateSecurityContextToken.a(numberFormatException);
                    }
                    LoginCredentials loginCredentials = null;
                    for (Object object32 : authenticationContext.getCredentials()) {
                        if (!authenticationResult.matchesSecurityToken(object32.getSecurityToken())) continue;
                        loginCredentials = object32;
                        break;
                    }
                    try {
                        if (loginCredentials == null) {
                            this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_AUTHENTICATION_FAILURE, e[0]);
                            return AssertionStatus.AUTH_FAILED;
                        }
                    }
                    catch (NumberFormatException numberFormatException) {
                        throw ServerCreateSecurityContextToken.a(numberFormatException);
                    }
                    stringArray = e;
                    object2 = map.get(stringArray[11]);
                    try {
                        if (object2 != null && !((String)object2).trim().isEmpty()) break block24;
                    }
                    catch (NumberFormatException numberFormatException) {
                        throw ServerCreateSecurityContextToken.a(numberFormatException);
                    }
                    stringArray = e;
                    object32 = map.get(stringArray[23]);
                    object2 = this.a((String)object32);
                }
                stringArray = e;
                object32 = map.get(stringArray[15]);
                try {
                    if (object32 != null && !((String)object32).trim().isEmpty()) break block25;
                }
                catch (NumberFormatException numberFormatException) {
                    throw ServerCreateSecurityContextToken.a(numberFormatException);
                }
                object32 = SoapConstants.WSU_NAMESPACE;
            }
            byArray = null;
            try {
                if (!Boolean.parseBoolean(map.get(e[6])) || !Boolean.parseBoolean(map.get(e[27]))) break block26;
            }
            catch (NumberFormatException numberFormatException) {
                throw ServerCreateSecurityContextToken.a(numberFormatException);
            }
            stringArray = e;
            String string2 = map.get(stringArray[4]);
            try {
                if (string2 == null || !string2.endsWith(stringArray[5])) break block26;
            }
            catch (NumberFormatException numberFormatException) {
                throw ServerCreateSecurityContextToken.a(numberFormatException);
            }
            stringArray = e;
            object = map.get(stringArray[14]);
            byArray = HexUtils.decodeBase64((String)object);
        }
        int n2 = ((CreateSecurityContextToken)this.assertion).getKeySize();
        stringArray = e;
        if (Boolean.parseBoolean(map.get(stringArray[24]))) {
            object = map.get(stringArray[26]);
            try {
                int n3 = Integer.parseInt((String)object);
                if (n3 > n2) {
                    n2 = n3;
                }
            }
            catch (NumberFormatException numberFormatException) {
                stringArray = e;
                this.logger.info(stringArray[25] + (String)object + stringArray[20]);
            }
        }
        try {
            object = this.d.createContextForUser(authenticationResult.getUser(), RstSoapMessageProcessor.getWsscNamespace(map), this.a(), byArray, n2);
        }
        catch (SessionCreationException sessionCreationException) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.STS_TOKEN_ISSUE_ERROR, new String[]{ExceptionUtils.getMessage((Throwable)sessionCreationException)}, ExceptionUtils.getDebugException((Throwable)sessionCreationException));
            return AssertionStatus.FALSIFIED;
        }
        String string3 = this.a((String)object2, (String)object32, ((SecureConversationSession)object).getIdentifier());
        String string4 = ((CreateSecurityContextToken)this.assertion).getVariablePrefix() + "." + CreateSecurityContextToken.VARIABLE_ISSUED_SCT;
        policyEnforcementContext.setVariable(string4, string3);
        return AssertionStatus.NONE;
    }

    private String a(String string) {
        return WsTrustConfig.deriveWsSecureConversationNamespace((String)string);
    }

    private String a(String string, String string2, String string3) {
        String[] stringArray = e;
        StringBuilder stringBuilder = new StringBuilder().append(stringArray[18]).append(stringArray[9]).append(SoapUtil.generateUniqueId((String)stringArray[12], (int)1)).append(stringArray[19]).append(stringArray[17]).append(string).append(stringArray[7]).append(stringArray[21]).append(string2).append(stringArray[8]).append(stringArray[2]).append(string3).append(stringArray[1]).append(stringArray[10]);
        return stringBuilder.toString();
    }

    private long a() {
        try {
            if (((CreateSecurityContextToken)this.assertion).isUseSystemDefaultSessionDuration()) {
                return this.d.getDefaultSessionDuration();
            }
        }
        catch (NumberFormatException numberFormatException) {
            throw ServerCreateSecurityContextToken.a(numberFormatException);
        }
        return ((CreateSecurityContextToken)this.assertion).getLifetime();
    }

    private static NumberFormatException a(NumberFormatException numberFormatException) {
        return numberFormatException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[28];
                var3_1 = 0;
                var2_2 = "Q&n.3 \u000e{5g9v \u0015ftm%# \u001e22d8v:\u0012wtj?\"&\u001f| b)7:\u001fvt~93<T\u0012.{|95t3v1e>?(\u0013w&5@\u0011\u001bh|95t3v1e>?(\u0013w&5\rb5y9? \u001dM1y89<\u0012p=e+$7%a1h83:%f-{/\u0005\\;e)3\u0013z5x\u00153 \u000e`;{3\t+\u0016w9n$\"\u00020t\u00030j\u0001\be'~p\u001f*G0\u001b.{|95t)w7~8?:\u0003Q;e>36\u000eF;`/8p e'T93-\u000f`1T)9 \fw&x+\"'\u0015|\u000be+;+\tb5h/\u0014A1h?$'\u000ek\u0017d$\"+\u0002f\u0000d!3 CF<nj\"/\bu1\u007fj;+\ta5l/v*\u0015w'+$9:Zq;e>7'\u001425e3v/\u000ff<n$\"'\u0019s b%8n\u0013|2d8;/\u000e{;ed\rp=e+$7%a1h83:\u0016e'T?\"'\u0016{ b/%\u0011\u0014s9n9&/\u0019w'{:i%# \u001eA1h?$+9}:}/$=\u001bf=d$\u0015!\u0014f1s>\u001b/\u0014s3n8\u000bj9g$%t\ra76h\u001a.#x)l\u001d\u001fq!y#\"79}:\u007f/.:.}?n$v\u00020t\u00025z\u000bj9g$%t\ra!6h\rb5y9? \u001dM1y89<\u0012e'T>$;\tf\u000be+;+\tb5h/\fz5x\u0015=+\u0003M'b03\u001b[3e%$'\u0014utb$ /\u0016{0+!37Za=q/vi";
                var4_3 = "Q&n.3 \u000e{5g9v \u0015ftm%# \u001e22d8v:\u0012wtj?\"&\u001f| b)7:\u001fvt~93<T\u0012.{|95t3v1e>?(\u0013w&5@\u0011\u001bh|95t3v1e>?(\u0013w&5\rb5y9? \u001dM1y89<\u0012p=e+$7%a1h83:%f-{/\u0005\\;e)3\u0013z5x\u00153 \u000e`;{3\t+\u0016w9n$\"\u00020t\u00030j\u0001\be'~p\u001f*G0\u001b.{|95t)w7~8?:\u0003Q;e>36\u000eF;`/8p e'T93-\u000f`1T)9 \fw&x+\"'\u0015|\u000be+;+\tb5h/\u0014A1h?$'\u000ek\u0017d$\"+\u0002f\u0000d!3 CF<nj\"/\bu1\u007fj;+\ta5l/v*\u0015w'+$9:Zq;e>7'\u001425e3v/\u000ff<n$\"'\u0019s b%8n\u0013|2d8;/\u000e{;ed\rp=e+$7%a1h83:\u0016e'T?\"'\u0016{ b/%\u0011\u0014s9n9&/\u0019w'{:i%# \u001eA1h?$+9}:}/$=\u001bf=d$\u0015!\u0014f1s>\u001b/\u0014s3n8\u000bj9g$%t\ra76h\u001a.#x)l\u001d\u001fq!y#\"79}:\u007f/.:.}?n$v\u00020t\u00025z\u000bj9g$%t\ra!6h\rb5y9? \u001dM1y89<\u0012e'T>$;\tf\u000be+;+\tb5h/\fz5x\u0015=+\u0003M'b03\u001b[3e%$'\u0014utb$ /\u0016{0+!37Za=q/vi".length();
                var1_4 = 49;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerCreateSecurityContextToken.c(v0, ServerCreateSecurityContextToken.b(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0001I\nm]_x\u000f\u0019\u0002M\u0000mL_l\u000b^\nm]Sa\u0018I\u0007mKZg\u0007I\u001dF";
                    var4_3 = "\u0001I\nm]_x\u000f\u0019\u0002M\u0000mL_l\u000b^\nm]Sa\u0018I\u0007mKZg\u0007I\u001dF".length();
                    var1_4 = 8;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 114;
                        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++] = ServerCreateSecurityContextToken.c(v0, ServerCreateSecurityContextToken.b(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;
            }
        }
        ServerCreateSecurityContextToken.e = var5;
    }

    private static char[] b(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 -> 24;
                    case 1 -> 94;
                    case 2 -> 1;
                    case 3 -> 64;
                    case 4 -> 92;
                    case 5 -> 68;
                    default -> 112;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

