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