blob: f279b656fc34b5ec883d3340cdf775dbdafb465a [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]c3b35c22008-09-27 03:19:422// 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]32b76ef2010-07-26 23:08:247#pragma once
[email protected]c3b35c22008-09-27 03:19:428
[email protected]13c8a092010-07-29 06:15:449#include <string>
10
[email protected]54fea2562010-11-17 14:40:4411#include "base/basictypes.h"
[email protected]8822f382010-07-30 21:49:0312#include "base/gtest_prod_util.h"
[email protected]3b63f8f42011-03-28 01:54:1513#include "base/memory/scoped_ptr.h"
[email protected]13c8a092010-07-29 06:15:4414#include "base/string16.h"
[email protected]c3b35c22008-09-27 03:19:4215#include "net/http/http_auth_handler.h"
[email protected]fa55e192010-02-15 14:25:5016#include "net/http/http_auth_handler_factory.h"
[email protected]c3b35c22008-09-27 03:19:4217
[email protected]c3b35c22008-09-27 03:19:4218namespace net {
19
20// Code for handling http digest authentication.
21class HttpAuthHandlerDigest : public HttpAuthHandler {
22 public:
[email protected]54fea2562010-11-17 14:40:4423 // 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]fa55e192010-02-15 14:25:5060 class Factory : public HttpAuthHandlerFactory {
61 public:
62 Factory();
63 virtual ~Factory();
64
[email protected]d100e44f2011-01-26 22:47:1165 // This factory owns the passed in |nonce_generator|.
66 void set_nonce_generator(const NonceGenerator* nonce_generator);
67
[email protected]fa55e192010-02-15 14:25:5068 virtual int CreateAuthHandler(HttpAuth::ChallengeTokenizer* challenge,
69 HttpAuth::Target target,
70 const GURL& origin,
[email protected]fa82f932010-05-20 11:09:2471 CreateReason reason,
72 int digest_nonce_count,
[email protected]ac5c06e2010-05-27 15:07:3873 const BoundNetLog& net_log,
[email protected]36c8e5f72010-06-07 14:17:1474 scoped_ptr<HttpAuthHandler>* handler);
[email protected]54fea2562010-11-17 14:40:4475
[email protected]54fea2562010-11-17 14:40:4476 private:
77 scoped_ptr<const NonceGenerator> nonce_generator_;
[email protected]fa55e192010-02-15 14:25:5078 };
79
[email protected]78994ab02010-12-08 18:06:4480 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge(
[email protected]eca50e122010-09-11 14:03:3081 HttpAuth::ChallengeTokenizer* challenge);
82
[email protected]c3b35c22008-09-27 03:19:4283 protected:
[email protected]eca50e122010-09-11 14:03:3084 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge);
[email protected]c3b35c22008-09-27 03:19:4285
[email protected]13c8a092010-07-29 06:15:4486 virtual int GenerateAuthTokenImpl(const string16* username,
87 const string16* password,
[email protected]bcc528e2010-06-10 15:03:2488 const HttpRequestInfo* request,
89 CompletionCallback* callback,
90 std::string* auth_token);
91
[email protected]c3b35c22008-09-27 03:19:4292 private:
[email protected]8822f382010-07-30 21:49:0393 FRIEND_TEST_ALL_PREFIXES(HttpAuthHandlerDigestTest, ParseChallenge);
94 FRIEND_TEST_ALL_PREFIXES(HttpAuthHandlerDigestTest, AssembleCredentials);
95 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, DigestPreAuthNonceCount);
[email protected]c3b35c22008-09-27 03:19:4296
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]eb833112010-11-15 18:30:14111 // Possible values for QualityOfProtection.
112 // auth-int is not supported, see http://crbug.com/62890 for justification.
[email protected]c3b35c22008-09-27 03:19:42113 enum QualityOfProtection {
[email protected]eb833112010-11-15 18:30:14114 QOP_UNSPECIFIED,
115 QOP_AUTH,
[email protected]c3b35c22008-09-27 03:19:42116 };
117
[email protected]54fea2562010-11-17 14:40:44118 // |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]a152364942010-08-12 10:19:40124 ~HttpAuthHandlerDigest();
[email protected]5389bc72009-11-05 23:34:24125
[email protected]c3b35c22008-09-27 03:19:42126 // Parse the challenge, saving the results into this instance.
127 // Returns true on success.
[email protected]fa55e192010-02-15 14:25:50128 bool ParseChallenge(HttpAuth::ChallengeTokenizer* challenge);
[email protected]c3b35c22008-09-27 03:19:42129
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]eb833112010-11-15 18:30:14138 static std::string QopToString(QualityOfProtection qop);
139 static std::string AlgorithmToString(DigestAlgorithm algorithm);
[email protected]c3b35c22008-09-27 03:19:42140
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]c3b35c22008-09-27 03:19:42144 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]13c8a092010-07-29 06:15:44150 const string16& username,
151 const string16& password,
[email protected]c3b35c22008-09-27 03:19:42152 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]13c8a092010-07-29 06:15:44158 const string16& username,
159 const string16& password,
[email protected]c3b35c22008-09-27 03:19:42160 const std::string& cnonce,
161 int nonce_count) const;
162
[email protected]c3b35c22008-09-27 03:19:42163 // 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]eb833112010-11-15 18:30:14169 QualityOfProtection qop_;
[email protected]f9ee6b52008-11-08 06:46:23170
171 int nonce_count_;
[email protected]54fea2562010-11-17 14:40:44172 const NonceGenerator* nonce_generator_;
[email protected]c3b35c22008-09-27 03:19:42173};
174
175} // namespace net
176
177#endif // NET_HTTP_HTTP_AUTH_HANDLER_DIGEST_H_