| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef NET_HTTP_HTTP_AUTH_CACHE_H_ | 5 #ifndef NET_HTTP_HTTP_AUTH_CACHE_H_ |
| 6 #define NET_HTTP_HTTP_AUTH_CACHE_H_ | 6 #define NET_HTTP_HTTP_AUTH_CACHE_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 12 #include "googleurl/src/gurl.h" | 12 #include "googleurl/src/gurl.h" |
| 13 #include "net/http/http_auth_handler.h" | 13 #include "net/http/http_auth_handler.h" |
| 14 // This is needed for the FRIEND_TEST() macro. | 14 // This is needed for the FRIEND_TEST() macro. |
| 15 #include "testing/gtest/include/gtest/gtest_prod.h" | 15 #include "testing/gtest/include/gtest/gtest_prod.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 // TODO(eroman): Can we change the key from (origin, realm) to | |
| 20 // (origin, realm, auth_scheme)? | |
| 21 | |
| 22 // HttpAuthCache stores HTTP authentication identities and challenge info. | 19 // HttpAuthCache stores HTTP authentication identities and challenge info. |
| 23 // For each realm the cache stores a HttpAuthCache::Entry, which holds: | 20 // For each (origin, realm, scheme) triple the cache stores a |
| 24 // - the realm name | 21 // HttpAuthCache::Entry, which holds: |
| 25 // - the origin server {scheme, host, port} | 22 // - the origin server {protocol scheme, host, port} |
| 26 // - the last identity used (username/password) | 23 // - the last identity used (username/password) |
| 27 // - the last auth handler used | 24 // - the last auth handler used (contains realm and authentication scheme) |
| 28 // - the list of paths which used this realm | 25 // - the list of paths which used this realm |
| 29 // Entries can be looked up by either (origin, realm) or (origin, path). | 26 // Entries can be looked up by either (origin, realm, scheme) or (origin, path). |
| 30 class HttpAuthCache { | 27 class HttpAuthCache { |
| 31 public: | 28 public: |
| 32 class Entry; | 29 class Entry; |
| 33 | 30 |
| 34 // Find the realm entry on server |origin| for realm |realm|. | 31 // Find the realm entry on server |origin| for realm |realm| and |
| 32 // scheme |scheme|. |
| 35 // |origin| - the {scheme, host, port} of the server. | 33 // |origin| - the {scheme, host, port} of the server. |
| 36 // |realm| - case sensitive realm string. | 34 // |realm| - case sensitive realm string. |
| 35 // |scheme| - case sensitive authentication scheme, should be lower-case. |
| 37 // returns - the matched entry or NULL. | 36 // returns - the matched entry or NULL. |
| 38 Entry* LookupByRealm(const GURL& origin, const std::string& realm); | 37 Entry* Lookup(const GURL& origin, const std::string& realm, |
| 38 const std::string& scheme); |
| 39 | 39 |
| 40 // Find the realm entry on server |origin| whose protection space includes | 40 // Find the entry on server |origin| whose protection space includes |
| 41 // |path|. This uses the assumption in RFC 2617 section 2 that deeper | 41 // |path|. This uses the assumption in RFC 2617 section 2 that deeper |
| 42 // paths lie in the same protection space. | 42 // paths lie in the same protection space. |
| 43 // |origin| - the {scheme, host, port} of the server. | 43 // |origin| - the {scheme, host, port} of the server. |
| 44 // |path| - absolute path of the resource, or empty string in case of | 44 // |path| - absolute path of the resource, or empty string in case of |
| 45 // proxy auth (which does not use the concept of paths). | 45 // proxy auth (which does not use the concept of paths). |
| 46 // returns - the matched entry or NULL. | 46 // returns - the matched entry or NULL. |
| 47 Entry* LookupByPath(const GURL& origin, const std::string& path); | 47 Entry* LookupByPath(const GURL& origin, const std::string& path); |
| 48 | 48 |
| 49 // Add a realm entry on server |origin| for realm |handler->realm()|, If an | 49 // Add an entry on server |origin| for realm |handler->realm()| and |
| 50 // entry for this realm already exists, update it rather than replace it -- | 50 // scheme |handler->scheme()|. If an entry for this (realm,scheme) |
| 51 // this preserves the realm's paths list. | 51 // already exists, update it rather than replace it -- this preserves the |
| 52 // paths list. |
| 52 // |origin| - the {scheme, host, port} of the server. | 53 // |origin| - the {scheme, host, port} of the server. |
| 53 // |handler| - handler for the challenge. | 54 // |handler| - handler for the challenge. |
| 54 // |username| - login information for the realm. | 55 // |username| - login information for the realm. |
| 55 // |password| - login information for the realm. | 56 // |password| - login information for the realm. |
| 56 // |path| - absolute path for a resource contained in the protection | 57 // |path| - absolute path for a resource contained in the protection |
| 57 // space; this will be added to the list of known paths. | 58 // space; this will be added to the list of known paths. |
| 58 // returns - the entry that was just added/updated. | 59 // returns - the entry that was just added/updated. |
| 59 Entry* Add(const GURL& origin, | 60 Entry* Add(const GURL& origin, |
| 60 HttpAuthHandler* handler, | 61 HttpAuthHandler* handler, |
| 61 const std::wstring& username, | 62 const std::wstring& username, |
| 62 const std::wstring& password, | 63 const std::wstring& password, |
| 63 const std::string& path); | 64 const std::string& path); |
| 64 | 65 |
| 65 // Remove realm entry on server |origin| for realm |realm| if one exists | 66 // Remove entry on server |origin| for realm |realm| and scheme |scheme| |
| 66 // AND if the cached identity matches (|username|, |password|). | 67 // if one exists AND if the cached identity matches (|username|, |password|). |
| 67 // |origin| - the {scheme, host, port} of the server. | 68 // |origin| - the {scheme, host, port} of the server. |
| 68 // |realm| - case sensitive realm string. | 69 // |realm| - case sensitive realm string. |
| 70 // |scheme| - authentication scheme |
| 69 // |username| - condition to match. | 71 // |username| - condition to match. |
| 70 // |password| - condition to match. | 72 // |password| - condition to match. |
| 71 // returns - true if an entry was removed. | 73 // returns - true if an entry was removed. |
| 72 bool Remove(const GURL& origin, | 74 bool Remove(const GURL& origin, |
| 73 const std::string& realm, | 75 const std::string& realm, |
| 76 const std::string& scheme, |
| 74 const std::wstring& username, | 77 const std::wstring& username, |
| 75 const std::wstring& password); | 78 const std::wstring& password); |
| 76 | 79 |
| 77 // Prevent unbounded memory growth. These are safeguards for abuse; it is | 80 // Prevent unbounded memory growth. These are safeguards for abuse; it is |
| 78 // not expected that the limits will be reached in ordinary usage. | 81 // not expected that the limits will be reached in ordinary usage. |
| 79 // This also defines the worst-case lookup times (which grow linearly | 82 // This also defines the worst-case lookup times (which grow linearly |
| 80 // with number of elements in the cache). | 83 // with number of elements in the cache). |
| 81 enum { kMaxNumPathsPerRealmEntry = 10 }; | 84 enum { kMaxNumPathsPerRealmEntry = 10 }; |
| 82 enum { kMaxNumRealmEntries = 10 }; | 85 enum { kMaxNumRealmEntries = 10 }; |
| 83 | 86 |
| 84 private: | 87 private: |
| 85 typedef std::list<Entry> EntryList; | 88 typedef std::list<Entry> EntryList; |
| 86 EntryList entries_; | 89 EntryList entries_; |
| 87 }; | 90 }; |
| 88 | 91 |
| 89 // An authentication realm entry. | 92 // An authentication realm entry. |
| 90 class HttpAuthCache::Entry { | 93 class HttpAuthCache::Entry { |
| 91 public: | 94 public: |
| 92 const GURL& origin() const { | 95 const GURL& origin() const { |
| 93 return origin_; | 96 return origin_; |
| 94 } | 97 } |
| 95 | 98 |
| 96 // The case-sensitive realm string of the challenge. | 99 // The case-sensitive realm string of the challenge. |
| 97 const std::string realm() const { | 100 const std::string realm() const { |
| 98 return handler_->realm(); | 101 return handler_->realm(); |
| 99 } | 102 } |
| 100 | 103 |
| 104 // The authentication scheme string of the challenge |
| 105 const std::string scheme() const { |
| 106 return handler_->scheme(); |
| 107 } |
| 108 |
| 101 // The handler for the challenge. | 109 // The handler for the challenge. |
| 102 HttpAuthHandler* handler() const { | 110 HttpAuthHandler* handler() const { |
| 103 return handler_.get(); | 111 return handler_.get(); |
| 104 } | 112 } |
| 105 | 113 |
| 106 // The login username. | 114 // The login username. |
| 107 const std::wstring& username() const { | 115 const std::wstring& username() const { |
| 108 return username_; | 116 return username_; |
| 109 } | 117 } |
| 110 | 118 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 138 scoped_refptr<HttpAuthHandler> handler_; | 146 scoped_refptr<HttpAuthHandler> handler_; |
| 139 | 147 |
| 140 // List of paths that define the realm's protection space. | 148 // List of paths that define the realm's protection space. |
| 141 typedef std::list<std::string> PathList; | 149 typedef std::list<std::string> PathList; |
| 142 PathList paths_; | 150 PathList paths_; |
| 143 }; | 151 }; |
| 144 | 152 |
| 145 } // namespace net | 153 } // namespace net |
| 146 | 154 |
| 147 #endif // NET_HTTP_HTTP_AUTH_CACHE_H_ | 155 #endif // NET_HTTP_HTTP_AUTH_CACHE_H_ |
| OLD | NEW |