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