blob: 1caf1a4c90ae163d17d8ecdafdd8da0484d5571d [file] [log] [blame]
[email protected]c3b35c22008-09-27 03:19:421// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// 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]c3b35c22008-09-27 03:19:4211#include "net/http/http_auth.h"
12
13namespace net {
14
15class HttpRequestInfo;
16class ProxyInfo;
17
18// HttpAuthHandler is the interface for the authentication schemes
19// (basic, digest, ...)
[email protected]3f918782009-02-28 01:29:2420// The registry mapping auth-schemes to implementations is hardcoded in
[email protected]c3b35c22008-09-27 03:19:4221// HttpAuth::CreateAuthHandler().
[email protected]f9ee6b52008-11-08 06:46:2322class HttpAuthHandler : public base::RefCounted<HttpAuthHandler> {
[email protected]c3b35c22008-09-27 03:19:4223 public:
[email protected]f9ee6b52008-11-08 06:46:2324 virtual ~HttpAuthHandler() { }
25
[email protected]c3b35c22008-09-27 03:19:4226 // Initialize the handler by parsing a challenge string.
27 bool InitFromChallenge(std::string::const_iterator begin,
28 std::string::const_iterator end,
[email protected]4de702f42009-09-18 17:46:1029 HttpAuth::Target target,
30 const GURL& origin);
[email protected]c3b35c22008-09-27 03:19:4231
32 // Lowercase name of the auth scheme
[email protected]e34c85d82008-12-02 06:59:0933 const std::string& scheme() const {
[email protected]c3b35c22008-09-27 03:19:4234 return scheme_;
35 }
36
37 // The realm value that was parsed during Init().
[email protected]e34c85d82008-12-02 06:59:0938 const std::string& realm() const {
[email protected]c3b35c22008-09-27 03:19:4239 return realm_;
40 }
41
42 // Numeric rank based on the challenge's security level. Higher
43 // numbers are better. Used by HttpAuth::ChooseBestChallenge().
44 int score() const {
45 return score_;
46 }
47
48 HttpAuth::Target target() const {
49 return target_;
50 }
[email protected]3f918782009-02-28 01:29:2451
52 // Returns true if the authentication scheme does not send the username and
53 // password in the clear.
54 bool encrypts_identity() const {
55 return (properties_ & ENCRYPTS_IDENTITY) != 0;
56 }
57
58 // Returns true if the authentication scheme is connection-based, for
59 // example, NTLM. A connection-based authentication scheme does not support
60 // preemptive authentication, and must use the same handler object
61 // throughout the life of an HTTP transaction.
62 bool is_connection_based() const {
63 return (properties_ & IS_CONNECTION_BASED) != 0;
64 }
65
66 // Returns true if the response to the current authentication challenge
67 // requires an identity.
68 // TODO(wtc): Find a better way to handle a multi-round challenge-response
69 // sequence used by a connection-based authentication scheme.
70 virtual bool NeedsIdentity() { return true; }
71
[email protected]ea9dc9a2009-09-05 00:43:3272 // Returns true if this is the final round of the authentication sequence.
73 // For Basic and Digest, the method always returns true because they are
74 // single-round schemes.
75 virtual bool IsFinalRound() { return true; }
76
[email protected]c3b35c22008-09-27 03:19:4277 // Generate the Authorization header value.
78 virtual std::string GenerateCredentials(const std::wstring& username,
79 const std::wstring& password,
80 const HttpRequestInfo* request,
81 const ProxyInfo* proxy) = 0;
82
83 protected:
[email protected]3f918782009-02-28 01:29:2484 enum Property {
85 ENCRYPTS_IDENTITY = 1 << 0,
86 IS_CONNECTION_BASED = 1 << 1,
87 };
88
89 // Initialize the handler by parsing a challenge string.
90 // Implementations are expcted to initialize the following members:
91 // scheme_, realm_, score_, properties_
[email protected]c3b35c22008-09-27 03:19:4292 virtual bool Init(std::string::const_iterator challenge_begin,
93 std::string::const_iterator challenge_end) = 0;
94
95 // The lowercase auth-scheme {"basic", "digest", "ntlm", ...}
[email protected]e34c85d82008-12-02 06:59:0996 std::string scheme_;
[email protected]c3b35c22008-09-27 03:19:4297
[email protected]4de702f42009-09-18 17:46:1098 // The realm. Used by "basic" and "digest".
[email protected]c3b35c22008-09-27 03:19:4299 std::string realm_;
100
[email protected]4de702f42009-09-18 17:46:10101 // The {scheme, host, port} for the authentication target. Used by "ntlm"
102 // to construct the service principal name.
103 GURL origin_;
104
[email protected]c3b35c22008-09-27 03:19:42105 // The score for this challenge. Higher numbers are better.
106 int score_;
107
108 // Whether this authentication request is for a proxy server, or an
109 // origin server.
110 HttpAuth::Target target_;
[email protected]3f918782009-02-28 01:29:24111
112 // A bitmask of the properties of the authentication scheme.
113 int properties_;
[email protected]c3b35c22008-09-27 03:19:42114};
115
116} // namespace net
117
118#endif // NET_HTTP_HTTP_AUTH_HANDLER_H_