package okhttp3.internal.authenticator; import com.google.common.net.HttpHeaders; import java.io.IOException; import java.net.Authenticator; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.PasswordAuthentication; import java.net.Proxy; import java.net.SocketAddress; import java.util.List; import o.C14881gav; import o.C14953gcr; import o.C14957gcv; import o.gdZ; import okhttp3.Address; import okhttp3.Authenticator; import okhttp3.Challenge; import okhttp3.Credentials; import okhttp3.Dns; import okhttp3.HttpUrl; import okhttp3.Request; import okhttp3.Response; import okhttp3.Route; /* loaded from: classes.dex */ public final class JavaNetAuthenticator implements Authenticator { private final Dns defaultDns; public JavaNetAuthenticator(Dns dns) { C14957gcv.e(dns, ""); this.defaultDns = dns; } public /* synthetic */ JavaNetAuthenticator(Dns dns, int i, C14953gcr c14953gcr) { this((i & 1) != 0 ? Dns.SYSTEM : dns); } @Override // okhttp3.Authenticator public final Request authenticate(Route route, Response response) throws IOException { PasswordAuthentication requestPasswordAuthentication; Address address; C14957gcv.e(response, ""); List challenges = response.challenges(); Request request = response.request(); HttpUrl url = request.url(); boolean z = response.code() == 407; Proxy proxy = route == null ? null : route.proxy(); if (proxy == null) { proxy = Proxy.NO_PROXY; } for (Challenge challenge : challenges) { if (gdZ.a("Basic", challenge.scheme(), true)) { Dns dns = (route == null || (address = route.address()) == null) ? null : address.dns(); if (dns == null) { dns = this.defaultDns; } if (z) { SocketAddress address2 = proxy.address(); if (address2 == null) { throw new NullPointerException("null cannot be cast to non-null type java.net.InetSocketAddress"); } InetSocketAddress inetSocketAddress = (InetSocketAddress) address2; String hostName = inetSocketAddress.getHostName(); C14957gcv.c(proxy, ""); requestPasswordAuthentication = java.net.Authenticator.requestPasswordAuthentication(hostName, connectToInetAddress(proxy, url, dns), inetSocketAddress.getPort(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), Authenticator.RequestorType.PROXY); } else { String host = url.host(); C14957gcv.c(proxy, ""); requestPasswordAuthentication = java.net.Authenticator.requestPasswordAuthentication(host, connectToInetAddress(proxy, url, dns), url.port(), url.scheme(), challenge.realm(), challenge.scheme(), url.url(), Authenticator.RequestorType.SERVER); } if (requestPasswordAuthentication != null) { String str = z ? HttpHeaders.PROXY_AUTHORIZATION : HttpHeaders.AUTHORIZATION; String userName = requestPasswordAuthentication.getUserName(); C14957gcv.c((Object) userName, ""); char[] password = requestPasswordAuthentication.getPassword(); C14957gcv.c(password, ""); return request.newBuilder().header(str, Credentials.basic(userName, new String(password), challenge.charset())).build(); } } } return null; } private final InetAddress connectToInetAddress(Proxy proxy, HttpUrl httpUrl, Dns dns) throws IOException { Proxy.Type type = proxy.type(); if (type != null && WhenMappings.$EnumSwitchMapping$0[type.ordinal()] == 1) { return (InetAddress) C14881gav.e((List) dns.lookup(httpUrl.host())); } SocketAddress address = proxy.address(); if (address == null) { throw new NullPointerException("null cannot be cast to non-null type java.net.InetSocketAddress"); } InetAddress address2 = ((InetSocketAddress) address).getAddress(); C14957gcv.c(address2, ""); return address2; } /* loaded from: classes.dex */ public final /* synthetic */ class WhenMappings { public static final int[] $EnumSwitchMapping$0; static { int[] iArr = new int[Proxy.Type.values().length]; iArr[Proxy.Type.DIRECT.ordinal()] = 1; $EnumSwitchMapping$0 = iArr; } } /* JADX WARN: Multi-variable type inference failed */ public JavaNetAuthenticator() { this(null, 1, 0 == true ? 1 : 0); } }