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
9 #include "nsHttpBasicAuth.h"
12 #include "mozilla/Base64.h"
13 #include "mozilla/ClearOnShutdown.h"
18 StaticRefPtr
<nsHttpBasicAuth
> nsHttpBasicAuth::gSingleton
;
20 already_AddRefed
<nsIHttpAuthenticator
> nsHttpBasicAuth::GetOrCreate() {
21 nsCOMPtr
<nsIHttpAuthenticator
> authenticator
;
23 authenticator
= gSingleton
;
25 gSingleton
= new nsHttpBasicAuth();
26 ClearOnShutdown(&gSingleton
);
27 authenticator
= gSingleton
;
30 return authenticator
.forget();
33 //-----------------------------------------------------------------------------
34 // nsHttpBasicAuth::nsISupports
35 //-----------------------------------------------------------------------------
37 NS_IMPL_ISUPPORTS(nsHttpBasicAuth
, nsIHttpAuthenticator
)
39 //-----------------------------------------------------------------------------
40 // nsHttpBasicAuth::nsIHttpAuthenticator
41 //-----------------------------------------------------------------------------
44 nsHttpBasicAuth::ChallengeReceived(nsIHttpAuthenticableChannel
* authChannel
,
45 const nsACString
& challenge
,
46 bool isProxyAuth
, nsISupports
** sessionState
,
47 nsISupports
** continuationState
,
48 bool* identityInvalid
) {
49 // if challenged, then the username:password that was sent must
51 *identityInvalid
= true;
55 nsHttpBasicAuth::GenerateCredentialsAsync(
56 nsIHttpAuthenticableChannel
* authChannel
,
57 nsIHttpAuthenticatorCallback
* aCallback
, const nsACString
& aChallenge
,
58 bool isProxyAuth
, const nsAString
& domain
, const nsAString
& username
,
59 const nsAString
& password
, nsISupports
* sessionState
,
60 nsISupports
* continuationState
, nsICancelable
** aCancellable
) {
61 return NS_ERROR_NOT_IMPLEMENTED
;
65 nsHttpBasicAuth::GenerateCredentials(
66 nsIHttpAuthenticableChannel
* authChannel
, const nsACString
& aChallenge
,
67 bool isProxyAuth
, const nsAString
& domain
, const nsAString
& user
,
68 const nsAString
& password
, nsISupports
** sessionState
,
69 nsISupports
** continuationState
, uint32_t* aFlags
, nsACString
& creds
) {
70 LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n",
71 aChallenge
.BeginReading()));
75 // we only know how to deal with Basic auth for http.
76 bool isBasicAuth
= StringBeginsWith(aChallenge
, "basic"_ns
,
77 nsCaseInsensitiveCStringComparator
);
78 NS_ENSURE_TRUE(isBasicAuth
, NS_ERROR_UNEXPECTED
);
80 // we work with UTF-8 around here
81 nsAutoCString userpass
;
82 CopyUTF16toUTF8(user
, userpass
);
83 userpass
.Append(':'); // always send a ':' (see bug 129565)
84 AppendUTF16toUTF8(password
, userpass
);
86 nsAutoCString authString
{"Basic "_ns
};
87 nsresult rv
= Base64EncodeAppend(userpass
, authString
);
88 NS_ENSURE_SUCCESS(rv
, rv
);
95 nsHttpBasicAuth::GetAuthFlags(uint32_t* flags
) {
96 *flags
= REQUEST_BASED
| REUSABLE_CREDENTIALS
| REUSABLE_CHALLENGE
;
101 } // namespace mozilla