/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.CertUtils
 *  com.l7tech.identity.AuthenticationException
 *  com.l7tech.identity.BadCredentialsException
 *  com.l7tech.identity.IdentityProviderConfig
 *  com.l7tech.identity.InvalidIdProviderCfgException
 *  com.l7tech.identity.User
 *  com.l7tech.identity.cert.ClientCertManager$VetoSave
 *  com.l7tech.identity.fed.FederatedGroup
 *  com.l7tech.identity.fed.FederatedIdentityProviderConfig
 *  com.l7tech.identity.fed.FederatedUser
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.policy.assertion.credential.CredentialFormat
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.security.cert.CertVerifier
 *  com.l7tech.security.cert.TrustedCert
 *  com.l7tech.security.cert.TrustedCert$TrustedFor
 *  com.l7tech.security.cert.TrustedCertManager
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.SessionSecurityToken
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.HexUtils
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.l7tech.server.identity.fed;

import com.l7tech.common.io.CertUtils;
import com.l7tech.identity.AuthenticationException;
import com.l7tech.identity.BadCredentialsException;
import com.l7tech.identity.IdentityProviderConfig;
import com.l7tech.identity.InvalidIdProviderCfgException;
import com.l7tech.identity.User;
import com.l7tech.identity.cert.ClientCertManager;
import com.l7tech.identity.fed.FederatedGroup;
import com.l7tech.identity.fed.FederatedIdentityProviderConfig;
import com.l7tech.identity.fed.FederatedUser;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.policy.assertion.credential.CredentialFormat;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.security.cert.CertVerifier;
import com.l7tech.security.cert.TrustedCert;
import com.l7tech.security.cert.TrustedCertManager;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.SessionSecurityToken;
import com.l7tech.server.audit.Auditor;
import com.l7tech.server.identity.AuthenticationResult;
import com.l7tech.server.identity.ConfigurableIdentityProvider;
import com.l7tech.server.identity.PersistentIdentityProviderImpl;
import com.l7tech.server.identity.SessionAuthenticator;
import com.l7tech.server.identity.cert.TrustedCertServices;
import com.l7tech.server.identity.fed.FederatedGroupManager;
import com.l7tech.server.identity.fed.FederatedIdentityProvider;
import com.l7tech.server.identity.fed.FederatedUserManager;
import com.l7tech.server.identity.fed.b;
import com.l7tech.server.identity.fed.c;
import com.l7tech.server.security.cert.CertValidationProcessor;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.HexUtils;
import java.security.cert.X509Certificate;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.SUPPORTS, rollbackFor={Throwable.class})
public class FederatedIdentityProviderImpl
extends PersistentIdentityProviderImpl<FederatedUser, FederatedGroup, FederatedUserManager, FederatedGroupManager>
implements FederatedIdentityProvider,
ConfigurableIdentityProvider {
    private c a;
    private b b;
    private SessionAuthenticator c;
    private FederatedIdentityProviderConfig d;
    private FederatedUserManager e;
    private FederatedGroupManager f;
    private TrustedCertManager g;
    private TrustedCertServices h;
    private CertValidationProcessor i;
    private final Set<Goid> j = new HashSet<Goid>();
    private static final Logger k;
    private static final int l = 5000;
    private static final String[] n;

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

    public FederatedUserManager getUserManager() {
        return this.e;
    }

    public FederatedGroupManager getGroupManager() {
        return this.f;
    }

    @Override
    @Transactional(propagation=Propagation.SUPPORTS)
    public Set<Goid> getValidTrustedCertOids() {
        return Collections.unmodifiableSet(this.j);
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, readOnly=true, noRollbackFor={AuthenticationException.class})
    public AuthenticationResult authenticate(LoginCredentials loginCredentials) throws AuthenticationException {
        if (loginCredentials.getFormat() == CredentialFormat.CLIENTCERT) {
            AuthenticationResult authenticationResult;
            User user;
            try {
                user = this.a.a(loginCredentials);
            }
            catch (AuthenticationException authenticationException) {
                String[] stringArray = n;
                throw new AuthenticationException(stringArray[12] + ExceptionUtils.getMessage((Throwable)authenticationException), (Throwable)authenticationException);
            }
            catch (Exception exception) {
                String[] stringArray = n;
                throw new AuthenticationException(stringArray[13] + ExceptionUtils.getMessage((Throwable)exception), (Throwable)exception);
            }
            try {
                authenticationResult = user == null ? null : new AuthenticationResult(user, loginCredentials.getSecurityTokens(), loginCredentials.getClientCert(), false);
            }
            catch (AuthenticationException authenticationException) {
                throw FederatedIdentityProviderImpl.a((Exception)((Object)authenticationException));
            }
            return authenticationResult;
        }
        if (loginCredentials.getFormat() == CredentialFormat.SAML) {
            AuthenticationResult authenticationResult;
            User user = this.b.a(loginCredentials);
            try {
                authenticationResult = user == null ? null : new AuthenticationResult(user, loginCredentials.getSecurityTokens(), loginCredentials.getClientCert(), false);
            }
            catch (AuthenticationException authenticationException) {
                throw FederatedIdentityProviderImpl.a((Exception)((Object)authenticationException));
            }
            return authenticationResult;
        }
        if (loginCredentials.getFormat() == CredentialFormat.SESSIONTOKEN) {
            AuthenticationResult authenticationResult;
            FederatedUser federatedUser = this.a(loginCredentials);
            try {
                authenticationResult = federatedUser == null ? null : this.c.authenticateSessionCredentials(loginCredentials, (User)federatedUser);
            }
            catch (AuthenticationException authenticationException) {
                throw FederatedIdentityProviderImpl.a((Exception)((Object)authenticationException));
            }
            return authenticationResult;
        }
        throw new BadCredentialsException(n[8]);
    }

    @Override
    public FederatedUser findUserByCredential(LoginCredentials loginCredentials) {
        return null;
    }

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

    public void test(boolean bl2, String string, char[] cArray) {
    }

    public void preSaveClientCert(FederatedUser federatedUser, X509Certificate[] x509CertificateArray) throws ClientCertManager.VetoSave {
        FederatedUser federatedUser2 = (FederatedUser)this.e.cast((User)federatedUser);
        String string = federatedUser2.getSubjectDn();
        String string2 = x509CertificateArray[0].getSubjectDN().getName();
        try {
            if (!CertUtils.isEqualDNCanonical((String)string, (String)string2)) {
                throw new ClientCertManager.VetoSave(n[2] + string + n[18] + string2 + "'");
            }
        }
        catch (FindException findException) {
            throw FederatedIdentityProviderImpl.a((Exception)((Object)findException));
        }
        try {
            try {
                if (!this.j.isEmpty()) {
                    this.a(x509CertificateArray);
                }
            }
            catch (ClientCertManager.VetoSave vetoSave) {
                throw FederatedIdentityProviderImpl.a((Exception)((Object)vetoSave));
            }
        }
        catch (FindException findException) {
            throw new ClientCertManager.VetoSave(n[0] + ExceptionUtils.getMessage((Throwable)findException), (Throwable)findException);
        }
    }

    private void a(X509Certificate[] x509CertificateArray) throws FindException, ClientCertManager.VetoSave {
        X509Certificate x509Certificate = x509CertificateArray[0];
        String string = CertUtils.getIssuerDN((X509Certificate)x509Certificate);
        Collection<TrustedCert> collection = this.h.getCertsBySubjectDnFiltered(string, true, EnumSet.of(TrustedCert.TrustedFor.SIGNING_CLIENT_CERTS), null);
        for (TrustedCert trustedCert : collection) {
            try {
                CertVerifier.cachedVerify((X509Certificate)x509Certificate, (X509Certificate)trustedCert.getCertificate());
                return;
            }
            catch (Exception exception) {
                String string2 = x509Certificate.getSubjectDN().toString();
                String string3 = ExceptionUtils.getMessage((Throwable)exception);
                String string4 = string;
                String string5 = string2;
                String[] stringArray = n;
                k.log(Level.WARNING, stringArray[7] + string5 + stringArray[19] + string4 + stringArray[15] + string3, exception);
            }
        }
        throw new ClientCertManager.VetoSave(n[1]);
    }

    public void setTrustedCertManager(TrustedCertManager trustedCertManager) {
        this.g = trustedCertManager;
    }

    @Override
    public void setIdentityProviderConfig(IdentityProviderConfig identityProviderConfig) throws InvalidIdProviderCfgException {
        Goid[] goidArray;
        block13: {
            block12: {
                try {
                    if (!(identityProviderConfig instanceof FederatedIdentityProviderConfig)) break block12;
                    this.d = (FederatedIdentityProviderConfig)identityProviderConfig;
                    break block13;
                }
                catch (FindException findException) {
                    throw FederatedIdentityProviderImpl.a((Exception)((Object)findException));
                }
            }
            String[] stringArray = n;
            throw new InvalidIdProviderCfgException(stringArray[5]);
        }
        try {
            if (this.e == null) {
                throw new InvalidIdProviderCfgException(n[16]);
            }
        }
        catch (FindException findException) {
            throw FederatedIdentityProviderImpl.a((Exception)((Object)findException));
        }
        try {
            if (this.f == null) {
                throw new InvalidIdProviderCfgException(n[21]);
            }
        }
        catch (FindException findException) {
            throw FederatedIdentityProviderImpl.a((Exception)((Object)findException));
        }
        this.e.configure(this);
        this.f.configure(this);
        for (Goid goid : goidArray = this.d.getTrustedCertGoids()) {
            String[] stringArray = n;
            String string = stringArray[20] + this.d.getName() + stringArray[22] + String.valueOf(goid);
            try {
                TrustedCert findException = (TrustedCert)this.g.getCachedEntity(goid, 5000);
                if (findException == null) {
                    k.log(Level.WARNING, string + stringArray[17]);
                    continue;
                }
                this.j.add(goid);
            }
            catch (FindException findException) {
                k.log(Level.SEVERE, string + n[6], findException);
            }
        }
        Auditor auditor = new Auditor(this, this.applicationContext, k);
        this.a = new c(this, this.h, this.clientCertManager, this.i, auditor, this.j);
        this.b = new b(this, this.h, this.clientCertManager, this.i, auditor, this.j);
        this.c = new SessionAuthenticator(this.d.getGoid());
    }

    public void setUserManager(FederatedUserManager federatedUserManager) {
        this.e = federatedUserManager;
    }

    public void setGroupManager(FederatedGroupManager federatedGroupManager) {
        this.f = federatedGroupManager;
    }

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

    public void setCertValidationProcessor(CertValidationProcessor certValidationProcessor) {
        this.i = certValidationProcessor;
    }

    public void setTrustedCertServices(TrustedCertServices trustedCertServices) {
        this.h = trustedCertServices;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        try {
            super.afterPropertiesSet();
            if (this.g == null) {
                throw new IllegalArgumentException(n[3]);
            }
        }
        catch (Exception exception) {
            throw FederatedIdentityProviderImpl.a(exception);
        }
        try {
            if (this.h == null) {
                throw new IllegalArgumentException(n[4]);
            }
        }
        catch (Exception exception) {
            throw FederatedIdentityProviderImpl.a(exception);
        }
        try {
            if (this.i == null) {
                throw new IllegalArgumentException(n[14]);
            }
        }
        catch (Exception exception) {
            throw FederatedIdentityProviderImpl.a(exception);
        }
    }

    private FederatedUser a(LoginCredentials loginCredentials) throws AuthenticationException {
        FederatedUser federatedUser;
        block12: {
            SecurityToken securityToken = loginCredentials.getSecurityToken();
            try {
                if (!(securityToken instanceof SessionSecurityToken)) {
                    throw new BadCredentialsException(n[9]);
                }
            }
            catch (FindException findException) {
                throw FederatedIdentityProviderImpl.a((Exception)((Object)findException));
            }
            SessionSecurityToken sessionSecurityToken = (SessionSecurityToken)securityToken;
            try {
                if (!sessionSecurityToken.getProviderId().equals((Object)this.getConfig().getGoid())) {
                    return null;
                }
            }
            catch (FindException findException) {
                throw FederatedIdentityProviderImpl.a((Exception)((Object)findException));
            }
            String string = sessionSecurityToken.getUserId();
            if (string != null) {
                try {
                    federatedUser = (FederatedUser)this.getUserManager().findByPrimaryKey(string);
                }
                catch (FindException findException) {
                    String[] stringArray = n;
                    throw new AuthenticationException(stringArray[10] + ExceptionUtils.getMessage((Throwable)findException), (Throwable)findException);
                }
                try {
                    if (federatedUser == null) {
                        throw new AuthenticationException(n[11]);
                    }
                    break block12;
                }
                catch (FindException findException) {
                    throw FederatedIdentityProviderImpl.a((Exception)((Object)findException));
                }
            }
            federatedUser = new FederatedUser(this.getConfig().getGoid(), null);
        }
        return federatedUser;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[23];
                var3_1 = 0;
                var2_2 = "\u000e,\u001bc \"\u0013/-Zm#(X{7\n!85F(6\u001fel$V)6\u0013g%$R/'\t;lc\u000e1\u001fsk4\u00138'\bul0R(b\u0014n8g@2%\u0014d(gQ\"b\u001bol.@(7\u001fsl$V)6Zu>2@/'\u001e!.>\u0013/*\u0013rl.W>,\u000eh8>\u0013+0\u0015w%#V)b\u000enl4Z<,Zb .V56Zb)5G(\u0019\u000e1\u001fsk4\u0013\u0003lO1ug`. \u0010d/3\u0013\u001f\fZ&+\u000f*\u001f!\u00185F(6\u001fel\u0004V)6\u0013g%$R/'ZL-)R<'\b!%4\u0013)'\u000bt%5V?-\u000f*\u001f!\u00185F(6\u001fel\u0004V)6\u0013g%$R/'ZR)5E2!\u001frl&A>b\bd=2Z)'\u001e\"\u001d'\u001ed>&G>&ZQ>(E2&\u001fsl\u0004\\5$\u0013fl5V*7\u0013s)#\u001awb\ri%$[{!\u0015t #\u00135-\u000e!.\"\u0013=-\u000fo(\u001f\u000e,\u001bc \"\u0013/-Zw)5Z=;Zb)5G{5\u0013u$gw\u0015b]@\u0018#\u0014&8gR.6\u0012d\"3Z8#\u000edl0Z/*\u0015t8g`\u001a\u000f6!#5\u0013\u0003lO1ugP>0\u000eh*.P:6\u001f!/5V?'\u0014u%&_(\u0010\u000e,\u001fy<\"P/'\u001e!8(X>,'\u001e0\bn>gR.6\u0012n>.I2,\u001d!?\"@(+\u0015ol$A>&\u001fo8.R71@!6\u0018-\u000fm()\u0014/b\u001bt8/\\)+\u0000dl4V(1\u0013n\"gP)'\u001ed\"3Z:.\t;l2@>0Zo#3\u0013=-\u000fo(&\u0018-\u000fm()\u0014/b\u001bt8/\\)+\u0000dl\u001f\u001dnrC!/5V?'\u0014u%&_(xZ%\u001e0\bn>gR.6\u0012n>.I2,\u001d!\u0014i\u0006k{Zb>\"W>,\u000eh-+@ab0\u000f*\u001f!\u000f\"A/+\u001ch/&G>b,` .W:6\u0013n\"gc)-\u0019d?4\\)b\u0013rl5V*7\u0013s)#\u0003|xZ\u0016\u000e1\u001fs\u0001&]:%\u001fsl.@{,\u0015ul4V/\u0018wb\ri%$[{,\u0015! (]<'\b!)?Z(6\t\"|&\u0015d?)\u0014/b\u0017`8$[{!\u001fs8`@{\u0011\u000fc&\"P/b>Ol`'|b\u001bf-.](6Zu>2@/'\u001e!%4@.'\b!/\"A/b\rh8/\u0013\u001f\fZ&\u001d\u001d'\u001ed>&G>&ZH(\"]/+\u000exl\u0017A44\u0013e)5\u0013|";
                var4_3 = "\u000e,\u001bc \"\u0013/-Zm#(X{7\n!85F(6\u001fel$V)6\u0013g%$R/'\t;lc\u000e1\u001fsk4\u00138'\bul0R(b\u0014n8g@2%\u0014d(gQ\"b\u001bol.@(7\u001fsl$V)6Zu>2@/'\u001e!.>\u0013/*\u0013rl.W>,\u000eh8>\u0013+0\u0015w%#V)b\u000enl4Z<,Zb .V56Zb)5G(\u0019\u000e1\u001fsk4\u0013\u0003lO1ug`. \u0010d/3\u0013\u001f\fZ&+\u000f*\u001f!\u00185F(6\u001fel\u0004V)6\u0013g%$R/'ZL-)R<'\b!%4\u0013)'\u000bt%5V?-\u000f*\u001f!\u00185F(6\u001fel\u0004V)6\u0013g%$R/'ZR)5E2!\u001frl&A>b\bd=2Z)'\u001e\"\u001d'\u001ed>&G>&ZQ>(E2&\u001fsl\u0004\\5$\u0013fl5V*7\u0013s)#\u001awb\ri%$[{!\u0015t #\u00135-\u000e!.\"\u0013=-\u000fo(\u001f\u000e,\u001bc \"\u0013/-Zw)5Z=;Zb)5G{5\u0013u$gw\u0015b]@\u0018#\u0014&8gR.6\u0012d\"3Z8#\u000edl0Z/*\u0015t8g`\u001a\u000f6!#5\u0013\u0003lO1ugP>0\u000eh*.P:6\u001f!/5V?'\u0014u%&_(\u0010\u000e,\u001fy<\"P/'\u001e!8(X>,'\u001e0\bn>gR.6\u0012n>.I2,\u001d!?\"@(+\u0015ol$A>&\u001fo8.R71@!6\u0018-\u000fm()\u0014/b\u001bt8/\\)+\u0000dl4V(1\u0013n\"gP)'\u001ed\"3Z:.\t;l2@>0Zo#3\u0013=-\u000fo(&\u0018-\u000fm()\u0014/b\u001bt8/\\)+\u0000dl\u001f\u001dnrC!/5V?'\u0014u%&_(xZ%\u001e0\bn>gR.6\u0012n>.I2,\u001d!\u0014i\u0006k{Zb>\"W>,\u000eh-+@ab0\u000f*\u001f!\u000f\"A/+\u001ch/&G>b,` .W:6\u0013n\"gc)-\u0019d?4\\)b\u0013rl5V*7\u0013s)#\u0003|xZ\u0016\u000e1\u001fs\u0001&]:%\u001fsl.@{,\u0015ul4V/\u0018wb\ri%$[{,\u0015! (]<'\b!)?Z(6\t\"|&\u0015d?)\u0014/b\u0017`8$[{!\u001fs8`@{\u0011\u000fc&\"P/b>Ol`'|b\u001bf-.](6Zu>2@/'\u001e!%4@.'\b!/\"A/b\rh8/\u0013\u001f\fZ&\u001d\u001d'\u001ed>&G>&ZH(\"]/+\u000exl\u0017A44\u0013e)5\u0013|".length();
                var1_4 = 40;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = FederatedIdentityProviderImpl.b(v0, FederatedIdentityProviderImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0006*\u000fn&\u0010H/9\u0007~$}@2x\u000et\"}Z$,\u001afx\u0012~08[2x\u0014tv\t[4+\u0014~2}j$*\u0014;u";
                    var4_3 = "\u0006*\u000fn&\u0010H/9\u0007~$}@2x\u000et\"}Z$,\u001afx\u0012~08[2x\u0014tv\t[4+\u0014~2}j$*\u0014;u".length();
                    var1_4 = 23;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 126;
                        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++] = FederatedIdentityProviderImpl.b(v0, FederatedIdentityProviderImpl.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;
            }
        }
        FederatedIdentityProviderImpl.n = var5;
        FederatedIdentityProviderImpl.k = Logger.getLogger(FederatedIdentityProviderImpl.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] ^ 0x57);
        }
        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 -> 63;
                    case 1 -> 38;
                    case 2 -> 30;
                    case 3 -> 101;
                    case 4 -> 40;
                    case 5 -> 35;
                    default -> 87;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

