Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/http/nsHttpBasicAuth.cpp
Line
Count
Source (jump to first uncovered line)
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 "plstr.h"
11
#include "nsString.h"
12
#include "mozilla/Base64.h"
13
14
namespace mozilla {
15
namespace net {
16
17
//-----------------------------------------------------------------------------
18
// nsHttpBasicAuth::nsISupports
19
//-----------------------------------------------------------------------------
20
21
NS_IMPL_ISUPPORTS(nsHttpBasicAuth, nsIHttpAuthenticator)
22
23
//-----------------------------------------------------------------------------
24
// nsHttpBasicAuth::nsIHttpAuthenticator
25
//-----------------------------------------------------------------------------
26
27
NS_IMETHODIMP
28
nsHttpBasicAuth::ChallengeReceived(nsIHttpAuthenticableChannel *authChannel,
29
                                   const char *challenge,
30
                                   bool isProxyAuth,
31
                                   nsISupports **sessionState,
32
                                   nsISupports **continuationState,
33
                                   bool *identityInvalid)
34
0
{
35
0
    // if challenged, then the username:password that was sent must
36
0
    // have been wrong.
37
0
    *identityInvalid = true;
38
0
    return NS_OK;
39
0
}
40
NS_IMETHODIMP
41
nsHttpBasicAuth::GenerateCredentialsAsync(nsIHttpAuthenticableChannel *authChannel,
42
                                          nsIHttpAuthenticatorCallback* aCallback,
43
                                          const char *challenge,
44
                                          bool isProxyAuth,
45
                                          const char16_t *domain,
46
                                          const char16_t *username,
47
                                          const char16_t *password,
48
                                          nsISupports *sessionState,
49
                                          nsISupports *continuationState,
50
                                          nsICancelable **aCancellable)
51
0
{
52
0
  return NS_ERROR_NOT_IMPLEMENTED;
53
0
}
54
55
NS_IMETHODIMP
56
nsHttpBasicAuth::GenerateCredentials(nsIHttpAuthenticableChannel *authChannel,
57
                                     const char *challenge,
58
                                     bool isProxyAuth,
59
                                     const char16_t *domain,
60
                                     const char16_t *user,
61
                                     const char16_t *password,
62
                                     nsISupports **sessionState,
63
                                     nsISupports **continuationState,
64
                                     uint32_t *aFlags,
65
                                     char **creds)
66
67
0
{
68
0
    LOG(("nsHttpBasicAuth::GenerateCredentials [challenge=%s]\n", challenge));
69
0
70
0
    NS_ENSURE_ARG_POINTER(creds);
71
0
72
0
    *aFlags = 0;
73
0
74
0
    // we only know how to deal with Basic auth for http.
75
0
    bool isBasicAuth = !PL_strncasecmp(challenge, "basic", 5);
76
0
    NS_ENSURE_TRUE(isBasicAuth, NS_ERROR_UNEXPECTED);
77
0
78
0
    // we work with UTF-8 around here
79
0
    nsAutoCString userpass;
80
0
    CopyUTF16toUTF8(mozilla::MakeStringSpan(user), userpass);
81
0
    userpass.Append(':'); // always send a ':' (see bug 129565)
82
0
    AppendUTF16toUTF8(mozilla::MakeStringSpan(password), userpass);
83
0
84
0
    nsAutoCString authString;
85
0
    nsresult rv = Base64Encode(userpass, authString);
86
0
    NS_ENSURE_SUCCESS(rv, rv);
87
0
88
0
    authString.InsertLiteral("Basic ", 0);
89
0
90
0
    *creds = ToNewCString(authString);
91
0
    return NS_OK;
92
0
}
93
94
NS_IMETHODIMP
95
nsHttpBasicAuth::GetAuthFlags(uint32_t *flags)
96
0
{
97
0
    *flags = REQUEST_BASED | REUSABLE_CREDENTIALS | REUSABLE_CHALLENGE;
98
0
    return NS_OK;
99
0
}
100
101
} // namespace net
102
} // namespace mozilla