/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.HttpConstants
 *  com.l7tech.gateway.common.audit.AssertionMessages
 *  com.l7tech.gateway.common.audit.AuditDetailMessage
 *  com.l7tech.message.Message
 *  com.l7tech.policy.assertion.AssertionStatus
 *  com.l7tech.policy.assertion.PolicyAssertionException
 *  com.l7tech.policy.assertion.credential.CredentialFinderException
 *  com.l7tech.policy.assertion.credential.LoginCredentials
 *  com.l7tech.policy.assertion.credential.http.HttpCredentialSourceAssertion
 *  com.l7tech.policy.assertion.credential.http.HttpDigest
 *  com.l7tech.security.token.SecurityToken
 *  com.l7tech.security.token.http.HttpDigestToken
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.HexUtils
 *  jakarta.mail.internet.HeaderTokenizer
 *  jakarta.mail.internet.HeaderTokenizer$Token
 *  jakarta.mail.internet.ParseException
 */
package com.l7tech.server.policy.assertion.credential.http;

import com.l7tech.common.http.HttpConstants;
import com.l7tech.gateway.common.audit.AssertionMessages;
import com.l7tech.gateway.common.audit.AuditDetailMessage;
import com.l7tech.message.Message;
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.policy.assertion.credential.http.HttpCredentialSourceAssertion;
import com.l7tech.policy.assertion.credential.http.HttpDigest;
import com.l7tech.security.token.SecurityToken;
import com.l7tech.security.token.http.HttpDigestToken;
import com.l7tech.server.policy.assertion.credential.DigestSessions;
import com.l7tech.server.policy.assertion.credential.http.ServerHttpCredentialSource;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.HexUtils;
import jakarta.mail.internet.HeaderTokenizer;
import jakarta.mail.internet.ParseException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class ServerHttpDigest
extends ServerHttpCredentialSource<HttpDigest> {
    private static final boolean d;
    private static final String[] g;

    public ServerHttpDigest(HttpDigest httpDigest) throws PolicyAssertionException {
        super(httpDigest);
    }

    @Override
    protected String realm() {
        return HexUtils.REALM;
    }

    @Override
    protected AssertionStatus checkAuthParams(Map map) {
        if (map == null) {
            return AssertionStatus.AUTH_REQUIRED;
        }
        String string = (String)map.get(HttpDigest.PARAM_NONCE);
        String string2 = (String)map.get(HttpDigest.PARAM_USERNAME);
        String string3 = (String)map.get(HttpDigest.PARAM_REALM);
        String string4 = (String)map.get(HttpDigest.PARAM_NC);
        String string5 = (String)map.get(HttpDigest.PARAM_CNONCE);
        String string6 = (String)map.get(HttpDigest.PARAM_QOP);
        String string7 = (String)map.get(HttpDigest.PARAM_URI);
        String string8 = (String)map.get(HttpDigest.PARAM_RESPONSE);
        if (string2 == null || string3 == null || string == null || string7 == null || string8 == null) {
            return AssertionStatus.AUTH_REQUIRED;
        }
        if (string6 != null && (string5 == null || string4 == null)) {
            return AssertionStatus.AUTH_REQUIRED;
        }
        DigestSessions digestSessions = DigestSessions.getInstance();
        if (digestSessions.use(string)) {
            this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPDIGEST_NONCE_VALID, string, string2);
            return AssertionStatus.NONE;
        }
        this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPDIGEST_NONCE_EXPIRED, string, string2);
        digestSessions.invalidate(string);
        return AssertionStatus.AUTH_FAILED;
    }

    protected LoginCredentials doFindCredentials(Message message, Map<String, String> map) throws CredentialFinderException {
        String string;
        String string2;
        String string3;
        block10: {
            try {
                if (map == null) {
                    return null;
                }
            }
            catch (CredentialFinderException credentialFinderException) {
                throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
            }
            string3 = map.get(HttpDigest.PARAM_USERNAME);
            string2 = map.get(HttpDigest.PARAM_REALM);
            string = map.get(HttpDigest.PARAM_RESPONSE);
            try {
                block9: {
                    try {
                        try {
                            map.put(HttpDigest.PARAM_URI, message.getHttpRequestKnob().getRequestUri());
                            map.put(HttpDigest.PARAM_METHOD, message.getHttpRequestKnob().getMethod().name());
                            if (string3 == null || string2 == null) break block9;
                        }
                        catch (CredentialFinderException credentialFinderException) {
                            throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
                        }
                        if (string != null) break block10;
                    }
                    catch (CredentialFinderException credentialFinderException) {
                        throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
                    }
                }
                return null;
            }
            catch (CredentialFinderException credentialFinderException) {
                throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
            }
        }
        return LoginCredentials.makeLoginCredentials((SecurityToken)new HttpDigestToken(string3, string, string2, map), ((HttpDigest)this.assertion).getClass());
    }

    @Override
    protected Map<String, String> findCredentialAuthParams(LoginCredentials loginCredentials, Map<String, String> map) {
        Object object = loginCredentials.getPayload();
        if (object instanceof Map) {
            return (Map)object;
        }
        return map;
    }

    @Override
    protected Map<String, String> challengeParams(Message message, Map<String, String> map) {
        String string;
        DigestSessions digestSessions = DigestSessions.getInstance();
        String string2 = string = map == null ? null : map.get(HttpDigest.PARAM_NONCE);
        if (string == null || string.length() == 0) {
            String string3 = digestSessions.generate(((HttpDigest)this.assertion).getNonceTimeout(), ((HttpDigest)this.assertion).getMaxNonceCount());
            this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPDIGEST_NONCE_GENERATED, string3);
            return this.b(string3);
        }
        if (!digestSessions.use(string)) {
            String string4 = map.get(HttpDigest.PARAM_USERNAME);
            this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPDIGEST_NONCE_EXPIRED, string, string4 == null ? g[6] : string4);
            digestSessions.invalidate(string);
            string = digestSessions.generate(((HttpDigest)this.assertion).getNonceTimeout(), ((HttpDigest)this.assertion).getMaxNonceCount());
            this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPDIGEST_NONCE_GENERATED, string);
        }
        return this.b(string);
    }

    @Override
    protected String scheme() {
        return HttpDigest.SCHEME;
    }

    @Override
    protected LoginCredentials findCredentials(Message message, Map<String, String> map) throws IOException, CredentialFinderException {
        String string;
        block7: {
            string = message.getHttpRequestKnob().getHeaderSingleValue(HttpConstants.HEADER_AUTHORIZATION);
            try {
                try {
                    if (string != null && string.length() != 0) break block7;
                }
                catch (IOException iOException) {
                    throw ServerHttpDigest.b(iOException);
                }
                return null;
            }
            catch (IOException iOException) {
                throw ServerHttpDigest.b(iOException);
            }
        }
        try {
            if (!ServerHttpDigest.a(map, string.trim())) {
                this.logAndAudit((AuditDetailMessage)AssertionMessages.HTTPCREDS_NA_AUTHN_HEADER);
                return null;
            }
        }
        catch (IOException iOException) {
            throw ServerHttpDigest.b(iOException);
        }
        return this.doFindCredentials(message, map);
    }

    static boolean a(Map<String, String> map, String string) throws CredentialFinderException {
        try {
            if (d) {
                return ServerHttpDigest.b(map, string);
            }
        }
        catch (CredentialFinderException credentialFinderException) {
            throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
        }
        return ServerHttpDigest.c(map, string);
    }

    static boolean b(Map<String, String> map, String string) throws CredentialFinderException {
        String[] stringArray = g;
        StringTokenizer stringTokenizer = new StringTokenizer(string, stringArray[0]);
        String string2 = null;
        while (stringTokenizer.hasMoreTokens()) {
            String string3;
            block13: {
                String string4;
                String string5;
                block11: {
                    block12: {
                        string3 = stringTokenizer.nextToken();
                        int n2 = string3.indexOf("=");
                        if (n2 < 0) break block13;
                        string5 = string3.substring(0, n2);
                        string4 = string3.substring(n2 + 1);
                        try {
                            if (!string4.startsWith("\"")) break block11;
                            if (!string4.endsWith("\"")) break block12;
                        }
                        catch (CredentialFinderException credentialFinderException) {
                            throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
                        }
                        string4 = string4.substring(1, string4.length() - 1);
                        break block11;
                    }
                    StringBuilder stringBuilder = new StringBuilder(string4.substring(1));
                    string4 = null;
                    while (stringTokenizer.hasMoreTokens()) {
                        string3 = stringTokenizer.nextToken();
                        if (string3.endsWith("\"")) {
                            stringBuilder.append(string3.substring(0, string3.length() - 1));
                            string4 = stringBuilder.toString();
                            break;
                        }
                        stringBuilder.append(string3);
                        stringBuilder.append(" ");
                    }
                    try {
                        if (string4 == null) {
                            throw new CredentialFinderException(g[11]);
                        }
                    }
                    catch (CredentialFinderException credentialFinderException) {
                        throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
                    }
                }
                map.put(string5, string4);
                continue;
            }
            if (string2 != null) {
                stringArray = g;
                throw new CredentialFinderException(stringArray[10] + string3 + stringArray[9]);
            }
            string2 = string3;
            map.put(HttpCredentialSourceAssertion.PARAM_SCHEME, string2);
            try {
                if (HttpDigest.SCHEME.equals(string2)) continue;
                return false;
            }
            catch (CredentialFinderException credentialFinderException) {
                throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
            }
        }
        return true;
    }

    /*
     * Loose catch block
     */
    static boolean c(Map<String, String> map, String string) throws CredentialFinderException {
        String string2;
        int n2 = string.indexOf(32);
        if (n2 > 0) {
            string2 = string.substring(0, n2);
            try {
                if (!HttpDigest.SCHEME.equals(string2)) {
                    return false;
                }
            }
            catch (ParseException parseException) {
                throw ServerHttpDigest.b((Exception)((Object)parseException));
            }
            map.put(HttpCredentialSourceAssertion.PARAM_SCHEME, HttpDigest.SCHEME);
        }
        String[] stringArray = g;
        string2 = new HeaderTokenizer(string.substring(n2 + 1), stringArray[13], false);
        try {
            while (true) {
                block21: {
                    HeaderTokenizer.Token token;
                    block23: {
                        StringBuilder stringBuilder;
                        String string3;
                        block22: {
                            block24: {
                                block20: {
                                    block18: {
                                        token = string2.next();
                                        try {
                                            if (token.getType() != -4) break block18;
                                            break;
                                        }
                                        catch (ParseException parseException) {
                                            throw ServerHttpDigest.b((Exception)((Object)parseException));
                                        }
                                    }
                                    stringArray = g;
                                    ServerHttpDigest.a(token, stringArray[3], -1);
                                    string3 = token.getValue();
                                    ServerHttpDigest.a((HeaderTokenizer)string2, stringArray[12]);
                                    token = string2.next();
                                    ServerHttpDigest.a(token, stringArray[2], 61);
                                    ServerHttpDigest.a((HeaderTokenizer)string2, stringArray[14]);
                                    token = string2.next();
                                    try {
                                        if (token.getType() != -2) break block20;
                                        map.put(string3, token.getValue());
                                        break block21;
                                    }
                                    catch (ParseException parseException) {
                                        throw ServerHttpDigest.b((Exception)((Object)parseException));
                                    }
                                }
                                if (!ServerHttpDigest.b(token.getType())) break block23;
                                stringBuilder = new StringBuilder();
                                while (true) {
                                    stringBuilder.append(token.getValue());
                                    if (!ServerHttpDigest.b(string2.peek().getType())) break;
                                    token = string2.next();
                                }
                                token = string2.next();
                                if (token.getType() == 32) break block22;
                                if (token.getType() == 9) break block22;
                                break block24;
                                catch (ParseException parseException) {
                                    throw ServerHttpDigest.b((Exception)((Object)parseException));
                                }
                            }
                            try {
                                block25: {
                                    if (token.getType() == 44) break block22;
                                    break block25;
                                    catch (ParseException parseException) {
                                        throw ServerHttpDigest.b((Exception)((Object)parseException));
                                    }
                                }
                                ServerHttpDigest.a(token, g[8], 32);
                            }
                            catch (ParseException parseException) {
                                throw ServerHttpDigest.b((Exception)((Object)parseException));
                            }
                        }
                        map.put(string3, stringBuilder.toString());
                        break block21;
                    }
                    stringArray = g;
                    ServerHttpDigest.a(token, stringArray[5], -1);
                }
                stringArray = g;
                ServerHttpDigest.a((HeaderTokenizer)string2, stringArray[1]);
            }
        }
        catch (ParseException parseException) {
            throw new CredentialFinderException(g[7] + ExceptionUtils.getMessage((Throwable)parseException), AssertionStatus.BAD_REQUEST);
        }
        return true;
    }

    private Map<String, String> b(String string) {
        HashMap<String, String> hashMap = new HashMap<String, String>();
        hashMap.put(HttpDigest.PARAM_QOP, HttpDigest.QOP_AUTH);
        hashMap.put(HttpDigest.PARAM_NONCE, string);
        hashMap.put(HttpDigest.PARAM_OPAQUE, HexUtils.encodeMd5Digest((byte[])HexUtils.getMd5Digest((byte[])string.getBytes())));
        return hashMap;
    }

    private static boolean b(int n2) {
        boolean bl2 = false;
        if (n2 != -3 && n2 != -4 && n2 != -2) {
            if (n2 == -1) {
                bl2 = true;
            } else if (!Character.isISOControl(n2) && n2 != 32 && n2 != 9 && n2 != 44) {
                bl2 = true;
            }
        }
        return bl2;
    }

    private static void a(HeaderTokenizer.Token token, String string, int n2) throws CredentialFinderException {
        try {
            if (token.getType() != n2) {
                throw new CredentialFinderException(g[15] + string + ".", AssertionStatus.BAD_REQUEST);
            }
        }
        catch (CredentialFinderException credentialFinderException) {
            throw ServerHttpDigest.b((Exception)((Object)credentialFinderException));
        }
    }

    private static void a(HeaderTokenizer headerTokenizer, String string) throws ParseException {
        while (true) {
            HeaderTokenizer.Token token = headerTokenizer.peek();
            try {
                if (string.indexOf(token.getType()) < 0) break;
                headerTokenizer.next();
            }
            catch (ParseException parseException) {
                throw ServerHttpDigest.b((Exception)((Object)parseException));
            }
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[16];
                var3_1 = 0;
                var2_2 = "!K\u0003!ba\u0006h\u001a4s_)\u0004c\n,wBn\u0004,<_mih\b)<@?o{\u000e3<C5qd\b8<R)nh\u00195{\\43n\u0019$vV4id\n-<[.i}E.~W\u001etj\u000e2fc;o~\u000e3\u0005{\n-gV\t1\u001e/y]5jcU\u0019H\u00193}Azm\u007f\u0004\"w@)tc\fazV;yh\u0019{2\t~\u000e1sA;ib\u0019\u0019*K(|\u0013\rJZF\u0000gG2r\u007f\u0002;w\u00132xl\u000f$`\u0012X\u0005$jC?~y\u000e%2E;qx\u000ea52X\u00055wA7tc\n5wWzlx\u00045wWzny\u0019(|TztcK\u0016Edw\\x\u001f)}A3ghK)wR>x\u007f\u0002\u0004K\u0006!7c\u001b\u0013g";
                var4_3 = "!K\u0003!ba\u0006h\u001a4s_)\u0004c\n,wBn\u0004,<_mih\b)<@?o{\u000e3<C5qd\b8<R)nh\u00195{\\43n\u0019$vV4id\n-<[.i}E.~W\u001etj\u000e2fc;o~\u000e3\u0005{\n-gV\t1\u001e/y]5jcU\u0019H\u00193}Azm\u007f\u0004\"w@)tc\fazV;yh\u0019{2\t~\u000e1sA;ib\u0019\u0019*K(|\u0013\rJZF\u0000gG2r\u007f\u0002;w\u00132xl\u000f$`\u0012X\u0005$jC?~y\u000e%2E;qx\u000ea52X\u00055wA7tc\n5wWzlx\u00045wWzny\u0019(|TztcK\u0016Edw\\x\u001f)}A3ghK)wR>x\u007f\u0002\u0004K\u0006!7c\u001b\u0013g".length();
                var1_4 = 2;
                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++] = ServerHttpDigest.d(v0, ServerHttpDigest.f(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "`/\",}W\u0019%\u001e\t\u001b`F\u0013$M\u0010\u0007h\u0005\u001e2_\u001d\f}\tV2F\t\flQ\u00133\u001e";
                    var4_3 = "`/\",}W\u0019%\u001e\t\u001b`F\u0013$M\u0010\u0007h\u0005\u001e2_\u001d\f}\tV2F\t\flQ\u00133\u001e".length();
                    var1_4 = 2;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 20;
                        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++] = ServerHttpDigest.d(v0, ServerHttpDigest.f(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;
            }
        }
        ServerHttpDigest.g = var5;
        ServerHttpDigest.d = ConfigFactory.getBooleanProperty((String)ServerHttpDigest.g[4], (boolean)false);
    }

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

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

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String d(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 -> 125;
                    case 1 -> 27;
                    case 2 -> 49;
                    case 3 -> 98;
                    case 4 -> 67;
                    case 5 -> 42;
                    default -> 109;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

