michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: // HttpLog.h should generally be included first michael@0: #include "HttpLog.h" michael@0: michael@0: #include "nsHttpBasicAuth.h" michael@0: #include "plbase64.h" michael@0: #include "nsString.h" michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsHttpBasicAuth michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: nsHttpBasicAuth::nsHttpBasicAuth() michael@0: { michael@0: } michael@0: michael@0: nsHttpBasicAuth::~nsHttpBasicAuth() michael@0: { michael@0: } michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsHttpBasicAuth::nsISupports michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMPL_ISUPPORTS(nsHttpBasicAuth, nsIHttpAuthenticator) michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsHttpBasicAuth::nsIHttpAuthenticator michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: nsHttpBasicAuth::ChallengeReceived(nsIHttpAuthenticableChannel *authChannel, michael@0: const char *challenge, michael@0: bool isProxyAuth, michael@0: nsISupports **sessionState, michael@0: nsISupports **continuationState, michael@0: bool *identityInvalid) michael@0: { michael@0: // if challenged, then the username:password that was sent must michael@0: // have been wrong. michael@0: *identityInvalid = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHttpBasicAuth::GenerateCredentials(nsIHttpAuthenticableChannel *authChannel, michael@0: const char *challenge, michael@0: bool isProxyAuth, michael@0: const char16_t *domain, michael@0: const char16_t *user, michael@0: const char16_t *password, michael@0: nsISupports **sessionState, michael@0: nsISupports **continuationState, michael@0: uint32_t *aFlags, michael@0: char **creds) michael@0: michael@0: { michael@0: LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n", challenge)); michael@0: michael@0: NS_ENSURE_ARG_POINTER(creds); michael@0: michael@0: *aFlags = 0; michael@0: michael@0: // we only know how to deal with Basic auth for http. michael@0: bool isBasicAuth = !PL_strncasecmp(challenge, "basic", 5); michael@0: NS_ENSURE_TRUE(isBasicAuth, NS_ERROR_UNEXPECTED); michael@0: michael@0: // we work with ASCII around here michael@0: nsAutoCString userpass; michael@0: LossyCopyUTF16toASCII(user, userpass); michael@0: userpass.Append(':'); // always send a ':' (see bug 129565) michael@0: if (password) michael@0: LossyAppendUTF16toASCII(password, userpass); michael@0: michael@0: // plbase64.h provides this worst-case output buffer size calculation. michael@0: // use calloc, since PL_Base64Encode does not null terminate. michael@0: *creds = (char *) calloc(6 + ((userpass.Length() + 2)/3)*4 + 1, 1); michael@0: if (!*creds) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: memcpy(*creds, "Basic ", 6); michael@0: PL_Base64Encode(userpass.get(), userpass.Length(), *creds + 6); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsHttpBasicAuth::GetAuthFlags(uint32_t *flags) michael@0: { michael@0: *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE; michael@0: return NS_OK; michael@0: } michael@0: michael@0: } // namespace mozilla::net michael@0: } // namespace mozilla