blob: a6faebeedbf436cee22117907e6f3515cacb9267 [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_H_
6#define NET_HTTP_HTTP_AUTH_HANDLER_H_
7
8#include <string>
9
[email protected]e5ae96a2010-04-14 20:12:4510#include "net/base/completion_callback.h"
[email protected]ac5c06e2010-05-27 15:07:3811#include "net/base/net_log.h"
[email protected]c3b35c22008-09-27 03:19:4212#include "net/http/http_auth.h"
13
[email protected]cb2825c2010-07-01 10:23:3814class Histogram;
15
[email protected]c3b35c22008-09-27 03:19:4216namespace net {
17
[email protected]e5ae96a2010-04-14 20:12:4518class HostResolver;
[email protected]c3b35c22008-09-27 03:19:4219class ProxyInfo;
[email protected]8c76ae22010-04-20 22:15:4320struct HttpRequestInfo;
[email protected]c3b35c22008-09-27 03:19:4221
22// HttpAuthHandler is the interface for the authentication schemes
[email protected]fa55e192010-02-15 14:25:5023// (basic, digest, NTLM, Negotiate).
24// HttpAuthHandler objects are typically created by an HttpAuthHandlerFactory.
[email protected]36c8e5f72010-06-07 14:17:1425class HttpAuthHandler {
[email protected]c3b35c22008-09-27 03:19:4226 public:
[email protected]7775c7d2010-06-21 17:50:2127 HttpAuthHandler();
[email protected]cb2825c2010-07-01 10:23:3828 virtual ~HttpAuthHandler();
[email protected]36c8e5f72010-06-07 14:17:1429
[email protected]fa55e192010-02-15 14:25:5030 // Initializes the handler using a challenge issued by a server.
31 // |challenge| must be non-NULL and have already tokenized the
32 // authentication scheme, but none of the tokens occuring after the
33 // authentication scheme. |target| and |origin| are both stored
34 // for later use, and are not part of the initial challenge.
35 bool InitFromChallenge(HttpAuth::ChallengeTokenizer* challenge,
[email protected]4de702f42009-09-18 17:46:1036 HttpAuth::Target target,
[email protected]ac5c06e2010-05-27 15:07:3837 const GURL& origin,
38 const BoundNetLog& net_log);
[email protected]c3b35c22008-09-27 03:19:4239
[email protected]bcc528e2010-06-10 15:03:2440 // Generates an authentication token, potentially asynchronously.
41 //
42 // When |username| and |password| are NULL, the default credentials for
43 // the currently logged in user are used. |AllowsDefaultCredentials()| MUST be
44 // true in this case.
45 //
46 // |request|, |callback|, and |auth_token| must be non-NULL.
47 //
48 // The return value is a net error code.
49 // If |OK| is returned, |*auth_token| is filled in with an authentication
50 // token which can be inserted in the HTTP request.
51 // If |ERR_IO_PENDING| is returned, |*auth_token| will be filled in
52 // asynchronously and |callback| will be invoked. The lifetime of
53 // |request|, |callback|, and |auth_token| must last until |callback| is
54 // invoked, but |username| and |password| are only used during the initial
55 // call.
56 // Otherwise, there was a problem generating a token synchronously, and the
57 // value of |*auth_token| is unspecified.
58 int GenerateAuthToken(const std::wstring* username,
59 const std::wstring* password,
60 const HttpRequestInfo* request,
61 CompletionCallback* callback,
62 std::string* auth_token);
63
[email protected]c3b35c22008-09-27 03:19:4264 // Lowercase name of the auth scheme
[email protected]e34c85d82008-12-02 06:59:0965 const std::string& scheme() const {
[email protected]c3b35c22008-09-27 03:19:4266 return scheme_;
67 }
68
69 // The realm value that was parsed during Init().
[email protected]e34c85d82008-12-02 06:59:0970 const std::string& realm() const {
[email protected]c3b35c22008-09-27 03:19:4271 return realm_;
72 }
73
[email protected]fa82f932010-05-20 11:09:2474 // The challenge which was issued when creating the handler.
75 const std::string challenge() const {
76 return auth_challenge_;
77 }
78
[email protected]c3b35c22008-09-27 03:19:4279 // Numeric rank based on the challenge's security level. Higher
80 // numbers are better. Used by HttpAuth::ChooseBestChallenge().
81 int score() const {
82 return score_;
83 }
84
85 HttpAuth::Target target() const {
86 return target_;
87 }
[email protected]3f918782009-02-28 01:29:2488
89 // Returns true if the authentication scheme does not send the username and
90 // password in the clear.
91 bool encrypts_identity() const {
92 return (properties_ & ENCRYPTS_IDENTITY) != 0;
93 }
94
95 // Returns true if the authentication scheme is connection-based, for
96 // example, NTLM. A connection-based authentication scheme does not support
97 // preemptive authentication, and must use the same handler object
98 // throughout the life of an HTTP transaction.
99 bool is_connection_based() const {
100 return (properties_ & IS_CONNECTION_BASED) != 0;
101 }
102
103 // Returns true if the response to the current authentication challenge
104 // requires an identity.
105 // TODO(wtc): Find a better way to handle a multi-round challenge-response
106 // sequence used by a connection-based authentication scheme.
107 virtual bool NeedsIdentity() { return true; }
108
[email protected]ea9dc9a2009-09-05 00:43:32109 // Returns true if this is the final round of the authentication sequence.
110 // For Basic and Digest, the method always returns true because they are
111 // single-round schemes.
112 virtual bool IsFinalRound() { return true; }
113
[email protected]c573f48c2010-05-13 18:23:27114 // Returns whether the default credentials may be used for the |origin| passed
115 // into |InitFromChallenge|. If true, the user does not need to be prompted
116 // for username and password to establish credentials.
[email protected]d7f16632010-03-29 18:02:36117 // NOTE: SSO is a potential security risk.
118 // TODO(cbentzel): Add a pointer to Firefox documentation about risk.
[email protected]b4955e7d2010-04-16 20:22:30119 virtual bool AllowsDefaultCredentials() { return false; }
[email protected]ac3fa8e22010-02-05 19:13:29120
[email protected]e5ae96a2010-04-14 20:12:45121 // Returns whether the canonical DNS name for the origin host needs to be
122 // resolved. The Negotiate auth scheme typically uses the canonical DNS
123 // name when constructing the Kerberos SPN.
124 virtual bool NeedsCanonicalName() { return false; }
125
[email protected]e5ae96a2010-04-14 20:12:45126 // Resolves the canonical name for the |origin_| host. The canonical
127 // name is used by the Negotiate scheme to generate a valid Kerberos
128 // SPN.
129 // The return value is a net error code.
130 virtual int ResolveCanonicalName(HostResolver* host_resolver,
[email protected]ac5c06e2010-05-27 15:07:38131 CompletionCallback* callback);
[email protected]e5ae96a2010-04-14 20:12:45132
[email protected]c3b35c22008-09-27 03:19:42133 protected:
[email protected]3f918782009-02-28 01:29:24134 enum Property {
135 ENCRYPTS_IDENTITY = 1 << 0,
136 IS_CONNECTION_BASED = 1 << 1,
137 };
138
[email protected]fa55e192010-02-15 14:25:50139 // Initializes the handler using a challenge issued by a server.
140 // |challenge| must be non-NULL and have already tokenized the
141 // authentication scheme, but none of the tokens occuring after the
142 // authentication scheme.
[email protected]3f918782009-02-28 01:29:24143 // Implementations are expcted to initialize the following members:
144 // scheme_, realm_, score_, properties_
[email protected]fa55e192010-02-15 14:25:50145 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) = 0;
[email protected]c3b35c22008-09-27 03:19:42146
[email protected]bcc528e2010-06-10 15:03:24147 // |GenerateAuthTokenImpl()} is the auth-scheme specific implementation
148 // of generating the next auth token. Callers sohuld use |GenerateAuthToken()|
149 // which will in turn call |GenerateAuthTokenImpl()|
150 virtual int GenerateAuthTokenImpl(const std::wstring* username,
151 const std::wstring* password,
152 const HttpRequestInfo* request,
153 CompletionCallback* callback,
154 std::string* auth_token) = 0;
155
[email protected]e5ae96a2010-04-14 20:12:45156 // The lowercase auth-scheme {"basic", "digest", "ntlm", "negotiate"}
[email protected]e34c85d82008-12-02 06:59:09157 std::string scheme_;
[email protected]c3b35c22008-09-27 03:19:42158
[email protected]4de702f42009-09-18 17:46:10159 // The realm. Used by "basic" and "digest".
[email protected]c3b35c22008-09-27 03:19:42160 std::string realm_;
161
[email protected]fa82f932010-05-20 11:09:24162 // The auth challenge.
163 std::string auth_challenge_;
164
[email protected]4de702f42009-09-18 17:46:10165 // The {scheme, host, port} for the authentication target. Used by "ntlm"
[email protected]e5ae96a2010-04-14 20:12:45166 // and "negotiate" to construct the service principal name.
[email protected]4de702f42009-09-18 17:46:10167 GURL origin_;
168
[email protected]c3b35c22008-09-27 03:19:42169 // The score for this challenge. Higher numbers are better.
170 int score_;
171
172 // Whether this authentication request is for a proxy server, or an
173 // origin server.
174 HttpAuth::Target target_;
[email protected]3f918782009-02-28 01:29:24175
176 // A bitmask of the properties of the authentication scheme.
177 int properties_;
[email protected]ac5c06e2010-05-27 15:07:38178
179 BoundNetLog net_log_;
[email protected]7775c7d2010-06-21 17:50:21180
181 private:
182 void OnGenerateAuthTokenComplete(int rv);
183 void FinishGenerateAuthToken();
[email protected]cb2825c2010-07-01 10:23:38184 static std::string GenerateHistogramNameFromScheme(const std::string& scheme);
[email protected]7775c7d2010-06-21 17:50:21185
186 CompletionCallback* original_callback_;
187 CompletionCallbackImpl<HttpAuthHandler> wrapper_callback_;
[email protected]cb2825c2010-07-01 10:23:38188 // When GenerateAuthToken was called.
189 base::TimeTicks generate_auth_token_start_;
190 scoped_refptr<Histogram> histogram_;
[email protected]c3b35c22008-09-27 03:19:42191};
192
193} // namespace net
194
195#endif // NET_HTTP_HTTP_AUTH_HANDLER_H_