/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.HttpCookie
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.HttpCookiesKnob
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.CredentialFinderException
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.credential.http.CookieCredentialSourceAssertion
 *  com.l7tech.security.token.OpaqueSecurityToken
 *  com.l7tech.security.token.SecurityToken
 *  org.apache.commons.lang3.StringUtils
 */
package com.l7tech.server.policy.assertion.credential.http;

import com.l7tech.common.http.HttpCookie;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.HttpCookiesKnob;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.AssertionStatus;
import com.l7tech.policy.assertion.PolicyAssertionException;
import com.l7tech.policy.assertion.credential.CredentialFinderException;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.credential.http.CookieCredentialSourceAssertion;
import com.l7tech.security.token.OpaqueSecurityToken;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.credential.ServerCredentialSourceAssertion;
import java.io.IOException;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

public class ServerCookieCredentialSourceAssertion
extends ServerCredentialSourceAssertion<CookieCredentialSourceAssertion> {
    private static final String d = "\"";
    private final String e;
    private static final String[] g;

    public ServerCookieCredentialSourceAssertion(CookieCredentialSourceAssertion cookieCredentialSourceAssertion) {
        super(cookieCredentialSourceAssertion);
        this.e = cookieCredentialSourceAssertion.getCookieName();
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        AssertionStatus assertionStatus;
        block3: {
            block4: {
                assertionStatus = super.checkRequest(policyEnforcementContext);
                try {
                    if (!AssertionStatus.NONE.equals(assertionStatus)) break block3;
                    if (!StringUtils.isNotBlank((CharSequence)((CookieCredentialSourceAssertion)this.assertion).getVariablePrefix())) break block4;
                }
                catch (IOException iOException) {
                    throw ServerCookieCredentialSourceAssertion.a(iOException);
                }
                for (HttpCookie httpCookie : policyEnforcementContext.getRequest().getHttpCookiesKnob().getCookies()) {
                    if (!httpCookie.getCookieName().equals(this.e)) continue;
                    String string = httpCookie.getCookieValue();
                    string = this.stripSurroundingQuotationMarks(string);
                    policyEnforcementContext.setVariable(((CookieCredentialSourceAssertion)this.assertion).getVariablePrefix() + "." + httpCookie.getCookieName(), string);
                    break block3;
                }
                break block3;
            }
            this.logAndAudit((AuditDetailMessage)AssertionMessages.ASSERTION_MISCONFIGURED, g[0]);
        }
        return assertionStatus;
    }

    protected String stripSurroundingQuotationMarks(String string) {
        if (string != null && string.length() >= 2 && string.startsWith(d) && string.endsWith(d)) {
            return string.substring(1, string.length() - 1);
        }
        return string;
    }

    @Override
    protected LoginCredentials findCredentials(Message message, Map<String, String> map) throws IOException, CredentialFinderException {
        HttpCookiesKnob httpCookiesKnob = message.getHttpCookiesKnob();
        for (HttpCookie httpCookie : httpCookiesKnob.getCookies()) {
            block5: {
                if (!this.e.equalsIgnoreCase(httpCookie.getCookieName())) continue;
                String string = httpCookie.getCookieValue();
                try {
                    try {
                        if (string == null || string.length() <= 0) break block5;
                    }
                    catch (IOException iOException) {
                        throw ServerCookieCredentialSourceAssertion.a(iOException);
                    }
                    this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCOOKIE_FOUND, this.e);
                    return LoginCredentials.makeLoginCredentials((SecurityToken)new OpaqueSecurityToken(null, string.toCharArray()), CookieCredentialSourceAssertion.class);
                }
                catch (IOException iOException) {
                    throw ServerCookieCredentialSourceAssertion.a(iOException);
                }
            }
            this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCOOKIE_FOUND_EMPTY, this.e);
            return null;
        }
        this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCOOKIE_NOT_FOUND, this.e);
        return null;
    }

    @Override
    protected AssertionStatus checkCredentials(LoginCredentials loginCredentials, Map<String, String> map) throws CredentialFinderException {
        block7: {
            try {
                if (loginCredentials == null) {
                    return AssertionStatus.AUTH_REQUIRED;
                }
            }
            catch (CredentialFinderException credentialFinderException) {
                throw ServerCookieCredentialSourceAssertion.a((Exception)((Object)credentialFinderException));
            }
            char[] cArray = loginCredentials.getCredentials();
            try {
                try {
                    if (cArray != null && cArray.length >= 1) break block7;
                }
                catch (CredentialFinderException credentialFinderException) {
                    throw ServerCookieCredentialSourceAssertion.a((Exception)((Object)credentialFinderException));
                }
                throw new CredentialFinderException(g[1]);
            }
            catch (CredentialFinderException credentialFinderException) {
                throw ServerCookieCredentialSourceAssertion.a((Exception)((Object)credentialFinderException));
            }
        }
        return AssertionStatus.NONE;
    }

    @Override
    protected void challenge(PolicyEnforcementContext policyEnforcementContext, Map<String, String> map) {
    }

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

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[2];
        int n2 = 0;
        String string = "=\u001bfHQ5[\u0012\u0016*[\u00107@\u0016\u0012/F\u00104B\u0016\u0017/XY\"V]T\u0005_^)]\u0007T5[DgQ\u001c\u001b-WUgQ\u001c\u001a2[H3\u0012\u0005\u00154WQ%^\u0016Z\" \u00115MY(\\S\u0017)Q[.WS\u001d5\u001e].A\u0000\u001d(Y\u0010(@S\u0011+ND>";
        int n3 = "=\u001bfHQ5[\u0012\u0016*[\u00107@\u0016\u0012/F\u00104B\u0016\u0017/XY\"V]T\u0005_^)]\u0007T5[DgQ\u001c\u001b-WUgQ\u001c\u001a2[H3\u0012\u0005\u00154WQ%^\u0016Z\" \u00115MY(\\S\u0017)Q[.WS\u001d5\u001e].A\u0000\u001d(Y\u0010(@S\u0011+ND>".length();
        int n4 = 65;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = ServerCookieCredentialSourceAssertion.c(53, ServerCookieCredentialSourceAssertion.a(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                g = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

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

