summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_prompt_http.html
blob: 0dc8fdf9cb32776a8b21f9ce4c96bf955960489d (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test HTTP auth prompts by loading authenticate.sjs</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 iframe = document.getElementById("iframe");

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

const AUTHENTICATE_PATH = new URL("authenticate.sjs", window.location.href).pathname;

let chromeScript = runInParent(SimpleTest.getTestFileURL("pwmgr_common.js"));

runInParent(() => {
  const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
  Cu.import("resource://gre/modules/Services.jsm");

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

  let login3A, login3B, login4;
  login3A = Cc["@mozilla.org/login-manager/loginInfo;1"].
            createInstance(Ci.nsILoginInfo);
  login3B = Cc["@mozilla.org/login-manager/loginInfo;1"].
            createInstance(Ci.nsILoginInfo);
  login4  = Cc["@mozilla.org/login-manager/loginInfo;1"].
            createInstance(Ci.nsILoginInfo);
  let httpUpgradeLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].
                         createInstance(Ci.nsILoginInfo);
  let httpsDowngradeLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].
                            createInstance(Ci.nsILoginInfo);
  let dedupeHttpUpgradeLogin  = Cc["@mozilla.org/login-manager/loginInfo;1"].
                                createInstance(Ci.nsILoginInfo);
  let dedupeHttpsUpgradeLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].
                                createInstance(Ci.nsILoginInfo);


  login3A.init("http://mochi.test:8888", null, "mochitest",
               "mochiuser1", "mochipass1", "", "");
  login3B.init("http://mochi.test:8888", null, "mochitest2",
               "mochiuser2", "mochipass2", "", "");
  login4.init("http://mochi.test:8888", null, "mochitest3",
              "mochiuser3", "mochipass3-old", "", "");
  // Logins to test scheme upgrades (allowed) and downgrades (disallowed)
  httpUpgradeLogin.init("http://example.com", null, "schemeUpgrade",
                        "httpUser", "httpPass", "", "");
  httpsDowngradeLogin.init("https://example.com", null, "schemeDowngrade",
                           "httpsUser", "httpsPass", "", "");
  // HTTP and HTTPS version of the same domain and realm but with different passwords.
  dedupeHttpUpgradeLogin.init("http://example.org", null, "schemeUpgradeDedupe",
                              "dedupeUser", "httpPass", "", "");
  dedupeHttpsUpgradeLogin.init("https://example.org", null, "schemeUpgradeDedupe",
                               "dedupeUser", "httpsPass", "", "");


  pwmgr.addLogin(login3A);
  pwmgr.addLogin(login3B);
  pwmgr.addLogin(login4);
  pwmgr.addLogin(httpUpgradeLogin);
  pwmgr.addLogin(httpsDowngradeLogin);
  pwmgr.addLogin(dedupeHttpUpgradeLogin);
  pwmgr.addLogin(dedupeHttpsUpgradeLogin);
});

