tor-browser

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

nsHttpAuthManager.cpp (3835B)


      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 // HttpLog.h should generally be included first
      7 #include "HttpLog.h"
      8 
      9 #include "nsHttpHandler.h"
     10 #include "nsHttpAuthManager.h"
     11 #include "nsNetUtil.h"
     12 #include "nsIPrincipal.h"
     13 
     14 namespace mozilla {
     15 namespace net {
     16 
     17 NS_IMPL_ISUPPORTS(nsHttpAuthManager, nsIHttpAuthManager)
     18 
     19 /* static */
     20 already_AddRefed<nsIHttpAuthCache>
     21 nsHttpAuthManager::GetHttpAuthCacheSingleton() {
     22  NS_ASSERTION(!IsNeckoChild(), "not a parent process");
     23 
     24  // Return only the non-private cache
     25  return do_AddRef(gHttpHandler->AuthCache(/* aPrivate = */ false));
     26 }
     27 
     28 nsresult nsHttpAuthManager::Init() {
     29  // get reference to the auth cache.  we assume that we will live
     30  // as long as gHttpHandler.  instantiate it if necessary.
     31 
     32  if (!gHttpHandler) {
     33    nsresult rv;
     34    nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
     35    if (NS_FAILED(rv)) return rv;
     36 
     37    nsCOMPtr<nsIProtocolHandler> handler;
     38    rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
     39    if (NS_FAILED(rv)) return rv;
     40 
     41    // maybe someone is overriding our HTTP handler implementation?
     42    NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED);
     43  }
     44 
     45  mAuthCache = gHttpHandler->AuthCache(false);
     46  mPrivateAuthCache = gHttpHandler->AuthCache(true);
     47  NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE);
     48  NS_ENSURE_TRUE(mPrivateAuthCache, NS_ERROR_FAILURE);
     49  return NS_OK;
     50 }
     51 
     52 NS_IMETHODIMP
     53 nsHttpAuthManager::GetAuthIdentity(
     54    const nsACString& aScheme, const nsACString& aHost, int32_t aPort,
     55    const nsACString& aAuthType, const nsACString& aRealm,
     56    const nsACString& aPath, nsAString& aUserDomain, nsAString& aUserName,
     57    nsAString& aUserPassword, bool aIsPrivate, nsIPrincipal* aPrincipal) {
     58  RefPtr<nsHttpAuthCache> auth_cache =
     59      aIsPrivate ? mPrivateAuthCache : mAuthCache;
     60  RefPtr<nsHttpAuthEntry> entry = nullptr;
     61  nsresult rv;
     62 
     63  nsAutoCString originSuffix;
     64  if (aPrincipal) {
     65    aPrincipal->OriginAttributesRef().CreateSuffix(originSuffix);
     66  }
     67 
     68  if (!aPath.IsEmpty()) {
     69    rv = auth_cache->GetAuthEntryForPath(aScheme, aHost, aPort, aPath,
     70                                         originSuffix, entry);
     71  } else {
     72    rv = auth_cache->GetAuthEntryForDomain(aScheme, aHost, aPort, aRealm,
     73                                           originSuffix, entry);
     74  }
     75 
     76  if (NS_FAILED(rv)) return rv;
     77  if (!entry) return NS_ERROR_UNEXPECTED;
     78 
     79  aUserDomain.Assign(entry->Domain());
     80  aUserName.Assign(entry->User());
     81  aUserPassword.Assign(entry->Pass());
     82  return NS_OK;
     83 }
     84 
     85 NS_IMETHODIMP
     86 nsHttpAuthManager::SetAuthIdentity(
     87    const nsACString& aScheme, const nsACString& aHost, int32_t aPort,
     88    const nsACString& aAuthType, const nsACString& aRealm,
     89    const nsACString& aPath, const nsAString& aUserDomain,
     90    const nsAString& aUserName, const nsAString& aUserPassword, bool aIsPrivate,
     91    nsIPrincipal* aPrincipal) {
     92  nsHttpAuthIdentity ident(aUserDomain, aUserName, aUserPassword);
     93 
     94  nsAutoCString originSuffix;
     95  if (aPrincipal) {
     96    aPrincipal->OriginAttributesRef().CreateSuffix(originSuffix);
     97  }
     98 
     99  RefPtr<nsHttpAuthCache> auth_cache =
    100      aIsPrivate ? mPrivateAuthCache : mAuthCache;
    101  return auth_cache->SetAuthEntry(aScheme, aHost, aPort, aPath, aRealm,
    102                                  ""_ns,  // credentials
    103                                  ""_ns,  // challenge
    104                                  originSuffix, &ident,
    105                                  nullptr);  // metadata
    106 }
    107 
    108 NS_IMETHODIMP
    109 nsHttpAuthManager::ClearAll() {
    110  mAuthCache->ClearAll();
    111  mPrivateAuthCache->ClearAll();
    112  return NS_OK;
    113 }
    114 
    115 }  // namespace net
    116 }  // namespace mozilla