blob: f5ce1be024e3758a34b25801df973ad723361cb8 [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_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]c3b35c22008-09-27 03:19:428
9#include <string>
10
[email protected]13c8a092010-07-29 06:15:4411#include "base/string16.h"
[email protected]5097dc82010-07-15 17:23:2312#include "base/time.h"
[email protected]e5ae96a2010-04-14 20:12:4513#include "net/base/completion_callback.h"
[email protected]ac5c06e2010-05-27 15:07:3814#include "net/base/net_log.h"
[email protected]c3b35c22008-09-27 03:19:4215#include "net/http/http_auth.h"
16
[email protected]835d7c82010-10-14 04:38:3817namespace base {
[email protected]cb2825c2010-07-01 10:23:3818class Histogram;
[email protected]835d7c82010-10-14 04:38:3819}
[email protected]cb2825c2010-07-01 10:23:3820
[email protected]c3b35c22008-09-27 03:19:4221namespace net {
22
[email protected]8c76ae22010-04-20 22:15:4323struct HttpRequestInfo;
[email protected]c3b35c22008-09-27 03:19:4224
25// HttpAuthHandler is the interface for the authentication schemes
[email protected]fa55e192010-02-15 14:25:5026// (basic, digest, NTLM, Negotiate).
27// HttpAuthHandler objects are typically created by an HttpAuthHandlerFactory.
[email protected]36c8e5f72010-06-07 14:17:1428class HttpAuthHandler {
[email protected]c3b35c22008-09-27 03:19:4229 public:
[email protected]7775c7d2010-06-21 17:50:2130 HttpAuthHandler();
[email protected]cb2825c2010-07-01 10:23:3831 virtual ~HttpAuthHandler();
[email protected]36c8e5f72010-06-07 14:17:1432
[email protected]fa55e192010-02-15 14:25:5033 // Initializes the handler using a challenge issued by a server.
34 // |challenge| must be non-NULL and have already tokenized the
35 // authentication scheme, but none of the tokens occuring after the
36 // authentication scheme. |target| and |origin| are both stored
37 // for later use, and are not part of the initial challenge.
38 bool InitFromChallenge(HttpAuth::ChallengeTokenizer* challenge,
[email protected]4de702f42009-09-18 17:46:1039 HttpAuth::Target target,
[email protected]ac5c06e2010-05-27 15:07:3840 const GURL& origin,
41 const BoundNetLog& net_log);
[email protected]c3b35c22008-09-27 03:19:4242
[email protected]eca50e122010-09-11 14:03:3043 // Determines how the previous authorization attempt was received.
44 //
45 // This is called when the server/proxy responds with a 401/407 after an
46 // earlier authorization attempt. Although this normally means that the
47 // previous attempt was rejected, in multi-round schemes such as
48 // NTLM+Negotiate it may indicate that another round of challenge+response
49 // is required. For Digest authentication it may also mean that the previous
50 // attempt used a stale nonce (and nonce-count) and that a new attempt should
51 // be made with a different nonce provided in the challenge.
52 //
53 // |challenge| must be non-NULL and have already tokenized the
54 // authentication scheme, but none of the tokens occuring after the
55 // authentication scheme.
56 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge(
57 HttpAuth::ChallengeTokenizer* challenge) = 0;
58
[email protected]bcc528e2010-06-10 15:03:2459 // Generates an authentication token, potentially asynchronously.
60 //
61 // When |username| and |password| are NULL, the default credentials for
62 // the currently logged in user are used. |AllowsDefaultCredentials()| MUST be
63 // true in this case.
64 //
65 // |request|, |callback|, and |auth_token| must be non-NULL.
66 //
67 // The return value is a net error code.
68 // If |OK| is returned, |*auth_token| is filled in with an authentication
69 // token which can be inserted in the HTTP request.
70 // If |ERR_IO_PENDING| is returned, |*auth_token| will be filled in
71 // asynchronously and |callback| will be invoked. The lifetime of
72 // |request|, |callback|, and |auth_token| must last until |callback| is
73 // invoked, but |username| and |password| are only used during the initial
74 // call.
75 // Otherwise, there was a problem generating a token synchronously, and the
76 // value of |*auth_token| is unspecified.
[email protected]13c8a092010-07-29 06:15:4477 int GenerateAuthToken(const string16* username,
78 const string16* password,
[email protected]bcc528e2010-06-10 15:03:2479 const HttpRequestInfo* request,
80 CompletionCallback* callback,
81 std::string* auth_token);
82
[email protected]c3b35c22008-09-27 03:19:4283 // Lowercase name of the auth scheme
[email protected]e34c85d82008-12-02 06:59:0984 const std::string& scheme() const {
[email protected]c3b35c22008-09-27 03:19:4285 return scheme_;
86 }
87
88 // The realm value that was parsed during Init().
[email protected]e34c85d82008-12-02 06:59:0989 const std::string& realm() const {
[email protected]c3b35c22008-09-27 03:19:4290 return realm_;
91 }
92
[email protected]fa82f932010-05-20 11:09:2493 // The challenge which was issued when creating the handler.
94 const std::string challenge() const {
95 return auth_challenge_;
96 }
97
[email protected]c3b35c22008-09-27 03:19:4298 // Numeric rank based on the challenge's security level. Higher
99 // numbers are better. Used by HttpAuth::ChooseBestChallenge().
100 int score() const {
101 return score_;
102 }
103
104 HttpAuth::Target target() const {
105 return target_;
106 }
[email protected]3f918782009-02-28 01:29:24107
108 // Returns true if the authentication scheme does not send the username and
109 // password in the clear.
110 bool encrypts_identity() const {
111 return (properties_ & ENCRYPTS_IDENTITY) != 0;
112 }
113
114 // Returns true if the authentication scheme is connection-based, for
115 // example, NTLM. A connection-based authentication scheme does not support
116 // preemptive authentication, and must use the same handler object
117 // throughout the life of an HTTP transaction.
118 bool is_connection_based() const {
119 return (properties_ & IS_CONNECTION_BASED) != 0;
120 }
121
122 // Returns true if the response to the current authentication challenge
123 // requires an identity.
124 // TODO(wtc): Find a better way to handle a multi-round challenge-response
125 // sequence used by a connection-based authentication scheme.
[email protected]7cf40912010-12-09 18:25:03126 virtual bool NeedsIdentity();
[email protected]3f918782009-02-28 01:29:24127
[email protected]c573f48c2010-05-13 18:23:27128 // Returns whether the default credentials may be used for the |origin| passed
129 // into |InitFromChallenge|. If true, the user does not need to be prompted
130 // for username and password to establish credentials.
[email protected]d7f16632010-03-29 18:02:36131 // NOTE: SSO is a potential security risk.
132 // TODO(cbentzel): Add a pointer to Firefox documentation about risk.
[email protected]7cf40912010-12-09 18:25:03133 virtual bool AllowsDefaultCredentials();
[email protected]ac3fa8e22010-02-05 19:13:29134
[email protected]c3b35c22008-09-27 03:19:42135 protected:
[email protected]3f918782009-02-28 01:29:24136 enum Property {
137 ENCRYPTS_IDENTITY = 1 << 0,
138 IS_CONNECTION_BASED = 1 << 1,
139 };
140
[email protected]fa55e192010-02-15 14:25:50141 // Initializes the handler using a challenge issued by a server.
142 // |challenge| must be non-NULL and have already tokenized the
143 // authentication scheme, but none of the tokens occuring after the
144 // authentication scheme.
[email protected]3f918782009-02-28 01:29:24145 // Implementations are expcted to initialize the following members:
146 // scheme_, realm_, score_, properties_
[email protected]fa55e192010-02-15 14:25:50147 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) = 0;
[email protected]c3b35c22008-09-27 03:19:42148
[email protected]bcc528e2010-06-10 15:03:24149 // |GenerateAuthTokenImpl()} is the auth-scheme specific implementation
150 // of generating the next auth token. Callers sohuld use |GenerateAuthToken()|
151 // which will in turn call |GenerateAuthTokenImpl()|
[email protected]13c8a092010-07-29 06:15:44152 virtual int GenerateAuthTokenImpl(const string16* username,
153 const string16* password,
[email protected]bcc528e2010-06-10 15:03:24154 const HttpRequestInfo* request,
155 CompletionCallback* callback,
156 std::string* auth_token) = 0;
157
[email protected]e5ae96a2010-04-14 20:12:45158 // The lowercase auth-scheme {"basic", "digest", "ntlm", "negotiate"}
[email protected]e34c85d82008-12-02 06:59:09159 std::string scheme_;
[email protected]c3b35c22008-09-27 03:19:42160
[email protected]4de702f42009-09-18 17:46:10161 // The realm. Used by "basic" and "digest".
[email protected]c3b35c22008-09-27 03:19:42162 std::string realm_;
163
[email protected]fa82f932010-05-20 11:09:24164 // The auth challenge.
165 std::string auth_challenge_;
166
[email protected]4de702f42009-09-18 17:46:10167 // The {scheme, host, port} for the authentication target. Used by "ntlm"
[email protected]e5ae96a2010-04-14 20:12:45168 // and "negotiate" to construct the service principal name.
[email protected]4de702f42009-09-18 17:46:10169 GURL origin_;
170
[email protected]c3b35c22008-09-27 03:19:42171 // The score for this challenge. Higher numbers are better.
172 int score_;
173
174 // Whether this authentication request is for a proxy server, or an
175 // origin server.
176 HttpAuth::Target target_;
[email protected]3f918782009-02-28 01:29:24177
178 // A bitmask of the properties of the authentication scheme.
179 int properties_;
[email protected]ac5c06e2010-05-27 15:07:38180
181 BoundNetLog net_log_;
[email protected]7775c7d2010-06-21 17:50:21182
183 private:
184 void OnGenerateAuthTokenComplete(int rv);
185 void FinishGenerateAuthToken();
[email protected]cb2825c2010-07-01 10:23:38186 static std::string GenerateHistogramNameFromScheme(const std::string& scheme);
[email protected]7775c7d2010-06-21 17:50:21187
188 CompletionCallback* original_callback_;
189 CompletionCallbackImpl<HttpAuthHandler> wrapper_callback_;
[email protected]cb2825c2010-07-01 10:23:38190 // When GenerateAuthToken was called.
191 base::TimeTicks generate_auth_token_start_;
[email protected]835d7c82010-10-14 04:38:38192 scoped_refptr<base::Histogram> histogram_;
[email protected]c3b35c22008-09-27 03:19:42193};
194
195} // namespace net
196
197#endif // NET_HTTP_HTTP_AUTH_HANDLER_H_