1 /*
2  * Copyright (C) 2015 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 okhttp3;
17
18 import java.io.IOException;
19 import javax.annotation.Nullable;
20
21 /**
22  * Performs either <strong>preemptive</strong> authentication before connecting to a proxy server,
23  * or <strong>reactive</strong> authentication after receiving a challenge from either an origin web
24  * server or proxy server.
25  *
26  * <h3>Preemptive Authentication</h3>
27  *
28  * <p>To make HTTPS calls using an HTTP proxy server OkHttp must first negotiate a connection with
29  * the proxy. This proxy connection is called a "TLS Tunnel" and is specified by <a
30  * href="https://tools.ietf.org/html/rfc2817">RFC 2817</a>. The HTTP CONNECT request that creates
31  * this tunnel connection is special: it does not participate in any {@linkplain Interceptor
32  * interceptors} or {@linkplain EventListener event listeners}. It doesn't include the motivating
33  * request's HTTP headers or even its full URL; only the target server's hostname is sent to the
34  * proxy.
35  *
36  * <p>Prior to sending any CONNECT request OkHttp always calls the proxy authenticator so that it
37  * may prepare preemptive authentication. OkHttp will call {@link #authenticate} with a fake {@code
38  * HTTP/1.1 407 Proxy Authentication Required} response that has a {@code Proxy-Authenticate:
39  * OkHttp-Preemptive} challenge. The proxy authenticator may return either either an authenticated
40  * request, or null to connect without authentication.
41  * <pre>   {@code
42  *    for (Challenge challenge : response.challenges()) {
43  *      // If this is preemptive auth, use a preemptive credential.
44  *      if (challenge.scheme().equalsIgnoreCase("OkHttp-Preemptive")) {
45  *        return response.request().newBuilder()
46  *            .header("Proxy-Authorization""secret")
47  *            .build();
48  *      }
49  *    }
50  *
51  *    return null// Didn't find a preemptive auth scheme.
52  * }</pre>
53  *
54  * <h3>Reactive Authentication</h3>
55  *
56  * <p>Implementations authenticate by returning a follow-up request that includes an authorization
57  * header, or they may decline the challenge by returning null. In this case the unauthenticated
58  * response will be returned to the caller that triggered it.
59  *
60  * <p>Implementations should check if the initial request already included an attempt to
61  * authenticate. If so it is likely that further attempts will not be useful and the authenticator
62  * should give up.
63  *
64  * <p>When reactive authentication is requested by an origin web server, the response code is 401
65  * and the implementation should respond with a new request that sets the "Authorization" header.
66  * <pre>   {@code
67  *
68  *    if (response.request().header("Authorization") != null) {
69  *      return null// Give up, we've already failed to authenticate.
70  *    }
71  *
72  *    String credential = Credentials.basic(...)
73  *    return response.request().newBuilder()
74  *        .header("Authorization", credential)
75  *        .build();
76  * }</pre>
77  *
78  * <p>When reactive authentication is requested by a proxy server, the response code is 407 and the
79  * implementation should respond with a new request that sets the "Proxy-Authorization" header.
80  * <pre>   {@code
81  *
82  *    if (response.request().header("Proxy-Authorization") != null) {
83  *      return null// Give up, we've already failed to authenticate.
84  *    }
85  *
86  *    String credential = Credentials.basic(...)
87  *    return response.request().newBuilder()
88  *        .header("Proxy-Authorization", credential)
89  *        .build();
90  * }</pre>
91  *
92  * <p>The proxy authenticator may implement preemptive authentication, reactive authentication, or
93  * both.
94  *
95  * <p>Applications may configure OkHttp with an authenticator for origin servers, or proxy servers,
96  * or both.
97  */

98 public interface Authenticator {
99   /** An authenticator that knows no credentials and makes no attempt to authenticate. */
100   Authenticator NONE = (route, response) -> null;
101
102   /**
103    * Returns a request that includes a credential to satisfy an authentication challenge in {@code
104    * response}. Returns null if the challenge cannot be satisfied.
105    *
106    * <p>The route is best effort, it currently may not always be provided even when logically
107    * available. It may also not be provided when an authenticator is re-used manually in an
108    * application interceptor, such as when implementing client-specific retries.
109    */

110   @Nullable Request authenticate(@Nullable Route route, Response response) throws IOException;
111 }
112