blob: 8d521b428b248a2dd18ed21fdd39b7d79f08a4d4 [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]c3b35c22008-09-27 03:19:4212#include "net/http/http_auth.h"
mikecironef22f9812016-10-04 03:40:1913#include "net/log/net_log_with_source.h"
[email protected]c3b35c22008-09-27 03:19:4214
15namespace net {
16
[email protected]df41d0d82014-03-13 00:43:2417class HttpAuthChallengeTokenizer;
[email protected]8c76ae22010-04-20 22:15:4318struct HttpRequestInfo;
asanka5ffd5d72016-03-23 16:20:4919class SSLInfo;
[email protected]c3b35c22008-09-27 03:19:4220
21// HttpAuthHandler is the interface for the authentication schemes
[email protected]fa55e192010-02-15 14:25:5022// (basic, digest, NTLM, Negotiate).
23// HttpAuthHandler objects are typically created by an HttpAuthHandlerFactory.
[email protected]172da1b2011-08-12 15:52:2624class NET_EXPORT_PRIVATE HttpAuthHandler {
[email protected]c3b35c22008-09-27 03:19:4225 public:
[email protected]7775c7d2010-06-21 17:50:2126 HttpAuthHandler();
[email protected]cb2825c2010-07-01 10:23:3827 virtual ~HttpAuthHandler();
[email protected]36c8e5f72010-06-07 14:17:1428
[email protected]fa55e192010-02-15 14:25:5029 // Initializes the handler using a challenge issued by a server.
30 // |challenge| must be non-NULL and have already tokenized the
[email protected]49639fa2011-12-20 23:22:4131 // authentication scheme, but none of the tokens occurring after the
[email protected]fa55e192010-02-15 14:25:5032 // authentication scheme. |target| and |origin| are both stored
33 // for later use, and are not part of the initial challenge.
[email protected]df41d0d82014-03-13 00:43:2434 bool InitFromChallenge(HttpAuthChallengeTokenizer* challenge,
[email protected]4de702f42009-09-18 17:46:1035 HttpAuth::Target target,
asanka5ffd5d72016-03-23 16:20:4936 const SSLInfo& ssl_info,
[email protected]ac5c06e2010-05-27 15:07:3837 const GURL& origin,
tfarina42834112016-09-22 13:38:2038 const NetLogWithSource& net_log);
[email protected]c3b35c22008-09-27 03:19:4239
[email protected]eca50e122010-09-11 14:03:3040 // Determines how the previous authorization attempt was received.
41 //
42 // This is called when the server/proxy responds with a 401/407 after an
43 // earlier authorization attempt. Although this normally means that the
44 // previous attempt was rejected, in multi-round schemes such as
45 // NTLM+Negotiate it may indicate that another round of challenge+response
46 // is required. For Digest authentication it may also mean that the previous
47 // attempt used a stale nonce (and nonce-count) and that a new attempt should
48 // be made with a different nonce provided in the challenge.
49 //
50 // |challenge| must be non-NULL and have already tokenized the
[email protected]49639fa2011-12-20 23:22:4151 // authentication scheme, but none of the tokens occurring after the
[email protected]eca50e122010-09-11 14:03:3052 // authentication scheme.
53 virtual HttpAuth::AuthorizationResult HandleAnotherChallenge(
[email protected]df41d0d82014-03-13 00:43:2454 HttpAuthChallengeTokenizer* challenge) = 0;
[email protected]eca50e122010-09-11 14:03:3055
[email protected]bcc528e2010-06-10 15:03:2456 // Generates an authentication token, potentially asynchronously.
57 //
[email protected]f3cf9802011-10-28 18:44:5858 // When |credentials| is NULL, the default credentials for the currently
59 // logged in user are used. |AllowsDefaultCredentials()| MUST be true in this
60 // case.
[email protected]bcc528e2010-06-10 15:03:2461 //
62 // |request|, |callback|, and |auth_token| must be non-NULL.
63 //
64 // The return value is a net error code.
[email protected]f3cf9802011-10-28 18:44:5865 //
[email protected]bcc528e2010-06-10 15:03:2466 // If |OK| is returned, |*auth_token| is filled in with an authentication
67 // token which can be inserted in the HTTP request.
[email protected]f3cf9802011-10-28 18:44:5868 //
[email protected]bcc528e2010-06-10 15:03:2469 // If |ERR_IO_PENDING| is returned, |*auth_token| will be filled in
70 // asynchronously and |callback| will be invoked. The lifetime of
71 // |request|, |callback|, and |auth_token| must last until |callback| is
[email protected]f3cf9802011-10-28 18:44:5872 // invoked, but |credentials| is only used during the initial call.
73 //
74 // All other return codes indicate that there was a problem generating a
75 // token, and the value of |*auth_token| is unspecified.
76 int GenerateAuthToken(const AuthCredentials* credentials,
[email protected]bcc528e2010-06-10 15:03:2477 const HttpRequestInfo* request,
[email protected]49639fa2011-12-20 23:22:4178 const CompletionCallback& callback,
[email protected]bcc528e2010-06-10 15:03:2479 std::string* auth_token);
80
[email protected]65f37772010-12-13 18:20:0681 // The authentication scheme as an enumerated value.
[email protected]547fc792011-01-13 13:31:1782 HttpAuth::Scheme auth_scheme() const {
[email protected]65f37772010-12-13 18:20:0683 return auth_scheme_;
84 }
85
[email protected]79cb5c12011-09-12 13:12:0486 // The realm, encoded as UTF-8. This may be empty.
[email protected]e34c85d82008-12-02 06:59:0987 const std::string& realm() const {
[email protected]c3b35c22008-09-27 03:19:4288 return realm_;
89 }
90
[email protected]fa82f932010-05-20 11:09:2491 // The challenge which was issued when creating the handler.
payal.pandey8a0c2c452015-04-28 06:36:0192 const std::string& challenge() const { return auth_challenge_; }
[email protected]fa82f932010-05-20 11:09:2493
[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.
asanka5ffd5d72016-03-23 16:20:49153 //
154 // If the request was sent over an encrypted connection, |ssl_info| is valid
155 // and describes the connection.
156 //
[email protected]49639fa2011-12-20 23:22:41157 // Implementations are expected to initialize the following members:
[email protected]3f918782009-02-28 01:29:24158 // scheme_, realm_, score_, properties_
asanka5ffd5d72016-03-23 16:20:49159 virtual bool Init(HttpAuthChallengeTokenizer* challenge,
160 const SSLInfo& ssl_info) = 0;
[email protected]c3b35c22008-09-27 03:19:42161
[email protected]bcc528e2010-06-10 15:03:24162 // |GenerateAuthTokenImpl()} is the auth-scheme specific implementation
[email protected]49639fa2011-12-20 23:22:41163 // of generating the next auth token. Callers should use |GenerateAuthToken()|
[email protected]bcc528e2010-06-10 15:03:24164 // which will in turn call |GenerateAuthTokenImpl()|
[email protected]f3cf9802011-10-28 18:44:58165 virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials,
[email protected]bcc528e2010-06-10 15:03:24166 const HttpRequestInfo* request,
[email protected]49639fa2011-12-20 23:22:41167 const CompletionCallback& callback,
[email protected]bcc528e2010-06-10 15:03:24168 std::string* auth_token) = 0;
169
[email protected]65f37772010-12-13 18:20:06170 // The auth-scheme as an enumerated value.
[email protected]547fc792011-01-13 13:31:17171 HttpAuth::Scheme auth_scheme_;
[email protected]c3b35c22008-09-27 03:19:42172
[email protected]79cb5c12011-09-12 13:12:04173 // The realm, encoded as UTF-8. Used by "basic" and "digest".
[email protected]c3b35c22008-09-27 03:19:42174 std::string realm_;
175
[email protected]fa82f932010-05-20 11:09:24176 // The auth challenge.
177 std::string auth_challenge_;
178
[email protected]4de702f42009-09-18 17:46:10179 // The {scheme, host, port} for the authentication target. Used by "ntlm"
[email protected]e5ae96a2010-04-14 20:12:45180 // and "negotiate" to construct the service principal name.
[email protected]4de702f42009-09-18 17:46:10181 GURL origin_;
182
[email protected]c3b35c22008-09-27 03:19:42183 // The score for this challenge. Higher numbers are better.
184 int score_;
185
186 // Whether this authentication request is for a proxy server, or an
187 // origin server.
188 HttpAuth::Target target_;
[email protected]3f918782009-02-28 01:29:24189
190 // A bitmask of the properties of the authentication scheme.
191 int properties_;
[email protected]ac5c06e2010-05-27 15:07:38192
tfarina42834112016-09-22 13:38:20193 NetLogWithSource net_log_;
[email protected]7775c7d2010-06-21 17:50:21194
195 private:
196 void OnGenerateAuthTokenComplete(int rv);
197 void FinishGenerateAuthToken();
198
[email protected]49639fa2011-12-20 23:22:41199 CompletionCallback callback_;
[email protected]c3b35c22008-09-27 03:19:42200};
201
202} // namespace net
203
204#endif // NET_HTTP_HTTP_AUTH_HANDLER_H_