nsHttpBasicAuth.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // HttpLog.h should generally be included first
  6. #include "HttpLog.h"
  7. #include "nsHttpBasicAuth.h"
  8. #include "plbase64.h"
  9. #include "plstr.h"
  10. #include "nsString.h"
  11. namespace mozilla {
  12. namespace net {
  13. //-----------------------------------------------------------------------------
  14. // nsHttpBasicAuth <public>
  15. //-----------------------------------------------------------------------------
  16. nsHttpBasicAuth::nsHttpBasicAuth()
  17. {
  18. }
  19. nsHttpBasicAuth::~nsHttpBasicAuth()
  20. {
  21. }
  22. //-----------------------------------------------------------------------------
  23. // nsHttpBasicAuth::nsISupports
  24. //-----------------------------------------------------------------------------
  25. NS_IMPL_ISUPPORTS(nsHttpBasicAuth, nsIHttpAuthenticator)
  26. //-----------------------------------------------------------------------------
  27. // nsHttpBasicAuth::nsIHttpAuthenticator
  28. //-----------------------------------------------------------------------------
  29. NS_IMETHODIMP
  30. nsHttpBasicAuth::ChallengeReceived(nsIHttpAuthenticableChannel *authChannel,
  31. const char *challenge,
  32. bool isProxyAuth,
  33. nsISupports **sessionState,
  34. nsISupports **continuationState,
  35. bool *identityInvalid)
  36. {
  37. // if challenged, then the username:password that was sent must
  38. // have been wrong.
  39. *identityInvalid = true;
  40. return NS_OK;
  41. }
  42. NS_IMETHODIMP
  43. nsHttpBasicAuth::GenerateCredentialsAsync(nsIHttpAuthenticableChannel *authChannel,
  44. nsIHttpAuthenticatorCallback* aCallback,
  45. const char *challenge,
  46. bool isProxyAuth,
  47. const char16_t *domain,
  48. const char16_t *username,
  49. const char16_t *password,
  50. nsISupports *sessionState,
  51. nsISupports *continuationState,
  52. nsICancelable **aCancellable)
  53. {
  54. return NS_ERROR_NOT_IMPLEMENTED;
  55. }
  56. NS_IMETHODIMP
  57. nsHttpBasicAuth::GenerateCredentials(nsIHttpAuthenticableChannel *authChannel,
  58. const char *challenge,
  59. bool isProxyAuth,
  60. const char16_t *domain,
  61. const char16_t *user,
  62. const char16_t *password,
  63. nsISupports **sessionState,
  64. nsISupports **continuationState,
  65. uint32_t *aFlags,
  66. char **creds)
  67. {
  68. LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n", challenge));
  69. NS_ENSURE_ARG_POINTER(creds);
  70. *aFlags = 0;
  71. // we only know how to deal with Basic auth for http.
  72. bool isBasicAuth = !PL_strncasecmp(challenge, "basic", 5);
  73. NS_ENSURE_TRUE(isBasicAuth, NS_ERROR_UNEXPECTED);
  74. // we work with ASCII around here
  75. nsAutoCString userpass;
  76. LossyCopyUTF16toASCII(user, userpass);
  77. userpass.Append(':'); // always send a ':' (see bug 129565)
  78. if (password)
  79. LossyAppendUTF16toASCII(password, userpass);
  80. // plbase64.h provides this worst-case output buffer size calculation.
  81. // use calloc, since PL_Base64Encode does not null terminate.
  82. *creds = (char *) calloc(6 + ((userpass.Length() + 2)/3)*4 + 1, 1);
  83. if (!*creds)
  84. return NS_ERROR_OUT_OF_MEMORY;
  85. memcpy(*creds, "Basic ", 6);
  86. PL_Base64Encode(userpass.get(), userpass.Length(), *creds + 6);
  87. return NS_OK;
  88. }
  89. NS_IMETHODIMP
  90. nsHttpBasicAuth::GetAuthFlags(uint32_t *flags)
  91. {
  92. *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE;
  93. return NS_OK;
  94. }
  95. } // namespace net
  96. } // namespace mozilla