1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/protocol/http/nsHttpBasicAuth.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,101 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +// HttpLog.h should generally be included first 1.10 +#include "HttpLog.h" 1.11 + 1.12 +#include "nsHttpBasicAuth.h" 1.13 +#include "plbase64.h" 1.14 +#include "nsString.h" 1.15 + 1.16 +namespace mozilla { 1.17 +namespace net { 1.18 + 1.19 +//----------------------------------------------------------------------------- 1.20 +// nsHttpBasicAuth <public> 1.21 +//----------------------------------------------------------------------------- 1.22 + 1.23 +nsHttpBasicAuth::nsHttpBasicAuth() 1.24 +{ 1.25 +} 1.26 + 1.27 +nsHttpBasicAuth::~nsHttpBasicAuth() 1.28 +{ 1.29 +} 1.30 + 1.31 +//----------------------------------------------------------------------------- 1.32 +// nsHttpBasicAuth::nsISupports 1.33 +//----------------------------------------------------------------------------- 1.34 + 1.35 +NS_IMPL_ISUPPORTS(nsHttpBasicAuth, nsIHttpAuthenticator) 1.36 + 1.37 +//----------------------------------------------------------------------------- 1.38 +// nsHttpBasicAuth::nsIHttpAuthenticator 1.39 +//----------------------------------------------------------------------------- 1.40 + 1.41 +NS_IMETHODIMP 1.42 +nsHttpBasicAuth::ChallengeReceived(nsIHttpAuthenticableChannel *authChannel, 1.43 + const char *challenge, 1.44 + bool isProxyAuth, 1.45 + nsISupports **sessionState, 1.46 + nsISupports **continuationState, 1.47 + bool *identityInvalid) 1.48 +{ 1.49 + // if challenged, then the username:password that was sent must 1.50 + // have been wrong. 1.51 + *identityInvalid = true; 1.52 + return NS_OK; 1.53 +} 1.54 + 1.55 +NS_IMETHODIMP 1.56 +nsHttpBasicAuth::GenerateCredentials(nsIHttpAuthenticableChannel *authChannel, 1.57 + const char *challenge, 1.58 + bool isProxyAuth, 1.59 + const char16_t *domain, 1.60 + const char16_t *user, 1.61 + const char16_t *password, 1.62 + nsISupports **sessionState, 1.63 + nsISupports **continuationState, 1.64 + uint32_t *aFlags, 1.65 + char **creds) 1.66 + 1.67 +{ 1.68 + LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n", challenge)); 1.69 + 1.70 + NS_ENSURE_ARG_POINTER(creds); 1.71 + 1.72 + *aFlags = 0; 1.73 + 1.74 + // we only know how to deal with Basic auth for http. 1.75 + bool isBasicAuth = !PL_strncasecmp(challenge, "basic", 5); 1.76 + NS_ENSURE_TRUE(isBasicAuth, NS_ERROR_UNEXPECTED); 1.77 + 1.78 + // we work with ASCII around here 1.79 + nsAutoCString userpass; 1.80 + LossyCopyUTF16toASCII(user, userpass); 1.81 + userpass.Append(':'); // always send a ':' (see bug 129565) 1.82 + if (password) 1.83 + LossyAppendUTF16toASCII(password, userpass); 1.84 + 1.85 + // plbase64.h provides this worst-case output buffer size calculation. 1.86 + // use calloc, since PL_Base64Encode does not null terminate. 1.87 + *creds = (char *) calloc(6 + ((userpass.Length() + 2)/3)*4 + 1, 1); 1.88 + if (!*creds) 1.89 + return NS_ERROR_OUT_OF_MEMORY; 1.90 + 1.91 + memcpy(*creds, "Basic ", 6); 1.92 + PL_Base64Encode(userpass.get(), userpass.Length(), *creds + 6); 1.93 + return NS_OK; 1.94 +} 1.95 + 1.96 +NS_IMETHODIMP 1.97 +nsHttpBasicAuth::GetAuthFlags(uint32_t *flags) 1.98 +{ 1.99 + *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE; 1.100 + return NS_OK; 1.101 +} 1.102 + 1.103 +} // namespace mozilla::net 1.104 +} // namespace mozilla