[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, |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame^] | 29 | HttpAuth::Target target, |
| 30 | const GURL& origin); |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 31 | |
| 32 | // Lowercase name of the auth scheme |
[email protected] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 33 | const std::string& scheme() const { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 34 | return scheme_; |
| 35 | } |
| 36 | |
| 37 | // The realm value that was parsed during Init(). |
[email protected] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 38 | const std::string& realm() const { |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 39 | 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] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 51 | |
| 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] | ea9dc9a | 2009-09-05 00:43:32 | [diff] [blame] | 72 | // 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] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 77 | // 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] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 84 | 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] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 92 | 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] | e34c85d8 | 2008-12-02 06:59:09 | [diff] [blame] | 96 | std::string scheme_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 97 | |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame^] | 98 | // The realm. Used by "basic" and "digest". |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 99 | std::string realm_; |
| 100 | |
[email protected] | 4de702f4 | 2009-09-18 17:46:10 | [diff] [blame^] | 101 | // The {scheme, host, port} for the authentication target. Used by "ntlm" |
| 102 | // to construct the service principal name. |
| 103 | GURL origin_; |
| 104 | |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 105 | // 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] | 3f91878 | 2009-02-28 01:29:24 | [diff] [blame] | 111 | |
| 112 | // A bitmask of the properties of the authentication scheme. |
| 113 | int properties_; |
[email protected] | c3b35c2 | 2008-09-27 03:19:42 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | } // namespace net |
| 117 | |
| 118 | #endif // NET_HTTP_HTTP_AUTH_HANDLER_H_ |