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