[email protected] | 9bd3095 | 2011-05-20 19:09:50 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef NET_HTTP_HTTP_AUTH_HANDLER_H_ |
| 6 | #define NET_HTTP_HTTP_AUTH_HANDLER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 10 | #include "net/base/completion_callback.h" |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 11 | #include "net/base/net_export.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 12 | #include "net/http/http_auth.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 13 | #include "net/log/net_log_with_source.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 14 | |
| 15 | namespace net { |
| 16 | |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 17 | class HttpAuthChallengeTokenizer; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame] | 18 | struct HttpRequestInfo; |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 19 | class SSLInfo; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 20 | |
| 21 | // HttpAuthHandler is the interface for the authentication schemes |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 22 | // (basic, digest, NTLM, Negotiate). |
| 23 | // HttpAuthHandler objects are typically created by an HttpAuthHandlerFactory. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 24 | class NET_EXPORT_PRIVATE HttpAuthHandler { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 25 | public: |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 26 | HttpAuthHandler(); |
[email protected] | cb2825c | 2010-07-01 10:23:38 | [diff] [blame] | 27 | virtual ~HttpAuthHandler(); |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 28 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 29 | // Initializes the handler using a challenge issued by a server. |
| 30 | // |challenge| must be non-NULL and have already tokenized the |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 31 | // authentication scheme, but none of the tokens occurring after the |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 32 | // authentication scheme. |target| and |origin| are both stored |
| 33 | // for later use, and are not part of the initial challenge. |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 34 | bool InitFromChallenge(HttpAuthChallengeTokenizer* challenge, |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 35 | HttpAuth::Target target, |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 36 | const SSLInfo& ssl_info, |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 37 | const GURL& origin, |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 38 | const NetLogWithSource& net_log); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 39 | |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 40 | // Determines how the previous authorization attempt was received. |
| 41 | // |
| 42 | // This is called when the server/proxy responds with a 401/407 after an |
| 43 | // earlier authorization attempt. Although this normally means that the |
| 44 | // previous attempt was rejected, in multi-round schemes such as |
| 45 | // NTLM+Negotiate it may indicate that another round of challenge+response |
| 46 | // is required. For Digest authentication it may also mean that the previous |
| 47 | // attempt used a stale nonce (and nonce-count) and that a new attempt should |
| 48 | // be made with a different nonce provided in the challenge. |
| 49 | // |
| 50 | // |challenge| must be non-NULL and have already tokenized the |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 51 | // authentication scheme, but none of the tokens occurring after the |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 52 | // authentication scheme. |
| 53 | virtual HttpAuth::AuthorizationResult HandleAnotherChallenge( |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame] | 54 | HttpAuthChallengeTokenizer* challenge) = 0; |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 55 | |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 56 | // Generates an authentication token, potentially asynchronously. |
| 57 | // |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 58 | // When |credentials| is NULL, the default credentials for the currently |
| 59 | // logged in user are used. |AllowsDefaultCredentials()| MUST be true in this |
| 60 | // case. |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 61 | // |
| 62 | // |request|, |callback|, and |auth_token| must be non-NULL. |
| 63 | // |
| 64 | // The return value is a net error code. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 65 | // |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 66 | // If |OK| is returned, |*auth_token| is filled in with an authentication |
| 67 | // token which can be inserted in the HTTP request. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 68 | // |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 69 | // If |ERR_IO_PENDING| is returned, |*auth_token| will be filled in |
| 70 | // asynchronously and |callback| will be invoked. The lifetime of |
| 71 | // |request|, |callback|, and |auth_token| must last until |callback| is |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 72 | // invoked, but |credentials| is only used during the initial call. |
| 73 | // |
| 74 | // All other return codes indicate that there was a problem generating a |
| 75 | // token, and the value of |*auth_token| is unspecified. |
| 76 | int GenerateAuthToken(const AuthCredentials* credentials, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 77 | const HttpRequestInfo* request, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 78 | const CompletionCallback& callback, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 79 | std::string* auth_token); |
| 80 | |
[email protected] | 65f3777 | 2010-12-13 18:20:06 | [diff] [blame] | 81 | // The authentication scheme as an enumerated value. |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 82 | HttpAuth::Scheme auth_scheme() const { |
[email protected] | 65f3777 | 2010-12-13 18:20:06 | [diff] [blame] | 83 | return auth_scheme_; |
| 84 | } |
| 85 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 86 | // The realm, encoded as UTF-8. This may be empty. |
[email protected] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 87 | const std::string& realm() const { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 88 | return realm_; |
| 89 | } |
| 90 | |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 91 | // The challenge which was issued when creating the handler. |
payal.pandey | 8a0c2c45 | 2015-04-28 06:36:01 | [diff] [blame] | 92 | const std::string& challenge() const { return auth_challenge_; } |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 93 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 94 | // Numeric rank based on the challenge's security level. Higher |
| 95 | // numbers are better. Used by HttpAuth::ChooseBestChallenge(). |
| 96 | int score() const { |
| 97 | return score_; |
| 98 | } |
| 99 | |
| 100 | HttpAuth::Target target() const { |
| 101 | return target_; |
| 102 | } |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 103 | |
[email protected] | 65f3777 | 2010-12-13 18:20:06 | [diff] [blame] | 104 | // Returns the proxy or server which issued the authentication challenge |
| 105 | // that this HttpAuthHandler is handling. The URL includes scheme, host, and |
| 106 | // port, but does not include path. |
| 107 | const GURL& origin() const { |
| 108 | return origin_; |
| 109 | } |
| 110 | |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 111 | // Returns true if the authentication scheme does not send the username and |
| 112 | // password in the clear. |
| 113 | bool encrypts_identity() const { |
| 114 | return (properties_ & ENCRYPTS_IDENTITY) != 0; |
| 115 | } |
| 116 | |
| 117 | // Returns true if the authentication scheme is connection-based, for |
| 118 | // example, NTLM. A connection-based authentication scheme does not support |
| 119 | // preemptive authentication, and must use the same handler object |
| 120 | // throughout the life of an HTTP transaction. |
| 121 | bool is_connection_based() const { |
| 122 | return (properties_ & IS_CONNECTION_BASED) != 0; |
| 123 | } |
| 124 | |
| 125 | // Returns true if the response to the current authentication challenge |
| 126 | // requires an identity. |
| 127 | // TODO(wtc): Find a better way to handle a multi-round challenge-response |
| 128 | // sequence used by a connection-based authentication scheme. |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 129 | virtual bool NeedsIdentity(); |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 130 | |
[email protected] | c573f48c | 2010-05-13 18:23:27 | [diff] [blame] | 131 | // Returns whether the default credentials may be used for the |origin| passed |
| 132 | // into |InitFromChallenge|. If true, the user does not need to be prompted |
| 133 | // for username and password to establish credentials. |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 134 | // NOTE: SSO is a potential security risk. |
| 135 | // TODO(cbentzel): Add a pointer to Firefox documentation about risk. |
[email protected] | 7cf4091 | 2010-12-09 18:25:03 | [diff] [blame] | 136 | virtual bool AllowsDefaultCredentials(); |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 137 | |
[email protected] | 26d84b0 | 2011-08-31 14:07:08 | [diff] [blame] | 138 | // Returns whether explicit credentials can be used with this handler. If |
| 139 | // true the user may be prompted for credentials if an implicit identity |
| 140 | // cannot be determined. |
| 141 | virtual bool AllowsExplicitCredentials(); |
| 142 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 143 | protected: |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 144 | enum Property { |
| 145 | ENCRYPTS_IDENTITY = 1 << 0, |
| 146 | IS_CONNECTION_BASED = 1 << 1, |
| 147 | }; |
| 148 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 149 | // Initializes the handler using a challenge issued by a server. |
| 150 | // |challenge| must be non-NULL and have already tokenized the |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 151 | // authentication scheme, but none of the tokens occurring after the |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 152 | // authentication scheme. |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 153 | // |
| 154 | // If the request was sent over an encrypted connection, |ssl_info| is valid |
| 155 | // and describes the connection. |
| 156 | // |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 157 | // Implementations are expected to initialize the following members: |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 158 | // scheme_, realm_, score_, properties_ |
asanka | 5ffd5d7 | 2016-03-23 16:20:49 | [diff] [blame] | 159 | virtual bool Init(HttpAuthChallengeTokenizer* challenge, |
| 160 | const SSLInfo& ssl_info) = 0; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 161 | |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 162 | // |GenerateAuthTokenImpl()} is the auth-scheme specific implementation |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 163 | // of generating the next auth token. Callers should use |GenerateAuthToken()| |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 164 | // which will in turn call |GenerateAuthTokenImpl()| |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 165 | virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 166 | const HttpRequestInfo* request, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 167 | const CompletionCallback& callback, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 168 | std::string* auth_token) = 0; |
| 169 | |
[email protected] | 65f3777 | 2010-12-13 18:20:06 | [diff] [blame] | 170 | // The auth-scheme as an enumerated value. |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 171 | HttpAuth::Scheme auth_scheme_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 172 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 173 | // The realm, encoded as UTF-8. Used by "basic" and "digest". |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 174 | std::string realm_; |
| 175 | |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 176 | // The auth challenge. |
| 177 | std::string auth_challenge_; |
| 178 | |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 179 | // The {scheme, host, port} for the authentication target. Used by "ntlm" |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 180 | // and "negotiate" to construct the service principal name. |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 181 | GURL origin_; |
| 182 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 183 | // The score for this challenge. Higher numbers are better. |
| 184 | int score_; |
| 185 | |
| 186 | // Whether this authentication request is for a proxy server, or an |
| 187 | // origin server. |
| 188 | HttpAuth::Target target_; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 189 | |
| 190 | // A bitmask of the properties of the authentication scheme. |
| 191 | int properties_; |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 192 | |
tfarina | 4283411 | 2016-09-22 13:38:20 | [diff] [blame] | 193 | NetLogWithSource net_log_; |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 194 | |
| 195 | private: |
| 196 | void OnGenerateAuthTokenComplete(int rv); |
| 197 | void FinishGenerateAuthToken(); |
| 198 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 199 | CompletionCallback callback_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 200 | }; |
| 201 | |
| 202 | } // namespace net |
| 203 | |
| 204 | #endif // NET_HTTP_HTTP_AUTH_HANDLER_H_ |