/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.WhirlycacheFactory
 *  com.l7tech.identity.AuthenticationException
 *  com.l7tech.identity.BadCredentialsException
 *  com.l7tech.identity.IdentityProvider
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.policy.assertion.credential.CredentialFormat
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.TimeSource
 *  com.whirlycott.cache.Cache
 */
package com.l7tech.server.identity;

import com.l7tech.common.io.WhirlycacheFactory;
import com.l7tech.identity.AuthenticationException;
import com.l7tech.identity.BadCredentialsException;
import com.l7tech.identity.IdentityProvider;
import com.l7tech.objectmodel.Goid;
import com.l7tech.policy.assertion.credential.CredentialFormat;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.server.identity.AuthenticatingIdentityProvider;
import com.l7tech.server.identity.AuthenticationResult;
import com.l7tech.server.identity.AuthenticationResultFailure;
import com.l7tech.server.identity.a;
import com.l7tech.server.identity.b;
import com.l7tech.server.identity.internal.InternalIdentityProvider;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.TimeSource;
import com.whirlycott.cache.Cache;
import java.util.logging.Level;
import java.util.logging.Logger;

public final class AuthCache {
    private static final Logger a;
    private static final int b = 59;
    private static final int c = 61;
    public static final int SUCCESS_CACHE_SIZE;
    public static final int FAILURE_CACHE_SIZE;
    private static final boolean d;
    private final TimeSource e;
    private final Cache f;
    private final Cache g;
    private final boolean h;
    private final boolean i;
    private static final String[] j;

    AuthCache() {
        this(j[10], new TimeSource(), SUCCESS_CACHE_SIZE, 59, FAILURE_CACHE_SIZE, 61);
    }

    AuthCache(String string, TimeSource timeSource, int n2, int n3, int n4, int n5) {
        Cache cache;
        String[] stringArray;
        Cache cache2;
        this.e = timeSource;
        if (n2 < 1) {
            cache2 = null;
        } else {
            stringArray = j;
            cache2 = WhirlycacheFactory.createCache((String)(string + stringArray[5]), (int)n2, (int)n3, (String)WhirlycacheFactory.POLICY_LFU);
        }
        this.f = cache2;
        boolean bl2 = this.h = this.f == null;
        if (this.h && a.isLoggable(Level.WARNING)) {
            stringArray = j;
            a.log(Level.WARNING, stringArray[0]);
        }
        if (n4 < 1) {
            cache = null;
        } else {
            stringArray = j;
            cache = WhirlycacheFactory.createCache((String)(string + stringArray[8]), (int)n4, (int)n5, (String)WhirlycacheFactory.POLICY_LFU);
        }
        this.g = cache;
        boolean bl3 = this.i = this.g == null;
        if (this.i && a.isLoggable(Level.WARNING)) {
            a.log(Level.WARNING, j[11]);
        }
    }

    public static int getDefaultAuthSuccessCacheTime() {
        return ConfigFactory.getIntProperty((String)j[3], (int)1000);
    }

    public static int getDefaultAuthFailureCacheTime() {
        return ConfigFactory.getIntProperty((String)j[16], (int)1000);
    }

    void a() {
        WhirlycacheFactory.shutdown((Cache)this.f);
        WhirlycacheFactory.shutdown((Cache)this.g);
    }

    public static AuthCache getInstance() {
        return com.l7tech.server.identity.b.a;
    }

