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