blob: 0c38641e1c6b3f561f1eccdf26d87385eddf4292 [file] [log] [blame]
[email protected]ac3fa8e22010-02-05 19:13:291// Copyright (c) 2010 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]8822f38c2010-07-30 21:49:0311#include "base/gtest_prod_util.h"
[email protected]13c8a092010-07-29 06:15:4412#include "base/string16.h"
[email protected]c3b35c22008-09-27 03:19:4213#include "net/http/http_auth_handler.h"
[email protected]fa55e192010-02-15 14:25:5014#include "net/http/http_auth_handler_factory.h"
[email protected]c3b35c22008-09-27 03:19:4215
[email protected]c3b35c22008-09-27 03:19:4216namespace net {
17
18// Code for handling http digest authentication.
19class HttpAuthHandlerDigest : public HttpAuthHandler {
20 public:
[email protected]fa55e192010-02-15 14:25:5021 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]fa82f932010-05-20 11:09:2429 CreateReason reason,
30 int digest_nonce_count,
[email protected]ac5c06e2010-05-27 15:07:3831 const BoundNetLog& net_log,
[email protected]36c8e5f72010-06-07 14:17:1432 scoped_ptr<HttpAuthHandler>* handler);
[email protected]fa55e192010-02-15 14:25:5033 };
34
[email protected]eca50e122010-09-11 14:03:3035 HttpAuth::AuthorizationResult HandleAnotherChallenge(
36 HttpAuth::ChallengeTokenizer* challenge);
37
[email protected]c3b35c22008-09-27 03:19:4238 protected:
[email protected]eca50e122010-09-11 14:03:3039 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge);
[email protected]c3b35c22008-09-27 03:19:4240
[email protected]13c8a092010-07-29 06:15:4441 virtual int GenerateAuthTokenImpl(const string16* username,
42 const string16* password,
[email protected]bcc528e2010-06-10 15:03:2443 const HttpRequestInfo* request,
44 CompletionCallback* callback,
45 std::string* auth_token);
46
[email protected]c3b35c22008-09-27 03:19:4247 private:
[email protected]8822f38c2010-07-30 21:49:0348 FRIEND_TEST_ALL_PREFIXES(HttpAuthHandlerDigestTest, ParseChallenge);
49 FRIEND_TEST_ALL_PREFIXES(HttpAuthHandlerDigestTest, AssembleCredentials);
50 FRIEND_TEST_ALL_PREFIXES(HttpNetworkTransactionTest, DigestPreAuthNonceCount);
[email protected]c3b35c22008-09-27 03:19:4251
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]a152364942010-08-12 10:19:4074 explicit HttpAuthHandlerDigest(int nonce_count);
75 ~HttpAuthHandlerDigest();
[email protected]5389bc72009-11-05 23:34:2476
[email protected]c3b35c22008-09-27 03:19:4277 // Parse the challenge, saving the results into this instance.
78 // Returns true on success.
[email protected]fa55e192010-02-15 14:25:5079 bool ParseChallenge(HttpAuth::ChallengeTokenizer* challenge);
[email protected]c3b35c22008-09-27 03:19:4280
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]c3b35c22008-09-27 03:19:4295 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]13c8a092010-07-29 06:15:44101 const string16& username,
102 const string16& password,
[email protected]c3b35c22008-09-27 03:19:42103 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]13c8a092010-07-29 06:15:44109 const string16& username,
110 const string16& password,
[email protected]c3b35c22008-09-27 03:19:42111 const std::string& cnonce,
112 int nonce_count) const;
113
[email protected]3c32c5f2010-05-18 15:18:12114 // 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]c3b35c22008-09-27 03:19:42119 // 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]13c8a092010-07-29 06:15:44125 int qop_; // Bitfield of QualityOfProtection
[email protected]f9ee6b52008-11-08 06:46:23126
127 int nonce_count_;
[email protected]3c32c5f2010-05-18 15:18:12128
129 // Forces the cnonce to be the same each time, for unit tests.
130 static bool fixed_cnonce_;
[email protected]c3b35c22008-09-27 03:19:42131};
132
133} // namespace net
134
135#endif // NET_HTTP_HTTP_AUTH_HANDLER_DIGEST_H_