nsHttpBasicAuth.cpp (3549B)
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/. */ 5 6 // HttpLog.h should generally be included first 7 #include "HttpLog.h" 8 9 #include "nsHttpBasicAuth.h" 10 #include "nsCRT.h" 11 #include "nsString.h" 12 #include "mozilla/Base64.h" 13 #include "mozilla/ClearOnShutdown.h" 14 15 namespace mozilla { 16 namespace net { 17 18 StaticRefPtr<nsHttpBasicAuth> nsHttpBasicAuth::gSingleton; 19 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; 28 } 29 30 return authenticator.forget(); 31 } 32 33 //----------------------------------------------------------------------------- 34 // nsHttpBasicAuth::nsISupports 35 //----------------------------------------------------------------------------- 36 37 NS_IMPL_ISUPPORTS(nsHttpBasicAuth, nsIHttpAuthenticator) 38 39 //----------------------------------------------------------------------------- 40 // nsHttpBasicAuth::nsIHttpAuthenticator 41 //----------------------------------------------------------------------------- 42 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; 53 } 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; 62 } 63 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())); 72 73 *aFlags = 0; 74 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); 79 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); 85 86 nsAutoCString authString{"Basic "_ns}; 87 nsresult rv = Base64EncodeAppend(userpass, authString); 88 NS_ENSURE_SUCCESS(rv, rv); 89 90 creds = authString; 91 return NS_OK; 92 } 93 94 NS_IMETHODIMP 95 nsHttpBasicAuth::GetAuthFlags(uint32_t* flags) { 96 *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE; 97 return NS_OK; 98 } 99 100 } // namespace net 101 } // namespace mozilla