[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [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_DIGEST_H_ |
| 6 | #define NET_HTTP_HTTP_AUTH_HANDLER_DIGEST_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 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 9 | #include <string> |
| 10 | |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 11 | #include "base/basictypes.h" |
[email protected] | 8822f38 | 2010-07-30 21:49:03 | [diff] [blame] | 12 | #include "base/gtest_prod_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 13 | #include "base/memory/scoped_ptr.h" |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 14 | #include "base/string16.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 15 | #include "net/http/http_auth_handler.h" |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 16 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 17 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 18 | namespace net { |
| 19 | |
| 20 | // Code for handling http digest authentication. |
| 21 | class HttpAuthHandlerDigest : public HttpAuthHandler { |
| 22 | public: |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 23 | // A NonceGenerator is a simple interface for generating client nonces. |
| 24 | // Unit tests can override the default client nonce behavior with fixed |
| 25 | // nonce generation to get reproducible results. |
| 26 | class NonceGenerator { |
| 27 | public: |
| 28 | NonceGenerator(); |
| 29 | virtual ~NonceGenerator(); |
| 30 | |
| 31 | // Generates a client nonce. |
| 32 | virtual std::string GenerateNonce() const = 0; |
| 33 | private: |
| 34 | DISALLOW_COPY_AND_ASSIGN(NonceGenerator); |
| 35 | }; |
| 36 | |
| 37 | // DynamicNonceGenerator does a random shuffle of 16 |
| 38 | // characters to generate a client nonce. |
| 39 | class DynamicNonceGenerator : public NonceGenerator { |
| 40 | public: |
| 41 | DynamicNonceGenerator(); |
| 42 | virtual std::string GenerateNonce() const; |
| 43 | private: |
| 44 | DISALLOW_COPY_AND_ASSIGN(DynamicNonceGenerator); |
| 45 | }; |
| 46 | |
| 47 | // FixedNonceGenerator always uses the same string specified at |
| 48 | // construction time as the client nonce. |
| 49 | class FixedNonceGenerator : public NonceGenerator { |
| 50 | public: |
| 51 | explicit FixedNonceGenerator(const std::string& nonce); |
| 52 | |
| 53 | virtual std::string GenerateNonce() const; |
| 54 | |
| 55 | private: |
| 56 | const std::string nonce_; |
| 57 | DISALLOW_COPY_AND_ASSIGN(FixedNonceGenerator); |
| 58 | }; |
| 59 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 60 | class Factory : public HttpAuthHandlerFactory { |
| 61 | public: |
| 62 | Factory(); |
| 63 | virtual ~Factory(); |
| 64 | |
[email protected] | d100e44f | 2011-01-26 22:47:11 | [diff] [blame] | 65 | // This factory owns the passed in |nonce_generator|. |
| 66 | void set_nonce_generator(const NonceGenerator* nonce_generator); |
| 67 | |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 68 | virtual int CreateAuthHandler(HttpAuth::ChallengeTokenizer* challenge, |
| 69 | HttpAuth::Target target, |
| 70 | const GURL& origin, |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 71 | CreateReason reason, |
| 72 | int digest_nonce_count, |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 73 | const BoundNetLog& net_log, |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 74 | scoped_ptr<HttpAuthHandler>* handler); |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 75 | |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 76 | private: |
| 77 | scoped_ptr<const NonceGenerator> nonce_generator_; |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 78 | }; |
| 79 | |
[email protected] | 78994ab0 | 2010-12-08 18:06:44 | [diff] [blame] | 80 | virtual HttpAuth::AuthorizationResult HandleAnotherChallenge( |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 81 | HttpAuth::ChallengeTokenizer* challenge); |
| 82 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 83 | protected: |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 84 | virtual bool Init(HttpAuth::ChallengeTokenizer* challenge); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 85 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 86 | virtual int GenerateAuthTokenImpl(const string16* username, |
| 87 | const string16* password, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 88 | const HttpRequestInfo* request, |
| 89 | CompletionCallback* callback, |
| 90 | std::string* auth_token); |
| 91 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 92 | private: |
[email protected] | 8822f38 | 2010-07-30 21:49:03 | [diff] [blame] | 93 | FRIEND_TEST_ALL_PREFIXES(HttpAuthHandlerDigestTest, ParseChallenge); |
| 94 | FRIEND_TEST_ALL_PREFIXES(HttpAuthHandlerDigestTest, AssembleCredentials); |
| 95 | FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, DigestPreAuthNonceCount); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 96 | |
| 97 | // Possible values for the "algorithm" property. |
| 98 | enum DigestAlgorithm { |
| 99 | // No algorithm was specified. According to RFC 2617 this means |
| 100 | // we should default to ALGORITHM_MD5. |
| 101 | ALGORITHM_UNSPECIFIED, |
| 102 | |
| 103 | // Hashes are run for every request. |
| 104 | ALGORITHM_MD5, |
| 105 | |
| 106 | // Hash is run only once during the first WWW-Authenticate handshake. |
| 107 | // (SESS means session). |
| 108 | ALGORITHM_MD5_SESS, |
| 109 | }; |
| 110 | |
[email protected] | eb83311 | 2010-11-15 18:30:14 | [diff] [blame] | 111 | // Possible values for QualityOfProtection. |
| 112 | // auth-int is not supported, see http://crbug.com/62890 for justification. |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 113 | enum QualityOfProtection { |
[email protected] | eb83311 | 2010-11-15 18:30:14 | [diff] [blame] | 114 | QOP_UNSPECIFIED, |
| 115 | QOP_AUTH, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 116 | }; |
| 117 | |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 118 | // |nonce_count| indicates how many times the server-specified nonce has |
| 119 | // been used so far. |
| 120 | // |nonce_generator| is used to create a client nonce, and is not owned by |
| 121 | // the handler. The lifetime of the |nonce_generator| must exceed that of this |
| 122 | // handler. |
| 123 | HttpAuthHandlerDigest(int nonce_count, const NonceGenerator* nonce_generator); |
[email protected] | a15236494 | 2010-08-12 10:19:40 | [diff] [blame] | 124 | ~HttpAuthHandlerDigest(); |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 125 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 126 | // Parse the challenge, saving the results into this instance. |
| 127 | // Returns true on success. |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 128 | bool ParseChallenge(HttpAuth::ChallengeTokenizer* challenge); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 129 | |
| 130 | // Parse an individual property. Returns true on success. |
| 131 | bool ParseChallengeProperty(const std::string& name, |
| 132 | const std::string& value); |
| 133 | |
| 134 | // Generates a random string, to be used for client-nonce. |
| 135 | static std::string GenerateNonce(); |
| 136 | |
| 137 | // Convert enum value back to string. |
[email protected] | eb83311 | 2010-11-15 18:30:14 | [diff] [blame] | 138 | static std::string QopToString(QualityOfProtection qop); |
| 139 | static std::string AlgorithmToString(DigestAlgorithm algorithm); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 140 | |
| 141 | // Extract the method and path of the request, as needed by |
| 142 | // the 'A2' production. (path may be a hostname for proxy). |
| 143 | void GetRequestMethodAndPath(const HttpRequestInfo* request, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 144 | std::string* method, |
| 145 | std::string* path) const; |
| 146 | |
| 147 | // Build up the 'response' production. |
| 148 | std::string AssembleResponseDigest(const std::string& method, |
| 149 | const std::string& path, |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 150 | const string16& username, |
| 151 | const string16& password, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 152 | const std::string& cnonce, |
| 153 | const std::string& nc) const; |
| 154 | |
| 155 | // Build up the value for (Authorization/Proxy-Authorization). |
| 156 | std::string AssembleCredentials(const std::string& method, |
| 157 | const std::string& path, |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 158 | const string16& username, |
| 159 | const string16& password, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 160 | const std::string& cnonce, |
| 161 | int nonce_count) const; |
| 162 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 163 | // Information parsed from the challenge. |
| 164 | std::string nonce_; |
| 165 | std::string domain_; |
| 166 | std::string opaque_; |
| 167 | bool stale_; |
| 168 | DigestAlgorithm algorithm_; |
[email protected] | eb83311 | 2010-11-15 18:30:14 | [diff] [blame] | 169 | QualityOfProtection qop_; |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 170 | |
| 171 | int nonce_count_; |
[email protected] | 54fea256 | 2010-11-17 14:40:44 | [diff] [blame] | 172 | const NonceGenerator* nonce_generator_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | } // namespace net |
| 176 | |
| 177 | #endif // NET_HTTP_HTTP_AUTH_HANDLER_DIGEST_H_ |