Back to index

lightning-sunbird  0.9+nobinonly
nsHttpAuthManager.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  *   Louie Zhao <louie.zhao@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 MPL, 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 MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsHttpHandler.h"
00039 #include "nsHttpChannel.h"
00040 #include "nsHttpAuthManager.h"
00041 #include "nsReadableUtils.h"
00042 #include "nsNetUtil.h"
00043 
00044 NS_IMPL_ISUPPORTS1(nsHttpAuthManager, nsIHttpAuthManager)
00045 
00046 nsHttpAuthManager::nsHttpAuthManager()
00047 {
00048 }
00049 
00050 nsresult nsHttpAuthManager::Init()
00051 {
00052   // get reference to the auth cache.  we assume that we will live
00053   // as long as gHttpHandler.  instantiate it if necessary.
00054 
00055   if (!gHttpHandler) {
00056     nsresult rv;
00057     nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
00058     if (NS_FAILED(rv))
00059       return rv;
00060 
00061     nsCOMPtr<nsIProtocolHandler> handler;
00062     rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
00063     if (NS_FAILED(rv))
00064       return rv;
00065 
00066     // maybe someone is overriding our HTTP handler implementation?
00067     NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED);
00068   }
00069        
00070   mAuthCache = gHttpHandler->AuthCache();
00071   NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE);
00072   return NS_OK;
00073 }
00074 
00075 nsHttpAuthManager::~nsHttpAuthManager()
00076 {
00077 }
00078 
00079 NS_IMETHODIMP
00080 nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme,
00081                                    const nsACString & aHost,
00082                                    PRInt32 aPort,
00083                                    const nsACString & aAuthType,
00084                                    const nsACString & aRealm,
00085                                    const nsACString & aPath,
00086                                    nsAString & aUserDomain,
00087                                    nsAString & aUserName,
00088                                    nsAString & aUserPassword)
00089 {
00090   nsHttpAuthEntry * entry = nsnull;
00091   nsresult rv;
00092   if (!aPath.IsEmpty())
00093     rv = mAuthCache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(),
00094                                          PromiseFlatCString(aHost).get(),
00095                                          aPort,
00096                                          PromiseFlatCString(aPath).get(),
00097                                          &entry);
00098   else
00099     rv = mAuthCache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(),
00100                                            PromiseFlatCString(aHost).get(),
00101                                            aPort,
00102                                            PromiseFlatCString(aRealm).get(),
00103                                            &entry);
00104 
00105   if (NS_FAILED(rv))
00106     return rv;
00107   if (!entry)
00108     return NS_ERROR_UNEXPECTED;
00109 
00110   aUserDomain.Assign(entry->Domain());
00111   aUserName.Assign(entry->User());
00112   aUserPassword.Assign(entry->Pass());
00113   return NS_OK;
00114 }
00115 
00116 NS_IMETHODIMP
00117 nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme,
00118                                    const nsACString & aHost,
00119                                    PRInt32 aPort,
00120                                    const nsACString & aAuthType,
00121                                    const nsACString & aRealm,
00122                                    const nsACString & aPath,
00123                                    const nsAString & aUserDomain,
00124                                    const nsAString & aUserName,
00125                                    const nsAString & aUserPassword)
00126 {
00127   nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(),
00128                            PromiseFlatString(aUserName).get(),
00129                            PromiseFlatString(aUserPassword).get());
00130 
00131   return mAuthCache->SetAuthEntry(PromiseFlatCString(aScheme).get(),
00132                                   PromiseFlatCString(aHost).get(),
00133                                   aPort,
00134                                   PromiseFlatCString(aPath).get(),
00135                                   PromiseFlatCString(aRealm).get(),
00136                                   nsnull,  // credentials
00137                                   nsnull,  // challenge
00138                                   ident,
00139                                   nsnull); // metadata
00140 }
00141 
00142 NS_IMETHODIMP
00143 nsHttpAuthManager::ClearAll()
00144 {
00145   return mAuthCache->ClearAll();
00146 }