/*
 * 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.HttpRequestKnob
 *  com.l7tech.message.HttpResponseKnob
 *  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.HttpCredentialSourceAssertion
 */
package com.l7tech.server.policy.assertion.credential.http;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.HttpRequestKnob;
import com.l7tech.message.HttpResponseKnob;
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.HttpCredentialSourceAssertion;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.credential.ServerCredentialSourceAssertion;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map;

public abstract class ServerHttpCredentialSource<AT extends HttpCredentialSourceAssertion>
extends ServerCredentialSourceAssertion<AT> {
    private static final String[] e;

    protected ServerHttpCredentialSource(AT AT) {
        super(AT);
    }

    @Override
    public AssertionStatus checkCredentials(LoginCredentials loginCredentials, Map<String, String> map) throws CredentialFinderException {
        String string;
        String string2;
        block8: {
            try {
                if (loginCredentials == null) {
                    return AssertionStatus.AUTH_REQUIRED;
                }
            }
            catch (CredentialFinderException credentialFinderException) {
                throw ServerHttpCredentialSource.a((Exception)((Object)credentialFinderException));
            }
            string2 = loginCredentials.getRealm();
            string = this.realm();
            try {
                if (string2 != null && string2.length() != 0) break block8;
            }
            catch (CredentialFinderException credentialFinderException) {
                throw ServerHttpCredentialSource.a((Exception)((Object)credentialFinderException));
            }
            string2 = string;
        }
        try {
            if (string2.equals(string)) {
                return this.checkAuthParams(map);
            }
        }
        catch (CredentialFinderException credentialFinderException) {
            throw ServerHttpCredentialSource.a((Exception)((Object)credentialFinderException));
        }
        String[] stringArray = e;
        throw new CredentialFinderException(stringArray[0] + string + stringArray[2] + string2, AssertionStatus.AUTH_FAILED);
    }

    @Override
    protected void challenge(PolicyEnforcementContext policyEnforcementContext, Map<String, String> map) {
        Map<String, String> map2;
        Iterator<String> iterator;
        String string = this.scheme();
        StringBuilder stringBuilder = new StringBuilder(string);
        stringBuilder.append(" ");
        String string2 = this.realm();
        if (string2 != null && string2.length() > 0) {
            stringBuilder.append(HttpCredentialSourceAssertion.PARAM_REALM);
            stringBuilder.append("=");
            stringBuilder.append(this.a(string2));
        }
        if ((iterator = (map2 = this.challengeParams(policyEnforcementContext.getRequest(), map)).keySet().iterator()).hasNext()) {
            String[] stringArray = e;
            stringBuilder.append(stringArray[1]);
        }
        while (iterator.hasNext()) {
            String string3 = iterator.next();
            String string4 = map2.get(string3);
            if (string3 == null || string4 == null) continue;
            stringBuilder.append(string3);
            stringBuilder.append("=");
            stringBuilder.append(this.a(string4));
            if (!iterator.hasNext()) continue;
            stringBuilder.append(e[3]);
        }
        String string5 = stringBuilder.toString();
        this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCREDS_CHALLENGING, string5);
        HttpResponseKnob httpResponseKnob = policyEnforcementContext.getResponse().getHttpResponseKnob();
        httpResponseKnob.addChallenge(string5);
    }

    protected abstract String realm();

    private String a(String string) {
        if (string == null) {
            return e[4];
        }
        return "\"" + string + "\"";
    }

    protected abstract AssertionStatus checkAuthParams(Map<String, String> var1);

    protected abstract Map<String, String> challengeParams(Message var1, Map<String, String> var2);

    protected abstract String scheme();

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        try {
            if (policyEnforcementContext.getRequest().getKnob(HttpRequestKnob.class) == null) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTP_CS_CANNOT_EXTRACT_CREDENTIALS);
                return AssertionStatus.NOT_APPLICABLE;
            }
        }
        catch (IOException iOException) {
            throw ServerHttpCredentialSource.a(iOException);
        }
        return super.checkRequest(policyEnforcementContext);
    }

    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 = "&]\u000b:|\u0001x\u001dK\u00077eB}N\u0018/.aDv\u0000]\u000ev6\u0002X\u0018\bS\u0014J1~U5S";
                var4_3 = "&]\u000b:|\u0001x\u001dK\u00077eB}N\u0018/.aDv\u0000]\u000ev6\u0002X\u0018\bS\u0014J1~U5S".length();
                var1_4 = 26;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServerHttpCredentialSource.c(v0, ServerHttpCredentialSource.d(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "J\n\u0002D\b";
                    var4_3 = "J\n\u0002D\b".length();
                    var1_4 = 2;
                    var0_5 = -1;
lbl22:
                    // 2 sources

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

    private static char[] d(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x65);
        }
        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 -> 4;
                    case 1 -> 72;
                    case 2 -> 26;
                    case 3 -> 38;
                    case 4 -> 97;
                    case 5 -> 81;
                    default -> 101;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

