Back to index

lightning-sunbird  0.9+nobinonly
nsSingleSignonPrompt.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Password Manager.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Brian Ryner.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Brian Ryner <bryner@brianryner.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsSingleSignonPrompt.h"
00040 #include "nsPasswordManager.h"
00041 #include "nsIServiceManager.h"
00042 
00043 NS_IMPL_ISUPPORTS2(nsSingleSignonPrompt,
00044                    nsIAuthPromptWrapper,
00045                    nsIAuthPrompt)
00046 
00047 // nsIAuthPrompt
00048 NS_IMETHODIMP
00049 nsSingleSignonPrompt::Prompt(const PRUnichar* aDialogTitle,
00050                              const PRUnichar* aText,
00051                              const PRUnichar* aPasswordRealm,
00052                              PRUint32 aSavePassword,
00053                              const PRUnichar* aDefaultText,
00054                              PRUnichar** aResult,
00055                              PRBool* aConfirm)
00056 {
00057   nsAutoString checkMsg;
00058   nsString emptyString;
00059   PRBool checkValue = PR_FALSE;
00060   PRBool *checkPtr = nsnull;
00061   PRUnichar* value = nsnull;
00062   nsCOMPtr<nsIPasswordManagerInternal> mgrInternal;
00063 
00064   if (nsPasswordManager::SingleSignonEnabled() && aPasswordRealm) {
00065     if (aSavePassword == SAVE_PASSWORD_PERMANENTLY) {
00066       nsPasswordManager::GetLocalizedString(NS_LITERAL_STRING("rememberValue"),
00067                                             checkMsg);
00068       checkPtr = &checkValue;
00069     }
00070 
00071     mgrInternal = do_GetService(NS_PASSWORDMANAGER_CONTRACTID);
00072     nsCAutoString outHost;
00073     nsAutoString outUser, outPassword;
00074 
00075     mgrInternal->FindPasswordEntry(NS_ConvertUCS2toUTF8(aPasswordRealm),
00076                                    emptyString,
00077                                    emptyString,
00078                                    outHost,
00079                                    outUser,
00080                                    outPassword);
00081 
00082     if (!outUser.IsEmpty()) {
00083       value = ToNewUnicode(outUser);
00084       checkValue = PR_TRUE;
00085     }
00086   }
00087 
00088   if (!value && aDefaultText)
00089     value = ToNewUnicode(nsDependentString(aDefaultText));
00090 
00091   mPrompt->Prompt(aDialogTitle,
00092                   aText,
00093                   &value,
00094                   checkMsg.get(),
00095                   checkPtr,
00096                   aConfirm);
00097 
00098   if (*aConfirm) {
00099     if (checkValue && value && value[0] != '\0') {
00100       // The user requested that we save the value
00101       // TODO: support SAVE_PASSWORD_FOR_SESSION
00102 
00103       nsCOMPtr<nsIPasswordManager> manager = do_QueryInterface(mgrInternal);
00104 
00105       manager->AddUser(NS_ConvertUCS2toUTF8(aPasswordRealm),
00106                        nsDependentString(value),
00107                        emptyString);
00108     }
00109 
00110     *aResult = value;
00111   } else {
00112     if (value)
00113       nsMemory::Free(value);
00114     *aResult = nsnull;
00115   }
00116 
00117   return NS_OK;
00118 }
00119 
00120 NS_IMETHODIMP
00121 nsSingleSignonPrompt::PromptUsernameAndPassword(const PRUnichar* aDialogTitle,
00122                                                 const PRUnichar* aText,
00123                                                 const PRUnichar* aPasswordRealm,
00124                                                 PRUint32 aSavePassword,
00125                                                 PRUnichar** aUser,
00126                                                 PRUnichar** aPassword,
00127                                                 PRBool* aConfirm)
00128 {
00129   nsAutoString checkMsg;
00130   nsString emptyString;
00131   PRBool checkValue = PR_FALSE;
00132   PRBool *checkPtr = nsnull;
00133   PRUnichar *user = nsnull, *password = nsnull;
00134   nsCOMPtr<nsIPasswordManagerInternal> mgrInternal;
00135 
00136   if (nsPasswordManager::SingleSignonEnabled() && aPasswordRealm) {
00137     if (aSavePassword == SAVE_PASSWORD_PERMANENTLY) {
00138       nsPasswordManager::GetLocalizedString(NS_LITERAL_STRING("rememberPassword"),
00139                                             checkMsg);
00140       checkPtr = &checkValue;
00141     }
00142 
00143     mgrInternal = do_GetService(NS_PASSWORDMANAGER_CONTRACTID);
00144     nsCAutoString outHost;
00145     nsAutoString outUser, outPassword;
00146 
00147     mgrInternal->FindPasswordEntry(NS_ConvertUCS2toUTF8(aPasswordRealm),
00148                                    emptyString,
00149                                    emptyString,
00150                                    outHost,
00151                                    outUser,
00152                                    outPassword);
00153 
00154     user = ToNewUnicode(outUser);
00155     password = ToNewUnicode(outPassword);
00156     if (!outUser.IsEmpty() || !outPassword.IsEmpty())
00157       checkValue = PR_TRUE;
00158   }
00159 
00160   mPrompt->PromptUsernameAndPassword(aDialogTitle,
00161                                      aText,
00162                                      &user,
00163                                      &password,
00164                                      checkMsg.get(),
00165                                      checkPtr,
00166                                      aConfirm);
00167 
00168   if (*aConfirm) {
00169     if (checkValue && user && password && (user[0] != '\0' || password[0] != '\0')) {
00170       // The user requested that we save the values
00171       // TODO: support SAVE_PASSWORD_FOR_SESSION
00172 
00173       nsCOMPtr<nsIPasswordManager> manager = do_QueryInterface(mgrInternal);
00174 
00175       manager->AddUser(NS_ConvertUCS2toUTF8(aPasswordRealm),
00176                        nsDependentString(user),
00177                        nsDependentString(password));
00178     }
00179 
00180     *aUser = user;
00181     *aPassword = password;
00182 
00183   } else {
00184     if (user)
00185       nsMemory::Free(user);
00186     if (password)
00187       nsMemory::Free(password);
00188 
00189     *aUser = *aPassword = nsnull;
00190   }
00191 
00192   return NS_OK;
00193 }
00194 
00195 NS_IMETHODIMP
00196 nsSingleSignonPrompt::PromptPassword(const PRUnichar* aDialogTitle,
00197                                      const PRUnichar* aText,
00198                                      const PRUnichar* aPasswordRealm,
00199                                      PRUint32 aSavePassword,
00200                                      PRUnichar** aPassword,
00201                                      PRBool* aConfirm)
00202 {
00203   nsAutoString checkMsg;
00204   nsString emptyString;
00205   PRBool checkValue = PR_FALSE;
00206   PRBool *checkPtr = nsnull;
00207   PRUnichar* password = nsnull;
00208   nsCOMPtr<nsIPasswordManagerInternal> mgrInternal;
00209 
00210   if (nsPasswordManager::SingleSignonEnabled() && aPasswordRealm) {
00211     if (aSavePassword == SAVE_PASSWORD_PERMANENTLY) {
00212       nsPasswordManager::GetLocalizedString(NS_LITERAL_STRING("rememberPassword"),
00213                                             checkMsg);
00214       checkPtr = &checkValue;
00215     }
00216 
00217     mgrInternal = do_GetService(NS_PASSWORDMANAGER_CONTRACTID);
00218     nsCAutoString outHost;
00219     nsAutoString outUser, outPassword;
00220 
00221     mgrInternal->FindPasswordEntry(NS_ConvertUCS2toUTF8(aPasswordRealm),
00222                                    emptyString,
00223                                    emptyString,
00224                                    outHost,
00225                                    outUser,
00226                                    outPassword);
00227 
00228     password = ToNewUnicode(outPassword);
00229     if (!outPassword.IsEmpty())
00230       checkValue = PR_TRUE;
00231   }
00232 
00233   mPrompt->PromptPassword(aDialogTitle,
00234                           aText,
00235                           &password,
00236                           checkMsg.get(),
00237                           checkPtr,
00238                           aConfirm);
00239 
00240   if (*aConfirm) {
00241     if (checkValue && password && password[0] != '\0') {
00242       // The user requested that we save the password
00243       // TODO: support SAVE_PASSWORD_FOR_SESSION
00244 
00245       nsCOMPtr<nsIPasswordManager> manager = do_QueryInterface(mgrInternal);
00246 
00247       manager->AddUser(NS_ConvertUCS2toUTF8(aPasswordRealm),
00248                        emptyString,
00249                        nsDependentString(password));
00250     }
00251 
00252     *aPassword = password;
00253 
00254   } else {
00255     if (password)
00256       nsMemory::Free(password);
00257     *aPassword = nsnull;
00258   }
00259 
00260   return NS_OK;
00261 }
00262 
00263 // nsIAuthPromptWrapper
00264 
00265 NS_IMETHODIMP
00266 nsSingleSignonPrompt::SetPromptDialogs(nsIPrompt* aDialogs)
00267 {
00268   mPrompt = aDialogs;
00269   return NS_OK;
00270 }