Home | History | Annotate | Download | only in http
      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.Request;
     22 import com.squareup.okhttp.Response;
     23 import java.io.IOException;
     24 import java.net.Authenticator.RequestorType;
     25 import java.net.InetAddress;
     26 import java.net.InetSocketAddress;
     27 import java.net.PasswordAuthentication;
     28 import java.net.Proxy;
     29 import java.net.URL;
     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 
     37   @Override public Request authenticate(Proxy proxy, Response response) throws IOException {
     38     List<Challenge> challenges = response.challenges();
     39     Request request = response.request();
     40     URL url = request.url();
     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.getHost(), getConnectToInetAddress(proxy, url), url.getPort(), url.getProtocol(),
     47           challenge.getRealm(), challenge.getScheme(), 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 
     59   @Override public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
     60     List<Challenge> challenges = response.challenges();
     61     Request request = response.request();
     62     URL url = request.url();
     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.getProtocol(), challenge.getRealm(), challenge.getScheme(), 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 
     82   private InetAddress getConnectToInetAddress(Proxy proxy, URL url) throws IOException {
     83     return (proxy != null && proxy.type() != Proxy.Type.DIRECT)
     84         ? ((InetSocketAddress) proxy.address()).getAddress()
     85         : InetAddress.getByName(url.getHost());
     86   }
     87 }
     88