/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.identity.BadCredentialsException
 *  com.l7tech.identity.MissingCredentialsException
 *  com.l7tech.identity.User
 *  com.l7tech.identity.internal.InternalUser
 *  com.l7tech.identity.ldap.LdapUser
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.util.HexUtils
 */
package com.l7tech.server.identity;

import com.l7tech.identity.BadCredentialsException;
import com.l7tech.identity.MissingCredentialsException;
import com.l7tech.identity.User;
import com.l7tech.identity.internal.InternalUser;
import com.l7tech.identity.ldap.LdapUser;
import com.l7tech.policy.assertion.credential.LoginCredentials;
import com.l7tech.server.identity.AuthenticationResult;
import com.l7tech.util.HexUtils;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class DigestAuthenticator {
    private static final Logger a;
    public static final String PARAM_USERNAME;
    public static final String PARAM_RESPONSE;
    public static final String PARAM_NONCE;
    public static final String PARAM_CNONCE;
    public static final String PARAM_QOP;
    public static final String PARAM_ALGORITHM;
    public static final String PARAM_NC;
    public static final String PARAM_URI;
    public static final String PARAM_OPAQUE;
    public static final String PARAM_METHOD;
    public static final String SCHEME;
    public static final String QOP_AUTH;
    public static final String QOP_AUTH_INT;
    public static final String ALGORITHM_MD5;
    private static final String[] b;

    public static AuthenticationResult authenticateDigestCredentials(LoginCredentials loginCredentials, User user) throws MissingCredentialsException, BadCredentialsException {
        String string;
        String string2;
        String string3;
        Object object;
        Object object2;
        Object object3 = loginCredentials.getPayload();
        String string4 = null;
        if (user instanceof InternalUser) {
            string4 = ((InternalUser)user).getHttpDigest();
        } else if (user instanceof LdapUser && (object2 = (object = (LdapUser)user).getPassword()) != null) {
            string4 = HexUtils.encodePasswd((String)object.getLogin(), (String)object2, (String)HexUtils.REALM);
        }
        try {
            if (string4 == null) {
                throw new BadCredentialsException(b[2]);
            }
        }
        catch (MissingCredentialsException missingCredentialsException) {
            throw DigestAuthenticator.a(missingCredentialsException);
        }
        object = loginCredentials.getCredentials();
        object2 = (Map)object3;
        if (object2 == null) {
            String[] stringArray = b;
            String string5 = stringArray[9];
            a.log(Level.SEVERE, string5);
            throw new MissingCredentialsException(string5);
        }
        String[] stringArray = b;
        String string6 = (String)object2.get(stringArray[11]);
        String string7 = (String)object2.get(stringArray[13]);
        String string8 = (String)object2.get(stringArray[1]) + ":" + (String)object2.get(stringArray[23]);
        String string9 = HexUtils.encodeMd5Digest((byte[])HexUtils.getMd5Digest((byte[])string8.getBytes()));
        if (!stringArray[18].equals(string6)) {
            string3 = string4 + ":" + string7 + ":" + string9;
        } else {
            stringArray = b;
            string2 = (String)object2.get(stringArray[26]);
            string = (String)object2.get(stringArray[5]);
            string3 = string4 + ":" + string7 + ":" + string2 + ":" + string + ":" + string6 + ":" + string9;
        }
        string2 = HexUtils.encodeMd5Digest((byte[])HexUtils.getMd5Digest((byte[])string3.getBytes()));
        string = new String((char[])object);
        String string10 = loginCredentials.getLogin();
        try {
            if (string.equals(string2)) {
                a.info(b[22] + string10 + b[4]);
                return new AuthenticationResult(user, loginCredentials.getSecurityTokens());
            }
        }
        catch (MissingCredentialsException missingCredentialsException) {
            throw DigestAuthenticator.a(missingCredentialsException);
        }
        stringArray = b;
        String string11 = stringArray[0] + string10 + stringArray[17];
        a.warning(string11);
        throw new BadCredentialsException(string11);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[27];
                var3_1 = 0;
                var2_2 = "qZwo\r\u0006ILfuBO>qZwo\rO;AZ2sB_tLHdx\rJtQZs\u007fANtTHanZD&@\ttr_\u000b0MNwnY\u000b5Q]zxC_=GHftBE\u0006`@ux^_4\u0004HgiEN:P@q|YN0\u0004Zg~NN'WOgqARtS@fu\rO=CLai\rH&AMwsYB5HZ<\u0006GG}sNN\u0002JJ\u0006KYslXN\u0003IM'FjF2YDL1W]2|X_<AGftNJ MF|=]J&EDwiHY'\u0004O}hCOtMG2QBL=Jj`xIN:P@sq^\u000b$EP~rLOu\bE\\fu\u0000B:P\u0003UFb\u0005JF|~H\u0005JF|~H\tEEur_B LD\u0004E\\fu\u0006ILfuBO\u0011\u0004OstAN0\u0004]}=@J GA<\u0004E\\fu\u0003Q[{\u0006GG}sNN\u0003UFb\u0005qZwo\r\u0003Q[{\bQZwoCJ9A";
                var4_3 = "qZwo\r\u0006ILfuBO>qZwo\rO;AZ2sB_tLHdx\rJtQZs\u007fANtTHanZD&@\ttr_\u000b0MNwnY\u000b5Q]zxC_=GHftBE\u0006`@ux^_4\u0004HgiEN:P@q|YN0\u0004Zg~NN'WOgqARtS@fu\rO=CLai\rH&AMwsYB5HZ<\u0006GG}sNN\u0002JJ\u0006KYslXN\u0003IM'FjF2YDL1W]2|X_<AGftNJ MF|=]J&EDwiHY'\u0004O}hCOtMG2QBL=Jj`xIN:P@sq^\u000b$EP~rLOu\bE\\fu\u0000B:P\u0003UFb\u0005JF|~H\u0005JF|~H\tEEur_B LD\u0004E\\fu\u0006ILfuBO\u0011\u0004OstAN0\u0004]}=@J GA<\u0004E\\fu\u0003Q[{\u0006GG}sNN\u0003UFb\u0005qZwo\r\u0003Q[{\bQZwoCJ9A".length();
                var1_4 = 5;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = DigestAuthenticator.a(v0, DigestAuthenticator.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "~dIEjm\u000fi\u0002bb";
                    var4_3 = "~dIEjm\u000fi\u0002bb".length();
                    var1_4 = 8;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 100;
                        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++] = DigestAuthenticator.a(v0, DigestAuthenticator.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;
            }
        }
        DigestAuthenticator.b = var5;
        DigestAuthenticator.QOP_AUTH = DigestAuthenticator.b[15];
        DigestAuthenticator.PARAM_NONCE = DigestAuthenticator.b[12];
        DigestAuthenticator.PARAM_METHOD = DigestAuthenticator.b[16];
        DigestAuthenticator.PARAM_QOP = DigestAuthenticator.b[21];
        DigestAuthenticator.PARAM_CNONCE = DigestAuthenticator.b[20];
        DigestAuthenticator.PARAM_URI = DigestAuthenticator.b[19];
        DigestAuthenticator.PARAM_NC = DigestAuthenticator.b[6];
        DigestAuthenticator.PARAM_ALGORITHM = DigestAuthenticator.b[14];
        DigestAuthenticator.SCHEME = DigestAuthenticator.b[3];
        DigestAuthenticator.ALGORITHM_MD5 = DigestAuthenticator.b[8];
        DigestAuthenticator.PARAM_USERNAME = DigestAuthenticator.b[24];
        DigestAuthenticator.PARAM_RESPONSE = DigestAuthenticator.b[25];
        DigestAuthenticator.PARAM_OPAQUE = DigestAuthenticator.b[7];
        DigestAuthenticator.QOP_AUTH_INT = DigestAuthenticator.b[10];
        DigestAuthenticator.a = Logger.getLogger(DigestAuthenticator.class.getName());
    }

    private static MissingCredentialsException a(MissingCredentialsException missingCredentialsException) {
        return missingCredentialsException;
    }

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

