michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // HttpLog.h should generally be included first michael@0: #include "HttpLog.h" michael@0: michael@0: #include "nsHttpHandler.h" michael@0: #include "nsHttpAuthManager.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIPrincipal.h" michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: NS_IMPL_ISUPPORTS(nsHttpAuthManager, nsIHttpAuthManager) michael@0: michael@0: nsHttpAuthManager::nsHttpAuthManager() michael@0: { michael@0: } michael@0: michael@0: nsresult nsHttpAuthManager::Init() michael@0: { michael@0: // get reference to the auth cache. we assume that we will live michael@0: // as long as gHttpHandler. instantiate it if necessary. michael@0: michael@0: if (!gHttpHandler) { michael@0: nsresult rv; michael@0: nsCOMPtr ios = do_GetIOService(&rv); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: nsCOMPtr handler; michael@0: rv = ios->GetProtocolHandler("http", getter_AddRefs(handler)); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: // maybe someone is overriding our HTTP handler implementation? michael@0: NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED); michael@0: } michael@0: michael@0: mAuthCache = gHttpHandler->AuthCache(false); michael@0: mPrivateAuthCache = gHttpHandler->AuthCache(true); michael@0: NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE); michael@0: NS_ENSURE_TRUE(mPrivateAuthCache, NS_ERROR_FAILURE); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsHttpAuthManager::~nsHttpAuthManager() michael@0: { michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme, michael@0: const nsACString & aHost, michael@0: int32_t aPort, michael@0: const nsACString & aAuthType, michael@0: const nsACString & aRealm, michael@0: const nsACString & aPath, michael@0: nsAString & aUserDomain, michael@0: nsAString & aUserName, michael@0: nsAString & aUserPassword, michael@0: bool aIsPrivate, michael@0: nsIPrincipal* aPrincipal) michael@0: { michael@0: nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache; michael@0: nsHttpAuthEntry * entry = nullptr; michael@0: nsresult rv; michael@0: uint32_t appId = NECKO_NO_APP_ID; michael@0: bool inBrowserElement = false; michael@0: if (aPrincipal) { michael@0: appId = aPrincipal->GetAppId(); michael@0: inBrowserElement = aPrincipal->GetIsInBrowserElement(); michael@0: } michael@0: michael@0: if (!aPath.IsEmpty()) michael@0: rv = auth_cache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(), michael@0: PromiseFlatCString(aHost).get(), michael@0: aPort, michael@0: PromiseFlatCString(aPath).get(), michael@0: appId, inBrowserElement, michael@0: &entry); michael@0: else michael@0: rv = auth_cache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(), michael@0: PromiseFlatCString(aHost).get(), michael@0: aPort, michael@0: PromiseFlatCString(aRealm).get(), michael@0: appId, inBrowserElement, michael@0: &entry); michael@0: michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: if (!entry) michael@0: return NS_ERROR_UNEXPECTED; michael@0: michael@0: aUserDomain.Assign(entry->Domain()); michael@0: aUserName.Assign(entry->User()); michael@0: aUserPassword.Assign(entry->Pass()); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme, michael@0: const nsACString & aHost, michael@0: int32_t aPort, michael@0: const nsACString & aAuthType, michael@0: const nsACString & aRealm, michael@0: const nsACString & aPath, michael@0: const nsAString & aUserDomain, michael@0: const nsAString & aUserName, michael@0: const nsAString & aUserPassword, michael@0: bool aIsPrivate, michael@0: nsIPrincipal* aPrincipal) michael@0: { michael@0: nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(), michael@0: PromiseFlatString(aUserName).get(), michael@0: PromiseFlatString(aUserPassword).get()); michael@0: michael@0: uint32_t appId = NECKO_NO_APP_ID; michael@0: bool inBrowserElement = false; michael@0: if (aPrincipal) { michael@0: appId = aPrincipal->GetAppId(); michael@0: inBrowserElement = aPrincipal->GetIsInBrowserElement(); michael@0: } michael@0: michael@0: nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache; michael@0: return auth_cache->SetAuthEntry(PromiseFlatCString(aScheme).get(), michael@0: PromiseFlatCString(aHost).get(), michael@0: aPort, michael@0: PromiseFlatCString(aPath).get(), michael@0: PromiseFlatCString(aRealm).get(), michael@0: nullptr, // credentials michael@0: nullptr, // challenge michael@0: appId, inBrowserElement, michael@0: &ident, michael@0: nullptr); // metadata michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHttpAuthManager::ClearAll() michael@0: { michael@0: nsresult rv = mAuthCache->ClearAll(); michael@0: nsresult rv2 = mPrivateAuthCache->ClearAll(); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: if (NS_FAILED(rv2)) michael@0: return rv2; michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace mozilla::net michael@0: } // namespace mozilla