blob: dfb50d43a608ac9de9c32c7c4ff2516efc9c6844 [file] [log] [blame]
[email protected]9bd30952011-05-20 19:09:501// 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_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]172da1b2011-08-12 15:52:2611#include "net/base/net_export.h"
[email protected]ac5c06e2010-05-27 15:07:3812#include "net/base/net_log.h"
[email protected]c3b35c22008-09-27 03:19:4213#include "net/http/http_auth.h"
14
15namespace net {
16
[email protected]df41d0d82014-03-13 00:43:2417class HttpAuthChallengeTokenizer;
[email protected]8c76ae22010-04-20 22:15:4318struct HttpRequestInfo;
[email protected]c3b35c22008-09-27 03:19:4219
20// HttpAuthHandler is the interface for the authentication schemes
[email protected]fa55e192010-02-15 14:25:5021// (basic, digest, NTLM, Negotiate).
22// HttpAuthHandler objects are typically created by an HttpAuthHandlerFactory.
[email protected]172da1b2011-08-12 15:52:2623class NET_EXPORT_PRIVATE HttpAuthHandler {
[email protected]c3b35c22008-09-27 03:19:4224 public:
[email protected]7775c7d2010-06-21 17:50:2125 HttpAuthHandler();
[email protected]cb2825c2010-07-01 10:23:3826 virtual ~HttpAuthHandler();
[email protected]36c8e5f72010-06-07 14:17:1427
[email protected]fa55e192010-02-15 14:25:5028 // Initializes the handler using a challenge issued by a server.
29 // |challenge| must be non-NULL and have already tokenized the
[email protected]49639fa2011-12-20 23:22:4130 // authentication scheme, but none of the tokens occurring after the
[email protected]fa55e192010-02-15 14:25:5031 // authentication scheme. |target| and |origin| are both stored
32 // for later use, and are not part of the initial challenge.
[email protected]df41d0d82014-03-13 00:43:2433 bool InitFromChallenge(HttpAuthChallengeTokenizer* challenge,
[email protected]4de702f42009-09-18 17:46:1034 HttpAuth::Target target,
[email protected]ac5c06e2010-05-27 15:07:3835 const GURL& origin,
36 const BoundNetLog& net_log);
[email protected]c3b35c22008-09-27 03:19:4237
[email protected]eca50e122010-09-11 14:03:3038 // Determines how the previous authorization attempt was received.
39 //
40 // This is called when the server/proxy responds with a 401/407 after an
41 // earlier authorization attempt. Although this normally means that the
42 // previous attempt was rejected, in multi-round schemes such as
43 // NTLM+Negotiate it may indicate that another round of challenge+response
44 // is required. For Digest authentication it may also mean that the previous
45 // attempt used a stale nonce (and nonce-count) and that a new attempt should
46 // be made with a different nonce provided in the challenge.
47 //
48 // |challenge| must be non-NULL and have already tokenized the
[email protected]49639fa2011-12-20 23:22:4149 // authentication scheme, but none of the tokens occurring after the
[email protected]eca50e122010-09-11 14:03:3050 // authentication scheme.
51 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge(
[email protected]df41d0d82014-03-13 00:43:2452 HttpAuthChallengeTokenizer* challenge) = 0;
[email protected]eca50e122010-09-11 14:03:3053
[email protected]bcc528e2010-06-10 15:03:2454 // Generates an authentication token, potentially asynchronously.
55 //
[email protected]f3cf9802011-10-28 18:44:5856 // When |credentials| is NULL, the default credentials for the currently
57 // logged in user are used. |AllowsDefaultCredentials()| MUST be true in this
58 // case.
[email protected]bcc528e2010-06-10 15:03:2459 //
60 // |request|, |callback|, and |auth_token| must be non-NULL.
61 //
62 // The return value is a net error code.
[email protected]f3cf9802011-10-28 18:44:5863 //
[email protected]bcc528e2010-06-10 15:03:2464 // If |OK| is returned, |*auth_token| is filled in with an authentication
65 // token which can be inserted in the HTTP request.
[email protected]f3cf9802011-10-28 18:44:5866 //
[email protected]bcc528e2010-06-10 15:03:2467 // If |ERR_IO_PENDING| is returned, |*auth_token| will be filled in
68 // asynchronously and |callback| will be invoked. The lifetime of
69 // |request|, |callback|, and |auth_token| must last until |callback| is
[email protected]f3cf9802011-10-28 18:44:5870 // invoked, but |credentials| is only used during the initial call.
71 //
72 // All other return codes indicate that there was a problem generating a
73 // token, and the value of |*auth_token| is unspecified.
74 int GenerateAuthToken(const AuthCredentials* credentials,
[email protected]bcc528e2010-06-10 15:03:2475 const HttpRequestInfo* request,
[email protected]49639fa2011-12-20 23:22:4176 const CompletionCallback& callback,
[email protected]bcc528e2010-06-10 15:03:2477 std::string* auth_token);
78
[email protected]65f37772010-12-13 18:20:0679 // The authentication scheme as an enumerated value.
[email protected]547fc792011-01-13 13:31:1780 HttpAuth::Scheme auth_scheme() const {
[email protected]65f37772010-12-13 18:20:0681 return auth_scheme_;
82 }
83
[email protected]79cb5c12011-09-12 13:12:0484 // The realm, encoded as UTF-8. This may be empty.
[email protected]e34c85d82008-12-02 06:59:0985 const std::string& realm() const {
[email protected]c3b35c22008-09-27 03:19:4286 return realm_;
87 }
88
[email protected]fa82f932010-05-20 11:09:2489 // The challenge which was issued when creating the handler.
90 const std::string challenge() const {
91 return auth_challenge_;
92 }
93
[email protected]c3b35c22008-09-27 03:19:4294 // Numeric rank based on the challenge's security level. Higher
95 // numbers are better. Used by HttpAuth::ChooseBestChallenge().
96 int score() const {
97 return score_;
98 }
99
100 HttpAuth::Target target() const {
101 return target_;
102 }
[email protected]3f918782009-02-28 01:29:24103
[email protected]65f37772010-12-13 18:20:06104 // Returns the proxy or server which issued the authentication challenge
105 // that this HttpAuthHandler is handling. The URL includes scheme, host, and
106 // port, but does not include path.
107 const GURL& origin() const {
108 return origin_;
109 }
110
[email protected]3f918782009-02-28 01:29:24111 // Returns true if the authentication scheme does not send the username and
112 // password in the clear.
113 bool encrypts_identity() const {
114 return (properties_ & ENCRYPTS_IDENTITY) != 0;
115 }
116
117 // Returns true if the authentication scheme is connection-based, for
118 // example, NTLM. A connection-based authentication scheme does not support
119 // preemptive authentication, and must use the same handler object
120 // throughout the life of an HTTP transaction.
121 bool is_connection_based() const {
122 return (properties_ & IS_CONNECTION_BASED) != 0;
123 }
124
125 // Returns true if the response to the current authentication challenge
126 // requires an identity.
127 // TODO(wtc): Find a better way to handle a multi-round challenge-response
128 // sequence used by a connection-based authentication scheme.
[email protected]7cf40912010-12-09 18:25:03129 virtual bool NeedsIdentity();
[email protected]3f918782009-02-28 01:29:24130
[email protected]c573f48c2010-05-13 18:23:27131 // Returns whether the default credentials may be used for the |origin| passed
132 // into |InitFromChallenge|. If true, the user does not need to be prompted
133 // for username and password to establish credentials.
[email protected]d7f16632010-03-29 18:02:36134 // NOTE: SSO is a potential security risk.
135 // TODO(cbentzel): Add a pointer to Firefox documentation about risk.
[email protected]7cf40912010-12-09 18:25:03136 virtual bool AllowsDefaultCredentials();
[email protected]ac3fa8e22010-02-05 19:13:29137
[email protected]26d84b02011-08-31 14:07:08138 // Returns whether explicit credentials can be used with this handler. If
139 // true the user may be prompted for credentials if an implicit identity
140 // cannot be determined.
141 virtual bool AllowsExplicitCredentials();
142
[email protected]c3b35c22008-09-27 03:19:42143 protected:
[email protected]3f918782009-02-28 01:29:24144 enum Property {
145 ENCRYPTS_IDENTITY = 1 << 0,
146 IS_CONNECTION_BASED = 1 << 1,
147 };
148
[email protected]fa55e192010-02-15 14:25:50149 // Initializes the handler using a challenge issued by a server.
150 // |challenge| must be non-NULL and have already tokenized the
[email protected]49639fa2011-12-20 23:22:41151 // authentication scheme, but none of the tokens occurring after the
[email protected]fa55e192010-02-15 14:25:50152 // authentication scheme.
[email protected]49639fa2011-12-20 23:22:41153 // Implementations are expected to initialize the following members:
[email protected]3f918782009-02-28 01:29:24154 // scheme_, realm_, score_, properties_
[email protected]df41d0d82014-03-13 00:43:24155 virtual bool Init(HttpAuthChallengeTokenizer* challenge) = 0;
[email protected]c3b35c22008-09-27 03:19:42156
[email protected]bcc528e2010-06-10 15:03:24157 // |GenerateAuthTokenImpl()} is the auth-scheme specific implementation
[email protected]49639fa2011-12-20 23:22:41158 // of generating the next auth token. Callers should use |GenerateAuthToken()|
[email protected]bcc528e2010-06-10 15:03:24159 // which will in turn call |GenerateAuthTokenImpl()|
[email protected]f3cf9802011-10-28 18:44:58160 virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials,
[email protected]bcc528e2010-06-10 15:03:24161 const HttpRequestInfo* request,
[email protected]49639fa2011-12-20 23:22:41162 const CompletionCallback& callback,
[email protected]bcc528e2010-06-10 15:03:24163 std::string* auth_token) = 0;
164
[email protected]65f37772010-12-13 18:20:06165 // The auth-scheme as an enumerated value.
[email protected]547fc792011-01-13 13:31:17166 HttpAuth::Scheme auth_scheme_;
[email protected]c3b35c22008-09-27 03:19:42167
[email protected]79cb5c12011-09-12 13:12:04168 // The realm, encoded as UTF-8. Used by "basic" and "digest".
[email protected]c3b35c22008-09-27 03:19:42169 std::string realm_;
170
[email protected]fa82f932010-05-20 11:09:24171 // The auth challenge.
172 std::string auth_challenge_;
173
[email protected]4de702f42009-09-18 17:46:10174 // The {scheme, host, port} for the authentication target. Used by "ntlm"
[email protected]e5ae96a2010-04-14 20:12:45175 // and "negotiate" to construct the service principal name.
[email protected]4de702f42009-09-18 17:46:10176 GURL origin_;
177
[email protected]c3b35c22008-09-27 03:19:42178 // The score for this challenge. Higher numbers are better.
179 int score_;
180
181 // Whether this authentication request is for a proxy server, or an
182 // origin server.
183 HttpAuth::Target target_;
[email protected]3f918782009-02-28 01:29:24184
185 // A bitmask of the properties of the authentication scheme.
186 int properties_;
[email protected]ac5c06e2010-05-27 15:07:38187
188 BoundNetLog net_log_;
[email protected]7775c7d2010-06-21 17:50:21189
190 private:
191 void OnGenerateAuthTokenComplete(int rv);
192 void FinishGenerateAuthToken();
193
[email protected]49639fa2011-12-20 23:22:41194 CompletionCallback callback_;
[email protected]c3b35c22008-09-27 03:19:42195};
196
197} // namespace net
198
199#endif // NET_HTTP_HTTP_AUTH_HANDLER_H_