/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.gateway.common.audit.SystemMessages
 *  com.l7tech.identity.AuthenticationException
 *  com.l7tech.identity.BadCredentialsException
 *  com.l7tech.identity.IdentityProviderConfig
 *  com.l7tech.identity.InvalidIdProviderCfgException
 *  com.l7tech.identity.ValidationException
 *  com.l7tech.identity.cert.ClientCertManager$VetoSave
 *  com.l7tech.identity.external.PolicyBackedIdentityProviderConfig
 *  com.l7tech.identity.external.VirtualPolicyUser
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.EntityHeaderSet
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.IdentityHeader
 *  com.l7tech.policy.assertion.credential.CredentialFormat
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.credential.http.HttpBasic
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.http.HttpBasicToken
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.HexUtils
 *  org.jetbrains.annotations.Nullable
 *  org.springframework.beans.BeansException
 *  org.springframework.context.ApplicationContext
 *  org.springframework.context.ApplicationContextAware
 */
package com.l7tech.server.identity.external;

import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.gateway.common.audit.SystemMessages;
import com.l7tech.identity.AuthenticationException;
import com.l7tech.identity.BadCredentialsException;
import com.l7tech.identity.IdentityProviderConfig;
import com.l7tech.identity.InvalidIdProviderCfgException;
import com.l7tech.identity.ValidationException;
import com.l7tech.identity.cert.ClientCertManager;
import com.l7tech.identity.external.PolicyBackedIdentityProviderConfig;
import com.l7tech.identity.external.VirtualPolicyUser;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.EntityHeaderSet;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.IdentityHeader;
import com.l7tech.policy.assertion.credential.CredentialFormat;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.policy.assertion.credential.http.HttpBasic;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.http.HttpBasicToken;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.identity.AuthenticationResult;
import com.l7tech.server.identity.external.PolicyBackedGroupManager;
import com.l7tech.server.identity.external.PolicyBackedIdentityProvider;
import com.l7tech.server.identity.external.PolicyBackedUserManager;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.HexUtils;
import java.math.BigInteger;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.logging.Logger;
import javax.security.auth.x500.X500Principal;
import org.jetbrains.annotations.Nullable;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class PolicyBackedIdentityProviderImpl
implements PolicyBackedIdentityProvider,
ApplicationContextAware {
    private static final Logger a;
    private Auditor b;
    private PolicyBackedIdentityProviderConfig c;
    private PolicyBackedUserManager d;
    private PolicyBackedGroupManager e;
    private static final String[] f;

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.b = new Auditor(this, applicationContext, a);
    }

    @Override
    public AuthenticationResult authenticate(LoginCredentials loginCredentials) throws AuthenticationException {
        return this.authenticate(loginCredentials, false);
    }

    @Override
    public AuthenticationResult authenticate(LoginCredentials loginCredentials, boolean bl2) throws AuthenticationException {
        CredentialFormat credentialFormat = loginCredentials.getFormat();
        try {
            if (credentialFormat == CredentialFormat.CLEARTEXT) {
                return this.d.authenticatePasswordCredentials(loginCredentials);
            }
        }
        catch (AuthenticationException authenticationException) {
            throw PolicyBackedIdentityProviderImpl.a((Exception)((Object)authenticationException));
        }
        String string = f[2] + String.valueOf(loginCredentials.getFormat());
        this.b.logAndAudit((AuditDetailMessage)SystemMessages.EXCEPTION_WARNING_WITH_MORE_INFO, string);
        throw new AuthenticationException(string);
    }

    @Override
    public VirtualPolicyUser findUserByCredential(LoginCredentials loginCredentials) throws FindException {
        block5: {
            CredentialFormat credentialFormat = loginCredentials.getFormat();
            if (credentialFormat == CredentialFormat.CLEARTEXT) {
                String string = loginCredentials.getLogin();
                try {
                    try {
                        if (string == null || string.length() <= 0) break block5;
                    }
                    catch (FindException findException) {
                        throw PolicyBackedIdentityProviderImpl.a((Exception)((Object)findException));
                    }
                    return new VirtualPolicyUser(this.c.getGoid(), string);
                }
                catch (FindException findException) {
                    throw PolicyBackedIdentityProviderImpl.a((Exception)((Object)findException));
                }
            }
        }
        return null;
    }

    @Override
    public X509Certificate findCertByIssuerAndSerial(X500Principal x500Principal, BigInteger bigInteger) throws FindException {
        return null;
    }

    @Override
    public X509Certificate findCertBySki(String string) throws FindException {
        return null;
    }

    @Override
    public X509Certificate findCertByThumbprintSHA1(String string) throws FindException {
        return null;
    }

    @Override
    public X509Certificate findCertBySubjectDn(X500Principal x500Principal) throws FindException {
        return null;
    }

    @Override
    public void setIdentityProviderConfig(IdentityProviderConfig identityProviderConfig) throws InvalidIdProviderCfgException {
        this.c = (PolicyBackedIdentityProviderConfig)identityProviderConfig;
        this.d.configure(this);
    }

    public IdentityProviderConfig getConfig() {
        return this.c;
    }

    public PolicyBackedUserManager getUserManager() {
        return this.d;
    }

    public PolicyBackedGroupManager getGroupManager() {
        return this.e;
    }

    public EntityHeaderSet<IdentityHeader> search(EntityType[] entityTypeArray, String string) throws FindException {
        EntityHeaderSet entityHeaderSet;
        block5: {
            EnumSet<EntityType> enumSet = EnumSet.noneOf(EntityType.class);
            enumSet.addAll(Arrays.asList(entityTypeArray));
            entityHeaderSet = new EntityHeaderSet();
            if (string != null) {
                String string2 = string.trim().replace("*", "");
                try {
                    try {
                        if (string2.length() <= 0 || !enumSet.contains(EntityType.USER)) break block5;
                    }
                    catch (FindException findException) {
                        throw PolicyBackedIdentityProviderImpl.a((Exception)((Object)findException));
                    }
                    entityHeaderSet.add((EntityHeader)new IdentityHeader(this.c.getGoid(), string2, EntityType.USER, string2, f[4] + string2 + "\"", string2, null));
                }
                catch (FindException findException) {
                    throw PolicyBackedIdentityProviderImpl.a((Exception)((Object)findException));
                }
            }
        }
        return entityHeaderSet;
    }

    public String getAuthRealm() {
        return HexUtils.REALM;
    }

    public void test(boolean bl2, @Nullable String string, @Nullable char[] cArray) throws InvalidIdProviderCfgException {
        try {
            if (bl2) {
                return;
            }
        }
        catch (BadCredentialsException badCredentialsException) {
            throw PolicyBackedIdentityProviderImpl.a((Exception)((Object)badCredentialsException));
        }
        try {
            if (string == null) {
                throw new InvalidIdProviderCfgException(f[0]);
            }
        }
        catch (BadCredentialsException badCredentialsException) {
            throw PolicyBackedIdentityProviderImpl.a((Exception)((Object)badCredentialsException));
        }
        try {
            if (cArray == null) {
                throw new InvalidIdProviderCfgException(f[1]);
            }
        }
        catch (BadCredentialsException badCredentialsException) {
            throw PolicyBackedIdentityProviderImpl.a((Exception)((Object)badCredentialsException));
        }
        try {
            this.getUserManager().authenticatePasswordCredentials(LoginCredentials.makeLoginCredentials((SecurityToken)new HttpBasicToken(string, cArray), HttpBasic.class));
        }
        catch (BadCredentialsException badCredentialsException) {
            throw new InvalidIdProviderCfgException(f[5] + ExceptionUtils.getMessage((Throwable)badCredentialsException), (Throwable)badCredentialsException);
        }
    }

    public void preSaveClientCert(VirtualPolicyUser virtualPolicyUser, X509Certificate[] x509CertificateArray) throws ClientCertManager.VetoSave {
        throw new ClientCertManager.VetoSave(f[3]);
    }

    public void setUserManager(PolicyBackedUserManager policyBackedUserManager) {
        this.d = policyBackedUserManager;
    }

    public void setGroupManager(PolicyBackedGroupManager policyBackedGroupManager) {
        this.e = policyBackedGroupManager;
    }

    public void validate(VirtualPolicyUser virtualPolicyUser) throws ValidationException {
    }

    public boolean hasClientCert(String string) throws AuthenticationException {
        return false;
    }

    @Override
    public Goid getDefaultRoleId() {
        return this.c.getDefaultRoleId();
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "\u0002oF/\u00101u6<W8\r$8&o_?\u00101u!*\u0012:\u0011*#*+W.C,;c @.\u00067u7 \u0012>\u00066!c.\u0012:\f)< 6\u001f(\u0002&>&+\u0012:\u0011*#*+W8MK\u0002oF/\u00101u3.A9\u0014*''o_?\u00101u!*\u0012:\u0011*#*+W.C,;c @.\u00067u7 \u0012>\u00066!c.\u0012:\f)< 6\u001f(\u0002&>&+\u0012:\u0011*#*+W8MU\u0002;F/\u000e5!c;]j\u00020!+*\\>\n&47*\u0012?\u0010,;$oG$\u00100%3 @>\u0006!u =W.\u0006+!*.^j\u0017<%&o]$C5:/&Q3N'4 $W.C5',9[.\u00067oc3\u0005*S>\u001670c!]>C6 3?]8\u0017 1c)]8C5:/&Q3N'4 $W.C\f\u0011c?@%\u0015,1&=";
                var4_3 = "\u0002oF/\u00101u6<W8\r$8&o_?\u00101u!*\u0012:\u0011*#*+W.C,;c @.\u00067u7 \u0012>\u00066!c.\u0012:\f)< 6\u001f(\u0002&>&+\u0012:\u0011*#*+W8MK\u0002oF/\u00101u3.A9\u0014*''o_?\u00101u!*\u0012:\u0011*#*+W.C,;c @.\u00067u7 \u0012>\u00066!c.\u0012:\f)< 6\u001f(\u0002&>&+\u0012:\u0011*#*+W8MU\u0002;F/\u000e5!c;]j\u00020!+*\\>\n&47*\u0012?\u0010,;$oG$\u00100%3 @>\u0006!u =W.\u0006+!*.^j\u0017<%&o]$C5:/&Q3N'4 $W.C5',9[.\u00067oc3\u0005*S>\u001670c!]>C6 3?]8\u0017 1c)]8C5:/&Q3N'4 $W.C\f\u0011c?@%\u0015,1&=".length();
                var1_4 = 75;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = PolicyBackedIdentityProviderImpl.a(v0, PolicyBackedIdentityProviderImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\n7B'\u00129<;rZ$\u001b*&??Jw\\)\n7\\#^;:;6J9\n1)2!\u000f1\u001f1$;6\u000f#\u0011x)+&G2\u0010,!=3[2Dx";
                    var4_3 = "\n7B'\u00129<;rZ$\u001b*&??Jw\\)\n7\\#^;:;6J9\n1)2!\u000f1\u001f1$;6\u000f#\u0011x)+&G2\u0010,!=3[2Dx".length();
                    var1_4 = 19;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 86;
                        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++] = PolicyBackedIdentityProviderImpl.a(v0, PolicyBackedIdentityProviderImpl.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;
            }
        }
        PolicyBackedIdentityProviderImpl.f = var5;
        PolicyBackedIdentityProviderImpl.a = Logger.getLogger(PolicyBackedIdentityProviderImpl.class.getName());
    }

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

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String a(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 -> 8;
                    case 1 -> 4;
                    case 2 -> 121;
                    case 3 -> 1;
                    case 4 -> 40;
                    case 5 -> 14;
                    default -> 30;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