    public AuthenticationResult getCachedAuthResult(LoginCredentials loginCredentials, IdentityProvider identityProvider) throws AuthenticationException {
        return this.getCachedAuthResult(loginCredentials, identityProvider, AuthCache.getDefaultAuthSuccessCacheTime(), AuthCache.getDefaultAuthFailureCacheTime());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Unable to fully structure code
     */
    public AuthenticationResult getCachedAuthResult(LoginCredentials var1_1, IdentityProvider var2_2, int var3_3, int var4_4) throws AuthenticationException {
        block31: {
            block30: {
                block29: {
                    block33: {
                        block32: {
                            var5_5 = var1_1.toString();
                            var6_6 = var2_2.getConfig().getGoid();
                            var7_7 = new a(var6_6, var1_1);
                            var8_8 = this.a(var7_7, var5_5, var2_2, var3_3, var4_4);
                            try {
                                if (var8_8 instanceof AuthenticationResult) {
                                    return new AuthenticationResult((AuthenticationResult)var8_8, var1_1.getSecurityTokens());
                                }
                            }
                            catch (AuthenticationException v0) {
                                throw AuthCache.a(v0);
                            }
                            try {
                                if (var8_8 instanceof AuthenticationResultFailure) {
                                    throw new BadCredentialsException(((AuthenticationResultFailure)var8_8).getFailureReason());
                                }
                            }
                            catch (AuthenticationException v1) {
                                throw AuthCache.a(v1);
                            }
                            try {
                                if (var8_8 != null) {
                                    return null;
                                }
                            }
                            catch (AuthenticationException v2) {
                                throw AuthCache.a(v2);
                            }
                            if (!(var2_2 instanceof InternalIdentityProvider)) break block29;
                            if (var1_1.getFormat() == CredentialFormat.CLEARTEXT) ** GOTO lbl42
                            break block32;
                            catch (AuthenticationException v3) {
                                throw AuthCache.a(v3);
                            }
                        }
                        if (var1_1.getFormat() == CredentialFormat.BASIC) ** GOTO lbl42
                        break block33;
                        catch (AuthenticationException v4) {
                            throw AuthCache.a(v4);
                        }
                    }
                    try {
                        block34: {
                            if (var1_1.getFormat() != CredentialFormat.DIGEST) break block29;
                            break block34;
                            catch (AuthenticationException v5) {
                                throw AuthCache.a(v5);
                            }
                        }
                        v6 = true;
                        break block30;
                    }
                    catch (AuthenticationException v7) {
                        throw AuthCache.a(v7);
                    }
                }
                v6 = false;
            }
            var9_9 = v6;
            if (!AuthCache.d) break block31;
            try {
                block35: {
                    if (this.h) break block31;
                    break block35;
                    catch (AuthenticationException v8) {
                        throw AuthCache.a(v8);
                    }
                }
                if (var9_9) break block31;
            }
            catch (AuthenticationException v9) {
                throw AuthCache.a(v9);
            }
            var11_11 = var10_10 = (Goid.toString((Goid)var6_6) + var5_5).intern();
            synchronized (var11_11) {
                var8_8 = this.a(var7_7, var5_5, var2_2, var3_3, var4_4);
                if (var8_8 instanceof AuthenticationResult) {
                    return new AuthenticationResult((AuthenticationResult)var8_8, var1_1.getSecurityTokens());
                }
                try {
                    if (var8_8 instanceof AuthenticationResultFailure) {
                        throw new BadCredentialsException(((AuthenticationResultFailure)var8_8).getFailureReason());
                    }
                }
                catch (AuthenticationException v10) {
                    throw AuthCache.a(v10);
                }
                if (var8_8 != null) {
                    return null;
                }
                return this.a(var1_1, var5_5, var7_7, var2_2);
            }
        }
        return this.a(var1_1, var5_5, var7_7, var2_2);
    }

    private AuthenticationResult a(LoginCredentials loginCredentials, String string, a a10, IdentityProvider identityProvider) throws AuthenticationException {
        AuthenticationException authenticationException;
        AuthenticationResult authenticationResult;
        block18: {
            String string2;
            block19: {
                block17: {
                    String string3;
                    Long l2;
                    AuthenticationResultFailure authenticationResultFailure;
                    AuthenticationResultFailure authenticationResultFailure2;
                    authenticationResult = null;
                    authenticationException = null;
                    try {
                        authenticationResult = ((AuthenticatingIdentityProvider)identityProvider).authenticate(loginCredentials);
                    }
                    catch (AuthenticationException authenticationException2) {
                        authenticationException = authenticationException2;
                    }
                    string2 = null;
                    try {
                        if (this.i || authenticationResult != null) break block17;
                    }
                    catch (AuthenticationException authenticationException3) {
                        throw AuthCache.a(authenticationException3);
                    }
                    String[] stringArray = j;
                    string2 = stringArray[12];
                    try {
                        AuthenticationResultFailure authenticationResultFailure3;
                        authenticationResultFailure2 = authenticationResultFailure3;
                        authenticationResultFailure = authenticationResultFailure3;
                        l2 = this.b();
                        string3 = authenticationException != null ? authenticationException.getMessage() : null;
                    }
                    catch (AuthenticationException authenticationException4) {
                        throw AuthCache.a(authenticationException4);
                    }
                    authenticationResultFailure2(l2, string3);
                    AuthenticationResultFailure authenticationResultFailure4 = authenticationResultFailure;
                    this.g.store((Object)a10, (Object)authenticationResultFailure4);
                    break block19;
                }
                if (!this.h) {
                    String[] stringArray = j;
                    string2 = stringArray[9];
                    this.f.store((Object)a10, (Object)authenticationResult);
                }
            }
            try {
                try {
                    if (string2 == null || !a.isLoggable(Level.FINE)) break block18;
                }
                catch (AuthenticationException authenticationException5) {
                    throw AuthCache.a(authenticationException5);
                }
                a.log(Level.FINE, j[14], new String[]{string2, string, identityProvider.getConfig().getName()});
            }
            catch (AuthenticationException authenticationException6) {
                throw AuthCache.a(authenticationException6);
            }
        }
        try {
            if (authenticationException != null) {
                throw authenticationException;
            }
        }
        catch (AuthenticationException authenticationException7) {
            throw AuthCache.a(authenticationException7);
        }
        try {
            if (authenticationResult != null) {
                return new AuthenticationResult(authenticationResult, loginCredentials.getSecurityTokens());
            }
        }
        catch (AuthenticationException authenticationException8) {
            throw AuthCache.a(authenticationException8);
        }
        return null;
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private Object a(a a10, String string, IdentityProvider identityProvider, int n2, int n3) {
        Object object;
        long l2;
        String[] stringArray;
        long l3;
        Object object2;
        Object object3;
        if (this.h && this.i) {
            return null;
        }
        Long l4 = null;
        AuthenticationResult authenticationResult = null;
        if (!this.h && (object3 = this.f.retrieve((Object)a10)) != null && object3 instanceof AuthenticationResult) {
            authenticationResult = (AuthenticationResult)object3;
        }
        object3 = null;
        if (authenticationResult == null) {
            if (this.i) return null;
            object2 = this.g.retrieve((Object)a10);
            if (object2 == null || !(object2 instanceof AuthenticationResultFailure)) return null;
            object3 = (AuthenticationResultFailure)object2;
            l4 = ((AuthenticationResultFailure)object3).getCacheFailureTime();
        }
        if (l4 != null) {
            l3 = l4;
            stringArray = j;
            object2 = stringArray[6];
            l2 = n3;
            object = object3;
        } else {
            l3 = authenticationResult.a();
            stringArray = j;
            object2 = stringArray[13];
            l2 = n2;
            object = authenticationResult;
        }
        if (l3 + l2 > this.b()) {
            if (!a.isLoggable(Level.FINE)) return object;
            stringArray = j;
            a.log(Level.FINE, stringArray[7], new String[]{object2, string, identityProvider.getConfig().getName()});
            return object;
        }
        if (!a.isLoggable(Level.FINE)) return null;
        a.log(Level.FINE, j[4], new String[]{object2, string, identityProvider.getConfig().getName()});
        return null;
    }

    private long b() {
        return this.e.currentTimeMillis();
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[17];
                var3_1 = 0;
                var2_2 = "W<.I\u00022Gb<!\n\u00064@l,#^\u000e\"Up \"DG\"Ug!$D\u0000a\\e:mH\u0002$Z$-$Y\u0006#Xa-m\\\u000e \u0014g&#L\u000e&Av(9C\b/0g& \u0004\u000bv@a*%\u0004\u0014$Fr,?\u0004\u000e%Qj=$^\u001eoUq=%i\u0006\"\\a\u00048^\u00029qj(/F\u0002%\u0019e<9B$ Wl,\u000bK\u000e-Av,\u000eK\u0004)QW 7O\u0017e<9B$ Wl,\u0000K\u001f\u0012Ag*(Y\u0014\u0015]i,=G(.B\u0002%\u0014\u007fy0\n\u0001.F$2|WG(G$:9K\u000b$\u0014k'mc\u0003\u0011\u0014&2\u007fWEz\u0014s !FG3Qe<9B\u0002/@m*,^\u0002\b*:8I\u0004$Gw\u0007b($F\u00123Q%Q:$D\u0000aWe*%O\u0003aO44mL\b3\u0014\u007fx0\n\b/\u0014M-\u001d\nE:\u0006yk\b*/,C\u000b4Fa\nw<.I\u00022Gb<!\tE<9B$ Wl,AB($F\u0002%\u0014e<9B\u0002/@m*,^\u000e.Z$*,I\u000f(Zci%K\u0014aVa,#\n\u0003(Ge+!O\u0003aBm(mI\b/Rm.8X\u00065]k'\u0006b($F\u0002%\u0007w<.I\u00022G/G(.B\u000e/S$2}WG Ap!(D\u0013(We=$E\taRk;mQV<\u0014k'mc\u0003\u0011\u0014&2\u007fWE";
                var4_3 = "W<.I\u00022Gb<!\n\u00064@l,#^\u000e\"Up \"DG\"Ug!$D\u0000a\\e:mH\u0002$Z$-$Y\u0006#Xa-m\\\u000e \u0014g&#L\u000e&Av(9C\b/0g& \u0004\u000bv@a*%\u0004\u0014$Fr,?\u0004\u000e%Qj=$^\u001eoUq=%i\u0006\"\\a\u00048^\u00029qj(/F\u0002%\u0019e<9B$ Wl,\u000bK\u000e-Av,\u000eK\u0004)QW 7O\u0017e<9B$ Wl,\u0000K\u001f\u0012Ag*(Y\u0014\u0015]i,=G(.B\u0002%\u0014\u007fy0\n\u0001.F$2|WG(G$:9K\u000b$\u0014k'mc\u0003\u0011\u0014&2\u007fWEz\u0014s !FG3Qe<9B\u0002/@m*,^\u0002\b*:8I\u0004$Gw\u0007b($F\u00123Q%Q:$D\u0000aWe*%O\u0003aO44mL\b3\u0014\u007fx0\n\b/\u0014M-\u001d\nE:\u0006yk\b*/,C\u000b4Fa\nw<.I\u00022Gb<!\tE<9B$ Wl,AB($F\u0002%\u0014e<9B\u0002/@m*,^\u000e.Z$*,I\u000f(Zci%K\u0014aVa,#\n\u0003(Ge+!O\u0003aBm(mI\b/Rm.8X\u00065]k'\u0006b($F\u0002%\u0007w<.I\u00022G/G(.B\u000e/S$2}WG Ap!(D\u0013(We=$E\taRk;mQV<\u0014k'mc\u0003\u0011\u0014&2\u007fWE".length();
                var1_4 = 69;
                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++] = AuthCache.a(v0, AuthCache.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "$}x\u0003ea\u0016-m_\u001eEc\u00106{O\nEh\u0010\u0016av\u000e\u0017$}x\u0003ea\u0016-mA\n^F\u0014,dy\u0019CT\u001c(m";
                    var4_3 = "$}x\u0003ea\u0016-m_\u001eEc\u00106{O\nEh\u0010\u0016av\u000e\u0017$}x\u0003ea\u0016-mA\n^F\u0014,dy\u0019CT\u001c(m".length();
                    var1_4 = 25;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 49;
                        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++] = AuthCache.a(v0, AuthCache.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;
            }
        }
        AuthCache.j = var5;
        AuthCache.a = Logger.getLogger(AuthCache.class.getName());
        var0_6 = AuthCache.j;
        AuthCache.SUCCESS_CACHE_SIZE = ConfigFactory.getIntProperty((String)var0_6[15], (int)200);
        AuthCache.FAILURE_CACHE_SIZE = ConfigFactory.getIntProperty((String)var0_6[2], (int)100);
        AuthCache.d = ConfigFactory.getBooleanProperty((String)var0_6[1], (boolean)true);
    }

    private static AuthenticationException a(AuthenticationException authenticationException) {
        return authenticationException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x44);
        }
        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 -> 116;
                    case 1 -> 57;
                    case 2 -> 61;
                    case 3 -> 90;
                    case 4 -> 23;
                    case 5 -> 49;
                    default -> 68;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

