Back to index

lightning-sunbird  0.9+nobinonly
nsJVMAuthTools.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.org code.
00016  *
00017  * The Initial Developer of the Original Code is Sun Microsystem.
00018  * Portions created by Sun Microsystem are Copyright (C) 2003 by
00019  * Sun Microsystem. All Rights Reserved.
00020  *    
00021  * Contributor(s):
00022  *   Joshua Xia <joshua.xia@sun.com>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the NPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the NPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "plstr.h"
00039 #include "nsNetUtil.h"
00040 #include "nsJVMAuthTools.h"
00041 #include "nsIHttpAuthManager.h"
00042 
00043 static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
00044 static NS_DEFINE_IID(kIJVMAUTHTOOLSIID, NS_IJVMAUTHTOOLS_IID);
00045 static NS_DEFINE_CID(kHttpAuthManagerCID, NS_HTTPAUTHMANAGER_CID);
00046 
00047 //---------------------------------------------------
00048 // implementation of interface nsIAuthenticationInfo
00049 // --------------------------------------------------
00050 NS_IMPL_ISUPPORTS1(nsAuthenticationInfoImp, nsIAuthenticationInfo)
00051 
00052 nsAuthenticationInfoImp::nsAuthenticationInfoImp(char* username,
00053                                                  char* password)
00054 {
00055     mUserName = username;
00056     mPassWord = password;
00057 }
00058 
00059 nsAuthenticationInfoImp::~nsAuthenticationInfoImp()
00060 {
00061     if (mUserName)
00062         nsMemory::Free(mUserName);
00063         
00064     if (mPassWord)
00065         nsMemory::Free(mPassWord);
00066 }
00067 
00068 /* readonly attribute string username; */
00069 NS_IMETHODIMP nsAuthenticationInfoImp::GetUsername(const char * *aUsername)
00070 {
00071     *aUsername = mUserName;
00072     return NS_OK;
00073 }
00074 
00075 /* readonly attribute string password; */
00076 NS_IMETHODIMP nsAuthenticationInfoImp::GetPassword(const char * *aPassword)
00077 {
00078     *aPassword = mPassWord;
00079     return NS_OK;
00080 }
00081 
00082 
00083 //---------------------------------------------------
00084 // implementation of interface nsIJVMAuthTools
00085 // --------------------------------------------------
00086 NS_IMPL_AGGREGATED(nsJVMAuthTools)
00087 
00088 nsJVMAuthTools::nsJVMAuthTools(nsISupports* outer)
00089 {
00090     NS_INIT_AGGREGATED(outer);
00091 }
00092 
00093 nsJVMAuthTools::~nsJVMAuthTools(void)
00094 {
00095 }
00096 
00097 NS_METHOD
00098 nsJVMAuthTools::Create(nsISupports* outer,
00099                        const nsIID& aIID,
00100                        void* *aInstancePtr)
00101 {
00102     if (!aInstancePtr)
00103         return NS_ERROR_INVALID_POINTER;
00104     *aInstancePtr = nsnull;
00105 
00106     if (outer && !aIID.Equals(kISupportsIID))
00107         return NS_ERROR_INVALID_ARG; 
00108 
00109     nsJVMAuthTools* authtools = new nsJVMAuthTools(outer);
00110     if (authtools == nsnull)
00111         return NS_ERROR_OUT_OF_MEMORY;
00112 
00113     nsresult rv = authtools->AggregatedQueryInterface(aIID, aInstancePtr);
00114     if(NS_FAILED(rv))
00115         delete authtools;
00116 
00117     return rv;
00118 }
00119 
00120 NS_METHOD
00121 nsJVMAuthTools::AggregatedQueryInterface(const nsIID& aIID,
00122                                          void** aInstancePtr)
00123 {
00124     if (aIID.Equals(kIJVMAUTHTOOLSIID)) {
00125         *aInstancePtr = this;
00126         AddRef();
00127         return NS_OK;
00128     }
00129     
00130     if (aIID.Equals(kISupportsIID)) {
00131         *aInstancePtr = GetInner();
00132         NS_ADDREF((nsISupports*)*aInstancePtr);
00133         return NS_OK;
00134     }
00135     return NS_NOINTERFACE;
00136 }
00137 
00138 NS_METHOD
00139 nsJVMAuthTools::GetAuthenticationInfo(const char* protocol,
00140                                       const char* host,
00141                                       PRInt32 port, 
00142                                       const char* scheme,
00143                                       const char* realm,
00144                                       nsIAuthenticationInfo **_retval)
00145 {
00146     if (!protocol || !host || !scheme || !realm)
00147         return NS_ERROR_INVALID_ARG;
00148 
00149     if (!PL_strcasecmp(protocol, "HTTP") && !PL_strcasecmp(protocol, "HTTPS"))
00150         return NS_ERROR_INVALID_ARG;
00151 
00152     nsCOMPtr<nsIHttpAuthManager> authManager = do_GetService(kHttpAuthManagerCID);
00153     if (!authManager)
00154         return NS_ERROR_FAILURE;
00155     
00156     nsDependentCString protocolString(protocol);
00157     nsDependentCString hostString(host);
00158     nsDependentCString schemeString(scheme);
00159     nsDependentCString realmString(realm);
00160     nsAutoString       domainString, username, password;
00161     
00162     nsresult rv = authManager->GetAuthIdentity(protocolString,
00163                                                hostString,
00164                                                port,
00165                                                schemeString,
00166                                                realmString,
00167                                                EmptyCString(), 
00168                                                domainString,
00169                                                username,
00170                                                password);
00171     if (NS_FAILED(rv))
00172         return NS_ERROR_FAILURE;
00173 
00174     nsAuthenticationInfoImp* authInfo = new nsAuthenticationInfoImp(
00175                                             ToNewUTF8String(username),
00176                                             ToNewUTF8String(password));
00177     NS_ENSURE_TRUE(authInfo, NS_ERROR_OUT_OF_MEMORY);
00178     NS_ADDREF(authInfo);
00179     *_retval = authInfo;
00180     
00181     return NS_OK;
00182 }
00183 
00184 NS_METHOD
00185 nsJVMAuthTools::SetAuthenticationInfo(const char* protocol,
00186                                       const char* host,
00187                                       PRInt32 port, 
00188                                       const char* scheme,
00189                                       const char* realm,
00190                                       const char *username,
00191                                       const char *password)
00192 {
00193     if (!protocol || !host || !scheme || !realm)
00194         return NS_ERROR_INVALID_ARG;
00195     
00196     if (!PL_strcasecmp(protocol, "HTTP") && !PL_strcasecmp(protocol, "HTTPS"))
00197         return NS_ERROR_INVALID_ARG;
00198 
00199     nsCOMPtr<nsIHttpAuthManager> authManager = do_GetService(kHttpAuthManagerCID);
00200     if (!authManager)
00201         return NS_ERROR_FAILURE;
00202     
00203     nsDependentCString protocolString(protocol);
00204     nsDependentCString hostString(host);
00205     nsDependentCString schemeString(scheme);
00206     nsDependentCString realmString(realm);
00207     
00208     nsresult rv = authManager->SetAuthIdentity(protocolString,
00209                                                hostString,
00210                                                port,
00211                                                schemeString,
00212                                                realmString,
00213                                                EmptyCString(),
00214                                                EmptyString(), 
00215                                                NS_ConvertUTF8toUCS2(username),
00216                                                NS_ConvertUTF8toUCS2(password));
00217     return rv;
00218 }
00219