2010-06-17 22:33:42 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* vim:set expandtab ts=4 sw=4 sts=4 cin: */
|
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/. */
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2013-06-22 20:01:06 +04:00
|
|
|
// HttpLog.h should generally be included first
|
|
|
|
#include "HttpLog.h"
|
|
|
|
|
2015-04-01 13:56:00 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2010-06-17 22:33:42 +04:00
|
|
|
#include "nsHttpChannelAuthProvider.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsHttpHandler.h"
|
|
|
|
#include "nsIHttpAuthenticator.h"
|
2015-12-30 21:47:55 +03:00
|
|
|
#include "nsIHttpChannelInternal.h"
|
2010-06-17 22:33:42 +04:00
|
|
|
#include "nsIAuthPrompt2.h"
|
|
|
|
#include "nsIAuthPromptProvider.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsEscape.h"
|
|
|
|
#include "nsAuthInformationHolder.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIPrompt.h"
|
2013-09-23 07:35:52 +04:00
|
|
|
#include "netCore.h"
|
|
|
|
#include "nsIHttpAuthenticableChannel.h"
|
|
|
|
#include "nsIURI.h"
|
2015-04-01 13:56:00 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-07-07 05:17:00 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsILoadContext.h"
|
|
|
|
#include "nsIURL.h"
|
2015-08-27 14:50:00 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2016-10-25 11:32:00 +03:00
|
|
|
#include "nsIProxiedChannel.h"
|
|
|
|
#include "nsIProxyInfo.h"
|
2012-12-05 11:33:20 +04:00
|
|
|
|
2014-01-08 02:05:56 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
2015-04-01 13:56:00 +03:00
|
|
|
#define SUBRESOURCE_AUTH_DIALOG_DISALLOW_ALL 0
|
|
|
|
#define SUBRESOURCE_AUTH_DIALOG_DISALLOW_CROSS_ORIGIN 1
|
|
|
|
#define SUBRESOURCE_AUTH_DIALOG_ALLOW_ALL 2
|
|
|
|
|
2015-08-27 14:50:00 +03:00
|
|
|
#define HTTP_AUTH_DIALOG_TOP_LEVEL_DOC 0
|
|
|
|
#define HTTP_AUTH_DIALOG_SAME_ORIGIN_SUBRESOURCE 1
|
|
|
|
#define HTTP_AUTH_DIALOG_CROSS_ORIGIN_SUBRESOURCE 2
|
2016-06-06 02:06:00 +03:00
|
|
|
#define HTTP_AUTH_DIALOG_XHR 3
|
2015-08-27 14:50:00 +03:00
|
|
|
|
2016-05-01 01:58:34 +03:00
|
|
|
#define HTTP_AUTH_BASIC_INSECURE 0
|
|
|
|
#define HTTP_AUTH_BASIC_SECURE 1
|
|
|
|
#define HTTP_AUTH_DIGEST_INSECURE 2
|
|
|
|
#define HTTP_AUTH_DIGEST_SECURE 3
|
|
|
|
#define HTTP_AUTH_NTLM_INSECURE 4
|
|
|
|
#define HTTP_AUTH_NTLM_SECURE 5
|
|
|
|
#define HTTP_AUTH_NEGOTIATE_INSECURE 6
|
|
|
|
#define HTTP_AUTH_NEGOTIATE_SECURE 7
|
|
|
|
|
2012-12-05 11:33:20 +04:00
|
|
|
static void
|
2015-10-29 12:43:00 +03:00
|
|
|
GetOriginAttributesSuffix(nsIChannel* aChan, nsACString &aSuffix)
|
2012-12-05 11:33:20 +04:00
|
|
|
{
|
2017-01-12 19:38:48 +03:00
|
|
|
OriginAttributes oa;
|
2015-10-29 12:43:00 +03:00
|
|
|
|
|
|
|
// Deliberately ignoring the result and going with defaults
|
2012-12-05 11:33:20 +04:00
|
|
|
if (aChan) {
|
2015-10-29 12:43:00 +03:00
|
|
|
NS_GetOriginAttributes(aChan, oa);
|
2012-12-05 11:33:20 +04:00
|
|
|
}
|
2015-10-29 12:43:00 +03:00
|
|
|
|
|
|
|
oa.CreateSuffix(aSuffix);
|
2012-12-05 11:33:20 +04:00
|
|
|
}
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsHttpChannelAuthProvider::nsHttpChannelAuthProvider()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mAuthChannel(nullptr)
|
2016-10-25 11:32:00 +03:00
|
|
|
, mPort(-1)
|
|
|
|
, mUsingSSL(false)
|
|
|
|
, mProxyUsingSSL(false)
|
2012-10-30 00:20:03 +04:00
|
|
|
, mIsPrivate(false)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mProxyAuthContinuationState(nullptr)
|
|
|
|
, mAuthContinuationState(nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mProxyAuth(false)
|
|
|
|
, mTriedProxyAuth(false)
|
|
|
|
, mTriedHostAuth(false)
|
|
|
|
, mSuppressDefensiveAuth(false)
|
2016-06-06 02:06:00 +03:00
|
|
|
, mCrossOrigin(false)
|
2016-11-25 11:33:00 +03:00
|
|
|
, mConnectionBased(false)
|
2013-08-13 23:36:58 +04:00
|
|
|
, mHttpHandler(gHttpHandler)
|
2010-06-17 22:33:42 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHttpChannelAuthProvider::~nsHttpChannelAuthProvider()
|
|
|
|
{
|
2013-05-16 17:30:41 +04:00
|
|
|
MOZ_ASSERT(!mAuthChannel, "Disconnect wasn't called");
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
|
2015-04-01 13:56:00 +03:00
|
|
|
uint32_t nsHttpChannelAuthProvider::sAuthAllowPref =
|
2015-09-08 18:21:19 +03:00
|
|
|
SUBRESOURCE_AUTH_DIALOG_ALLOW_ALL;
|
2015-04-01 13:56:00 +03:00
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpChannelAuthProvider::InitializePrefs()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mozilla::Preferences::AddUintVarCache(&sAuthAllowPref,
|
2015-09-08 18:21:19 +03:00
|
|
|
"network.auth.subresource-http-auth-allow",
|
|
|
|
SUBRESOURCE_AUTH_DIALOG_ALLOW_ALL);
|
2015-04-01 13:56:00 +03:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHttpChannelAuthProvider::Init(nsIHttpAuthenticableChannel *channel)
|
|
|
|
{
|
2013-05-16 17:30:41 +04:00
|
|
|
MOZ_ASSERT(channel, "channel expected!");
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
mAuthChannel = channel;
|
|
|
|
|
|
|
|
nsresult rv = mAuthChannel->GetURI(getter_AddRefs(mURI));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2016-12-28 09:43:47 +03:00
|
|
|
rv = mAuthChannel->GetIsSSL(&mUsingSSL);
|
2010-06-17 22:33:42 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2016-10-25 11:32:00 +03:00
|
|
|
nsCOMPtr<nsIProxiedChannel> proxied(do_QueryInterface(channel));
|
|
|
|
if (proxied) {
|
|
|
|
nsCOMPtr<nsIProxyInfo> pi;
|
|
|
|
rv = proxied->GetProxyInfo(getter_AddRefs(pi));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (pi) {
|
|
|
|
nsAutoCString proxyType;
|
|
|
|
rv = pi->GetType(proxyType);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mProxyUsingSSL = proxyType.EqualsLiteral("https");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = mURI->GetAsciiHost(mHost);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// reject the URL if it doesn't specify a host
|
|
|
|
if (mHost.IsEmpty())
|
|
|
|
return NS_ERROR_MALFORMED_URI;
|
|
|
|
|
|
|
|
rv = mURI->GetPort(&mPort);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-10-30 00:20:03 +04:00
|
|
|
nsCOMPtr<nsIChannel> bareChannel = do_QueryInterface(channel);
|
|
|
|
mIsPrivate = NS_UsePrivateBrowsing(bareChannel);
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHttpChannelAuthProvider::ProcessAuthentication(uint32_t httpStatus,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool SSLConnectFailed)
|
2010-06-17 22:33:42 +04:00
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::ProcessAuthentication "
|
|
|
|
"[this=%p channel=%p code=%u SSLConnectFailed=%d]\n",
|
|
|
|
this, mAuthChannel, httpStatus, SSLConnectFailed));
|
|
|
|
|
2013-05-16 17:30:41 +04:00
|
|
|
MOZ_ASSERT(mAuthChannel, "Channel not initialized");
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIProxyInfo> proxyInfo;
|
|
|
|
nsresult rv = mAuthChannel->GetProxyInfo(getter_AddRefs(proxyInfo));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (proxyInfo) {
|
|
|
|
mProxyInfo = do_QueryInterface(proxyInfo);
|
|
|
|
if (!mProxyInfo) return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString challenges;
|
2012-02-12 22:53:31 +04:00
|
|
|
mProxyAuth = (httpStatus == 407);
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = PrepareForAuthentication(mProxyAuth);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (mProxyAuth) {
|
|
|
|
// only allow a proxy challenge if we have a proxy server configured.
|
2012-06-22 23:06:00 +04:00
|
|
|
// otherwise, we could inadvertently expose the user's proxy
|
2010-06-17 22:33:42 +04:00
|
|
|
// credentials to an origin server. We could attempt to proceed as
|
|
|
|
// if we had received a 401 from the server, but why risk flirting
|
|
|
|
// with trouble? IE similarly rejects 407s when a proxy server is
|
|
|
|
// not configured, so there's no reason not to do the same.
|
|
|
|
if (!UsingHttpProxy()) {
|
|
|
|
LOG(("rejecting 407 when proxy server not configured!\n"));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
if (UsingSSL() && !SSLConnectFailed) {
|
|
|
|
// we need to verify that this challenge came from the proxy
|
|
|
|
// server itself, and not some server on the other side of the
|
|
|
|
// SSL tunnel.
|
|
|
|
LOG(("rejecting 407 from origin server!\n"));
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
rv = mAuthChannel->GetProxyChallenges(challenges);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rv = mAuthChannel->GetWWWChallenges(challenges);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString creds;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = GetCredentials(challenges.get(), mProxyAuth, creds);
|
|
|
|
if (rv == NS_ERROR_IN_PROGRESS)
|
|
|
|
return rv;
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
LOG(("unable to authenticate\n"));
|
|
|
|
else {
|
|
|
|
// set the authentication credentials
|
|
|
|
if (mProxyAuth)
|
|
|
|
rv = mAuthChannel->SetProxyCredentials(creds);
|
|
|
|
else
|
|
|
|
rv = mAuthChannel->SetWWWCredentials(creds);
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2016-01-21 17:33:00 +03:00
|
|
|
nsHttpChannelAuthProvider::AddAuthorizationHeaders(bool aDontUseCachedWWWCreds)
|
2010-06-17 22:33:42 +04:00
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::AddAuthorizationHeaders? "
|
|
|
|
"[this=%p channel=%p]\n", this, mAuthChannel));
|
|
|
|
|
2013-05-16 17:30:41 +04:00
|
|
|
MOZ_ASSERT(mAuthChannel, "Channel not initialized");
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIProxyInfo> proxyInfo;
|
|
|
|
nsresult rv = mAuthChannel->GetProxyInfo(getter_AddRefs(proxyInfo));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (proxyInfo) {
|
|
|
|
mProxyInfo = do_QueryInterface(proxyInfo);
|
|
|
|
if (!mProxyInfo) return NS_ERROR_NO_INTERFACE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t loadFlags;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = mAuthChannel->GetLoadFlags(&loadFlags);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// this getter never fails
|
2012-10-30 00:20:03 +04:00
|
|
|
nsHttpAuthCache *authCache = gHttpHandler->AuthCache(mIsPrivate);
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
// check if proxy credentials should be sent
|
|
|
|
const char *proxyHost = ProxyHost();
|
2016-01-21 17:33:00 +03:00
|
|
|
if (proxyHost && UsingHttpProxy()) {
|
2010-06-17 22:33:42 +04:00
|
|
|
SetAuthorizationHeader(authCache, nsHttp::Proxy_Authorization,
|
|
|
|
"http", proxyHost, ProxyPort(),
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, // proxy has no path
|
2010-06-17 22:33:42 +04:00
|
|
|
mProxyIdent);
|
2016-01-21 17:33:00 +03:00
|
|
|
}
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2011-02-02 05:36:15 +03:00
|
|
|
if (loadFlags & nsIRequest::LOAD_ANONYMOUS) {
|
|
|
|
LOG(("Skipping Authorization header for anonymous load\n"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-01-21 17:33:00 +03:00
|
|
|
if (aDontUseCachedWWWCreds) {
|
|
|
|
LOG(("Authorization header already present:"
|
|
|
|
" skipping adding auth header from cache\n"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
// check if server credentials should be sent
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path, scheme;
|
2010-06-17 22:33:42 +04:00
|
|
|
if (NS_SUCCEEDED(GetCurrentPath(path)) &&
|
|
|
|
NS_SUCCEEDED(mURI->GetScheme(scheme))) {
|
|
|
|
SetAuthorizationHeader(authCache, nsHttp::Authorization,
|
|
|
|
scheme.get(),
|
|
|
|
Host(),
|
|
|
|
Port(),
|
|
|
|
path.get(),
|
|
|
|
mIdent);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHttpChannelAuthProvider::CheckForSuperfluousAuth()
|
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::CheckForSuperfluousAuth? "
|
|
|
|
"[this=%p channel=%p]\n", this, mAuthChannel));
|
|
|
|
|
2013-05-16 17:30:41 +04:00
|
|
|
MOZ_ASSERT(mAuthChannel, "Channel not initialized");
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
// we've been called because it has been determined that this channel is
|
|
|
|
// getting loaded without taking the userpass from the URL. if the URL
|
|
|
|
// contained a userpass, then (provided some other conditions are true),
|
|
|
|
// we'll give the user an opportunity to abort the channel as this might be
|
|
|
|
// an attempt to spoof a different site (see bug 232567).
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!ConfirmAuth(NS_LITERAL_STRING("SuperfluousAuth"), true)) {
|
2010-06-17 22:33:42 +04:00
|
|
|
// calling cancel here sets our mStatus and aborts the HTTP
|
|
|
|
// transaction, which prevents OnDataAvailable events.
|
2017-01-12 12:49:03 +03:00
|
|
|
Unused << mAuthChannel->Cancel(NS_ERROR_ABORT);
|
2010-06-17 22:33:42 +04:00
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHttpChannelAuthProvider::Cancel(nsresult status)
|
|
|
|
{
|
2013-05-16 17:30:41 +04:00
|
|
|
MOZ_ASSERT(mAuthChannel, "Channel not initialized");
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
if (mAsyncPromptAuthCancelable) {
|
|
|
|
mAsyncPromptAuthCancelable->Cancel(status);
|
2012-07-30 18:20:58 +04:00
|
|
|
mAsyncPromptAuthCancelable = nullptr;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
2016-07-14 10:32:00 +03:00
|
|
|
|
|
|
|
if (mGenerateCredentialsCancelable) {
|
|
|
|
mGenerateCredentialsCancelable->Cancel(status);
|
|
|
|
mGenerateCredentialsCancelable = nullptr;
|
|
|
|
}
|
2010-06-17 22:33:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHttpChannelAuthProvider::Disconnect(nsresult status)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mAuthChannel = nullptr;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
if (mAsyncPromptAuthCancelable) {
|
|
|
|
mAsyncPromptAuthCancelable->Cancel(status);
|
2012-07-30 18:20:58 +04:00
|
|
|
mAsyncPromptAuthCancelable = nullptr;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
|
2016-07-14 10:32:00 +03:00
|
|
|
if (mGenerateCredentialsCancelable) {
|
|
|
|
mGenerateCredentialsCancelable->Cancel(status);
|
|
|
|
mGenerateCredentialsCancelable = nullptr;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
NS_IF_RELEASE(mProxyAuthContinuationState);
|
|
|
|
NS_IF_RELEASE(mAuthContinuationState);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// buf contains "domain\user"
|
|
|
|
static void
|
2014-01-04 19:02:17 +04:00
|
|
|
ParseUserDomain(char16_t *buf,
|
|
|
|
const char16_t **user,
|
|
|
|
const char16_t **domain)
|
2010-06-17 22:33:42 +04:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *p = buf;
|
2010-06-17 22:33:42 +04:00
|
|
|
while (*p && *p != '\\') ++p;
|
|
|
|
if (!*p)
|
|
|
|
return;
|
|
|
|
*p = '\0';
|
|
|
|
*domain = buf;
|
|
|
|
*user = p + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function for setting identity from raw user:pass
|
|
|
|
static void
|
|
|
|
SetIdent(nsHttpAuthIdentity &ident,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t authFlags,
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t *userBuf,
|
|
|
|
char16_t *passBuf)
|
2010-06-17 22:33:42 +04:00
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *user = userBuf;
|
|
|
|
const char16_t *domain = nullptr;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
if (authFlags & nsIHttpAuthenticator::IDENTITY_INCLUDES_DOMAIN)
|
|
|
|
ParseUserDomain(userBuf, &user, &domain);
|
|
|
|
|
2017-01-12 12:49:03 +03:00
|
|
|
DebugOnly<nsresult> rv = ident.Set(domain, user, passBuf);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// helper function for getting an auth prompt from an interface requestor
|
|
|
|
static void
|
2011-09-29 10:19:26 +04:00
|
|
|
GetAuthPrompt(nsIInterfaceRequestor *ifreq, bool proxyAuth,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsIAuthPrompt2 **result)
|
|
|
|
{
|
|
|
|
if (!ifreq)
|
|
|
|
return;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t promptReason;
|
2010-06-17 22:33:42 +04:00
|
|
|
if (proxyAuth)
|
|
|
|
promptReason = nsIAuthPromptProvider::PROMPT_PROXY;
|
|
|
|
else
|
|
|
|
promptReason = nsIAuthPromptProvider::PROMPT_NORMAL;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAuthPromptProvider> promptProvider = do_GetInterface(ifreq);
|
|
|
|
if (promptProvider)
|
|
|
|
promptProvider->GetAuthPrompt(promptReason,
|
|
|
|
NS_GET_IID(nsIAuthPrompt2),
|
|
|
|
reinterpret_cast<void**>(result));
|
|
|
|
else
|
|
|
|
NS_QueryAuthPrompt2(ifreq, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate credentials for the given challenge, and update the auth cache.
|
|
|
|
nsresult
|
|
|
|
nsHttpChannelAuthProvider::GenCredsAndSetEntry(nsIHttpAuthenticator *auth,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool proxyAuth,
|
2010-06-17 22:33:42 +04:00
|
|
|
const char *scheme,
|
|
|
|
const char *host,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port,
|
2010-06-17 22:33:42 +04:00
|
|
|
const char *directory,
|
|
|
|
const char *realm,
|
|
|
|
const char *challenge,
|
|
|
|
const nsHttpAuthIdentity &ident,
|
|
|
|
nsCOMPtr<nsISupports> &sessionState,
|
|
|
|
char **result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsISupports *ss = sessionState;
|
|
|
|
|
|
|
|
// set informations that depend on whether
|
|
|
|
// we're authenticating against a proxy
|
|
|
|
// or a webserver
|
|
|
|
nsISupports **continuationState;
|
|
|
|
|
|
|
|
if (proxyAuth) {
|
|
|
|
continuationState = &mProxyAuthContinuationState;
|
|
|
|
} else {
|
|
|
|
continuationState = &mAuthContinuationState;
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:32:00 +03:00
|
|
|
rv = auth->GenerateCredentialsAsync(mAuthChannel,
|
|
|
|
this,
|
|
|
|
challenge,
|
|
|
|
proxyAuth,
|
|
|
|
ident.Domain(),
|
|
|
|
ident.User(),
|
|
|
|
ident.Password(),
|
|
|
|
ss,
|
|
|
|
*continuationState,
|
|
|
|
getter_AddRefs(mGenerateCredentialsCancelable));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// Calling generate credentials async, results will be dispatched to the
|
|
|
|
// main thread by calling OnCredsGenerated method
|
|
|
|
return NS_ERROR_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t generateFlags;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = auth->GenerateCredentials(mAuthChannel,
|
|
|
|
challenge,
|
|
|
|
proxyAuth,
|
|
|
|
ident.Domain(),
|
|
|
|
ident.User(),
|
|
|
|
ident.Password(),
|
|
|
|
&ss,
|
|
|
|
&*continuationState,
|
|
|
|
&generateFlags,
|
|
|
|
result);
|
|
|
|
|
|
|
|
sessionState.swap(ss);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// don't log this in release build since it could contain sensitive info.
|
|
|
|
#ifdef DEBUG
|
|
|
|
LOG(("generated creds: %s\n", *result));
|
|
|
|
#endif
|
|
|
|
|
2016-07-14 10:32:00 +03:00
|
|
|
return UpdateCache(auth, scheme, host, port, directory, realm,
|
|
|
|
challenge, ident, *result, generateFlags, sessionState);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpChannelAuthProvider::UpdateCache(nsIHttpAuthenticator *auth,
|
|
|
|
const char *scheme,
|
|
|
|
const char *host,
|
|
|
|
int32_t port,
|
|
|
|
const char *directory,
|
|
|
|
const char *realm,
|
|
|
|
const char *challenge,
|
|
|
|
const nsHttpAuthIdentity &ident,
|
|
|
|
const char *creds,
|
|
|
|
uint32_t generateFlags,
|
|
|
|
nsISupports *sessionState)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
uint32_t authFlags;
|
|
|
|
rv = auth->GetAuthFlags(&authFlags);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
// find out if this authenticator allows reuse of credentials and/or
|
|
|
|
// challenge.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool saveCreds =
|
2010-06-17 22:33:42 +04:00
|
|
|
0 != (authFlags & nsIHttpAuthenticator::REUSABLE_CREDENTIALS);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool saveChallenge =
|
2010-06-17 22:33:42 +04:00
|
|
|
0 != (authFlags & nsIHttpAuthenticator::REUSABLE_CHALLENGE);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool saveIdentity =
|
2010-06-17 22:33:42 +04:00
|
|
|
0 == (generateFlags & nsIHttpAuthenticator::USING_INTERNAL_IDENTITY);
|
|
|
|
|
|
|
|
// this getter never fails
|
2012-10-30 00:20:03 +04:00
|
|
|
nsHttpAuthCache *authCache = gHttpHandler->AuthCache(mIsPrivate);
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2012-12-05 11:33:20 +04:00
|
|
|
nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
|
2015-10-29 12:43:00 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
GetOriginAttributesSuffix(chan, suffix);
|
2012-12-05 11:33:20 +04:00
|
|
|
|
2016-07-14 10:32:00 +03:00
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
// create a cache entry. we do this even though we don't yet know that
|
|
|
|
// these credentials are valid b/c we need to avoid prompting the user
|
|
|
|
// more than once in case the credentials are valid.
|
|
|
|
//
|
|
|
|
// if the credentials are not reusable, then we don't bother sticking
|
|
|
|
// them in the auth cache.
|
|
|
|
rv = authCache->SetAuthEntry(scheme, host, port, directory, realm,
|
2016-07-14 10:32:00 +03:00
|
|
|
saveCreds ? creds : nullptr,
|
2012-07-30 18:20:58 +04:00
|
|
|
saveChallenge ? challenge : nullptr,
|
2015-10-29 12:43:00 +03:00
|
|
|
suffix,
|
2012-07-30 18:20:58 +04:00
|
|
|
saveIdentity ? &ident : nullptr,
|
2010-06-17 22:33:42 +04:00
|
|
|
sessionState);
|
|
|
|
return rv;
|
2016-07-14 10:32:00 +03:00
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHttpChannelAuthProvider::PrepareForAuthentication(bool proxyAuth)
|
2010-06-17 22:33:42 +04:00
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::PrepareForAuthentication "
|
|
|
|
"[this=%p channel=%p]\n", this, mAuthChannel));
|
|
|
|
|
|
|
|
if (!proxyAuth) {
|
|
|
|
// reset the current proxy continuation state because our last
|
|
|
|
// authentication attempt was completed successfully.
|
|
|
|
NS_IF_RELEASE(mProxyAuthContinuationState);
|
|
|
|
LOG((" proxy continuation state has been reset"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!UsingHttpProxy() || mProxyAuthType.IsEmpty())
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// We need to remove any Proxy_Authorization header left over from a
|
|
|
|
// non-request based authentication handshake (e.g., for NTLM auth).
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString contractId;
|
2010-06-17 22:33:42 +04:00
|
|
|
contractId.Assign(NS_HTTP_AUTHENTICATOR_CONTRACTID_PREFIX);
|
|
|
|
contractId.Append(mProxyAuthType);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIHttpAuthenticator> precedingAuth =
|
|
|
|
do_GetService(contractId.get(), &rv);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t precedingAuthFlags;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = precedingAuth->GetAuthFlags(&precedingAuthFlags);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!(precedingAuthFlags & nsIHttpAuthenticator::REQUEST_BASED)) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString challenges;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = mAuthChannel->GetProxyChallenges(challenges);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// delete the proxy authorization header because we weren't
|
|
|
|
// asked to authenticate
|
|
|
|
rv = mAuthChannel->SetProxyCredentials(EmptyCString());
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
LOG((" cleared proxy authorization header"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpChannelAuthProvider::GetCredentials(const char *challenges,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool proxyAuth,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsAFlatCString &creds)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIHttpAuthenticator> auth;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString challenge;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsCString authType; // force heap allocation to enable string sharing since
|
|
|
|
// we'll be assigning this value into mAuthType.
|
|
|
|
|
|
|
|
// set informations that depend on whether we're authenticating against a
|
|
|
|
// proxy or a webserver
|
|
|
|
nsISupports **currentContinuationState;
|
|
|
|
nsCString *currentAuthType;
|
|
|
|
|
|
|
|
if (proxyAuth) {
|
|
|
|
currentContinuationState = &mProxyAuthContinuationState;
|
|
|
|
currentAuthType = &mProxyAuthType;
|
|
|
|
} else {
|
|
|
|
currentContinuationState = &mAuthContinuationState;
|
|
|
|
currentAuthType = &mAuthType;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_NOT_AVAILABLE;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool gotCreds = false;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
// figure out which challenge we can handle and which authenticator to use.
|
|
|
|
for (const char *eol = challenges - 1; eol; ) {
|
|
|
|
const char *p = eol + 1;
|
|
|
|
|
|
|
|
// get the challenge string (LF separated -- see nsHttpHeaderArray)
|
2012-07-30 18:20:58 +04:00
|
|
|
if ((eol = strchr(p, '\n')) != nullptr)
|
2010-06-17 22:33:42 +04:00
|
|
|
challenge.Assign(p, eol - p);
|
|
|
|
else
|
|
|
|
challenge.Assign(p);
|
|
|
|
|
|
|
|
rv = GetAuthenticator(challenge.get(), authType, getter_AddRefs(auth));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
//
|
|
|
|
// if we've already selected an auth type from a previous challenge
|
|
|
|
// received while processing this channel, then skip others until
|
|
|
|
// we find a challenge corresponding to the previously tried auth
|
|
|
|
// type.
|
|
|
|
//
|
|
|
|
if (!currentAuthType->IsEmpty() && authType != *currentAuthType)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//
|
|
|
|
// we allow the routines to run all the way through before we
|
|
|
|
// decide if they are valid.
|
|
|
|
//
|
|
|
|
// we don't worry about the auth cache being altered because that
|
|
|
|
// would have been the last step, and if the error is from updating
|
|
|
|
// the authcache it wasn't really altered anyway. -CTN
|
|
|
|
//
|
|
|
|
// at this point the code is really only useful for client side
|
|
|
|
// errors (it will not automatically fail over to do a different
|
|
|
|
// auth type if the server keeps rejecting what is being sent, even
|
|
|
|
// if a particular auth method only knows 1 thing, like a
|
|
|
|
// non-identity based authentication method)
|
|
|
|
//
|
|
|
|
rv = GetCredentialsForChallenge(challenge.get(), authType.get(),
|
|
|
|
proxyAuth, auth, creds);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
gotCreds = true;
|
2010-06-17 22:33:42 +04:00
|
|
|
*currentAuthType = authType;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2017-02-09 13:21:38 +03:00
|
|
|
if (rv == NS_ERROR_IN_PROGRESS) {
|
2010-06-17 22:33:42 +04:00
|
|
|
// authentication prompt has been invoked and result is
|
|
|
|
// expected asynchronously, save current challenge being
|
|
|
|
// processed and all remaining challenges to use later in
|
|
|
|
// OnAuthAvailable and now immediately return
|
|
|
|
mCurrentChallenge = challenge;
|
2012-07-30 18:20:58 +04:00
|
|
|
mRemainingChallenges = eol ? eol+1 : nullptr;
|
2010-06-17 22:33:42 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the auth type and continuation state
|
|
|
|
NS_IF_RELEASE(*currentContinuationState);
|
|
|
|
currentAuthType->Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!gotCreds && !currentAuthType->IsEmpty()) {
|
|
|
|
// looks like we never found the auth type we were looking for.
|
|
|
|
// reset the auth type and continuation state, and try again.
|
|
|
|
currentAuthType->Truncate();
|
|
|
|
NS_IF_RELEASE(*currentContinuationState);
|
|
|
|
|
|
|
|
rv = GetCredentials(challenges, proxyAuth, creds);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHttpChannelAuthProvider::GetAuthorizationMembers(bool proxyAuth,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsCSubstring& scheme,
|
|
|
|
const char*& host,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t& port,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsCSubstring& path,
|
|
|
|
nsHttpAuthIdentity*& ident,
|
|
|
|
nsISupports**& continuationState)
|
|
|
|
{
|
|
|
|
if (proxyAuth) {
|
2013-05-16 17:30:41 +04:00
|
|
|
MOZ_ASSERT (UsingHttpProxy(),
|
|
|
|
"proxyAuth is true, but no HTTP proxy is configured!");
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
host = ProxyHost();
|
|
|
|
port = ProxyPort();
|
|
|
|
ident = &mProxyIdent;
|
|
|
|
scheme.AssignLiteral("http");
|
|
|
|
|
|
|
|
continuationState = &mProxyAuthContinuationState;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
host = Host();
|
|
|
|
port = Port();
|
|
|
|
ident = &mIdent;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
rv = GetCurrentPath(path);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = mURI->GetScheme(scheme);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
continuationState = &mAuthContinuationState;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpChannelAuthProvider::GetCredentialsForChallenge(const char *challenge,
|
|
|
|
const char *authType,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool proxyAuth,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsIHttpAuthenticator *auth,
|
|
|
|
nsAFlatCString &creds)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::GetCredentialsForChallenge "
|
|
|
|
"[this=%p channel=%p proxyAuth=%d challenges=%s]\n",
|
|
|
|
this, mAuthChannel, proxyAuth, challenge));
|
|
|
|
|
|
|
|
// this getter never fails
|
2012-10-30 00:20:03 +04:00
|
|
|
nsHttpAuthCache *authCache = gHttpHandler->AuthCache(mIsPrivate);
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t authFlags;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsresult rv = auth->GetAuthFlags(&authFlags);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString realm;
|
2010-06-17 22:33:42 +04:00
|
|
|
ParseRealm(challenge, realm);
|
|
|
|
|
|
|
|
// if no realm, then use the auth type as the realm. ToUpperCase so the
|
|
|
|
// ficticious realm stands out a bit more.
|
|
|
|
// XXX this will cause some single signon misses!
|
|
|
|
// XXX this was meant to be used with NTLM, which supplies no realm.
|
|
|
|
/*
|
|
|
|
if (realm.IsEmpty()) {
|
|
|
|
realm = authType;
|
|
|
|
ToUpperCase(realm);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
// set informations that depend on whether
|
|
|
|
// we're authenticating against a proxy
|
|
|
|
// or a webserver
|
|
|
|
const char *host;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsHttpAuthIdentity *ident;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path, scheme;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool identFromURI = false;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsISupports **continuationState;
|
|
|
|
|
|
|
|
rv = GetAuthorizationMembers(proxyAuth, scheme, host, port,
|
|
|
|
path, ident, continuationState);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-10-24 18:05:13 +04:00
|
|
|
uint32_t loadFlags;
|
|
|
|
rv = mAuthChannel->GetLoadFlags(&loadFlags);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
if (!proxyAuth) {
|
|
|
|
// if this is the first challenge, then try using the identity
|
|
|
|
// specified in the URL.
|
|
|
|
if (mIdent.IsEmpty()) {
|
|
|
|
GetIdentityFromURI(authFlags, mIdent);
|
|
|
|
identFromURI = !mIdent.IsEmpty();
|
|
|
|
}
|
2012-10-24 18:05:13 +04:00
|
|
|
|
|
|
|
if ((loadFlags & nsIRequest::LOAD_ANONYMOUS) && !identFromURI) {
|
|
|
|
LOG(("Skipping authentication for anonymous non-proxy request\n"));
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let explicit URL credentials pass
|
|
|
|
// regardless of the LOAD_ANONYMOUS flag
|
|
|
|
}
|
|
|
|
else if ((loadFlags & nsIRequest::LOAD_ANONYMOUS) && !UsingHttpProxy()) {
|
|
|
|
LOG(("Skipping authentication for anonymous non-proxy request\n"));
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
|
2012-12-05 11:33:20 +04:00
|
|
|
nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
|
2015-10-29 12:43:00 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
GetOriginAttributesSuffix(chan, suffix);
|
2012-12-05 11:33:20 +04:00
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
//
|
|
|
|
// if we already tried some credentials for this transaction, then
|
|
|
|
// we need to possibly clear them from the cache, unless the credentials
|
|
|
|
// in the cache have changed, in which case we'd want to give them a
|
|
|
|
// try instead.
|
|
|
|
//
|
2012-07-30 18:20:58 +04:00
|
|
|
nsHttpAuthEntry *entry = nullptr;
|
2016-12-27 14:05:13 +03:00
|
|
|
Unused << authCache->GetAuthEntryForDomain(scheme.get(), host, port,
|
|
|
|
realm.get(), suffix, &entry);
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
// hold reference to the auth session state (in case we clear our
|
|
|
|
// reference to the entry).
|
|
|
|
nsCOMPtr<nsISupports> sessionStateGrip;
|
|
|
|
if (entry)
|
|
|
|
sessionStateGrip = entry->mMetaData;
|
|
|
|
|
2017-04-26 18:02:05 +03:00
|
|
|
// remember if we already had the continuation state. it means we are in
|
|
|
|
// the middle of the authentication exchange and the connection must be
|
|
|
|
// kept sticky then (and only then).
|
|
|
|
bool authAtProgress = !!*continuationState;
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
// for digest auth, maybe our cached nonce value simply timed out...
|
2011-09-29 10:19:26 +04:00
|
|
|
bool identityInvalid;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsISupports *sessionState = sessionStateGrip;
|
|
|
|
rv = auth->ChallengeReceived(mAuthChannel,
|
|
|
|
challenge,
|
|
|
|
proxyAuth,
|
|
|
|
&sessionState,
|
|
|
|
&*continuationState,
|
|
|
|
&identityInvalid);
|
|
|
|
sessionStateGrip.swap(sessionState);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
LOG((" identity invalid = %d\n", identityInvalid));
|
|
|
|
|
2016-11-25 11:33:00 +03:00
|
|
|
if (mConnectionBased && identityInvalid) {
|
|
|
|
// If the flag is set and identity is invalid, it means we received the first
|
|
|
|
// challange for a new negotiation round after negotiating a connection based
|
|
|
|
// auth failed (invalid password).
|
|
|
|
// The mConnectionBased flag is set later for the newly received challenge,
|
|
|
|
// so here it reflects the previous 401/7 response schema.
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = mAuthChannel->CloseStickyConnection();
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2016-11-29 12:44:00 +03:00
|
|
|
if (!proxyAuth) {
|
2017-04-26 18:02:05 +03:00
|
|
|
// We must clear proxy ident in the following scenario + explanation:
|
|
|
|
// - we are authenticating to an NTLM proxy and an NTLM server
|
|
|
|
// - we successfully authenticated to the proxy, mProxyIdent keeps
|
|
|
|
// the user name/domain and password, the identity has also been cached
|
|
|
|
// - we just threw away the connection because we are now asking for
|
|
|
|
// creds for the server (WWW auth)
|
|
|
|
// - hence, we will have to auth to the proxy again as well
|
|
|
|
// - if we didn't clear the proxy identity, it would be considered
|
|
|
|
// as non-valid and we would ask the user again ; clearing it forces
|
|
|
|
// use of the cached identity and not asking the user again
|
|
|
|
mProxyIdent.Clear();
|
2016-11-29 12:44:00 +03:00
|
|
|
}
|
2016-11-25 11:33:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
mConnectionBased = !!(authFlags & nsIHttpAuthenticator::CONNECTION_BASED);
|
2017-03-29 03:15:23 +03:00
|
|
|
if (mConnectionBased) {
|
|
|
|
rv = mAuthChannel->ForceNoSpdy();
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
2016-11-25 11:33:00 +03:00
|
|
|
|
2017-04-26 18:02:05 +03:00
|
|
|
// It's legal if the peer closes the connection after the first 401/7.
|
|
|
|
// Making the connection sticky will prevent its restart giving the user
|
|
|
|
// a 'network reset' error every time. Hence, we mark the connection
|
|
|
|
// as restartable.
|
|
|
|
mAuthChannel->ConnectionRestartable(mConnectionBased && !authAtProgress);
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
if (identityInvalid) {
|
|
|
|
if (entry) {
|
|
|
|
if (ident->Equals(entry->Identity())) {
|
2012-10-24 18:05:12 +04:00
|
|
|
if (!identFromURI) {
|
|
|
|
LOG((" clearing bad auth cache entry\n"));
|
|
|
|
// ok, we've already tried this user identity, so clear the
|
|
|
|
// corresponding entry from the auth cache.
|
|
|
|
authCache->ClearAuthEntry(scheme.get(), host,
|
2012-12-05 11:33:20 +04:00
|
|
|
port, realm.get(),
|
2015-10-29 12:43:00 +03:00
|
|
|
suffix);
|
2012-10-24 18:05:12 +04:00
|
|
|
entry = nullptr;
|
|
|
|
ident->Clear();
|
|
|
|
}
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
else if (!identFromURI ||
|
2013-01-04 03:36:16 +04:00
|
|
|
(nsCRT::strcmp(ident->User(),
|
2012-10-24 18:05:13 +04:00
|
|
|
entry->Identity().User()) == 0 &&
|
2012-10-24 18:05:13 +04:00
|
|
|
!(loadFlags &
|
|
|
|
(nsIChannel::LOAD_ANONYMOUS |
|
|
|
|
nsIChannel::LOAD_EXPLICIT_CREDENTIALS)))) {
|
2010-06-17 22:33:42 +04:00
|
|
|
LOG((" taking identity from auth cache\n"));
|
|
|
|
// the password from the auth cache is more likely to be
|
|
|
|
// correct than the one in the URL. at least, we know that it
|
|
|
|
// works with the given username. it is possible for a server
|
|
|
|
// to distinguish logons based on the supplied password alone,
|
|
|
|
// but that would be quite unusual... and i don't think we need
|
|
|
|
// to worry about such unorthodox cases.
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = ident->Set(entry->Identity());
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2011-10-17 18:59:28 +04:00
|
|
|
identFromURI = false;
|
2010-06-17 22:33:42 +04:00
|
|
|
if (entry->Creds()[0] != '\0') {
|
|
|
|
LOG((" using cached credentials!\n"));
|
|
|
|
creds.Assign(entry->Creds());
|
|
|
|
return entry->AddPath(path.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!identFromURI) {
|
|
|
|
// hmm... identity invalid, but no auth entry! the realm probably
|
|
|
|
// changed (see bug 201986).
|
|
|
|
ident->Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!entry && ident->IsEmpty()) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t level = nsIAuthPrompt2::LEVEL_NONE;
|
2016-10-25 11:32:00 +03:00
|
|
|
if ((!proxyAuth && mUsingSSL) || (proxyAuth && mProxyUsingSSL))
|
2010-06-17 22:33:42 +04:00
|
|
|
level = nsIAuthPrompt2::LEVEL_SECURE;
|
|
|
|
else if (authFlags & nsIHttpAuthenticator::IDENTITY_ENCRYPTED)
|
|
|
|
level = nsIAuthPrompt2::LEVEL_PW_ENCRYPTED;
|
|
|
|
|
2016-05-01 01:58:34 +03:00
|
|
|
// Collect statistics on how frequently the various types of HTTP
|
|
|
|
// authentication are used over SSL and non-SSL connections.
|
|
|
|
if (gHttpHandler->IsTelemetryEnabled()) {
|
|
|
|
if (NS_LITERAL_CSTRING("basic").LowerCaseEqualsASCII(authType)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_AUTH_TYPE_STATS,
|
|
|
|
UsingSSL() ? HTTP_AUTH_BASIC_SECURE : HTTP_AUTH_BASIC_INSECURE);
|
|
|
|
} else if (NS_LITERAL_CSTRING("digest").LowerCaseEqualsASCII(authType)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_AUTH_TYPE_STATS,
|
|
|
|
UsingSSL() ? HTTP_AUTH_DIGEST_SECURE : HTTP_AUTH_DIGEST_INSECURE);
|
|
|
|
} else if (NS_LITERAL_CSTRING("ntlm").LowerCaseEqualsASCII(authType)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_AUTH_TYPE_STATS,
|
|
|
|
UsingSSL() ? HTTP_AUTH_NTLM_SECURE : HTTP_AUTH_NTLM_INSECURE);
|
|
|
|
} else if (NS_LITERAL_CSTRING("negotiate").LowerCaseEqualsASCII(authType)) {
|
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_AUTH_TYPE_STATS,
|
|
|
|
UsingSSL() ? HTTP_AUTH_NEGOTIATE_SECURE : HTTP_AUTH_NEGOTIATE_INSECURE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-01 13:56:00 +03:00
|
|
|
// Depending on the pref setting, the authentication dialog may be
|
|
|
|
// blocked for all sub-resources, blocked for cross-origin
|
|
|
|
// sub-resources, or always allowed for sub-resources.
|
|
|
|
// For more details look at the bug 647010.
|
2016-06-06 02:06:00 +03:00
|
|
|
// BlockPrompt will set mCrossOrigin parameter as well.
|
2015-04-01 13:56:00 +03:00
|
|
|
if (BlockPrompt()) {
|
2016-06-06 02:06:00 +03:00
|
|
|
LOG(("nsHttpChannelAuthProvider::GetCredentialsForChallenge: "
|
|
|
|
"Prompt is blocked [this=%p pref=%d]\n",
|
|
|
|
this, sAuthAllowPref));
|
|
|
|
return NS_ERROR_ABORT;
|
2015-04-01 13:56:00 +03:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
// at this point we are forced to interact with the user to get
|
|
|
|
// their username and password for this domain.
|
|
|
|
rv = PromptForIdentity(level, proxyAuth, realm.get(),
|
|
|
|
authType, authFlags, *ident);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2011-10-17 18:59:28 +04:00
|
|
|
identFromURI = false;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (identFromURI) {
|
|
|
|
// Warn the user before automatically using the identity from the URL
|
|
|
|
// to automatically log them into a site (see bug 232567).
|
2011-10-17 18:59:28 +04:00
|
|
|
if (!ConfirmAuth(NS_LITERAL_STRING("AutomaticAuth"), false)) {
|
2010-06-17 22:33:42 +04:00
|
|
|
// calling cancel here sets our mStatus and aborts the HTTP
|
|
|
|
// transaction, which prevents OnDataAvailable events.
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = mAuthChannel->Cancel(NS_ERROR_ABORT);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2010-06-17 22:33:42 +04:00
|
|
|
// this return code alone is not equivalent to Cancel, since
|
|
|
|
// it only instructs our caller that authentication failed.
|
|
|
|
// without an explicit call to Cancel, our caller would just
|
|
|
|
// load the page that accompanies the HTTP auth challenge.
|
|
|
|
return NS_ERROR_ABORT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// get credentials for the given user:pass
|
|
|
|
//
|
|
|
|
// always store the credentials we're trying now so that they will be used
|
|
|
|
// on subsequent links. This will potentially remove good credentials from
|
|
|
|
// the cache. This is ok as we don't want to use cached credentials if the
|
|
|
|
// user specified something on the URI or in another manner. This is so
|
|
|
|
// that we don't transparently authenticate as someone they're not
|
|
|
|
// expecting to authenticate as.
|
|
|
|
//
|
|
|
|
nsXPIDLCString result;
|
|
|
|
rv = GenCredsAndSetEntry(auth, proxyAuth, scheme.get(), host, port,
|
|
|
|
path.get(), realm.get(), challenge, *ident,
|
|
|
|
sessionStateGrip, getter_Copies(result));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
creds = result;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-04-01 13:56:00 +03:00
|
|
|
bool
|
|
|
|
nsHttpChannelAuthProvider::BlockPrompt()
|
|
|
|
{
|
2015-12-30 21:47:55 +03:00
|
|
|
// Verify that it's ok to prompt for credentials here, per spec
|
|
|
|
// http://xhr.spec.whatwg.org/#the-send%28%29-method
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpChannelInternal> chanInternal = do_QueryInterface(mAuthChannel);
|
|
|
|
MOZ_ASSERT(chanInternal);
|
|
|
|
|
2016-01-22 14:55:54 +03:00
|
|
|
if (chanInternal->GetBlockAuthPrompt()) {
|
2016-06-06 02:06:00 +03:00
|
|
|
LOG(("nsHttpChannelAuthProvider::BlockPrompt: Prompt is blocked "
|
|
|
|
"[this=%p channel=%p]\n", this, mAuthChannel));
|
2015-12-30 21:47:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-04-01 13:56:00 +03:00
|
|
|
nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
|
|
|
|
nsCOMPtr<nsILoadInfo> loadInfo;
|
|
|
|
chan->GetLoadInfo(getter_AddRefs(loadInfo));
|
2016-06-06 02:06:00 +03:00
|
|
|
|
|
|
|
// We will treat loads w/o loadInfo as a top level document.
|
|
|
|
bool topDoc = true;
|
|
|
|
bool xhr = false;
|
|
|
|
|
|
|
|
if (loadInfo) {
|
|
|
|
if (loadInfo->GetExternalContentPolicyType() !=
|
|
|
|
nsIContentPolicy::TYPE_DOCUMENT) {
|
|
|
|
topDoc = false;
|
|
|
|
}
|
|
|
|
if (loadInfo->GetExternalContentPolicyType() ==
|
|
|
|
nsIContentPolicy::TYPE_XMLHTTPREQUEST) {
|
|
|
|
xhr = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!topDoc && !xhr) {
|
|
|
|
nsCOMPtr<nsIURI> topURI;
|
2016-12-27 14:05:13 +03:00
|
|
|
Unused << chanInternal->GetTopWindowURI(getter_AddRefs(topURI));
|
2016-06-06 02:06:00 +03:00
|
|
|
|
|
|
|
if (!topURI) {
|
|
|
|
// If we do not have topURI try the loadingPrincipal.
|
|
|
|
nsCOMPtr<nsIPrincipal> loadingPrinc = loadInfo->LoadingPrincipal();
|
|
|
|
if (loadingPrinc) {
|
|
|
|
loadingPrinc->GetURI(getter_AddRefs(topURI));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!NS_SecurityCompareURIs(topURI, mURI, true)) {
|
|
|
|
mCrossOrigin = true;
|
|
|
|
}
|
|
|
|
}
|
2015-04-01 13:56:00 +03:00
|
|
|
}
|
|
|
|
|
2015-08-27 14:50:00 +03:00
|
|
|
if (gHttpHandler->IsTelemetryEnabled()) {
|
2016-06-06 02:06:00 +03:00
|
|
|
if (topDoc) {
|
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_AUTH_DIALOG_STATS,
|
|
|
|
HTTP_AUTH_DIALOG_TOP_LEVEL_DOC);
|
|
|
|
} else if (xhr) {
|
2015-08-27 14:50:00 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_AUTH_DIALOG_STATS,
|
2016-06-06 02:06:00 +03:00
|
|
|
HTTP_AUTH_DIALOG_XHR);
|
|
|
|
} else if (!mCrossOrigin) {
|
2015-08-27 14:50:00 +03:00
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_AUTH_DIALOG_STATS,
|
2016-06-06 02:06:00 +03:00
|
|
|
HTTP_AUTH_DIALOG_SAME_ORIGIN_SUBRESOURCE);
|
|
|
|
} else {
|
|
|
|
Telemetry::Accumulate(Telemetry::HTTP_AUTH_DIALOG_STATS,
|
|
|
|
HTTP_AUTH_DIALOG_CROSS_ORIGIN_SUBRESOURCE);
|
2015-08-27 14:50:00 +03:00
|
|
|
}
|
2015-04-01 13:56:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (sAuthAllowPref) {
|
|
|
|
case SUBRESOURCE_AUTH_DIALOG_DISALLOW_ALL:
|
|
|
|
// Do not open the http-authentication credentials dialog for
|
|
|
|
// the sub-resources.
|
2016-06-06 02:06:00 +03:00
|
|
|
return !topDoc && !xhr;
|
2015-04-01 13:56:00 +03:00
|
|
|
case SUBRESOURCE_AUTH_DIALOG_DISALLOW_CROSS_ORIGIN:
|
2016-06-06 02:06:00 +03:00
|
|
|
// Open the http-authentication credentials dialog for
|
2015-04-01 13:56:00 +03:00
|
|
|
// the sub-resources only if they are not cross-origin.
|
2016-06-06 02:06:00 +03:00
|
|
|
return !topDoc && !xhr && mCrossOrigin;
|
2015-04-01 13:56:00 +03:00
|
|
|
case SUBRESOURCE_AUTH_DIALOG_ALLOW_ALL:
|
|
|
|
// Allow the http-authentication dialog.
|
|
|
|
return false;
|
|
|
|
default:
|
|
|
|
// This is an invalid value.
|
|
|
|
MOZ_ASSERT(false, "A non valid value!");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
inline void
|
|
|
|
GetAuthType(const char *challenge, nsCString &authType)
|
|
|
|
{
|
|
|
|
const char *p;
|
|
|
|
|
|
|
|
// get the challenge type
|
2012-07-30 18:20:58 +04:00
|
|
|
if ((p = strchr(challenge, ' ')) != nullptr)
|
2010-06-17 22:33:42 +04:00
|
|
|
authType.Assign(challenge, p - challenge);
|
|
|
|
else
|
|
|
|
authType.Assign(challenge);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpChannelAuthProvider::GetAuthenticator(const char *challenge,
|
|
|
|
nsCString &authType,
|
|
|
|
nsIHttpAuthenticator **auth)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::GetAuthenticator [this=%p channel=%p]\n",
|
|
|
|
this, mAuthChannel));
|
|
|
|
|
|
|
|
GetAuthType(challenge, authType);
|
|
|
|
|
|
|
|
// normalize to lowercase
|
|
|
|
ToLowerCase(authType);
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString contractid;
|
2010-06-17 22:33:42 +04:00
|
|
|
contractid.Assign(NS_HTTP_AUTHENTICATOR_CONTRACTID_PREFIX);
|
|
|
|
contractid.Append(authType);
|
|
|
|
|
|
|
|
return CallGetService(contractid.get(), auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHttpChannelAuthProvider::GetIdentityFromURI(uint32_t authFlags,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsHttpAuthIdentity &ident)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::GetIdentityFromURI [this=%p channel=%p]\n",
|
|
|
|
this, mAuthChannel));
|
|
|
|
|
|
|
|
nsAutoString userBuf;
|
|
|
|
nsAutoString passBuf;
|
|
|
|
|
|
|
|
// XXX i18n
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString buf;
|
2010-06-17 22:33:42 +04:00
|
|
|
mURI->GetUsername(buf);
|
|
|
|
if (!buf.IsEmpty()) {
|
|
|
|
NS_UnescapeURL(buf);
|
|
|
|
CopyASCIItoUTF16(buf, userBuf);
|
|
|
|
mURI->GetPassword(buf);
|
|
|
|
if (!buf.IsEmpty()) {
|
|
|
|
NS_UnescapeURL(buf);
|
|
|
|
CopyASCIItoUTF16(buf, passBuf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!userBuf.IsEmpty()) {
|
2014-01-04 19:02:17 +04:00
|
|
|
SetIdent(ident, authFlags, (char16_t *) userBuf.get(),
|
|
|
|
(char16_t *) passBuf.get());
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpChannelAuthProvider::ParseRealm(const char *challenge,
|
|
|
|
nsACString &realm)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// From RFC2617 section 1.2, the realm value is defined as such:
|
|
|
|
//
|
|
|
|
// realm = "realm" "=" realm-value
|
|
|
|
// realm-value = quoted-string
|
|
|
|
//
|
|
|
|
// but, we'll accept anything after the the "=" up to the first space, or
|
|
|
|
// end-of-line, if the string is not quoted.
|
|
|
|
//
|
2014-01-25 03:07:37 +04:00
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
const char *p = PL_strcasestr(challenge, "realm=");
|
|
|
|
if (p) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool has_quote = false;
|
2010-06-17 22:33:42 +04:00
|
|
|
p += 6;
|
|
|
|
if (*p == '"') {
|
2011-10-17 18:59:28 +04:00
|
|
|
has_quote = true;
|
2010-06-17 22:33:42 +04:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
|
2014-01-25 03:07:37 +04:00
|
|
|
const char *end;
|
|
|
|
if (has_quote) {
|
|
|
|
end = p;
|
|
|
|
while (*end) {
|
|
|
|
if (*end == '\\') {
|
|
|
|
// escaped character, store that one instead if not zero
|
|
|
|
if (!*++end)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (*end == '\"')
|
|
|
|
// end of string
|
|
|
|
break;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2014-01-25 03:07:37 +04:00
|
|
|
realm.Append(*end);
|
|
|
|
++end;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// realm given without quotes
|
2010-06-17 22:33:42 +04:00
|
|
|
end = strchr(p, ' ');
|
2014-01-25 03:07:37 +04:00
|
|
|
if (end)
|
|
|
|
realm.Assign(p, end - p);
|
|
|
|
else
|
|
|
|
realm.Assign(p);
|
|
|
|
}
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class nsHTTPAuthInformation : public nsAuthInformationHolder {
|
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHTTPAuthInformation(uint32_t aFlags, const nsString& aRealm,
|
2010-06-17 22:33:42 +04:00
|
|
|
const nsCString& aAuthType)
|
|
|
|
: nsAuthInformationHolder(aFlags, aRealm, aAuthType) {}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void SetToHttpAuthIdentity(uint32_t authFlags,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsHttpAuthIdentity& identity);
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHTTPAuthInformation::SetToHttpAuthIdentity(uint32_t authFlags,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsHttpAuthIdentity& identity)
|
|
|
|
{
|
2017-01-12 12:49:03 +03:00
|
|
|
DebugOnly<nsresult> rv = identity.Set(Domain().get(), User().get(), Password().get());
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsHttpChannelAuthProvider::PromptForIdentity(uint32_t level,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool proxyAuth,
|
2010-06-17 22:33:42 +04:00
|
|
|
const char *realm,
|
|
|
|
const char *authType,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t authFlags,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsHttpAuthIdentity &ident)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::PromptForIdentity [this=%p channel=%p]\n",
|
|
|
|
this, mAuthChannel));
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks;
|
|
|
|
rv = mAuthChannel->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = mAuthChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAuthPrompt2> authPrompt;
|
|
|
|
GetAuthPrompt(callbacks, proxyAuth, getter_AddRefs(authPrompt));
|
|
|
|
if (!authPrompt && loadGroup) {
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> cbs;
|
|
|
|
loadGroup->GetNotificationCallbacks(getter_AddRefs(cbs));
|
|
|
|
GetAuthPrompt(cbs, proxyAuth, getter_AddRefs(authPrompt));
|
|
|
|
}
|
|
|
|
if (!authPrompt)
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
|
|
|
|
// XXX i18n: need to support non-ASCII realm strings (see bug 41489)
|
|
|
|
NS_ConvertASCIItoUTF16 realmU(realm);
|
|
|
|
|
|
|
|
// prompt the user...
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t promptFlags = 0;
|
2010-06-17 22:33:42 +04:00
|
|
|
if (proxyAuth)
|
|
|
|
{
|
|
|
|
promptFlags |= nsIAuthInformation::AUTH_PROXY;
|
|
|
|
if (mTriedProxyAuth)
|
|
|
|
promptFlags |= nsIAuthInformation::PREVIOUS_FAILED;
|
2011-10-17 18:59:28 +04:00
|
|
|
mTriedProxyAuth = true;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
promptFlags |= nsIAuthInformation::AUTH_HOST;
|
|
|
|
if (mTriedHostAuth)
|
|
|
|
promptFlags |= nsIAuthInformation::PREVIOUS_FAILED;
|
2011-10-17 18:59:28 +04:00
|
|
|
mTriedHostAuth = true;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (authFlags & nsIHttpAuthenticator::IDENTITY_INCLUDES_DOMAIN)
|
|
|
|
promptFlags |= nsIAuthInformation::NEED_DOMAIN;
|
|
|
|
|
2016-06-06 02:06:00 +03:00
|
|
|
if (mCrossOrigin) {
|
|
|
|
promptFlags |= nsIAuthInformation::CROSS_ORIGIN_SUB_RESOURCE;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsHTTPAuthInformation> holder =
|
2010-06-17 22:33:42 +04:00
|
|
|
new nsHTTPAuthInformation(promptFlags, realmU,
|
|
|
|
nsDependentCString(authType));
|
|
|
|
if (!holder)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel(do_QueryInterface(mAuthChannel, &rv));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv =
|
2012-07-30 18:20:58 +04:00
|
|
|
authPrompt->AsyncPromptAuth(channel, this, nullptr, level, holder,
|
2010-06-17 22:33:42 +04:00
|
|
|
getter_AddRefs(mAsyncPromptAuthCancelable));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// indicate using this error code that authentication prompt
|
|
|
|
// result is expected asynchronously
|
|
|
|
rv = NS_ERROR_IN_PROGRESS;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Fall back to synchronous prompt
|
2011-09-29 10:19:26 +04:00
|
|
|
bool retval = false;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = authPrompt->PromptAuth(channel, level, holder, &retval);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
if (!retval)
|
|
|
|
rv = NS_ERROR_ABORT;
|
|
|
|
else
|
|
|
|
holder->SetToHttpAuthIdentity(authFlags, ident);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remember that we successfully showed the user an auth dialog
|
|
|
|
if (!proxyAuth)
|
2011-10-17 18:59:28 +04:00
|
|
|
mSuppressDefensiveAuth = true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2016-11-15 19:00:00 +03:00
|
|
|
if (mConnectionBased) {
|
|
|
|
// Connection can be reset by the server in the meantime user is entering
|
|
|
|
// the credentials. Result would be just a "Connection was reset" error.
|
|
|
|
// Hence, we drop the current regardless if the user would make it on time
|
|
|
|
// to provide credentials.
|
|
|
|
// It's OK to send the NTLM type 1 message (response to the plain "NTLM"
|
|
|
|
// challenge) on a new connection.
|
2017-01-12 12:49:03 +03:00
|
|
|
{
|
|
|
|
DebugOnly<nsresult> rv = mAuthChannel->CloseStickyConnection();
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
2016-11-15 19:00:00 +03:00
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHttpChannelAuthProvider::OnAuthAvailable(nsISupports *aContext,
|
|
|
|
nsIAuthInformation *aAuthInfo)
|
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::OnAuthAvailable [this=%p channel=%p]",
|
|
|
|
this, mAuthChannel));
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mAsyncPromptAuthCancelable = nullptr;
|
2010-06-17 22:33:42 +04:00
|
|
|
if (!mAuthChannel)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
const char *host;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsHttpAuthIdentity *ident;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString path, scheme;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsISupports **continuationState;
|
|
|
|
rv = GetAuthorizationMembers(mProxyAuth, scheme, host, port,
|
|
|
|
path, ident, continuationState);
|
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
OnAuthCancelled(aContext, false);
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString realm;
|
2010-06-17 22:33:42 +04:00
|
|
|
ParseRealm(mCurrentChallenge.get(), realm);
|
|
|
|
|
2012-12-05 11:33:20 +04:00
|
|
|
nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
|
2015-10-29 12:43:00 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
GetOriginAttributesSuffix(chan, suffix);
|
2012-12-05 11:33:20 +04:00
|
|
|
|
2012-10-30 00:20:03 +04:00
|
|
|
nsHttpAuthCache *authCache = gHttpHandler->AuthCache(mIsPrivate);
|
2012-07-30 18:20:58 +04:00
|
|
|
nsHttpAuthEntry *entry = nullptr;
|
2016-12-27 14:05:13 +03:00
|
|
|
Unused << authCache->GetAuthEntryForDomain(scheme.get(), host, port,
|
|
|
|
realm.get(), suffix,
|
|
|
|
&entry);
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsISupports> sessionStateGrip;
|
|
|
|
if (entry)
|
|
|
|
sessionStateGrip = entry->mMetaData;
|
|
|
|
|
|
|
|
nsAuthInformationHolder* holder =
|
|
|
|
static_cast<nsAuthInformationHolder*>(aAuthInfo);
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = ident->Set(holder->Domain().get(),
|
|
|
|
holder->User().get(),
|
|
|
|
holder->Password().get());
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString unused;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsCOMPtr<nsIHttpAuthenticator> auth;
|
|
|
|
rv = GetAuthenticator(mCurrentChallenge.get(), unused,
|
|
|
|
getter_AddRefs(auth));
|
|
|
|
if (NS_FAILED(rv)) {
|
2013-05-16 17:30:41 +04:00
|
|
|
MOZ_ASSERT(false, "GetAuthenticator failed");
|
2011-10-17 18:59:28 +04:00
|
|
|
OnAuthCancelled(aContext, true);
|
2010-06-17 22:33:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPIDLCString creds;
|
|
|
|
rv = GenCredsAndSetEntry(auth, mProxyAuth,
|
|
|
|
scheme.get(), host, port, path.get(),
|
|
|
|
realm.get(), mCurrentChallenge.get(), *ident,
|
|
|
|
sessionStateGrip, getter_Copies(creds));
|
|
|
|
|
|
|
|
mCurrentChallenge.Truncate();
|
|
|
|
if (NS_FAILED(rv)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
OnAuthCancelled(aContext, true);
|
2010-06-17 22:33:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ContinueOnAuthAvailable(creds);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHttpChannelAuthProvider::OnAuthCancelled(nsISupports *aContext,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool userCancel)
|
2010-06-17 22:33:42 +04:00
|
|
|
{
|
|
|
|
LOG(("nsHttpChannelAuthProvider::OnAuthCancelled [this=%p channel=%p]",
|
|
|
|
this, mAuthChannel));
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mAsyncPromptAuthCancelable = nullptr;
|
2010-06-17 22:33:42 +04:00
|
|
|
if (!mAuthChannel)
|
|
|
|
return NS_OK;
|
|
|
|
|
2016-11-25 11:33:00 +03:00
|
|
|
// When user cancels or auth fails we want to close the connection for
|
|
|
|
// connection based schemes like NTLM. Some servers don't like re-negotiation
|
|
|
|
// on the same connection.
|
2017-01-12 12:49:03 +03:00
|
|
|
nsresult rv;
|
2016-11-25 11:33:00 +03:00
|
|
|
if (mConnectionBased) {
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = mAuthChannel->CloseStickyConnection();
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2016-11-25 11:33:00 +03:00
|
|
|
mConnectionBased = false;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
if (userCancel) {
|
|
|
|
if (!mRemainingChallenges.IsEmpty()) {
|
|
|
|
// there are still some challenges to process, do so
|
|
|
|
|
2016-07-28 17:09:32 +03:00
|
|
|
// Get rid of current continuationState to avoid reusing it in
|
|
|
|
// next challenges since it is no longer relevant.
|
|
|
|
if (mProxyAuth) {
|
|
|
|
NS_IF_RELEASE(mProxyAuthContinuationState);
|
|
|
|
} else {
|
|
|
|
NS_IF_RELEASE(mAuthContinuationState);
|
|
|
|
}
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString creds;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = GetCredentials(mRemainingChallenges.get(), mProxyAuth, creds);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// GetCredentials loaded the credentials from the cache or
|
|
|
|
// some other way in a synchronous manner, process those
|
|
|
|
// credentials now
|
|
|
|
mRemainingChallenges.Truncate();
|
|
|
|
return ContinueOnAuthAvailable(creds);
|
|
|
|
}
|
2017-02-09 13:21:38 +03:00
|
|
|
if (rv == NS_ERROR_IN_PROGRESS) {
|
2010-06-17 22:33:42 +04:00
|
|
|
// GetCredentials successfully queued another authprompt for
|
|
|
|
// a challenge from the list, we are now waiting for the user
|
|
|
|
// to provide the credentials
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, we failed...
|
|
|
|
}
|
|
|
|
|
|
|
|
mRemainingChallenges.Truncate();
|
|
|
|
}
|
|
|
|
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = mAuthChannel->OnAuthCancelled(userCancel);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-07-14 10:32:00 +03:00
|
|
|
NS_IMETHODIMP nsHttpChannelAuthProvider::OnCredsGenerated(const char *aGeneratedCreds,
|
|
|
|
uint32_t aFlags,
|
|
|
|
nsresult aResult,
|
|
|
|
nsISupports* aSessionState,
|
|
|
|
nsISupports* aContinuationState)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// When channel is closed, do not proceed
|
|
|
|
if (!mAuthChannel) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
mGenerateCredentialsCancelable = nullptr;
|
|
|
|
|
|
|
|
if (NS_FAILED(aResult)) {
|
|
|
|
return OnAuthCancelled(nullptr, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We want to update m(Proxy)AuthContinuationState in case it was changed by
|
|
|
|
// nsHttpNegotiateAuth::GenerateCredentials
|
|
|
|
nsCOMPtr<nsISupports> contState(aContinuationState);
|
|
|
|
if (mProxyAuth) {
|
|
|
|
contState.swap(mProxyAuthContinuationState);
|
|
|
|
} else {
|
|
|
|
contState.swap(mAuthContinuationState);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHttpAuthenticator> auth;
|
|
|
|
nsAutoCString unused;
|
|
|
|
rv = GetAuthenticator(mCurrentChallenge.get(), unused, getter_AddRefs(auth));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
const char *host;
|
|
|
|
int32_t port;
|
|
|
|
nsHttpAuthIdentity *ident;
|
|
|
|
nsAutoCString directory, scheme;
|
|
|
|
nsISupports **unusedContinuationState;
|
|
|
|
|
|
|
|
// Get realm from challenge
|
|
|
|
nsAutoCString realm;
|
|
|
|
ParseRealm(mCurrentChallenge.get(), realm);
|
|
|
|
|
|
|
|
rv = GetAuthorizationMembers(mProxyAuth, scheme, host, port,
|
|
|
|
directory, ident, unusedContinuationState);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = UpdateCache(auth, scheme.get(), host, port, directory.get(),
|
|
|
|
realm.get(), mCurrentChallenge.get(), *ident,
|
|
|
|
aGeneratedCreds, aFlags, aSessionState);
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2016-07-14 10:32:00 +03:00
|
|
|
mCurrentChallenge.Truncate();
|
|
|
|
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = ContinueOnAuthAvailable(nsDependentCString(aGeneratedCreds));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2016-07-14 10:32:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-17 22:33:42 +04:00
|
|
|
nsresult
|
|
|
|
nsHttpChannelAuthProvider::ContinueOnAuthAvailable(const nsCSubstring& creds)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
if (mProxyAuth)
|
|
|
|
rv = mAuthChannel->SetProxyCredentials(creds);
|
|
|
|
else
|
|
|
|
rv = mAuthChannel->SetWWWCredentials(creds);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// drop our remaining list of challenges. We don't need them, because we
|
|
|
|
// have now authenticated against a challenge and will be sending that
|
|
|
|
// information to the server (or proxy). If it doesn't accept our
|
|
|
|
// authentication it'll respond with failure and resend the challenge list
|
|
|
|
mRemainingChallenges.Truncate();
|
|
|
|
|
2017-01-12 12:49:03 +03:00
|
|
|
Unused << mAuthChannel->OnAuthAvailable();
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-06-17 22:33:42 +04:00
|
|
|
nsHttpChannelAuthProvider::ConfirmAuth(const nsString &bundleKey,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doYesNoPrompt)
|
2010-06-17 22:33:42 +04:00
|
|
|
{
|
|
|
|
// skip prompting the user if
|
|
|
|
// 1) we've already prompted the user
|
|
|
|
// 2) we're not a toplevel channel
|
|
|
|
// 3) the userpass length is less than the "phishy" threshold
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t loadFlags;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsresult rv = mAuthChannel->GetLoadFlags(&loadFlags);
|
Bug 671185 - Incorrect return of NS_ERROR_* codes in functions returning PRBool, r=mak,ehsan,taras,biesi,pike,khuey,dholbert,josh,bjacob,bsmith
2011-07-26 08:57:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
if (mSuppressDefensiveAuth ||
|
|
|
|
!(loadFlags & nsIChannel::LOAD_INITIAL_DOCUMENT_URI))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString userPass;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = mURI->GetUserPass(userPass);
|
|
|
|
if (NS_FAILED(rv) ||
|
|
|
|
(userPass.Length() < gHttpHandler->PhishyUserPassLength()))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
// we try to confirm by prompting the user. if we cannot do so, then
|
|
|
|
// assume the user said ok. this is done to keep things working in
|
|
|
|
// embedded builds, where the string bundle might not be present, etc.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleService =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
|
|
|
if (!bundleService)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
bundleService->CreateBundle(NECKO_MSGS_URL, getter_AddRefs(bundle));
|
|
|
|
if (!bundle)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString host;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = mURI->GetHost(host);
|
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString user;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = mURI->GetUsername(user);
|
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
NS_ConvertUTF8toUTF16 ucsHost(host), ucsUser(user);
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t *strs[2] = { ucsHost.get(), ucsUser.get() };
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsXPIDLString msg;
|
|
|
|
bundle->FormatStringFromName(bundleKey.get(), strs, 2, getter_Copies(msg));
|
|
|
|
if (!msg)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIInterfaceRequestor> callbacks;
|
|
|
|
rv = mAuthChannel->GetNotificationCallbacks(getter_AddRefs(callbacks));
|
Bug 671185 - Incorrect return of NS_ERROR_* codes in functions returning PRBool, r=mak,ehsan,taras,biesi,pike,khuey,dholbert,josh,bjacob,bsmith
2011-07-26 08:57:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
|
|
|
rv = mAuthChannel->GetLoadGroup(getter_AddRefs(loadGroup));
|
Bug 671185 - Incorrect return of NS_ERROR_* codes in functions returning PRBool, r=mak,ehsan,taras,biesi,pike,khuey,dholbert,josh,bjacob,bsmith
2011-07-26 08:57:58 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIPrompt> prompt;
|
|
|
|
NS_QueryNotificationCallbacks(callbacks, loadGroup, NS_GET_IID(nsIPrompt),
|
|
|
|
getter_AddRefs(prompt));
|
|
|
|
if (!prompt)
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
// do not prompt again
|
2011-10-17 18:59:28 +04:00
|
|
|
mSuppressDefensiveAuth = true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool confirmed;
|
2010-06-17 22:33:42 +04:00
|
|
|
if (doYesNoPrompt) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t choice;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool checkState = false;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = prompt->ConfirmEx(nullptr, msg,
|
2010-06-17 22:33:42 +04:00
|
|
|
nsIPrompt::BUTTON_POS_1_DEFAULT +
|
|
|
|
nsIPrompt::STD_YES_NO_BUTTONS,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, nullptr, nullptr, nullptr,
|
2010-06-17 22:33:42 +04:00
|
|
|
&checkState, &choice);
|
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
confirmed = choice == 0;
|
|
|
|
}
|
|
|
|
else {
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = prompt->Confirm(nullptr, msg, &confirmed);
|
2010-06-17 22:33:42 +04:00
|
|
|
if (NS_FAILED(rv))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return confirmed;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHttpChannelAuthProvider::SetAuthorizationHeader(nsHttpAuthCache *authCache,
|
|
|
|
nsHttpAtom header,
|
|
|
|
const char *scheme,
|
|
|
|
const char *host,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t port,
|
2010-06-17 22:33:42 +04:00
|
|
|
const char *path,
|
|
|
|
nsHttpAuthIdentity &ident)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
nsHttpAuthEntry *entry = nullptr;
|
2010-06-17 22:33:42 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// set informations that depend on whether
|
|
|
|
// we're authenticating against a proxy
|
|
|
|
// or a webserver
|
|
|
|
nsISupports **continuationState;
|
|
|
|
|
|
|
|
if (header == nsHttp::Proxy_Authorization) {
|
|
|
|
continuationState = &mProxyAuthContinuationState;
|
|
|
|
} else {
|
|
|
|
continuationState = &mAuthContinuationState;
|
|
|
|
}
|
|
|
|
|
2012-12-05 11:33:20 +04:00
|
|
|
nsCOMPtr<nsIChannel> chan = do_QueryInterface(mAuthChannel);
|
2015-10-29 12:43:00 +03:00
|
|
|
nsAutoCString suffix;
|
|
|
|
GetOriginAttributesSuffix(chan, suffix);
|
2012-12-05 11:33:20 +04:00
|
|
|
|
|
|
|
rv = authCache->GetAuthEntryForPath(scheme, host, port, path,
|
2015-10-29 12:43:00 +03:00
|
|
|
suffix, &entry);
|
2010-06-17 22:33:42 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// if we are trying to add a header for origin server auth and if the
|
|
|
|
// URL contains an explicit username, then try the given username first.
|
|
|
|
// we only want to do this, however, if we know the URL requires auth
|
|
|
|
// based on the presence of an auth cache entry for this URL (which is
|
|
|
|
// true since we are here). but, if the username from the URL matches
|
|
|
|
// the username from the cache, then we should prefer the password
|
|
|
|
// stored in the cache since that is most likely to be valid.
|
|
|
|
if (header == nsHttp::Authorization && entry->Domain()[0] == '\0') {
|
|
|
|
GetIdentityFromURI(0, ident);
|
|
|
|
// if the usernames match, then clear the ident so we will pick
|
|
|
|
// up the one from the auth cache instead.
|
2012-10-24 18:05:13 +04:00
|
|
|
// when this is undesired, specify LOAD_EXPLICIT_CREDENTIALS load
|
|
|
|
// flag.
|
2013-01-04 03:36:16 +04:00
|
|
|
if (nsCRT::strcmp(ident.User(), entry->User()) == 0) {
|
2012-10-24 18:05:13 +04:00
|
|
|
uint32_t loadFlags;
|
|
|
|
if (NS_SUCCEEDED(mAuthChannel->GetLoadFlags(&loadFlags)) &&
|
2013-01-11 05:56:51 +04:00
|
|
|
!(loadFlags & nsIChannel::LOAD_EXPLICIT_CREDENTIALS)) {
|
2012-10-24 18:05:13 +04:00
|
|
|
ident.Clear();
|
|
|
|
}
|
|
|
|
}
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool identFromURI;
|
2010-06-17 22:33:42 +04:00
|
|
|
if (ident.IsEmpty()) {
|
2017-01-12 12:49:03 +03:00
|
|
|
rv = ident.Set(entry->Identity());
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
2011-10-17 18:59:28 +04:00
|
|
|
identFromURI = false;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
else
|
2011-10-17 18:59:28 +04:00
|
|
|
identFromURI = true;
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
nsXPIDLCString temp;
|
|
|
|
const char *creds = entry->Creds();
|
|
|
|
const char *challenge = entry->Challenge();
|
|
|
|
// we can only send a preemptive Authorization header if we have either
|
|
|
|
// stored credentials or a stored challenge from which to derive
|
|
|
|
// credentials. if the identity is from the URI, then we cannot use
|
|
|
|
// the stored credentials.
|
|
|
|
if ((!creds[0] || identFromURI) && challenge[0]) {
|
|
|
|
nsCOMPtr<nsIHttpAuthenticator> auth;
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString unused;
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = GetAuthenticator(challenge, unused, getter_AddRefs(auth));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool proxyAuth = (header == nsHttp::Proxy_Authorization);
|
2010-06-17 22:33:42 +04:00
|
|
|
rv = GenCredsAndSetEntry(auth, proxyAuth, scheme, host, port,
|
|
|
|
path, entry->Realm(), challenge, ident,
|
|
|
|
entry->mMetaData, getter_Copies(temp));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
creds = temp.get();
|
|
|
|
|
|
|
|
// make sure the continuation state is null since we do not
|
|
|
|
// support mixing preemptive and 'multirequest' authentication.
|
|
|
|
NS_IF_RELEASE(*continuationState);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (creds[0]) {
|
|
|
|
LOG((" adding \"%s\" request header\n", header.get()));
|
2017-01-12 12:49:03 +03:00
|
|
|
if (header == nsHttp::Proxy_Authorization) {
|
|
|
|
rv = mAuthChannel->SetProxyCredentials(nsDependentCString(creds));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rv = mAuthChannel->SetWWWCredentials(nsDependentCString(creds));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
2010-06-17 22:33:42 +04:00
|
|
|
|
|
|
|
// suppress defensive auth prompting for this channel since we know
|
|
|
|
// that we already prompted at least once this session. we only do
|
|
|
|
// this for non-proxy auth since the URL's userpass is not used for
|
|
|
|
// proxy auth.
|
|
|
|
if (header == nsHttp::Authorization)
|
2011-10-17 18:59:28 +04:00
|
|
|
mSuppressDefensiveAuth = true;
|
2010-06-17 22:33:42 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ident.Clear(); // don't remember the identity
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHttpChannelAuthProvider::GetCurrentPath(nsACString &path)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIURL> url = do_QueryInterface(mURI);
|
|
|
|
if (url)
|
|
|
|
rv = url->GetDirectory(path);
|
|
|
|
else
|
|
|
|
rv = mURI->GetPath(path);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsHttpChannelAuthProvider, nsICancelable,
|
2016-07-14 10:32:00 +03:00
|
|
|
nsIHttpChannelAuthProvider, nsIAuthPromptCallback, nsIHttpAuthenticatorCallback)
|
2014-01-08 02:05:56 +04:00
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
2014-01-08 02:05:56 +04:00
|
|
|
} // namespace mozilla
|