tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nsHttpAuthCache.h (7981B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef nsHttpAuthCache_h__
      7 #define nsHttpAuthCache_h__
      8 
      9 #include "nsError.h"
     10 #include "nsTArray.h"
     11 #include "nsClassHashtable.h"
     12 #include "nsCOMPtr.h"
     13 #include "nsHashKeys.h"
     14 #include "nsStringFwd.h"
     15 #include "nsIHttpAuthCache.h"
     16 #include "nsIObserver.h"
     17 
     18 namespace mozilla {
     19 
     20 class OriginAttributesPattern;
     21 
     22 namespace net {
     23 
     24 //-----------------------------------------------------------------------------
     25 // nsHttpAuthIdentity
     26 //-----------------------------------------------------------------------------
     27 
     28 class nsHttpAuthIdentity {
     29 public:
     30  nsHttpAuthIdentity() = default;
     31  nsHttpAuthIdentity(const nsAString& domain, const nsAString& user,
     32                     const nsAString& password)
     33      : mUser(user), mPass(password), mDomain(domain) {}
     34  ~nsHttpAuthIdentity() { Clear(); }
     35 
     36  const nsString& Domain() const { return mDomain; }
     37  const nsString& User() const { return mUser; }
     38  const nsString& Password() const { return mPass; }
     39 
     40  void Clear();
     41 
     42  bool Equals(const nsHttpAuthIdentity& ident) const;
     43  bool IsEmpty() const {
     44    return mUser.IsEmpty() && mPass.IsEmpty() && mDomain.IsEmpty();
     45  }
     46 
     47 private:
     48  nsString mUser;
     49  nsString mPass;
     50  nsString mDomain;
     51 };
     52 
     53 // This is an XPCOM wrapper for nsHttpAuthIdentity
     54 class AuthIdentity final : public nsIHttpAuthIdentity {
     55 public:
     56  NS_DECL_ISUPPORTS
     57  NS_DECL_NSIHTTPAUTHIDENTITY
     58 
     59  explicit AuthIdentity(const nsHttpAuthIdentity& aIdent) : mIdent(aIdent) {}
     60 
     61 private:
     62  virtual ~AuthIdentity() = default;
     63  nsHttpAuthIdentity mIdent;
     64 };
     65 
     66 //-----------------------------------------------------------------------------
     67 // nsHttpAuthEntry
     68 //-----------------------------------------------------------------------------
     69 
     70 class nsHttpAuthEntry : public nsIHttpAuthEntry {
     71 public:
     72  NS_DECL_ISUPPORTS
     73  NS_DECL_NSIHTTPAUTHENTRY
     74 
     75  nsHttpAuthEntry(const nsACString& path, const nsACString& realm,
     76                  const nsACString& creds, const nsACString& challenge,
     77                  const nsHttpAuthIdentity* ident, nsISupports* metadata) {
     78    DebugOnly<nsresult> rv =
     79        Set(path, realm, creds, challenge, ident, metadata);
     80    MOZ_ASSERT(NS_SUCCEEDED(rv));
     81  }
     82 
     83  const nsCString& Realm() const { return mRealm; }
     84  const nsCString& Creds() const { return mCreds; }
     85  const nsCString& Challenge() const { return mChallenge; }
     86  const nsString& Domain() const { return mIdent.Domain(); }
     87  const nsString& User() const { return mIdent.User(); }
     88  const nsString& Pass() const { return mIdent.Password(); }
     89 
     90  const nsHttpAuthIdentity& Identity() const { return mIdent; }
     91 
     92  [[nodiscard]] nsresult AddPath(const nsACString& aPath);
     93 
     94  nsCOMPtr<nsISupports> mMetaData;
     95 
     96 private:
     97  virtual ~nsHttpAuthEntry() = default;
     98 
     99  [[nodiscard]] nsresult Set(const nsACString& path, const nsACString& realm,
    100                             const nsACString& creds,
    101                             const nsACString& challenge,
    102                             const nsHttpAuthIdentity* ident,
    103                             nsISupports* metadata);
    104 
    105  nsHttpAuthIdentity mIdent;
    106 
    107  nsTArray<nsCString> mPaths;
    108 
    109  nsCString mRealm;
    110  nsCString mCreds;
    111  nsCString mChallenge;
    112 
    113  friend class nsHttpAuthNode;
    114  friend class nsHttpAuthCache;
    115  friend mozilla::DefaultDelete<nsHttpAuthEntry>;  // needs to call the
    116                                                   // destructor
    117 };
    118 
    119 //-----------------------------------------------------------------------------
    120 // nsHttpAuthNode
    121 //-----------------------------------------------------------------------------
    122 
    123 class nsHttpAuthNode {
    124 private:
    125  using EntryList = nsTArray<RefPtr<nsHttpAuthEntry>>;
    126 
    127  nsHttpAuthNode();
    128  ~nsHttpAuthNode();
    129 
    130  // path can be null, in which case we'll search for an entry
    131  // with a null path.
    132  nsHttpAuthEntry* LookupEntryByPath(const nsACString& path);
    133 
    134  // realm must not be null
    135  nsHttpAuthEntry* LookupEntryByRealm(const nsACString& realm);
    136  EntryList::const_iterator LookupEntryItrByRealm(
    137      const nsACString& realm) const;
    138 
    139  // if a matching entry is found, then credentials will be changed.
    140  [[nodiscard]] nsresult SetAuthEntry(const nsACString& path,
    141                                      const nsACString& realm,
    142                                      const nsACString& creds,
    143                                      const nsACString& challenge,
    144                                      const nsHttpAuthIdentity* ident,
    145                                      nsISupports* metadata);
    146 
    147  void ClearAuthEntry(const nsACString& realm);
    148 
    149  uint32_t EntryCount() { return mList.Length(); }
    150 
    151 private:
    152  EntryList mList;
    153 
    154  friend class nsHttpAuthCache;
    155  friend mozilla::DefaultDelete<nsHttpAuthNode>;  // needs to call the
    156                                                  // destructor
    157 };
    158 
    159 //-----------------------------------------------------------------------------
    160 // nsHttpAuthCache
    161 //  (holds a hash table from host:port to nsHttpAuthNode)
    162 //-----------------------------------------------------------------------------
    163 
    164 class nsHttpAuthCache : public nsIHttpAuthCache, public nsIObserver {
    165 public:
    166  NS_DECL_ISUPPORTS
    167  NS_DECL_NSIHTTPAUTHCACHE
    168  NS_DECL_NSIOBSERVER
    169 
    170  nsHttpAuthCache();
    171 
    172  // |scheme|, |host|, and |port| are required
    173  // |path| can be null
    174  // |entry| is either null or a weak reference
    175  [[nodiscard]] nsresult GetAuthEntryForPath(const nsACString& scheme,
    176                                             const nsACString& host,
    177                                             int32_t port,
    178                                             const nsACString& path,
    179                                             nsACString const& originSuffix,
    180                                             RefPtr<nsHttpAuthEntry>& entry);
    181 
    182  // |scheme|, |host|, and |port| are required
    183  // |realm| must not be null
    184  // |entry| is either null or a weak reference
    185  [[nodiscard]] nsresult GetAuthEntryForDomain(const nsACString& scheme,
    186                                               const nsACString& host,
    187                                               int32_t port,
    188                                               const nsACString& realm,
    189                                               nsACString const& originSuffix,
    190                                               RefPtr<nsHttpAuthEntry>& entry);
    191 
    192  // |scheme|, |host|, and |port| are required
    193  // |path| can be null
    194  // |realm| must not be null
    195  // if |credentials|, |user|, |pass|, and |challenge| are each
    196  // null, then the entry is deleted.
    197  [[nodiscard]] nsresult SetAuthEntry(
    198      const nsACString& scheme, const nsACString& host, int32_t port,
    199      const nsACString& path, const nsACString& realm, const nsACString& creds,
    200      const nsACString& challenge, nsACString const& originSuffix,
    201      const nsHttpAuthIdentity* ident, nsISupports* metadata);
    202 
    203  void ClearAuthEntry(const nsACString& scheme, const nsACString& host,
    204                      int32_t port, const nsACString& realm,
    205                      nsACString const& originSuffix);
    206 
    207  // expire all existing auth list entries including proxy auths.
    208  void ClearAll();
    209 
    210  // For testing only.
    211  void CollectKeys(nsTArray<nsCString>& aValue);
    212 
    213 private:
    214  nsHttpAuthNode* LookupAuthNode(const nsACString& scheme,
    215                                 const nsACString& host, int32_t port,
    216                                 nsACString const& originSuffix,
    217                                 nsCString& key);
    218  void ClearOriginData(OriginAttributesPattern const& pattern);
    219 
    220 private:
    221  virtual ~nsHttpAuthCache();
    222 
    223  using AuthNodeTable = nsClassHashtable<nsCStringHashKey, nsHttpAuthNode>;
    224  AuthNodeTable mDB;  // "host:port" --> nsHttpAuthNode
    225 };
    226 
    227 }  // namespace net
    228 }  // namespace mozilla
    229 
    230 #endif  // nsHttpAuthCache_h__