[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 1 | // 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] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 10 | #include "base/ref_counted.h" |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 11 | #include "net/http/http_auth.h" |
| 12 | |
| 13 | namespace net { |
| 14 | |
| 15 | class HttpRequestInfo; |
| 16 | class ProxyInfo; |
| 17 | |
| 18 | // HttpAuthHandler is the interface for the authentication schemes |
| 19 | // (basic, digest, ...) |
[email protected] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame^] | 20 | // The registry mapping auth-schemes to implementations is hardcoded in |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 21 | // HttpAuth::CreateAuthHandler(). |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 22 | class HttpAuthHandler : public base::RefCounted<HttpAuthHandler> { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 23 | public: |
[email protected] | f9ee6b5 | 2008-11-08 06:46:23 | [diff] [blame] | 24 | virtual ~HttpAuthHandler() { } |
| 25 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 26 | // 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] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 32 | const std::string& scheme() const { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 33 | return scheme_; |
| 34 | } |
| 35 | |
| 36 | // The realm value that was parsed during Init(). |
[email protected] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 37 | const std::string& realm() const { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 38 | 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] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame^] | 50 | |
| 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] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 71 | // 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] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame^] | 78 | 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] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 86 | 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] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 90 | std::string scheme_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 91 | |
| 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] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame^] | 101 | |
| 102 | // A bitmask of the properties of the authentication scheme. |
| 103 | int properties_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | } // namespace net |
| 107 | |
| 108 | #endif // NET_HTTP_HTTP_AUTH_HANDLER_H_ |