Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/netwerk/protocol/http/nsHttpAuthManager.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 "nsHttpHandler.h"
10
#include "nsHttpAuthManager.h"
11
#include "nsNetUtil.h"
12
#include "nsIPrincipal.h"
13
14
namespace mozilla {
15
namespace net {
16
17
NS_IMPL_ISUPPORTS(nsHttpAuthManager, nsIHttpAuthManager)
18
19
nsHttpAuthManager::nsHttpAuthManager()
20
  : mAuthCache(nullptr)
21
  , mPrivateAuthCache(nullptr)
22
0
{
23
0
}
24
25
nsresult nsHttpAuthManager::Init()
26
0
{
27
0
  // get reference to the auth cache.  we assume that we will live
28
0
  // as long as gHttpHandler.  instantiate it if necessary.
29
0
30
0
  if (!gHttpHandler) {
31
0
    nsresult rv;
32
0
    nsCOMPtr<nsIIOService> ios = do_GetIOService(&rv);
33
0
    if (NS_FAILED(rv))
34
0
      return rv;
35
0
36
0
    nsCOMPtr<nsIProtocolHandler> handler;
37
0
    rv = ios->GetProtocolHandler("http", getter_AddRefs(handler));
38
0
    if (NS_FAILED(rv))
39
0
      return rv;
40
0
41
0
    // maybe someone is overriding our HTTP handler implementation?
42
0
    NS_ENSURE_TRUE(gHttpHandler, NS_ERROR_UNEXPECTED);
43
0
  }
44
0
45
0
  mAuthCache = gHttpHandler->AuthCache(false);
46
0
  mPrivateAuthCache = gHttpHandler->AuthCache(true);
47
0
  NS_ENSURE_TRUE(mAuthCache, NS_ERROR_FAILURE);
48
0
  NS_ENSURE_TRUE(mPrivateAuthCache, NS_ERROR_FAILURE);
49
0
  return NS_OK;
50
0
}
51
52
NS_IMETHODIMP
53
nsHttpAuthManager::GetAuthIdentity(const nsACString & aScheme,
54
                                   const nsACString & aHost,
55
                                   int32_t aPort,
56
                                   const nsACString & aAuthType,
57
                                   const nsACString & aRealm,
58
                                   const nsACString & aPath,
59
                                   nsAString & aUserDomain,
60
                                   nsAString & aUserName,
61
                                   nsAString & aUserPassword,
62
                                   bool aIsPrivate,
63
                                   nsIPrincipal* aPrincipal)
64
0
{
65
0
  nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache;
66
0
  nsHttpAuthEntry * entry = nullptr;
67
0
  nsresult rv;
68
0
69
0
  nsAutoCString originSuffix;
70
0
  if (aPrincipal) {
71
0
    aPrincipal->OriginAttributesRef().CreateSuffix(originSuffix);
72
0
  }
73
0
74
0
  if (!aPath.IsEmpty())
75
0
    rv = auth_cache->GetAuthEntryForPath(PromiseFlatCString(aScheme).get(),
76
0
                                         PromiseFlatCString(aHost).get(),
77
0
                                         aPort,
78
0
                                         PromiseFlatCString(aPath).get(),
79
0
                                         originSuffix,
80
0
                                         &entry);
81
0
  else
82
0
    rv = auth_cache->GetAuthEntryForDomain(PromiseFlatCString(aScheme).get(),
83
0
                                           PromiseFlatCString(aHost).get(),
84
0
                                           aPort,
85
0
                                           PromiseFlatCString(aRealm).get(),
86
0
                                           originSuffix,
87
0
                                           &entry);
88
0
89
0
  if (NS_FAILED(rv))
90
0
    return rv;
91
0
  if (!entry)
92
0
    return NS_ERROR_UNEXPECTED;
93
0
94
0
  aUserDomain.Assign(entry->Domain());
95
0
  aUserName.Assign(entry->User());
96
0
  aUserPassword.Assign(entry->Pass());
97
0
  return NS_OK;
98
0
}
99
100
NS_IMETHODIMP
101
nsHttpAuthManager::SetAuthIdentity(const nsACString & aScheme,
102
                                   const nsACString & aHost,
103
                                   int32_t aPort,
104
                                   const nsACString & aAuthType,
105
                                   const nsACString & aRealm,
106
                                   const nsACString & aPath,
107
                                   const nsAString & aUserDomain,
108
                                   const nsAString & aUserName,
109
                                   const nsAString & aUserPassword,
110
                                   bool aIsPrivate,
111
                                   nsIPrincipal* aPrincipal)
112
0
{
113
0
  nsHttpAuthIdentity ident(PromiseFlatString(aUserDomain).get(),
114
0
                           PromiseFlatString(aUserName).get(),
115
0
                           PromiseFlatString(aUserPassword).get());
116
0
117
0
  nsAutoCString originSuffix;
118
0
  if (aPrincipal) {
119
0
    aPrincipal->OriginAttributesRef().CreateSuffix(originSuffix);
120
0
  }
121
0
122
0
  nsHttpAuthCache* auth_cache = aIsPrivate ? mPrivateAuthCache : mAuthCache;
123
0
  return auth_cache->SetAuthEntry(PromiseFlatCString(aScheme).get(),
124
0
                                  PromiseFlatCString(aHost).get(),
125
0
                                  aPort,
126
0
                                  PromiseFlatCString(aPath).get(),
127
0
                                  PromiseFlatCString(aRealm).get(),
128
0
                                  nullptr,  // credentials
129
0
                                  nullptr,  // challenge
130
0
                                  originSuffix,
131
0
                                  &ident,
132
0
                                  nullptr); // metadata
133
0
}
134
135
NS_IMETHODIMP
136
nsHttpAuthManager::ClearAll()
137
0
{
138
0
  nsresult rv = mAuthCache->ClearAll();
139
0
  nsresult rv2 = mPrivateAuthCache->ClearAll();
140
0
  if (NS_FAILED(rv))
141
0
    return rv;
142
0
  if (NS_FAILED(rv2))
143
0
    return rv2;
144
0
  return NS_OK;
145
0
}
146
147
} // namespace net
148
} // namespace mozilla