netwerk/protocol/http/nsHttpBasicAuth.cpp

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:02505c666407
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 "nsString.h"
12
13 namespace mozilla {
14 namespace net {
15
16 //-----------------------------------------------------------------------------
17 // nsHttpBasicAuth <public>
18 //-----------------------------------------------------------------------------
19
20 nsHttpBasicAuth::nsHttpBasicAuth()
21 {
22 }
23
24 nsHttpBasicAuth::~nsHttpBasicAuth()
25 {
26 }
27
28 //-----------------------------------------------------------------------------
29 // nsHttpBasicAuth::nsISupports
30 //-----------------------------------------------------------------------------
31
32 NS_IMPL_ISUPPORTS(nsHttpBasicAuth, nsIHttpAuthenticator)
33
34 //-----------------------------------------------------------------------------
35 // nsHttpBasicAuth::nsIHttpAuthenticator
36 //-----------------------------------------------------------------------------
37
38 NS_IMETHODIMP
39 nsHttpBasicAuth::ChallengeReceived(nsIHttpAuthenticableChannel *authChannel,
40 const char *challenge,
41 bool isProxyAuth,
42 nsISupports **sessionState,
43 nsISupports **continuationState,
44 bool *identityInvalid)
45 {
46 // if challenged, then the username:password that was sent must
47 // have been wrong.
48 *identityInvalid = true;
49 return NS_OK;
50 }
51
52 NS_IMETHODIMP
53 nsHttpBasicAuth::GenerateCredentials(nsIHttpAuthenticableChannel *authChannel,
54 const char *challenge,
55 bool isProxyAuth,
56 const char16_t *domain,
57 const char16_t *user,
58 const char16_t *password,
59 nsISupports **sessionState,
60 nsISupports **continuationState,
61 uint32_t *aFlags,
62 char **creds)
63
64 {
65 LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n", challenge));
66
67 NS_ENSURE_ARG_POINTER(creds);
68
69 *aFlags = 0;
70
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
75 // we work with ASCII around here
76 nsAutoCString userpass;
77 LossyCopyUTF16toASCII(user, userpass);
78 userpass.Append(':'); // always send a ':' (see bug 129565)
79 if (password)
80 LossyAppendUTF16toASCII(password, userpass);
81
82 // plbase64.h provides this worst-case output buffer size calculation.
83 // use calloc, since PL_Base64Encode does not null terminate.
84 *creds = (char *) calloc(6 + ((userpass.Length() + 2)/3)*4 + 1, 1);
85 if (!*creds)
86 return NS_ERROR_OUT_OF_MEMORY;
87
88 memcpy(*creds, "Basic ", 6);
89 PL_Base64Encode(userpass.get(), userpass.Length(), *creds + 6);
90 return NS_OK;
91 }
92
93 NS_IMETHODIMP
94 nsHttpBasicAuth::GetAuthFlags(uint32_t *flags)
95 {
96 *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE;
97 return NS_OK;
98 }
99
100 } // namespace mozilla::net
101 } // namespace mozilla

mercurial