package okhttp3.internal.authenticator;

import java.io.EOFException;
import java.net.Authenticator;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.SocketAddress;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import kotlin.jvm.internal.o;
import okhttp3.Authenticator;
import okhttp3.Challenge;
import okhttp3.Credentials;
import okhttp3.Dns;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;
import okhttp3.internal.http.HttpHeaders;
import okhttp3.internal.platform.Platform;
import p5.C2740j;
import p5.C2743m;
import x4.AbstractC3251q;
import x4.y;

/* JADX INFO: loaded from: classes3.dex */
public final class JavaNetAuthenticator implements Authenticator {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Dns f22186b;

    public /* synthetic */ class WhenMappings {

        /* JADX INFO: renamed from: a, reason: collision with root package name */
        public static final /* synthetic */ int[] f22187a;

        static {
            int[] iArr = new int[Proxy.Type.values().length];
            try {
                iArr[Proxy.Type.DIRECT.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            f22187a = iArr;
        }
    }

    public JavaNetAuthenticator() {
        this(0);
    }

    public static InetAddress b(Proxy proxy, HttpUrl httpUrl, Dns dns) {
        Proxy.Type type = proxy.type();
        if ((type == null ? -1 : WhenMappings.f22187a[type.ordinal()]) == 1) {
            return (InetAddress) AbstractC3251q.e0(dns.a(httpUrl.f22027d));
        }
        SocketAddress socketAddressAddress = proxy.address();
        o.f(socketAddressAddress, "null cannot be cast to non-null type java.net.InetSocketAddress");
        InetAddress address = ((InetSocketAddress) socketAddressAddress).getAddress();
        o.g(address, "address() as InetSocketAddress).address");
        return address;
    }

    /* JADX WARN: Removed duplicated region for block: B:23:0x0060  */
    /* JADX WARN: Removed duplicated region for block: B:25:0x0063  */
    /* JADX WARN: Removed duplicated region for block: B:26:0x0066  */
    /* JADX WARN: Removed duplicated region for block: B:30:0x0072  */
    @Override // okhttp3.Authenticator
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final Request a(Route route, Response response) {
        String str;
        List<Challenge> list;
        PasswordAuthentication passwordAuthenticationRequestPasswordAuthentication;
        Charset ISO_8859_1;
        Dns dns;
        o.h(response, "response");
        int i6 = response.f22143d;
        if (i6 == 401) {
            str = "WWW-Authenticate";
        } else {
            if (i6 != 407) {
                list = y.f26065a;
                Request request = response.f22140a;
                HttpUrl httpUrl = request.f22125a;
                boolean z6 = i6 == 407;
                Proxy proxy = route == null ? route.f22169b : Proxy.NO_PROXY;
                for (Challenge challenge : list) {
                    if ("Basic".equalsIgnoreCase(challenge.f21954a)) {
                        Dns dns2 = (route == null || (dns = route.f22168a.f21889a) == null) ? this.f22186b : dns;
                        String str2 = challenge.f21954a;
                        Map map = challenge.f21955b;
                        if (z6) {
                            SocketAddress socketAddressAddress = proxy.address();
                            o.f(socketAddressAddress, "null cannot be cast to non-null type java.net.InetSocketAddress");
                            InetSocketAddress inetSocketAddress = (InetSocketAddress) socketAddressAddress;
                            try {
                                passwordAuthenticationRequestPasswordAuthentication = java.net.Authenticator.requestPasswordAuthentication(inetSocketAddress.getHostName(), b(proxy, httpUrl, dns2), inetSocketAddress.getPort(), httpUrl.f22024a, (String) map.get("realm"), str2, new URL(httpUrl.f22032i), Authenticator.RequestorType.PROXY);
                            } catch (MalformedURLException e6) {
                                throw new RuntimeException(e6);
                            }
                        } else {
                            String str3 = httpUrl.f22027d;
                            o.g(proxy, "proxy");
                            try {
                                passwordAuthenticationRequestPasswordAuthentication = java.net.Authenticator.requestPasswordAuthentication(str3, b(proxy, httpUrl, dns2), httpUrl.f22028e, httpUrl.f22024a, (String) map.get("realm"), str2, new URL(httpUrl.f22032i), Authenticator.RequestorType.SERVER);
                            } catch (MalformedURLException e7) {
                                throw new RuntimeException(e7);
                            }
                        }
                        if (passwordAuthenticationRequestPasswordAuthentication != null) {
                            String str4 = z6 ? "Proxy-Authorization" : "Authorization";
                            String userName = passwordAuthenticationRequestPasswordAuthentication.getUserName();
                            o.g(userName, "auth.userName");
                            char[] password = passwordAuthenticationRequestPasswordAuthentication.getPassword();
                            o.g(password, "auth.password");
                            String str5 = new String(password);
                            String str6 = (String) map.get("charset");
                            if (str6 != null) {
                                try {
                                    ISO_8859_1 = Charset.forName(str6);
                                    o.g(ISO_8859_1, "forName(charset)");
                                } catch (Exception unused) {
                                    ISO_8859_1 = StandardCharsets.ISO_8859_1;
                                    o.g(ISO_8859_1, "ISO_8859_1");
                                }
                            } else {
                                ISO_8859_1 = StandardCharsets.ISO_8859_1;
                                o.g(ISO_8859_1, "ISO_8859_1");
                            }
                            int i7 = Credentials.f22001a;
                            String str7 = userName + ':' + str5;
                            C2743m c2743m = C2743m.f23101d;
                            o.h(str7, "<this>");
                            byte[] bytes = str7.getBytes(ISO_8859_1);
                            o.g(bytes, "getBytes(...)");
                            String strConcat = "Basic ".concat(new C2743m(bytes).a());
                            Request.Builder builderB = request.b();
                            builderB.e(str4, strConcat);
                            return builderB.b();
                        }
                    }
                }
                return null;
            }
            str = "Proxy-Authenticate";
        }
        String str8 = str;
        C2743m c2743m2 = HttpHeaders.f22381a;
        Headers headers = response.f22145f;
        ArrayList arrayList = new ArrayList();
        int size = headers.size();
        for (int i8 = 0; i8 < size; i8++) {
            if (str8.equalsIgnoreCase(headers.h(i8))) {
                C2740j c2740j = new C2740j();
                c2740j.W(headers.l(i8));
                try {
                    HttpHeaders.b(c2740j, arrayList);
                } catch (EOFException e8) {
                    Platform.Companion.get().log("Unable to parse challenge", 5, e8);
                }
            }
        }
        list = arrayList;
        Request request2 = response.f22140a;
        HttpUrl httpUrl2 = request2.f22125a;
        if (i6 == 407) {
        }
        if (route == null) {
        }
        while (r0.hasNext()) {
        }
        return null;
    }

    public JavaNetAuthenticator(int i6) {
        Dns defaultDns = Dns.f22008a;
        o.h(defaultDns, "defaultDns");
        this.f22186b = defaultDns;
    }
}
