2004-08-11 11:00:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-03-24 06:59:47 +03:00
|
|
|
|
2013-06-22 20:01:06 +04:00
|
|
|
// HttpLog.h should generally be included first
|
|
|
|
#include "HttpLog.h"
|
|
|
|
|
2003-03-24 06:59:47 +03:00
|
|
|
#include "nsHttpHandler.h"
|
|
|
|
#include "nsHttpChannel.h"
|
|
|
|
#include "nsHttpAuthManager.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsNetUtil.h"
|
2012-12-05 11:33:20 +04:00
|
|
|
#include "nsIPrincipal.h"
|
2003-03-24 06:59:47 +03:00
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsHttpAuthManager, nsIHttpAuthManager)
|
|
|
|
|
|
|
|
nsHttpAuthManager::nsHttpAuthManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsHttpAuthManager::Init()
|
|
|
|
{
|
2004-04-08 03:34:35 +04:00
|
|
|
// get reference to the auth cache. we assume that we will live
|
|
|
|
// as long as gHttpHandler. instantiate it if necessary.
|
|
|
|
|
2003-03-24 06:59:47 +03:00
|
|
|
if (!gHttpHandler) {
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
|
2004-04-08 03:34:35 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2003-03-24 06:59:47 +03:00
|
|
|
nsCOMPtr<nsIProtocolHandler> handler;
|
|
|
|
rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
|
2004-04-08 03:34:35 +04:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// maybe someone is overriding our HTTP handler implementation?
|
|
|
|
NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED);
|
2003-03-24 06:59:47 +03:00
|
|
|
}
|
|
|
|
|
2012-10-30 00:20:03 +04:00
|
|
|
mAuthCache = gHttpHandler->AuthCache(false);
|
|
|
|
mPrivateAuthCache = gHttpHandler->AuthCache(true);
|
2003-03-24 06:59:47 +03:00
|
|
|
NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE);
|
2012-10-30 00:20:03 +04:00
|
|
|
NS_ENSURE_TRUE(mPrivateAuthCache, NS_ERROR_FAILURE);
|
2003-03-24 06:59:47 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHttpAuthManager::~nsHttpAuthManager()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2004-04-08 03:34:35 +04:00
|
|
|
nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme,
|
|
|
|
const nsACString & aHost,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aPort,
|
2004-04-08 03:34:35 +04:00
|
|
|
const nsACString & aAuthType,
|
2003-03-24 06:59:47 +03:00
|
|
|
const nsACString & aRealm,
|
|
|
|
const nsACString & aPath,
|
|
|
|
nsAString & aUserDomain,
|
|
|
|
nsAString & aUserName,
|
2012-10-30 00:20:03 +04:00
|
|
|
nsAString & aUserPassword,
|
2012-12-05 11:33:20 +04:00
|
|
|
bool aIsPrivate,
|
|
|
|
nsIPrincipal* aPrincipal)
|
2003-03-24 06:59:47 +03:00
|
|
|
{
|
2012-10-30 00:20:03 +04:00
|
|
|
nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsHttpAuthEntry * entry = nullptr;
|
2003-03-24 06:59:47 +03:00
|
|
|
nsresult rv;
|
2012-12-05 11:33:20 +04:00
|
|
|
uint32_t appId = NECKO_NO_APP_ID;
|
|
|
|
bool inBrowserElement = false;
|
|
|
|
if (aPrincipal) {
|
|
|
|
appId = aPrincipal->GetAppId();
|
|
|
|
inBrowserElement = aPrincipal->GetIsInBrowserElement();
|
|
|
|
}
|
|
|
|
|
2003-08-12 08:27:47 +04:00
|
|
|
if (!aPath.IsEmpty())
|
2012-10-30 00:20:03 +04:00
|
|
|
rv = auth_cache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(),
|
2004-04-08 03:34:35 +04:00
|
|
|
PromiseFlatCString(aHost).get(),
|
2003-08-12 08:27:47 +04:00
|
|
|
aPort,
|
|
|
|
PromiseFlatCString(aPath).get(),
|
2012-12-05 11:33:20 +04:00
|
|
|
appId, inBrowserElement,
|
2004-04-08 03:34:35 +04:00
|
|
|
&entry);
|
|
|
|
else
|
2012-10-30 00:20:03 +04:00
|
|
|
rv = auth_cache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(),
|
2004-04-08 03:34:35 +04:00
|
|
|
PromiseFlatCString(aHost).get(),
|
|
|
|
aPort,
|
|
|
|
PromiseFlatCString(aRealm).get(),
|
2012-12-05 11:33:20 +04:00
|
|
|
appId, inBrowserElement,
|
2004-04-08 03:34:35 +04:00
|
|
|
&entry);
|
2003-03-24 06:59:47 +03:00
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!entry)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2004-04-08 03:34:35 +04:00
|
|
|
aUserDomain.Assign(entry->Domain());
|
2003-03-24 06:59:47 +03:00
|
|
|
aUserName.Assign(entry->User());
|
|
|
|
aUserPassword.Assign(entry->Pass());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2004-04-08 03:34:35 +04:00
|
|
|
nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme,
|
|
|
|
const nsACString & aHost,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aPort,
|
2004-04-08 03:34:35 +04:00
|
|
|
const nsACString & aAuthType,
|
2003-03-24 06:59:47 +03:00
|
|
|
const nsACString & aRealm,
|
|
|
|
const nsACString & aPath,
|
|
|
|
const nsAString & aUserDomain,
|
|
|
|
const nsAString & aUserName,
|
2012-10-30 00:20:03 +04:00
|
|
|
const nsAString & aUserPassword,
|
2012-12-05 11:33:20 +04:00
|
|
|
bool aIsPrivate,
|
|
|
|
nsIPrincipal* aPrincipal)
|
2003-03-24 06:59:47 +03:00
|
|
|
{
|
2003-03-26 08:05:49 +03:00
|
|
|
nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(),
|
|
|
|
PromiseFlatString(aUserName).get(),
|
|
|
|
PromiseFlatString(aUserPassword).get());
|
|
|
|
|
2012-12-05 11:33:20 +04:00
|
|
|
uint32_t appId = NECKO_NO_APP_ID;
|
|
|
|
bool inBrowserElement = false;
|
|
|
|
if (aPrincipal) {
|
|
|
|
appId = aPrincipal->GetAppId();
|
|
|
|
inBrowserElement = aPrincipal->GetIsInBrowserElement();
|
|
|
|
}
|
|
|
|
|
2012-10-30 00:20:03 +04:00
|
|
|
nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache;
|
|
|
|
return auth_cache->SetAuthEntry(PromiseFlatCString(aScheme).get(),
|
2004-04-08 03:34:35 +04:00
|
|
|
PromiseFlatCString(aHost).get(),
|
2003-03-24 06:59:47 +03:00
|
|
|
aPort,
|
|
|
|
PromiseFlatCString(aPath).get(),
|
|
|
|
PromiseFlatCString(aRealm).get(),
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, // credentials
|
|
|
|
nullptr, // challenge
|
2012-12-05 11:33:20 +04:00
|
|
|
appId, inBrowserElement,
|
2010-02-06 18:06:48 +03:00
|
|
|
&ident,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr); // metadata
|
2003-03-24 06:59:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHttpAuthManager::ClearAll()
|
|
|
|
{
|
2012-10-30 00:20:03 +04:00
|
|
|
nsresult rv = mAuthCache->ClearAll();
|
|
|
|
nsresult rv2 = mPrivateAuthCache->ClearAll();
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (NS_FAILED(rv2))
|
|
|
|
return rv2;
|
|
|
|
return NS_OK;
|
2003-03-24 06:59:47 +03:00
|
|
|
}
|