[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] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 12 | #include "net/base/net_log.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 13 | #include "net/http/http_auth.h" |
| 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; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 19 | |
| 20 | // HttpAuthHandler is the interface for the authentication schemes |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 21 | // (basic, digest, NTLM, Negotiate). |
| 22 | // HttpAuthHandler objects are typically created by an HttpAuthHandlerFactory. |
[email protected] | 172da1b | 2011-08-12 15:52:26 | [diff] [blame] | 23 | class NET_EXPORT_PRIVATE HttpAuthHandler { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 24 | public: |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 25 | HttpAuthHandler(); |
[email protected] | cb2825c | 2010-07-01 10:23:38 | [diff] [blame] | 26 | virtual ~HttpAuthHandler(); |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 27 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 28 | // Initializes the handler using a challenge issued by a server. |
| 29 | // |challenge| must be non-NULL and have already tokenized the |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 30 | // authentication scheme, but none of the tokens occurring after the |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 31 | // authentication scheme. |target| and |origin| are both stored |
| 32 | // for later use, and are not part of the initial challenge. |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame^] | 33 | bool InitFromChallenge(HttpAuthChallengeTokenizer* challenge, |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 34 | HttpAuth::Target target, |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 35 | const GURL& origin, |
| 36 | const BoundNetLog& net_log); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 37 | |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 38 | // Determines how the previous authorization attempt was received. |
| 39 | // |
| 40 | // This is called when the server/proxy responds with a 401/407 after an |
| 41 | // earlier authorization attempt. Although this normally means that the |
| 42 | // previous attempt was rejected, in multi-round schemes such as |
| 43 | // NTLM+Negotiate it may indicate that another round of challenge+response |
| 44 | // is required. For Digest authentication it may also mean that the previous |
| 45 | // attempt used a stale nonce (and nonce-count) and that a new attempt should |
| 46 | // be made with a different nonce provided in the challenge. |
| 47 | // |
| 48 | // |challenge| must be non-NULL and have already tokenized the |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 49 | // authentication scheme, but none of the tokens occurring after the |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 50 | // authentication scheme. |
| 51 | virtual HttpAuth::AuthorizationResult HandleAnotherChallenge( |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame^] | 52 | HttpAuthChallengeTokenizer* challenge) = 0; |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 53 | |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 54 | // Generates an authentication token, potentially asynchronously. |
| 55 | // |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 56 | // When |credentials| is NULL, the default credentials for the currently |
| 57 | // logged in user are used. |AllowsDefaultCredentials()| MUST be true in this |
| 58 | // case. |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 59 | // |
| 60 | // |request|, |callback|, and |auth_token| must be non-NULL. |
| 61 | // |
| 62 | // The return value is a net error code. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 63 | // |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 64 | // If |OK| is returned, |*auth_token| is filled in with an authentication |
| 65 | // token which can be inserted in the HTTP request. |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 66 | // |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 67 | // If |ERR_IO_PENDING| is returned, |*auth_token| will be filled in |
| 68 | // asynchronously and |callback| will be invoked. The lifetime of |
| 69 | // |request|, |callback|, and |auth_token| must last until |callback| is |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 70 | // invoked, but |credentials| is only used during the initial call. |
| 71 | // |
| 72 | // All other return codes indicate that there was a problem generating a |
| 73 | // token, and the value of |*auth_token| is unspecified. |
| 74 | int GenerateAuthToken(const AuthCredentials* credentials, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 75 | const HttpRequestInfo* request, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 76 | const CompletionCallback& callback, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 77 | std::string* auth_token); |
| 78 | |
[email protected] | 65f3777 | 2010-12-13 18:20:06 | [diff] [blame] | 79 | // The authentication scheme as an enumerated value. |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 80 | HttpAuth::Scheme auth_scheme() const { |
[email protected] | 65f3777 | 2010-12-13 18:20:06 | [diff] [blame] | 81 | return auth_scheme_; |
| 82 | } |
| 83 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 84 | // The realm, encoded as UTF-8. This may be empty. |
[email protected] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 85 | const std::string& realm() const { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 86 | return realm_; |
| 87 | } |
| 88 | |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 89 | // The challenge which was issued when creating the handler. |
| 90 | const std::string challenge() const { |
| 91 | return auth_challenge_; |
| 92 | } |
| 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. |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 153 | // Implementations are expected to initialize the following members: |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 154 | // scheme_, realm_, score_, properties_ |
[email protected] | df41d0d8 | 2014-03-13 00:43:24 | [diff] [blame^] | 155 | virtual bool Init(HttpAuthChallengeTokenizer* challenge) = 0; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 156 | |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 157 | // |GenerateAuthTokenImpl()} is the auth-scheme specific implementation |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 158 | // of generating the next auth token. Callers should use |GenerateAuthToken()| |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 159 | // which will in turn call |GenerateAuthTokenImpl()| |
[email protected] | f3cf980 | 2011-10-28 18:44:58 | [diff] [blame] | 160 | virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 161 | const HttpRequestInfo* request, |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 162 | const CompletionCallback& callback, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 163 | std::string* auth_token) = 0; |
| 164 | |
[email protected] | 65f3777 | 2010-12-13 18:20:06 | [diff] [blame] | 165 | // The auth-scheme as an enumerated value. |
[email protected] | 547fc79 | 2011-01-13 13:31:17 | [diff] [blame] | 166 | HttpAuth::Scheme auth_scheme_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 167 | |
[email protected] | 79cb5c1 | 2011-09-12 13:12:04 | [diff] [blame] | 168 | // The realm, encoded as UTF-8. Used by "basic" and "digest". |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 169 | std::string realm_; |
| 170 | |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 171 | // The auth challenge. |
| 172 | std::string auth_challenge_; |
| 173 | |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 174 | // The {scheme, host, port} for the authentication target. Used by "ntlm" |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 175 | // and "negotiate" to construct the service principal name. |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 176 | GURL origin_; |
| 177 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 178 | // The score for this challenge. Higher numbers are better. |
| 179 | int score_; |
| 180 | |
| 181 | // Whether this authentication request is for a proxy server, or an |
| 182 | // origin server. |
| 183 | HttpAuth::Target target_; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 184 | |
| 185 | // A bitmask of the properties of the authentication scheme. |
| 186 | int properties_; |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 187 | |
| 188 | BoundNetLog net_log_; |
[email protected] | 7775c7d | 2010-06-21 17:50:21 | [diff] [blame] | 189 | |
| 190 | private: |
| 191 | void OnGenerateAuthTokenComplete(int rv); |
| 192 | void FinishGenerateAuthToken(); |
| 193 | |
[email protected] | 49639fa | 2011-12-20 23:22:41 | [diff] [blame] | 194 | CompletionCallback callback_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | } // namespace net |
| 198 | |
| 199 | #endif // NET_HTTP_HTTP_AUTH_HANDLER_H_ |