summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_prompt_promptAuth_proxy.html
blob: 95dd4c7bc737da848fdcfa9a7d99b49321638480 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test promptAuth proxy prompts</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <script type="text/javascript" src="prompt_common.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
<p id="display"></p>

<div id="content" style="display: none">
  <iframe id="iframe"></iframe>
</div>

<pre id="test">
<script class="testbody" type="text/javascript">
var state, action;
var pwmgr;
var proxyLogin;
var isOk;
var mozproxy, proxiedHost = "http://mochi.test:8888";
var proxyChannel;
var systemPrincipal = SpecialPowers.Services.scriptSecurityManager.getSystemPrincipal();
var ioService = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);

var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);

var level = Ci.nsIAuthPrompt2.LEVEL_NONE;

var proxyAuthinfo = {
  username : "",
  password : "",
  domain   : "",

  flags : Ci.nsIAuthInformation.AUTH_PROXY,
  authenticationScheme : "basic",
  realm : ""
};

// Force parent to not look for tab-modal prompts, as they're not used for auth prompts.
isTabModal = false;

const Cc_promptFac = Cc["@mozilla.org/passwordmanager/authpromptfactory;1"];
ok(Cc_promptFac != null, "Access Cc[@mozilla.org/passwordmanager/authpromptfactory;1]");

const Ci_promptFac = Ci.nsIPromptFactory;
ok(Ci_promptFac != null, "Access Ci.nsIPromptFactory");

const promptFac = Cc_promptFac.getService(Ci_promptFac);
ok(promptFac != null, "promptFac getService()");

var prompter2 = promptFac.getPrompt(window, Ci.nsIAuthPrompt2);

function initLogins(pi) {
  pwmgr = Cc["@mozilla.org/login-manager;1"].
          getService(Ci.nsILoginManager);

  mozproxy = "moz-proxy://" + SpecialPowers.wrap(pi).host + ":" +
              SpecialPowers.wrap(pi).port;

  proxyLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].
               createInstance(Ci.nsILoginInfo);

  proxyLogin.init(mozproxy, null, "Proxy Realm",
                  "proxuser", "proxpass", "", "");

  pwmgr.addLogin(proxyLogin);
}

var startupCompleteResolver;
var startupComplete = new Promise(resolve => startupCompleteResolver = resolve);

function proxyChannelListener() { }
proxyChannelListener.prototype = {
  onStartRequest: function(request, context) {
    startupCompleteResolver();
  },
  onStopRequest: function(request, context, status) { }
};

var resolveCallback = SpecialPowers.wrapCallbackObject({
  QueryInterface : function (iid) {
    const interfaces = [Ci.nsIProtocolProxyCallback, Ci.nsISupports];

    if (!interfaces.some( function(v) { return iid.equals(v); } ))
      throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
    return this;
  },

  onProxyAvailable : function (req, uri, pi, status) {
    initLogins(pi);

    // I'm cheating a bit here... We should probably do some magic foo to get
    // something implementing nsIProxiedProtocolHandler and then call
    // NewProxiedChannel(), so we have something that's definately a proxied
    // channel. But Mochitests use a proxy for a number of hosts, so just
    // requesting a normal channel will give us a channel that's proxied.
    // The proxyChannel needs to move to at least on-modify-request to
    // have valid ProxyInfo, but we use OnStartRequest during startup()
    // for simplicity.
    proxyChannel = ioService.newChannel2(proxiedHost,
                                         null,
                                         null,
                                         null,      // aLoadingNode
                                         systemPrincipal,
                                         null,      // aTriggeringPrincipal
                                         Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
                                         Ci.nsIContentPolicy.TYPE_OTHER);
    proxyChannel.asyncOpen2(SpecialPowers.wrapCallbackObject(new proxyChannelListener()));
  }
});

function startup() {
  // Need to allow for arbitrary network servers defined in PAC instead of a hardcoded moz-proxy.
  var ios = SpecialPowers.Cc["@mozilla.org/network/io-service;1"].
    getService(SpecialPowers.Ci.nsIIOService);

  var pps = SpecialPowers.Cc["@mozilla.org/network/protocol-proxy-service;1"].getService();

  var channel = ios.newChannel2("http://example.com",
                                null,
                                null,
                                null,      // aLoadingNode
                                systemPrincipal,
                                null,      // aTriggeringPrincipal
                                Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
                                Ci.nsIContentPolicy.TYPE_OTHER);
  pps.asyncResolve(channel, 0, resolveCallback);
}

startup();

add_task(function* setup() {
  info("Waiting for startup to complete...");
  yield startupComplete;
});

