1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/protocol/http/nsHttpAuthManager.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,153 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +// HttpLog.h should generally be included first 1.10 +#include "HttpLog.h" 1.11 + 1.12 +#include "nsHttpHandler.h" 1.13 +#include "nsHttpAuthManager.h" 1.14 +#include "nsNetUtil.h" 1.15 +#include "nsIPrincipal.h" 1.16 + 1.17 +namespace mozilla { 1.18 +namespace net { 1.19 + 1.20 +NS_IMPL_ISUPPORTS(nsHttpAuthManager, nsIHttpAuthManager) 1.21 + 1.22 +nsHttpAuthManager::nsHttpAuthManager() 1.23 +{ 1.24 +} 1.25 + 1.26 +nsresult nsHttpAuthManager::Init() 1.27 +{ 1.28 + // get reference to the auth cache. we assume that we will live 1.29 + // as long as gHttpHandler. instantiate it if necessary. 1.30 + 1.31 + if (!gHttpHandler) { 1.32 + nsresult rv; 1.33 + nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv); 1.34 + if (NS_FAILED(rv)) 1.35 + return rv; 1.36 + 1.37 + nsCOMPtr<nsIProtocolHandler> handler; 1.38 + rv = ios->GetProtocolHandler("http", getter_AddRefs(handler)); 1.39 + if (NS_FAILED(rv)) 1.40 + return rv; 1.41 + 1.42 + // maybe someone is overriding our HTTP handler implementation? 1.43 + NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED); 1.44 + } 1.45 + 1.46 + mAuthCache = gHttpHandler->AuthCache(false); 1.47 + mPrivateAuthCache = gHttpHandler->AuthCache(true); 1.48 + NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE); 1.49 + NS_ENSURE_TRUE(mPrivateAuthCache, NS_ERROR_FAILURE); 1.50 + return NS_OK; 1.51 +} 1.52 + 1.53 +nsHttpAuthManager::~nsHttpAuthManager() 1.54 +{ 1.55 +} 1.56 + 1.57 +NS_IMETHODIMP 1.58 +nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme, 1.59 + const nsACString & aHost, 1.60 + int32_t aPort, 1.61 + const nsACString & aAuthType, 1.62 + const nsACString & aRealm, 1.63 + const nsACString & aPath, 1.64 + nsAString & aUserDomain, 1.65 + nsAString & aUserName, 1.66 + nsAString & aUserPassword, 1.67 + bool aIsPrivate, 1.68 + nsIPrincipal* aPrincipal) 1.69 +{ 1.70 + nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache; 1.71 + nsHttpAuthEntry * entry = nullptr; 1.72 + nsresult rv; 1.73 + uint32_t appId = NECKO_NO_APP_ID; 1.74 + bool inBrowserElement = false; 1.75 + if (aPrincipal) { 1.76 + appId = aPrincipal->GetAppId(); 1.77 + inBrowserElement = aPrincipal->GetIsInBrowserElement(); 1.78 + } 1.79 + 1.80 + if (!aPath.IsEmpty()) 1.81 + rv = auth_cache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(), 1.82 + PromiseFlatCString(aHost).get(), 1.83 + aPort, 1.84 + PromiseFlatCString(aPath).get(), 1.85 + appId, inBrowserElement, 1.86 + &entry); 1.87 + else 1.88 + rv = auth_cache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(), 1.89 + PromiseFlatCString(aHost).get(), 1.90 + aPort, 1.91 + PromiseFlatCString(aRealm).get(), 1.92 + appId, inBrowserElement, 1.93 + &entry); 1.94 + 1.95 + if (NS_FAILED(rv)) 1.96 + return rv; 1.97 + if (!entry) 1.98 + return NS_ERROR_UNEXPECTED; 1.99 + 1.100 + aUserDomain.Assign(entry->Domain()); 1.101 + aUserName.Assign(entry->User()); 1.102 + aUserPassword.Assign(entry->Pass()); 1.103 + return NS_OK; 1.104 +} 1.105 + 1.106 +NS_IMETHODIMP 1.107 +nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme, 1.108 + const nsACString & aHost, 1.109 + int32_t aPort, 1.110 + const nsACString & aAuthType, 1.111 + const nsACString & aRealm, 1.112 + const nsACString & aPath, 1.113 + const nsAString & aUserDomain, 1.114 + const nsAString & aUserName, 1.115 + const nsAString & aUserPassword, 1.116 + bool aIsPrivate, 1.117 + nsIPrincipal* aPrincipal) 1.118 +{ 1.119 + nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(), 1.120 + PromiseFlatString(aUserName).get(), 1.121 + PromiseFlatString(aUserPassword).get()); 1.122 + 1.123 + uint32_t appId = NECKO_NO_APP_ID; 1.124 + bool inBrowserElement = false; 1.125 + if (aPrincipal) { 1.126 + appId = aPrincipal->GetAppId(); 1.127 + inBrowserElement = aPrincipal->GetIsInBrowserElement(); 1.128 + } 1.129 + 1.130 + nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache; 1.131 + return auth_cache->SetAuthEntry(PromiseFlatCString(aScheme).get(), 1.132 + PromiseFlatCString(aHost).get(), 1.133 + aPort, 1.134 + PromiseFlatCString(aPath).get(), 1.135 + PromiseFlatCString(aRealm).get(), 1.136 + nullptr, // credentials 1.137 + nullptr, // challenge 1.138 + appId, inBrowserElement, 1.139 + &ident, 1.140 + nullptr); // metadata 1.141 +} 1.142 + 1.143 +NS_IMETHODIMP 1.144 +nsHttpAuthManager::ClearAll() 1.145 +{ 1.146 + nsresult rv = mAuthCache->ClearAll(); 1.147 + nsresult rv2 = mPrivateAuthCache->ClearAll(); 1.148 + if (NS_FAILED(rv)) 1.149 + return rv; 1.150 + if (NS_FAILED(rv2)) 1.151 + return rv2; 1.152 + return NS_OK; 1.153 +} 1.154 + 1.155 +} // namespace mozilla::net 1.156 +} // namespace mozilla