1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is Sun Microsystem.
18 : * Portions created by Sun Microsystem are Copyright (C) 2003 by
19 : * Sun Microsystem. All Rights Reserved.
20 : *
21 : * Contributor(s):
22 : * Louie Zhao <louie.zhao@sun.com>
23 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either the GNU General Public License Version 2 or later (the "GPL"), or
26 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "nsHttpHandler.h"
39 : #include "nsHttpChannel.h"
40 : #include "nsHttpAuthManager.h"
41 : #include "nsReadableUtils.h"
42 : #include "nsNetUtil.h"
43 :
44 2016 : NS_IMPL_ISUPPORTS1(nsHttpAuthManager, nsIHttpAuthManager)
45 :
46 36 : nsHttpAuthManager::nsHttpAuthManager()
47 : {
48 36 : }
49 :
50 36 : nsresult nsHttpAuthManager::Init()
51 : {
52 : // get reference to the auth cache. we assume that we will live
53 : // as long as gHttpHandler. instantiate it if necessary.
54 :
55 36 : if (!gHttpHandler) {
56 : nsresult rv;
57 38 : nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
58 19 : if (NS_FAILED(rv))
59 0 : return rv;
60 :
61 38 : nsCOMPtr<nsIProtocolHandler> handler;
62 19 : rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
63 19 : if (NS_FAILED(rv))
64 0 : return rv;
65 :
66 : // maybe someone is overriding our HTTP handler implementation?
67 19 : NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED);
68 : }
69 :
70 36 : mAuthCache = gHttpHandler->AuthCache();
71 36 : NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE);
72 36 : return NS_OK;
73 : }
74 :
75 72 : nsHttpAuthManager::~nsHttpAuthManager()
76 : {
77 144 : }
78 :
79 : NS_IMETHODIMP
80 8 : nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme,
81 : const nsACString & aHost,
82 : PRInt32 aPort,
83 : const nsACString & aAuthType,
84 : const nsACString & aRealm,
85 : const nsACString & aPath,
86 : nsAString & aUserDomain,
87 : nsAString & aUserName,
88 : nsAString & aUserPassword)
89 : {
90 8 : nsHttpAuthEntry * entry = nsnull;
91 : nsresult rv;
92 8 : if (!aPath.IsEmpty())
93 0 : rv = mAuthCache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(),
94 0 : PromiseFlatCString(aHost).get(),
95 : aPort,
96 0 : PromiseFlatCString(aPath).get(),
97 0 : &entry);
98 : else
99 8 : rv = mAuthCache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(),
100 8 : PromiseFlatCString(aHost).get(),
101 : aPort,
102 8 : PromiseFlatCString(aRealm).get(),
103 8 : &entry);
104 :
105 8 : if (NS_FAILED(rv))
106 4 : return rv;
107 4 : if (!entry)
108 0 : return NS_ERROR_UNEXPECTED;
109 :
110 4 : aUserDomain.Assign(entry->Domain());
111 4 : aUserName.Assign(entry->User());
112 4 : aUserPassword.Assign(entry->Pass());
113 4 : return NS_OK;
114 : }
115 :
116 : NS_IMETHODIMP
117 4 : nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme,
118 : const nsACString & aHost,
119 : PRInt32 aPort,
120 : const nsACString & aAuthType,
121 : const nsACString & aRealm,
122 : const nsACString & aPath,
123 : const nsAString & aUserDomain,
124 : const nsAString & aUserName,
125 : const nsAString & aUserPassword)
126 : {
127 4 : nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(),
128 4 : PromiseFlatString(aUserName).get(),
129 12 : PromiseFlatString(aUserPassword).get());
130 :
131 4 : return mAuthCache->SetAuthEntry(PromiseFlatCString(aScheme).get(),
132 4 : PromiseFlatCString(aHost).get(),
133 : aPort,
134 4 : PromiseFlatCString(aPath).get(),
135 4 : PromiseFlatCString(aRealm).get(),
136 : nsnull, // credentials
137 : nsnull, // challenge
138 : &ident,
139 4 : nsnull); // metadata
140 : }
141 :
142 : NS_IMETHODIMP
143 158 : nsHttpAuthManager::ClearAll()
144 : {
145 158 : return mAuthCache->ClearAll();
146 : }
|