1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
39 #include "nsPasswordManager.h"
40 #include "nsPassword.h"
43 #include "nsReadableUtils.h"
45 ////////////////////////////////////////////////////////////////////////////////
47 class nsPasswordManagerEnumerator
: public nsISimpleEnumerator
53 nsPasswordManagerEnumerator(PRBool aDecrypt
= PR_TRUE
) :
54 mHostCount(0), mUserCount(0), mDecrypt(aDecrypt
)
58 NS_IMETHOD
HasMoreElements(PRBool
*result
)
60 *result
= SINGSIGN_HostCount() > mHostCount
;
64 NS_IMETHOD
GetNext(nsISupports
**result
)
69 nsresult rv
= SINGSIGN_Enumerate(mHostCount
, mUserCount
++, mDecrypt
, &host
, &user
, &pswd
);
75 if (mUserCount
== SINGSIGN_UserCount(mHostCount
)) {
79 nsIPassword
*password
= new nsPassword(host
, user
, pswd
);
80 // note that memory is handed off to "new nsPassword" in a non-xpcom fashion
81 if (password
== nsnull
) {
85 return NS_ERROR_OUT_OF_MEMORY
;
92 virtual ~nsPasswordManagerEnumerator()
102 NS_IMPL_ISUPPORTS1(nsPasswordManagerEnumerator
, nsISimpleEnumerator
)
104 ////////////////////////////////////////////////////////////////////////////////
106 class nsPasswordManagerRejectEnumerator
: public nsISimpleEnumerator
112 nsPasswordManagerRejectEnumerator() : mRejectCount(0)
116 NS_IMETHOD
HasMoreElements(PRBool
*result
)
118 *result
= SINGSIGN_RejectCount() > mRejectCount
;
122 NS_IMETHOD
GetNext(nsISupports
**result
)
125 nsresult rv
= SINGSIGN_RejectEnumerate(mRejectCount
++, &host
);
130 nsIPassword
*password
= new nsPassword(host
, nsnull
, nsnull
); /* only first argument used */
131 if (password
== nsnull
) {
132 nsMemory::Free(host
);
133 return NS_ERROR_OUT_OF_MEMORY
;
140 virtual ~nsPasswordManagerRejectEnumerator()
145 PRInt32 mRejectCount
;
148 NS_IMPL_ISUPPORTS1(nsPasswordManagerRejectEnumerator
, nsISimpleEnumerator
)
150 ////////////////////////////////////////////////////////////////////////////////
151 // nsPasswordManager Implementation
153 NS_IMPL_ISUPPORTS3(nsPasswordManager
, nsIPasswordManager
, nsIPasswordManagerInternal
, nsISupportsWeakReference
)
155 nsPasswordManager::nsPasswordManager()
159 nsPasswordManager::~nsPasswordManager(void)
164 nsresult
nsPasswordManager::Init()
169 NS_IMETHODIMP
nsPasswordManager::GetEnumerator(nsISimpleEnumerator
* *entries
)
172 nsPasswordManagerEnumerator
* enumerator
= new nsPasswordManagerEnumerator();
173 if (enumerator
== nsnull
) {
174 return NS_ERROR_OUT_OF_MEMORY
;
176 NS_ADDREF(enumerator
);
177 *entries
= enumerator
;
181 NS_IMETHODIMP
nsPasswordManager::AddUser(const nsACString
& aHost
, const nsAString
& aUser
, const nsAString
& aPwd
) {
182 SINGSIGN_StorePassword(PromiseFlatCString(aHost
).get(),
183 PromiseFlatString(aUser
).get(),
184 PromiseFlatString(aPwd
).get());
188 NS_IMETHODIMP
nsPasswordManager::RemoveUser(const nsACString
& aHost
, const nsAString
& aUser
)
190 return ::SINGSIGN_RemoveUser(PromiseFlatCString(aHost
).get(),
191 PromiseFlatString(aUser
).get(), PR_TRUE
);
194 NS_IMETHODIMP
nsPasswordManager::GetRejectEnumerator(nsISimpleEnumerator
* *entries
)
197 nsPasswordManagerRejectEnumerator
* enumerator
= new nsPasswordManagerRejectEnumerator();
198 if (enumerator
== nsnull
) {
199 return NS_ERROR_OUT_OF_MEMORY
;
201 NS_ADDREF(enumerator
);
202 *entries
= enumerator
;
206 NS_IMETHODIMP
nsPasswordManager::RemoveReject(const nsACString
& aHost
)
208 return ::SINGSIGN_RemoveReject(PromiseFlatCString(aHost
).get());
212 nsPasswordManager::FindPasswordEntry
213 (const nsACString
& aHostURI
, const nsAString
& aUsername
, const nsAString
& aPassword
,
214 nsACString
& aHostURIFound
, nsAString
& aUsernameFound
, nsAString
& aPasswordFound
)
217 nsCOMPtr
<nsIPassword
> passwordElem
;
219 nsCOMPtr
<nsISimpleEnumerator
> enumerator
= new nsPasswordManagerEnumerator(PR_FALSE
);
221 return NS_ERROR_OUT_OF_MEMORY
;
224 PRBool hasMoreElements
= PR_FALSE
;
225 enumerator
->HasMoreElements(&hasMoreElements
);
227 // Emumerate through set of saved logins
228 while (hasMoreElements
) {
229 rv
= enumerator
->GetNext(getter_AddRefs(passwordElem
));
235 // Get the contents of this saved login
236 nsCAutoString hostURI
;
237 nsAutoString encUsername
, username
;
238 nsAutoString encPassword
, password
;
240 passwordElem
->GetHost(hostURI
);
241 passwordElem
->GetUser(encUsername
);
242 passwordElem
->GetPassword(encPassword
);
244 // Check for a match with the input parameters, treating null input values as
246 PRBool hostURIOK
= aHostURI
.IsEmpty() || hostURI
.Equals(aHostURI
);
248 rv
= Wallet_Decrypt(encUsername
, username
);
252 rv
= Wallet_Decrypt(encPassword
, password
);
258 PRBool usernameOK
= aUsername
.IsEmpty() || username
.Equals(aUsername
);
259 PRBool passwordOK
= aPassword
.IsEmpty() || password
.Equals(aPassword
);
261 // If a match is found, return success
262 if (hostURIOK
&& usernameOK
&& passwordOK
) {
263 aHostURIFound
= hostURI
;
264 aUsernameFound
= username
;
265 aPasswordFound
= password
;
269 enumerator
->HasMoreElements(&hasMoreElements
);
273 return NS_ERROR_FAILURE
;
276 NS_IMETHODIMP
nsPasswordManager::AddReject(const nsACString
& host
)
278 return ::SINGSIGN_AddReject(PromiseFlatCString(host
).get());
282 nsPasswordManager::AddUserFull(const nsACString
& aKey
,
283 const nsAString
& aUser
,
284 const nsAString
& aPassword
,
285 const nsAString
& aUserFieldName
,
286 const nsAString
& aPassFieldName
)
288 return NS_ERROR_NOT_IMPLEMENTED
;
292 nsPasswordManager::AddUserFull2(const nsACString
& aKey
,
293 const nsAString
& aUser
,
294 const nsAString
& aPassword
,
295 const nsAString
& aUserFieldName
,
296 const nsAString
& aPassFieldName
,
297 const nsACString
& aActionURL
)
299 return NS_ERROR_NOT_IMPLEMENTED
;
302 NS_IMETHODIMP
nsPasswordManager::ReadPasswords(nsIFile
* aPasswordFile
)
304 return NS_ERROR_NOT_IMPLEMENTED
;