[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_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] | 8822f38c | 2010-07-30 21:49:03 | [diff] [blame] | 11 | #include "base/gtest_prod_util.h" |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 12 | #include "base/string16.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 13 | #include "net/http/http_auth_handler.h" |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 14 | #include "net/http/http_auth_handler_factory.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 15 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 16 | namespace net { |
| 17 | |
| 18 | // Code for handling http digest authentication. |
| 19 | class HttpAuthHandlerDigest : public HttpAuthHandler { |
| 20 | public: |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 21 | class Factory : public HttpAuthHandlerFactory { |
| 22 | public: |
| 23 | Factory(); |
| 24 | virtual ~Factory(); |
| 25 | |
| 26 | virtual int CreateAuthHandler(HttpAuth::ChallengeTokenizer* challenge, |
| 27 | HttpAuth::Target target, |
| 28 | const GURL& origin, |
[email protected] | fa82f93 | 2010-05-20 11:09:24 | [diff] [blame] | 29 | CreateReason reason, |
| 30 | int digest_nonce_count, |
[email protected] | ac5c06e | 2010-05-27 15:07:38 | [diff] [blame] | 31 | const BoundNetLog& net_log, |
[email protected] | 36c8e5f7 | 2010-06-07 14:17:14 | [diff] [blame] | 32 | scoped_ptr<HttpAuthHandler>* handler); |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 33 | }; |
| 34 | |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 35 | HttpAuth::AuthorizationResult HandleAnotherChallenge( |
| 36 | HttpAuth::ChallengeTokenizer* challenge); |
| 37 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 38 | protected: |
[email protected] | eca50e12 | 2010-09-11 14:03:30 | [diff] [blame] | 39 | virtual bool Init(HttpAuth::ChallengeTokenizer* challenge); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 40 | |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 41 | virtual int GenerateAuthTokenImpl(const string16* username, |
| 42 | const string16* password, |
[email protected] | bcc528e | 2010-06-10 15:03:24 | [diff] [blame] | 43 | const HttpRequestInfo* request, |
| 44 | CompletionCallback* callback, |
| 45 | std::string* auth_token); |
| 46 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 47 | private: |
[email protected] | 8822f38c | 2010-07-30 21:49:03 | [diff] [blame] | 48 | FRIEND_TEST_ALL_PREFIXES(HttpAuthHandlerDigestTest, ParseChallenge); |
| 49 | FRIEND_TEST_ALL_PREFIXES(HttpAuthHandlerDigestTest, AssembleCredentials); |
| 50 | FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, DigestPreAuthNonceCount); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 51 | |
| 52 | // Possible values for the "algorithm" property. |
| 53 | enum DigestAlgorithm { |
| 54 | // No algorithm was specified. According to RFC 2617 this means |
| 55 | // we should default to ALGORITHM_MD5. |
| 56 | ALGORITHM_UNSPECIFIED, |
| 57 | |
| 58 | // Hashes are run for every request. |
| 59 | ALGORITHM_MD5, |
| 60 | |
| 61 | // Hash is run only once during the first WWW-Authenticate handshake. |
| 62 | // (SESS means session). |
| 63 | ALGORITHM_MD5_SESS, |
| 64 | }; |
| 65 | |
| 66 | // Possible values for "qop" -- may be or-ed together if there were |
| 67 | // multiple comma separated values. |
| 68 | enum QualityOfProtection { |
| 69 | QOP_UNSPECIFIED = 0, |
| 70 | QOP_AUTH = 1 << 0, |
| 71 | QOP_AUTH_INT = 1 << 1, |
| 72 | }; |
| 73 | |
[email protected] | a15236494 | 2010-08-12 10:19:40 | [diff] [blame] | 74 | explicit HttpAuthHandlerDigest(int nonce_count); |
| 75 | ~HttpAuthHandlerDigest(); |
[email protected] | 5389bc7 | 2009-11-05 23:34:24 | [diff] [blame] | 76 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 77 | // Parse the challenge, saving the results into this instance. |
| 78 | // Returns true on success. |
[email protected] | fa55e19 | 2010-02-15 14:25:50 | [diff] [blame] | 79 | bool ParseChallenge(HttpAuth::ChallengeTokenizer* challenge); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 80 | |
| 81 | // Parse an individual property. Returns true on success. |
| 82 | bool ParseChallengeProperty(const std::string& name, |
| 83 | const std::string& value); |
| 84 | |
| 85 | // Generates a random string, to be used for client-nonce. |
| 86 | static std::string GenerateNonce(); |
| 87 | |
| 88 | // Convert enum value back to string. |
| 89 | static std::string QopToString(int qop); |
| 90 | static std::string AlgorithmToString(int algorithm); |
| 91 | |
| 92 | // Extract the method and path of the request, as needed by |
| 93 | // the 'A2' production. (path may be a hostname for proxy). |
| 94 | void GetRequestMethodAndPath(const HttpRequestInfo* request, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 95 | std::string* method, |
| 96 | std::string* path) const; |
| 97 | |
| 98 | // Build up the 'response' production. |
| 99 | std::string AssembleResponseDigest(const std::string& method, |
| 100 | const std::string& path, |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 101 | const string16& username, |
| 102 | const string16& password, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 103 | const std::string& cnonce, |
| 104 | const std::string& nc) const; |
| 105 | |
| 106 | // Build up the value for (Authorization/Proxy-Authorization). |
| 107 | std::string AssembleCredentials(const std::string& method, |
| 108 | const std::string& path, |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 109 | const string16& username, |
| 110 | const string16& password, |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 111 | const std::string& cnonce, |
| 112 | int nonce_count) const; |
| 113 | |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 114 | // Forces cnonce to be the same each time. This is used for unit tests. |
| 115 | static void SetFixedCnonce(bool fixed_cnonce) { |
| 116 | fixed_cnonce_ = fixed_cnonce; |
| 117 | } |
| 118 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 119 | // Information parsed from the challenge. |
| 120 | std::string nonce_; |
| 121 | std::string domain_; |
| 122 | std::string opaque_; |
| 123 | bool stale_; |
| 124 | DigestAlgorithm algorithm_; |
[email protected] | 13c8a09 | 2010-07-29 06:15:44 | [diff] [blame] | 125 | int qop_; // Bitfield of QualityOfProtection |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 126 | |
| 127 | int nonce_count_; |
[email protected] | 3c32c5f | 2010-05-18 15:18:12 | [diff] [blame] | 128 | |
| 129 | // Forces the cnonce to be the same each time, for unit tests. |
| 130 | static bool fixed_cnonce_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | } // namespace net |
| 134 | |
| 135 | #endif // NET_HTTP_HTTP_AUTH_HANDLER_DIGEST_H_ |