[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] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10 | #include "base/ref_counted.h" |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 11 | #include "net/base/completion_callback.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 12 | #include "net/http/http_auth.h" |
| 13 | |
| 14 | namespace net { |
| 15 | |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 16 | class BoundNetLog; |
| 17 | class HostResolver; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 18 | class ProxyInfo; |
[email protected] | 8c76ae2 | 2010-04-20 22:15:43 | [diff] [blame^] | 19 | struct HttpRequestInfo; |
[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] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 24 | class HttpAuthHandler : public base::RefCounted<HttpAuthHandler> { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 25 | public: |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 26 | // Initializes the handler using a challenge issued by a server. |
| 27 | // |challenge| must be non-NULL and have already tokenized the |
| 28 | // authentication scheme, but none of the tokens occuring after the |
| 29 | // authentication scheme. |target| and |origin| are both stored |
| 30 | // for later use, and are not part of the initial challenge. |
| 31 | bool InitFromChallenge(HttpAuth::ChallengeTokenizer* challenge, |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 32 | HttpAuth::Target target, |
| 33 | const GURL& origin); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 34 | |
| 35 | // Lowercase name of the auth scheme |
[email protected] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 36 | const std::string& scheme() const { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 37 | return scheme_; |
| 38 | } |
| 39 | |
| 40 | // The realm value that was parsed during Init(). |
[email protected] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 41 | const std::string& realm() const { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 42 | return realm_; |
| 43 | } |
| 44 | |
| 45 | // Numeric rank based on the challenge's security level. Higher |
| 46 | // numbers are better. Used by HttpAuth::ChooseBestChallenge(). |
| 47 | int score() const { |
| 48 | return score_; |
| 49 | } |
| 50 | |
| 51 | HttpAuth::Target target() const { |
| 52 | return target_; |
| 53 | } |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 54 | |
| 55 | // Returns true if the authentication scheme does not send the username and |
| 56 | // password in the clear. |
| 57 | bool encrypts_identity() const { |
| 58 | return (properties_ & ENCRYPTS_IDENTITY) != 0; |
| 59 | } |
| 60 | |
| 61 | // Returns true if the authentication scheme is connection-based, for |
| 62 | // example, NTLM. A connection-based authentication scheme does not support |
| 63 | // preemptive authentication, and must use the same handler object |
| 64 | // throughout the life of an HTTP transaction. |
| 65 | bool is_connection_based() const { |
| 66 | return (properties_ & IS_CONNECTION_BASED) != 0; |
| 67 | } |
| 68 | |
| 69 | // Returns true if the response to the current authentication challenge |
| 70 | // requires an identity. |
| 71 | // TODO(wtc): Find a better way to handle a multi-round challenge-response |
| 72 | // sequence used by a connection-based authentication scheme. |
| 73 | virtual bool NeedsIdentity() { return true; } |
| 74 | |
[email protected] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 75 | // Returns true if this is the final round of the authentication sequence. |
| 76 | // For Basic and Digest, the method always returns true because they are |
| 77 | // single-round schemes. |
| 78 | virtual bool IsFinalRound() { return true; } |
| 79 | |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 80 | // Returns whether the default credentials may be used for the |origin| passed
|
| 81 | // into |InitFromChallenge|. If true, the user does not need to be prompted
|
| 82 | // for username and password to establish credentials.
|
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 83 | // NOTE: SSO is a potential security risk. |
| 84 | // TODO(cbentzel): Add a pointer to Firefox documentation about risk. |
[email protected] | b4955e7d | 2010-04-16 20:22:30 | [diff] [blame] | 85 | virtual bool AllowsDefaultCredentials() { return false; } |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 86 | |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 87 | // Returns whether the canonical DNS name for the origin host needs to be |
| 88 | // resolved. The Negotiate auth scheme typically uses the canonical DNS |
| 89 | // name when constructing the Kerberos SPN. |
| 90 | virtual bool NeedsCanonicalName() { return false; } |
| 91 | |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 92 | // TODO(cbentzel): Separate providing credentials from generating the |
| 93 | // authentication token in the API. |
| 94 | |
| 95 | // Generates an authentication token. |
| 96 | // The return value is an error code. If the code is not |OK|, the value of |
| 97 | // |*auth_token| is unspecified. |
| 98 | // |auth_token| is a return value and must be non-NULL. |
| 99 | virtual int GenerateAuthToken(const std::wstring& username, |
| 100 | const std::wstring& password, |
| 101 | const HttpRequestInfo* request, |
| 102 | const ProxyInfo* proxy, |
| 103 | std::string* auth_token) = 0; |
| 104 | |
| 105 | // Generates an authentication token using default credentials. |
| 106 | // The return value is an error code. If the code is not |OK|, the value of |
| 107 | // |*auth_token| is unspecified. |
| 108 | // |auth_token| is a return value and must be non-NULL. |
[email protected] | d7f1663 | 2010-03-29 18:02:36 | [diff] [blame] | 109 | // This should only be called if |SupportsDefaultCredentials| returns true. |
[email protected] | ac3fa8e2 | 2010-02-05 19:13:29 | [diff] [blame] | 110 | virtual int GenerateDefaultAuthToken(const HttpRequestInfo* request, |
| 111 | const ProxyInfo* proxy, |
| 112 | std::string* auth_token) = 0; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 113 | |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 114 | // Resolves the canonical name for the |origin_| host. The canonical |
| 115 | // name is used by the Negotiate scheme to generate a valid Kerberos |
| 116 | // SPN. |
| 117 | // The return value is a net error code. |
| 118 | virtual int ResolveCanonicalName(HostResolver* host_resolver, |
| 119 | CompletionCallback* callback, |
| 120 | const BoundNetLog& net_log); |
| 121 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 122 | protected: |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 123 | enum Property { |
| 124 | ENCRYPTS_IDENTITY = 1 << 0, |
| 125 | IS_CONNECTION_BASED = 1 << 1, |
| 126 | }; |
| 127 | |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 128 | friend class base::RefCounted<HttpAuthHandler>; |
| 129 | |
| 130 | virtual ~HttpAuthHandler() { } |
| 131 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 132 | // Initializes the handler using a challenge issued by a server. |
| 133 | // |challenge| must be non-NULL and have already tokenized the |
| 134 | // authentication scheme, but none of the tokens occuring after the |
| 135 | // authentication scheme. |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 136 | // Implementations are expcted to initialize the following members: |
| 137 | // scheme_, realm_, score_, properties_ |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 138 | virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) = 0; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 139 | |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 140 | // The lowercase auth-scheme {"basic", "digest", "ntlm", "negotiate"} |
[email protected] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 141 | std::string scheme_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 142 | |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 143 | // The realm. Used by "basic" and "digest". |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 144 | std::string realm_; |
| 145 | |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 146 | // The {scheme, host, port} for the authentication target. Used by "ntlm" |
[email protected] | e5ae96a | 2010-04-14 20:12:45 | [diff] [blame] | 147 | // and "negotiate" to construct the service principal name. |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame] | 148 | GURL origin_; |
| 149 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 150 | // The score for this challenge. Higher numbers are better. |
| 151 | int score_; |
| 152 | |
| 153 | // Whether this authentication request is for a proxy server, or an |
| 154 | // origin server. |
| 155 | HttpAuth::Target target_; |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 156 | |
| 157 | // A bitmask of the properties of the authentication scheme. |
| 158 | int properties_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 159 | }; |
| 160 | |
| 161 | } // namespace net |
| 162 | |
| 163 | #endif // NET_HTTP_HTTP_AUTH_HANDLER_H_ |