netwerk/protocol/http/nsHttpBasicAuth.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 // HttpLog.h should generally be included first
michael@0 7 #include "HttpLog.h"
michael@0 8
michael@0 9 #include "nsHttpBasicAuth.h"
michael@0 10 #include "plbase64.h"
michael@0 11 #include "nsString.h"
michael@0 12
michael@0 13 namespace mozilla {
michael@0 14 namespace net {
michael@0 15
michael@0 16 //-----------------------------------------------------------------------------
michael@0 17 // nsHttpBasicAuth <public>
michael@0 18 //-----------------------------------------------------------------------------
michael@0 19
michael@0 20 nsHttpBasicAuth::nsHttpBasicAuth()
michael@0 21 {
michael@0 22 }
michael@0 23
michael@0 24 nsHttpBasicAuth::~nsHttpBasicAuth()
michael@0 25 {
michael@0 26 }
michael@0 27
michael@0 28 //-----------------------------------------------------------------------------
michael@0 29 // nsHttpBasicAuth::nsISupports
michael@0 30 //-----------------------------------------------------------------------------
michael@0 31
michael@0 32 NS_IMPL_ISUPPORTS(nsHttpBasicAuth, nsIHttpAuthenticator)
michael@0 33
michael@0 34 //-----------------------------------------------------------------------------
michael@0 35 // nsHttpBasicAuth::nsIHttpAuthenticator
michael@0 36 //-----------------------------------------------------------------------------
michael@0 37
michael@0 38 NS_IMETHODIMP
michael@0 39 nsHttpBasicAuth::ChallengeReceived(nsIHttpAuthenticableChannel *authChannel,
michael@0 40 const char *challenge,
michael@0 41 bool isProxyAuth,
michael@0 42 nsISupports **sessionState,
michael@0 43 nsISupports **continuationState,
michael@0 44 bool *identityInvalid)
michael@0 45 {
michael@0 46 // if challenged, then the username:password that was sent must
michael@0 47 // have been wrong.
michael@0 48 *identityInvalid = true;
michael@0 49 return NS_OK;
michael@0 50 }
michael@0 51
michael@0 52 NS_IMETHODIMP
michael@0 53 nsHttpBasicAuth::GenerateCredentials(nsIHttpAuthenticableChannel *authChannel,
michael@0 54 const char *challenge,
michael@0 55 bool isProxyAuth,
michael@0 56 const char16_t *domain,
michael@0 57 const char16_t *user,
michael@0 58 const char16_t *password,
michael@0 59 nsISupports **sessionState,
michael@0 60 nsISupports **continuationState,
michael@0 61 uint32_t *aFlags,
michael@0 62 char **creds)
michael@0 63
michael@0 64 {
michael@0 65 LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n", challenge));
michael@0 66
michael@0 67 NS_ENSURE_ARG_POINTER(creds);
michael@0 68
michael@0 69 *aFlags = 0;
michael@0 70
michael@0 71 // we only know how to deal with Basic auth for http.
michael@0 72 bool isBasicAuth = !PL_strncasecmp(challenge, "basic", 5);
michael@0 73 NS_ENSURE_TRUE(isBasicAuth, NS_ERROR_UNEXPECTED);
michael@0 74
michael@0 75 // we work with ASCII around here
michael@0 76 nsAutoCString userpass;
michael@0 77 LossyCopyUTF16toASCII(user, userpass);
michael@0 78 userpass.Append(':'); // always send a ':' (see bug 129565)
michael@0 79 if (password)
michael@0 80 LossyAppendUTF16toASCII(password, userpass);
michael@0 81
michael@0 82 // plbase64.h provides this worst-case output buffer size calculation.
michael@0 83 // use calloc, since PL_Base64Encode does not null terminate.
michael@0 84 *creds = (char *) calloc(6 + ((userpass.Length() + 2)/3)*4 + 1, 1);
michael@0 85 if (!*creds)
michael@0 86 return NS_ERROR_OUT_OF_MEMORY;
michael@0 87
michael@0 88 memcpy(*creds, "Basic ", 6);
michael@0 89 PL_Base64Encode(userpass.get(), userpass.Length(), *creds + 6);
michael@0 90 return NS_OK;
michael@0 91 }
michael@0 92
michael@0 93 NS_IMETHODIMP
michael@0 94 nsHttpBasicAuth::GetAuthFlags(uint32_t *flags)
michael@0 95 {
michael@0 96 *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE;
michael@0 97 return NS_OK;
michael@0 98 }
michael@0 99
michael@0 100 } // namespace mozilla::net
michael@0 101 } // namespace mozilla

mercurial