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