1 /* 2 * Copyright (C) 2013 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.squareup.okhttp.internal.http; 17 18 import com.squareup.okhttp.Authenticator; 19 import com.squareup.okhttp.Challenge; 20 import com.squareup.okhttp.Credentials; 21 import com.squareup.okhttp.HttpUrl; 22 import com.squareup.okhttp.Request; 23 import com.squareup.okhttp.Response; 24 import java.io.IOException; 25 import java.net.Authenticator.RequestorType; 26 import java.net.InetAddress; 27 import java.net.InetSocketAddress; 28 import java.net.PasswordAuthentication; 29 import java.net.Proxy; 30 import java.util.List; 31 32 /** Adapts {@link java.net.Authenticator} to {@link com.squareup.okhttp.Authenticator}. */ 33 public final class AuthenticatorAdapter implements Authenticator { 34 /** Uses the global authenticator to get the password. */ 35 public static final Authenticator INSTANCE = new AuthenticatorAdapter(); 36 authenticate(Proxy proxy, Response response)37 @Override public Request authenticate(Proxy proxy, Response response) throws IOException { 38 List<Challenge> challenges = response.challenges(); 39 Request request = response.request(); 40 HttpUrl url = request.httpUrl(); 41 for (int i = 0, size = challenges.size(); i < size; i++) { 42 Challenge challenge = challenges.get(i); 43 if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue; 44 45 PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication( 46 url.host(), getConnectToInetAddress(proxy, url), url.port(), url.scheme(), 47 challenge.getRealm(), challenge.getScheme(), url.url(), RequestorType.SERVER); 48 if (auth == null) continue; 49 50 String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword())); 51 return request.newBuilder() 52 .header("Authorization", credential) 53 .build(); 54 } 55 return null; 56 57 } 58 authenticateProxy(Proxy proxy, Response response)59 @Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException { 60 List<Challenge> challenges = response.challenges(); 61 Request request = response.request(); 62 HttpUrl url = request.httpUrl(); 63 for (int i = 0, size = challenges.size(); i < size; i++) { 64 Challenge challenge = challenges.get(i); 65 if (!"Basic".equalsIgnoreCase(challenge.getScheme())) continue; 66 67 InetSocketAddress proxyAddress = (InetSocketAddress) proxy.address(); 68 PasswordAuthentication auth = java.net.Authenticator.requestPasswordAuthentication( 69 proxyAddress.getHostName(), getConnectToInetAddress(proxy, url), proxyAddress.getPort(), 70 url.scheme(), challenge.getRealm(), challenge.getScheme(), url.url(), 71 RequestorType.PROXY); 72 if (auth == null) continue; 73 74 String credential = Credentials.basic(auth.getUserName(), new String(auth.getPassword())); 75 return request.newBuilder() 76 .header("Proxy-Authorization", credential) 77 .build(); 78 } 79 return null; 80 } 81 getConnectToInetAddress(Proxy proxy, HttpUrl url)82 private InetAddress getConnectToInetAddress(Proxy proxy, HttpUrl url) throws IOException { 83 return (proxy != null && proxy.type() != Proxy.Type.DIRECT) 84 ? ((InetSocketAddress) proxy.address()).getAddress() 85 : InetAddress.getByName(url.host()); 86 } 87 } 88