blob: 3b32e184bc5a6765f467adbb86135e2b1d84300e [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,
29 HttpAuth::Target target);
30
31 // Lowercase name of the auth scheme
[email protected]e34c85d82008-12-02 06:59:0932 const std::string& scheme() const {
[email protected]c3b35c22008-09-27 03:19:4233 return scheme_;
34 }
35
36 // The realm value that was parsed during Init().
[email protected]e34c85d82008-12-02 06:59:0937 const std::string& realm() const {
[email protected]c3b35c22008-09-27 03:19:4238 return realm_;
39 }
40
41 // Numeric rank based on the challenge's security level. Higher
42 // numbers are better. Used by HttpAuth::ChooseBestChallenge().
43 int score() const {
44 return score_;
45 }
46
47 HttpAuth::Target target() const {
48 return target_;
49 }
[email protected]3f918782009-02-28 01:29:2450
51 // Returns true if the authentication scheme does not send the username and
52 // password in the clear.
53 bool encrypts_identity() const {
54 return (properties_ & ENCRYPTS_IDENTITY) != 0;
55 }
56
57 // Returns true if the authentication scheme is connection-based, for
58 // example, NTLM. A connection-based authentication scheme does not support
59 // preemptive authentication, and must use the same handler object
60 // throughout the life of an HTTP transaction.
61 bool is_connection_based() const {
62 return (properties_ & IS_CONNECTION_BASED) != 0;
63 }
64
65 // Returns true if the response to the current authentication challenge
66 // requires an identity.
67 // TODO(wtc): Find a better way to handle a multi-round challenge-response
68 // sequence used by a connection-based authentication scheme.
69 virtual bool NeedsIdentity() { return true; }
70
[email protected]c3b35c22008-09-27 03:19:4271 // Generate the Authorization header value.
72 virtual std::string GenerateCredentials(const std::wstring& username,
73 const std::wstring& password,
74 const HttpRequestInfo* request,
75 const ProxyInfo* proxy) = 0;
76
77 protected:
[email protected]3f918782009-02-28 01:29:2478 enum Property {
79 ENCRYPTS_IDENTITY = 1 << 0,
80 IS_CONNECTION_BASED = 1 << 1,
81 };
82
83 // Initialize the handler by parsing a challenge string.
84 // Implementations are expcted to initialize the following members:
85 // scheme_, realm_, score_, properties_
[email protected]c3b35c22008-09-27 03:19:4286 virtual bool Init(std::string::const_iterator challenge_begin,
87 std::string::const_iterator challenge_end) = 0;
88
89 // The lowercase auth-scheme {"basic", "digest", "ntlm", ...}
[email protected]e34c85d82008-12-02 06:59:0990 std::string scheme_;
[email protected]c3b35c22008-09-27 03:19:4291
92 // The realm.
93 std::string realm_;
94
95 // The score for this challenge. Higher numbers are better.
96 int score_;
97
98 // Whether this authentication request is for a proxy server, or an
99 // origin server.
100 HttpAuth::Target target_;
[email protected]3f918782009-02-28 01:29:24101
102 // A bitmask of the properties of the authentication scheme.
103 int properties_;
[email protected]c3b35c22008-09-27 03:19:42104};
105
106} // namespace net
107
108#endif // NET_HTTP_HTTP_AUTH_HANDLER_H_