add_task(function* test_noAutologin() {
  // test proxy login (default = no autologin), make sure it prompts.
  state = {
    msg         : "The proxy moz-proxy://127.0.0.1:8888 is requesting a username and password. The site says: “Proxy Realm”",
    title       : "Authentication Required",
    textValue   : "proxuser",
    passValue   : "proxpass",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  action = {
    buttonClick : "ok",
  };
  proxyAuthinfo.username = "";
  proxyAuthinfo.password = "";
  proxyAuthinfo.realm    = "Proxy Realm";
  proxyAuthinfo.flags    = Ci.nsIAuthInformation.AUTH_PROXY;

  var time1 = pwmgr.findLogins({}, mozproxy, null, "Proxy Realm")[0].QueryInterface(Ci.nsILoginMetaInfo).timeLastUsed;
  promptDone = handlePrompt(state, action);
  isOk = prompter2.promptAuth(proxyChannel, level, proxyAuthinfo);
  yield promptDone;
  var time2 = pwmgr.findLogins({}, mozproxy, null, "Proxy Realm")[0].QueryInterface(Ci.nsILoginMetaInfo).timeLastUsed;

  ok(isOk, "Checking dialog return value (accept)");
  isnot(time1, time2, "Checking that timeLastUsed was updated");
  is(proxyAuthinfo.username, "proxuser", "Checking returned username");
  is(proxyAuthinfo.password, "proxpass", "Checking returned password");
});

add_task(function* test_autologin() {
  // test proxy login (with autologin)

  // Enable the autologin pref.
  prefs.setBoolPref("signon.autologin.proxy", true);

  proxyAuthinfo.username = "";
  proxyAuthinfo.password = "";
  proxyAuthinfo.realm    = "Proxy Realm";
  proxyAuthinfo.flags    = Ci.nsIAuthInformation.AUTH_PROXY;

  time1 = pwmgr.findLogins({}, mozproxy, null, "Proxy Realm")[0].QueryInterface(Ci.nsILoginMetaInfo).timeLastUsed;
  isOk = prompter2.promptAuth(proxyChannel, level, proxyAuthinfo);
  time2 = pwmgr.findLogins({}, mozproxy, null, "Proxy Realm")[0].QueryInterface(Ci.nsILoginMetaInfo).timeLastUsed;

  ok(isOk, "Checking dialog return value (accept)");
  isnot(time1, time2, "Checking that timeLastUsed was updated");
  is(proxyAuthinfo.username, "proxuser", "Checking returned username");
  is(proxyAuthinfo.password, "proxpass", "Checking returned password");
});

add_task(function* test_autologin_incorrect() {
  // test proxy login (with autologin), ensure it prompts after a failed auth.
  state = {
    msg         : "The proxy moz-proxy://127.0.0.1:8888 is requesting a username and password. The site says: “Proxy Realm”",
    title       : "Authentication Required",
    textValue   : "proxuser",
    passValue   : "proxpass",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  action = {
    buttonClick : "ok",
  };

  proxyAuthinfo.username = "";
  proxyAuthinfo.password = "";
  proxyAuthinfo.realm    = "Proxy Realm";
  proxyAuthinfo.flags    = (Ci.nsIAuthInformation.AUTH_PROXY | Ci.nsIAuthInformation.PREVIOUS_FAILED);

  time1 = pwmgr.findLogins({}, mozproxy, null, "Proxy Realm")[0].QueryInterface(Ci.nsILoginMetaInfo).timeLastUsed;
  promptDone = handlePrompt(state, action);
  isOk = prompter2.promptAuth(proxyChannel, level, proxyAuthinfo);
  yield promptDone;
  time2 = pwmgr.findLogins({}, mozproxy, null, "Proxy Realm")[0].QueryInterface(Ci.nsILoginMetaInfo).timeLastUsed;

  ok(isOk, "Checking dialog return value (accept)");
  isnot(time1, time2, "Checking that timeLastUsed was updated");
  is(proxyAuthinfo.username, "proxuser", "Checking returned username");
  is(proxyAuthinfo.password, "proxpass", "Checking returned password");
});

add_task(function* test_autologin_private() {
  // test proxy login (with autologin), ensure it prompts in Private Browsing mode.
  state = {
    msg         : "the message",
    title       : "the title",
    textValue   : "proxuser",
    passValue   : "proxpass",
  };
  action = {
    buttonClick : "ok",
  };

  proxyAuthinfo.username = "";
  proxyAuthinfo.password = "";
  proxyAuthinfo.realm    = "Proxy Realm";
  proxyAuthinfo.flags    = Ci.nsIAuthInformation.AUTH_PROXY;

  prefs.clearUserPref("signon.autologin.proxy");

  // XXX check for and kill popup notification??
  // XXX check for checkbox / checkstate on old prompts?
  // XXX check NTLM domain stuff
});
</script>
</pre>
</body>
</html>