blob: 40ebe8d28b529db17f0d0f62988bc35af45666b6 [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]f9ee6b52008-11-08 06:46:2310#include "base/ref_counted.h"
[email protected]e5ae96a2010-04-14 20:12:4511#include "net/base/completion_callback.h"
[email protected]c3b35c22008-09-27 03:19:4212#include "net/http/http_auth.h"
13
14namespace net {
15
[email protected]e5ae96a2010-04-14 20:12:4516class BoundNetLog;
17class HostResolver;
[email protected]c3b35c22008-09-27 03:19:4218class ProxyInfo;
[email protected]8c76ae22010-04-20 22:15:4319struct HttpRequestInfo;
[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]f9ee6b52008-11-08 06:46:2324class HttpAuthHandler : public base::RefCounted<HttpAuthHandler> {
[email protected]c3b35c22008-09-27 03:19:4225 public:
[email protected]fa55e192010-02-15 14:25:5026 // Initializes the handler using a challenge issued by a server.
27 // |challenge| must be non-NULL and have already tokenized the
28 // authentication scheme, but none of the tokens occuring after the
29 // authentication scheme. |target| and |origin| are both stored
30 // for later use, and are not part of the initial challenge.
31 bool InitFromChallenge(HttpAuth::ChallengeTokenizer* challenge,
[email protected]4de702f42009-09-18 17:46:1032 HttpAuth::Target target,
33 const GURL& origin);
[email protected]c3b35c22008-09-27 03:19:4234
35 // Lowercase name of the auth scheme
[email protected]e34c85d82008-12-02 06:59:0936 const std::string& scheme() const {
[email protected]c3b35c22008-09-27 03:19:4237 return scheme_;
38 }
39
40 // The realm value that was parsed during Init().
[email protected]e34c85d82008-12-02 06:59:0941 const std::string& realm() const {
[email protected]c3b35c22008-09-27 03:19:4242 return realm_;
43 }
44
45 // Numeric rank based on the challenge's security level. Higher
46 // numbers are better. Used by HttpAuth::ChooseBestChallenge().
47 int score() const {
48 return score_;
49 }
50
51 HttpAuth::Target target() const {
52 return target_;
53 }
[email protected]3f918782009-02-28 01:29:2454
55 // Returns true if the authentication scheme does not send the username and
56 // password in the clear.
57 bool encrypts_identity() const {
58 return (properties_ & ENCRYPTS_IDENTITY) != 0;
59 }
60
61 // Returns true if the authentication scheme is connection-based, for
62 // example, NTLM. A connection-based authentication scheme does not support
63 // preemptive authentication, and must use the same handler object
64 // throughout the life of an HTTP transaction.
65 bool is_connection_based() const {
66 return (properties_ & IS_CONNECTION_BASED) != 0;
67 }
68
69 // Returns true if the response to the current authentication challenge
70 // requires an identity.
71 // TODO(wtc): Find a better way to handle a multi-round challenge-response
72 // sequence used by a connection-based authentication scheme.
73 virtual bool NeedsIdentity() { return true; }
74
[email protected]ea9dc9a2009-09-05 00:43:3275 // Returns true if this is the final round of the authentication sequence.
76 // For Basic and Digest, the method always returns true because they are
77 // single-round schemes.
78 virtual bool IsFinalRound() { return true; }
79
[email protected]b4955e7d2010-04-16 20:22:3080 // Returns whether the default credentials may be used for the |origin| passed
81 // into |InitFromChallenge|. If true, the user does not need to be prompted
82 // for username and password to establish credentials.
[email protected]d7f16632010-03-29 18:02:3683 // NOTE: SSO is a potential security risk.
84 // TODO(cbentzel): Add a pointer to Firefox documentation about risk.
[email protected]b4955e7d2010-04-16 20:22:3085 virtual bool AllowsDefaultCredentials() { return false; }
[email protected]ac3fa8e22010-02-05 19:13:2986
[email protected]e5ae96a2010-04-14 20:12:4587 // Returns whether the canonical DNS name for the origin host needs to be
88 // resolved. The Negotiate auth scheme typically uses the canonical DNS
89 // name when constructing the Kerberos SPN.
90 virtual bool NeedsCanonicalName() { return false; }
91
[email protected]ac3fa8e22010-02-05 19:13:2992 // TODO(cbentzel): Separate providing credentials from generating the
93 // authentication token in the API.
94
95 // Generates an authentication token.
96 // The return value is an error code. If the code is not |OK|, the value of
97 // |*auth_token| is unspecified.
98 // |auth_token| is a return value and must be non-NULL.
99 virtual int GenerateAuthToken(const std::wstring& username,
100 const std::wstring& password,
101 const HttpRequestInfo* request,
102 const ProxyInfo* proxy,
103 std::string* auth_token) = 0;
104
105 // Generates an authentication token using default credentials.
106 // The return value is an error code. If the code is not |OK|, the value of
107 // |*auth_token| is unspecified.
108 // |auth_token| is a return value and must be non-NULL.
[email protected]d7f16632010-03-29 18:02:36109 // This should only be called if |SupportsDefaultCredentials| returns true.
[email protected]ac3fa8e22010-02-05 19:13:29110 virtual int GenerateDefaultAuthToken(const HttpRequestInfo* request,
111 const ProxyInfo* proxy,
112 std::string* auth_token) = 0;
[email protected]c3b35c22008-09-27 03:19:42113
[email protected]e5ae96a2010-04-14 20:12:45114 // Resolves the canonical name for the |origin_| host. The canonical
115 // name is used by the Negotiate scheme to generate a valid Kerberos
116 // SPN.
117 // The return value is a net error code.
118 virtual int ResolveCanonicalName(HostResolver* host_resolver,
119 CompletionCallback* callback,
120 const BoundNetLog& net_log);
121
[email protected]c3b35c22008-09-27 03:19:42122 protected:
[email protected]3f918782009-02-28 01:29:24123 enum Property {
124 ENCRYPTS_IDENTITY = 1 << 0,
125 IS_CONNECTION_BASED = 1 << 1,
126 };
127
[email protected]5389bc72009-11-05 23:34:24128 friend class base::RefCounted<HttpAuthHandler>;
129
130 virtual ~HttpAuthHandler() { }
131
[email protected]fa55e192010-02-15 14:25:50132 // Initializes the handler using a challenge issued by a server.
133 // |challenge| must be non-NULL and have already tokenized the
134 // authentication scheme, but none of the tokens occuring after the
135 // authentication scheme.
[email protected]3f918782009-02-28 01:29:24136 // Implementations are expcted to initialize the following members:
137 // scheme_, realm_, score_, properties_
[email protected]fa55e192010-02-15 14:25:50138 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) = 0;
[email protected]c3b35c22008-09-27 03:19:42139
[email protected]e5ae96a2010-04-14 20:12:45140 // The lowercase auth-scheme {"basic", "digest", "ntlm", "negotiate"}
[email protected]e34c85d82008-12-02 06:59:09141 std::string scheme_;
[email protected]c3b35c22008-09-27 03:19:42142
[email protected]4de702f42009-09-18 17:46:10143 // The realm. Used by "basic" and "digest".
[email protected]c3b35c22008-09-27 03:19:42144 std::string realm_;
145
[email protected]4de702f42009-09-18 17:46:10146 // The {scheme, host, port} for the authentication target. Used by "ntlm"
[email protected]e5ae96a2010-04-14 20:12:45147 // and "negotiate" to construct the service principal name.
[email protected]4de702f42009-09-18 17:46:10148 GURL origin_;
149
[email protected]c3b35c22008-09-27 03:19:42150 // The score for this challenge. Higher numbers are better.
151 int score_;
152
153 // Whether this authentication request is for a proxy server, or an
154 // origin server.
155 HttpAuth::Target target_;
[email protected]3f918782009-02-28 01:29:24156
157 // A bitmask of the properties of the authentication scheme.
158 int properties_;
[email protected]c3b35c22008-09-27 03:19:42159};
160
161} // namespace net
162
163#endif // NET_HTTP_HTTP_AUTH_HANDLER_H_