Line data Source code
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 :
17 0 : NS_IMPL_ISUPPORTS(nsHttpAuthManager, nsIHttpAuthManager)
18 :
19 0 : nsHttpAuthManager::nsHttpAuthManager()
20 : {
21 0 : }
22 :
23 0 : nsresult nsHttpAuthManager::Init()
24 : {
25 : // get reference to the auth cache. we assume that we will live
26 : // as long as gHttpHandler. instantiate it if necessary.
27 :
28 0 : if (!gHttpHandler) {
29 : nsresult rv;
30 0 : nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
31 0 : if (NS_FAILED(rv))
32 0 : return rv;
33 :
34 0 : nsCOMPtr<nsIProtocolHandler> handler;
35 0 : rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
36 0 : if (NS_FAILED(rv))
37 0 : return rv;
38 :
39 : // maybe someone is overriding our HTTP handler implementation?
40 0 : NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED);
41 : }
42 :
43 0 : mAuthCache = gHttpHandler->AuthCache(false);
44 0 : mPrivateAuthCache = gHttpHandler->AuthCache(true);
45 0 : NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE);
46 0 : NS_ENSURE_TRUE(mPrivateAuthCache, NS_ERROR_FAILURE);
47 0 : return NS_OK;
48 : }
49 :
50 0 : nsHttpAuthManager::~nsHttpAuthManager()
51 : {
52 0 : }
53 :
54 : NS_IMETHODIMP
55 0 : nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme,
56 : const nsACString & aHost,
57 : int32_t aPort,
58 : const nsACString & aAuthType,
59 : const nsACString & aRealm,
60 : const nsACString & aPath,
61 : nsAString & aUserDomain,
62 : nsAString & aUserName,
63 : nsAString & aUserPassword,
64 : bool aIsPrivate,
65 : nsIPrincipal* aPrincipal)
66 : {
67 0 : nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache;
68 0 : nsHttpAuthEntry * entry = nullptr;
69 : nsresult rv;
70 :
71 0 : nsAutoCString originSuffix;
72 0 : if (aPrincipal) {
73 0 : aPrincipal->OriginAttributesRef().CreateSuffix(originSuffix);
74 : }
75 :
76 0 : if (!aPath.IsEmpty())
77 0 : rv = auth_cache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(),
78 0 : PromiseFlatCString(aHost).get(),
79 : aPort,
80 0 : PromiseFlatCString(aPath).get(),
81 : originSuffix,
82 0 : &entry);
83 : else
84 0 : rv = auth_cache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(),
85 0 : PromiseFlatCString(aHost).get(),
86 : aPort,
87 0 : PromiseFlatCString(aRealm).get(),
88 : originSuffix,
89 0 : &entry);
90 :
91 0 : if (NS_FAILED(rv))
92 0 : return rv;
93 0 : if (!entry)
94 0 : return NS_ERROR_UNEXPECTED;
95 :
96 0 : aUserDomain.Assign(entry->Domain());
97 0 : aUserName.Assign(entry->User());
98 0 : aUserPassword.Assign(entry->Pass());
99 0 : return NS_OK;
100 : }
101 :
102 : NS_IMETHODIMP
103 0 : nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme,
104 : const nsACString & aHost,
105 : int32_t aPort,
106 : const nsACString & aAuthType,
107 : const nsACString & aRealm,
108 : const nsACString & aPath,
109 : const nsAString & aUserDomain,
110 : const nsAString & aUserName,
111 : const nsAString & aUserPassword,
112 : bool aIsPrivate,
113 : nsIPrincipal* aPrincipal)
114 : {
115 0 : nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(),
116 0 : PromiseFlatString(aUserName).get(),
117 0 : PromiseFlatString(aUserPassword).get());
118 :
119 0 : nsAutoCString originSuffix;
120 0 : if (aPrincipal) {
121 0 : aPrincipal->OriginAttributesRef().CreateSuffix(originSuffix);
122 : }
123 :
124 0 : nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache;
125 0 : return auth_cache->SetAuthEntry(PromiseFlatCString(aScheme).get(),
126 0 : PromiseFlatCString(aHost).get(),
127 : aPort,
128 0 : PromiseFlatCString(aPath).get(),
129 0 : PromiseFlatCString(aRealm).get(),
130 : nullptr, // credentials
131 : nullptr, // challenge
132 : originSuffix,
133 : &ident,
134 0 : nullptr); // metadata
135 : }
136 :
137 : NS_IMETHODIMP
138 0 : nsHttpAuthManager::ClearAll()
139 : {
140 0 : nsresult rv = mAuthCache->ClearAll();
141 0 : nsresult rv2 = mPrivateAuthCache->ClearAll();
142 0 : if (NS_FAILED(rv))
143 0 : return rv;
144 0 : if (NS_FAILED(rv2))
145 0 : return rv2;
146 0 : return NS_OK;
147 : }
148 :
149 : } // namespace net
150 : } // namespace mozilla
|