Back to index

lightning-sunbird  0.9+nobinonly
nsHttpBasicAuth.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
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Gagan Saksena <gagan@netscape.com> (original author)
00024  *   Mike Shaver <shaver@zeroknowledge.com>
00025  *   Christopher Blizzard <blizzard@mozilla.org>
00026  *   Darin Fisher <darin@netscape.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #include <stdlib.h>
00043 #include "nsHttp.h"
00044 #include "nsHttpBasicAuth.h"
00045 #include "plbase64.h"
00046 #include "plstr.h"
00047 #include "prmem.h"
00048 #include "nsString.h"
00049 
00050 //-----------------------------------------------------------------------------
00051 // nsHttpBasicAuth <public>
00052 //-----------------------------------------------------------------------------
00053 
00054 nsHttpBasicAuth::nsHttpBasicAuth()
00055 {
00056 }
00057 
00058 nsHttpBasicAuth::~nsHttpBasicAuth()
00059 {
00060 }
00061 
00062 //-----------------------------------------------------------------------------
00063 // nsHttpBasicAuth::nsISupports
00064 //-----------------------------------------------------------------------------
00065 
00066 NS_IMPL_ISUPPORTS1(nsHttpBasicAuth, nsIHttpAuthenticator)
00067 
00068 //-----------------------------------------------------------------------------
00069 // nsHttpBasicAuth::nsIHttpAuthenticator
00070 //-----------------------------------------------------------------------------
00071 
00072 NS_IMETHODIMP
00073 nsHttpBasicAuth::ChallengeReceived(nsIHttpChannel *httpChannel,
00074                                    const char *challenge,
00075                                    PRBool isProxyAuth,
00076                                    nsISupports **sessionState,
00077                                    nsISupports **continuationState,
00078                                    PRBool *identityInvalid)
00079 {
00080     // if challenged, then the username:password that was sent must
00081     // have been wrong.
00082     *identityInvalid = PR_TRUE;
00083     return NS_OK;
00084 }
00085 
00086 NS_IMETHODIMP
00087 nsHttpBasicAuth::GenerateCredentials(nsIHttpChannel *httpChannel,
00088                                      const char *challenge,
00089                                      PRBool isProxyAuth,
00090                                      const PRUnichar *domain,
00091                                      const PRUnichar *user,
00092                                      const PRUnichar *password,
00093                                      nsISupports **sessionState,
00094                                      nsISupports **continuationState,
00095                                      char **creds)
00096 
00097 {
00098     LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n", challenge));
00099 
00100     NS_ENSURE_ARG_POINTER(creds);
00101 
00102     // we only know how to deal with Basic auth for http.
00103     PRBool isBasicAuth = !PL_strncasecmp(challenge, "basic", 5);
00104     NS_ENSURE_TRUE(isBasicAuth, NS_ERROR_UNEXPECTED);
00105 
00106     // we work with ASCII around here
00107     nsCAutoString userpass;
00108     LossyCopyUTF16toASCII(user, userpass);
00109     userpass.Append(':'); // always send a ':' (see bug 129565)
00110     if (password)
00111         LossyAppendUTF16toASCII(password, userpass);
00112 
00113     // plbase64.h provides this worst-case output buffer size calculation.
00114     // use calloc, since PL_Base64Encode does not null terminate.
00115     *creds = (char *) calloc(6 + ((userpass.Length() + 2)/3)*4 + 1, 1);
00116     if (!*creds)
00117         return NS_ERROR_OUT_OF_MEMORY;
00118 
00119     memcpy(*creds, "Basic ", 6);
00120     PL_Base64Encode(userpass.get(), userpass.Length(), *creds + 6);
00121     return NS_OK;
00122 }
00123 
00124 NS_IMETHODIMP
00125 nsHttpBasicAuth::GetAuthFlags(nsresult *flags)
00126 {
00127     *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE;
00128     return NS_OK;
00129 }