/*
 * 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.Assertion
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.CredentialFinderException
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.util.ExceptionUtils
 */
package com.l7tech.server.policy.assertion.credential;

import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
import com.l7tech.policy.assertion.Assertion;
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.server.message.AuthenticationContext;
import com.l7tech.server.message.PolicyEnforcementContext;
import com.l7tech.server.policy.assertion.AbstractServerAssertion;
import com.l7tech.util.ExceptionUtils;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public abstract class ServerCredentialSourceAssertion<A extends Assertion>
extends AbstractServerAssertion<A> {
    private static final String f = ServerCredentialSourceAssertion.b(121, ServerCredentialSourceAssertion.c("\nNplgN96D`)h\u001a3%M$?i\u001b('D$"));

    protected ServerCredentialSourceAssertion(A a10) {
        super(a10);
        if (!a10.isCredentialSource()) {
            throw new IllegalArgumentException(f + String.valueOf(a10));
        }
    }

    @Override
    public AssertionStatus checkRequest(PolicyEnforcementContext policyEnforcementContext) throws IOException, PolicyAssertionException {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        try {
            LoginCredentials loginCredentials;
            AuthenticationContext authenticationContext;
            block11: {
                authenticationContext = policyEnforcementContext.getDefaultAuthenticationContext();
                loginCredentials = authenticationContext.getLastCredentials();
                try {
                    if (loginCredentials == null || loginCredentials.getCredentialSourceAssertion().equals(this.assertion.getClass())) break block11;
                }
                catch (CredentialFinderException credentialFinderException) {
                    throw ServerCredentialSourceAssertion.a(credentialFinderException);
                }
                loginCredentials = null;
            }
            if (loginCredentials == null) {
                loginCredentials = this.findCredentials(policyEnforcementContext.getRequest(), hashMap);
            }
            if (loginCredentials == null) {
                policyEnforcementContext.setAuthenticationMissing();
                this.challenge(policyEnforcementContext, hashMap);
                this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCREDS_AUTH_REQUIRED);
                return AssertionStatus.AUTH_REQUIRED;
            }
            authenticationContext.addCredentials(loginCredentials);
            return this.checkCredentials(loginCredentials, this.findCredentialAuthParams(loginCredentials, hashMap));
        }
        catch (CredentialFinderException credentialFinderException) {
            AssertionStatus assertionStatus;
            block13: {
                block12: {
                    assertionStatus = credentialFinderException.getStatus();
                    try {
                        if (assertionStatus == null) {
                            this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_INFO, null, credentialFinderException);
                            throw new PolicyAssertionException(this.assertion, credentialFinderException.getMessage(), (Throwable)credentialFinderException);
                        }
                    }
                    catch (CredentialFinderException credentialFinderException2) {
                        throw ServerCredentialSourceAssertion.a(credentialFinderException2);
                    }
                    this.challenge(policyEnforcementContext, hashMap);
                    String string = ExceptionUtils.getMessage((Throwable)credentialFinderException);
                    try {
                        this.logAndAudit((AuditDetailMessage)AssertionMessages.EXCEPTION_INFO_WITH_MORE_INFO, new String[]{string}, (Throwable)null);
                        if (assertionStatus != AssertionStatus.AUTH_REQUIRED) break block12;
                        policyEnforcementContext.setAuthenticationMissing();
                        break block13;
                    }
                    catch (CredentialFinderException credentialFinderException3) {
                        throw ServerCredentialSourceAssertion.a(credentialFinderException3);
                    }
                }
                policyEnforcementContext.setRequestPolicyViolated();
            }
            return assertionStatus;
        }
    }

    protected Map<String, String> findCredentialAuthParams(LoginCredentials loginCredentials, Map<String, String> map) {
        return map;
    }

    protected abstract LoginCredentials findCredentials(Message var1, Map<String, String> var2) throws IOException, CredentialFinderException;

    protected abstract AssertionStatus checkCredentials(LoginCredentials var1, Map<String, String> var2) throws CredentialFinderException;

    protected abstract void challenge(PolicyEnforcementContext var1, Map<String, String> var2);

    private static CredentialFinderException a(CredentialFinderException credentialFinderException) {
        return credentialFinderException;
    }

    private static char[] c(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x23);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(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 -> 61;
                    case 1 -> 88;
                    case 2 -> 125;
                    case 3 -> 53;
                    case 4 -> 127;
                    case 5 -> 23;
                    default -> 35;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

