Bug 1909986 - For sidebar revamp, only show chatbot entrypoints (context menu, shortc...
[gecko.git] / netwerk / protocol / http / nsHttpBasicAuth.cpp
blobc3de5cb5e8bb6541289845351ae6d4bc8bc206d9
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 "nsCRT.h"
11 #include "nsString.h"
12 #include "mozilla/Base64.h"
13 #include "mozilla/ClearOnShutdown.h"
15 namespace mozilla {
16 namespace net {
18 StaticRefPtr<nsHttpBasicAuth> nsHttpBasicAuth::gSingleton;
20 already_AddRefed<nsIHttpAuthenticator> nsHttpBasicAuth::GetOrCreate() {
21 nsCOMPtr<nsIHttpAuthenticator> authenticator;
22 if (gSingleton) {
23 authenticator = gSingleton;
24 } else {
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 //-----------------------------------------------------------------------------
43 NS_IMETHODIMP
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
50 // have been wrong.
51 *identityInvalid = true;
52 return NS_OK;
54 NS_IMETHODIMP
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;
64 NS_IMETHODIMP
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()));
73 *aFlags = 0;
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);
90 creds = authString;
91 return NS_OK;
94 NS_IMETHODIMP
95 nsHttpBasicAuth::GetAuthFlags(uint32_t* flags) {
96 *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE;
97 return NS_OK;
100 } // namespace net
101 } // namespace mozilla