add_task(function* test_iframe() {
  let state = {
    msg         : "http://mochi.test:8888 is requesting your username and password. The site says: “mochitest”",
    title       : "Authentication Required",
    textValue   : "mochiuser1",
    passValue   : "mochipass1",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  let action = {
    buttonClick : "ok",
  };
  promptDone = handlePrompt(state, action);

  // The following tests are driven by iframe loads

  var iframeLoaded = onloadPromiseFor("iframe");
  iframe.src = "authenticate.sjs?user=mochiuser1&pass=mochipass1";
  yield promptDone;
  yield iframeLoaded;
  checkEchoedAuthInfo({user: "mochiuser1", pass: "mochipass1"},
                      iframe.contentDocument);

  state = {
    msg         : "http://mochi.test:8888 is requesting your username and password. The site says: “mochitest2”",
    title       : "Authentication Required",
    textValue   : "mochiuser2",
    passValue   : "mochipass2",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  action = {
    buttonClick : "ok",
  };
  promptDone = handlePrompt(state, action);
  // We've already authenticated to this host:port. For this next
  // request, the existing auth should be sent, we'll get a 401 reply,
  // and we should prompt for new auth.
  iframeLoaded = onloadPromiseFor("iframe");
  iframe.src = "authenticate.sjs?user=mochiuser2&pass=mochipass2&realm=mochitest2";
  yield promptDone;
  yield iframeLoaded;
  checkEchoedAuthInfo({user: "mochiuser2", pass: "mochipass2"},
                      iframe.contentDocument);

  // Now make a load that requests the realm from test 1000. It was
  // already provided there, so auth will *not* be prompted for -- the
  // networking layer already knows it!
  iframeLoaded = onloadPromiseFor("iframe");
  iframe.src = "authenticate.sjs?user=mochiuser1&pass=mochipass1";
  yield iframeLoaded;
  checkEchoedAuthInfo({user: "mochiuser1", pass: "mochipass1"},
                      iframe.contentDocument);

  // Same realm we've already authenticated to, but with a different
  // expected password (to trigger an auth prompt, and change-password
  // popup notification).
  state = {
    msg         : "http://mochi.test:8888 is requesting your username and password. The site says: “mochitest”",
    title       : "Authentication Required",
    textValue   : "mochiuser1",
    passValue   : "mochipass1",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  action = {
    buttonClick : "ok",
    passField   : "mochipass1-new",
  };
  promptDone = handlePrompt(state, action);
  iframeLoaded = onloadPromiseFor("iframe");
  let promptShownPromise = promisePromptShown("passwordmgr-prompt-change");
  iframe.src = "authenticate.sjs?user=mochiuser1&pass=mochipass1-new";
  yield promptDone;
  yield iframeLoaded;
  checkEchoedAuthInfo({user: "mochiuser1", pass: "mochipass1-new"},
                      iframe.contentDocument);
  yield promptShownPromise;

  // Same as last test, but for a realm we haven't already authenticated
  // to (but have an existing saved login for, so that we'll trigger
  // a change-password popup notification.
  state = {
    msg         : "http://mochi.test:8888 is requesting your username and password. The site says: “mochitest3”",
    title       : "Authentication Required",
    textValue   : "mochiuser3",
    passValue   : "mochipass3-old",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  action = {
    buttonClick : "ok",
    passField   : "mochipass3-new",
  };
  promptDone = handlePrompt(state, action);
  iframeLoaded = onloadPromiseFor("iframe");
  promptShownPromise = promisePromptShown("passwordmgr-prompt-change");
  iframe.src = "authenticate.sjs?user=mochiuser3&pass=mochipass3-new&realm=mochitest3";
  yield promptDone;
  yield iframeLoaded;
  checkEchoedAuthInfo({user: "mochiuser3", pass: "mochipass3-new"},
                      iframe.contentDocument);
  yield promptShownPromise;

  // Housekeeping: Delete login4 to test the save prompt in the next test.
  runInParent(() => {
    const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
    Cu.import("resource://gre/modules/Services.jsm");

    var tmpLogin = Cc["@mozilla.org/login-manager/loginInfo;1"].
                   createInstance(Ci.nsILoginInfo);
    tmpLogin.init("http://mochi.test:8888", null, "mochitest3",
                  "mochiuser3", "mochipass3-old", "", "");
    Services.logins.removeLogin(tmpLogin);

    // Clear cached auth from this subtest, and avoid leaking due to bug 459620.
    var authMgr = Cc['@mozilla.org/network/http-auth-manager;1'].
                  getService(Ci.nsIHttpAuthManager);
    authMgr.clearAll();
  });

  state = {
    msg         : "http://mochi.test:8888 is requesting your username and password. The site says: “mochitest3”",
    title       : "Authentication Required",
    textValue   : "",
    passValue   : "",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  action = {
    buttonClick : "ok",
    textField   : "mochiuser3",
    passField   : "mochipass3-old",
  };
  // Trigger a new prompt, so we can test adding a new login.
  promptDone = handlePrompt(state, action);

  iframeLoaded = onloadPromiseFor("iframe");
  promptShownPromise = promisePromptShown("passwordmgr-prompt-save");
  iframe.src = "authenticate.sjs?user=mochiuser3&pass=mochipass3-old&realm=mochitest3";
  yield promptDone;
  yield iframeLoaded;
  checkEchoedAuthInfo({user: "mochiuser3", pass: "mochipass3-old"},
                      iframe.contentDocument);
  yield promptShownPromise;
});

add_task(function* test_schemeUpgrade() {
  let state = {
    msg         : "https://example.com is requesting your username and password. " +
                  "WARNING: Your password will not be sent to the website you are currently visiting!",
    title       : "Authentication Required",
    textValue   : "httpUser",
    passValue   : "httpPass",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  let action = {
    buttonClick : "ok",
  };
  let promptDone = handlePrompt(state, action);

  // The following tests are driven by iframe loads

  let iframeLoaded = onloadPromiseFor("iframe");
  iframe.src = "https://example.com" + AUTHENTICATE_PATH +
               "?user=httpUser&pass=httpPass&realm=schemeUpgrade";
  yield promptDone;
  yield iframeLoaded;
  checkEchoedAuthInfo({user: "httpUser", pass: "httpPass"},
                      SpecialPowers.wrap(iframe).contentDocument);
});

add_task(function* test_schemeDowngrade() {
  let state = {
    msg         : "http://example.com is requesting your username and password. " +
                  "WARNING: Your password will not be sent to the website you are currently visiting!",
    title       : "Authentication Required",
    textValue   : "", // empty because we shouldn't downgrade
    passValue   : "",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  let action = {
    buttonClick : "cancel",
  };
  let promptDone = handlePrompt(state, action);

  // The following tests are driven by iframe loads

  let iframeLoaded = onloadPromiseFor("iframe");
  iframe.src = "http://example.com" + AUTHENTICATE_PATH +
               "?user=unused&pass=unused&realm=schemeDowngrade";
  yield promptDone;
  yield iframeLoaded;
});

add_task(function* test_schemeUpgrade_dedupe() {
  let state = {
    msg         : "https://example.org is requesting your username and password. " +
                  "WARNING: Your password will not be sent to the website you are currently visiting!",
    title       : "Authentication Required",
    textValue   : "dedupeUser",
    passValue   : "httpsPass",
    iconClass   : "authentication-icon question-icon",
    titleHidden : true,
    textHidden  : false,
    passHidden  : false,
    checkHidden : true,
    checkMsg    : "",
    checked     : false,
    focused     : "textField",
    defButton   : "button0",
  };
  let action = {
    buttonClick : "ok",
  };
  let promptDone = handlePrompt(state, action);

  // The following tests are driven by iframe loads

  let iframeLoaded = onloadPromiseFor("iframe");
  iframe.src = "https://example.org" + AUTHENTICATE_PATH +
               "?user=dedupeUser&pass=httpsPass&realm=schemeUpgradeDedupe";
  yield promptDone;
  yield iframeLoaded;
  checkEchoedAuthInfo({user: "dedupeUser", pass: "httpsPass"},
                      SpecialPowers.wrap(iframe).contentDocument);
});
</script>
</pre>
</body>
</html>