summaryrefslogtreecommitdiffstats
path: root/toolkit/components/passwordmgr/test/mochitest/test_prompt_promptAuth_proxy.html
blob: 6f78d62f1b10d655da53240bf3592b26dac3c19d (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test promptAuth proxy prompts</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="pwmgr_common.js"></script>
  <script type="text/javascript" src="../../../prompts/test/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">
/* eslint-disable mozilla/use-chromeutils-generateqi */

const LEVEL = Ci.nsIAuthPrompt2.LEVEL_NONE;

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

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

// Let prompt_common know what kind of modal type is enabled for auth prompts.
modalType = authPromptModalType;

let chromeScript = runInParent(() => {
  const promptFac = Cc[
    "@mozilla.org/passwordmanager/authpromptfactory;1"
  ].getService(Ci.nsIPromptFactory);

  let systemPrincipal = Services.scriptSecurityManager.getSystemPrincipal();

  let chromeWin = Services.wm.getMostRecentWindow("navigator:browser");
  let prompter2 = promptFac.getPrompt(chromeWin, Ci.nsIAuthPrompt2);
  prompter2.QueryInterface(Ci.nsILoginManagerAuthPrompter).browser =
    chromeWin.gBrowser.selectedBrowser;

  let mozproxyURL;
  let proxyChannel;

  addMessageListener("init", () => init());

  addMessageListener("proxyPrompter", function onMessage(msg) {
    let args = [...msg.args];

    args[0] = proxyChannel;
    let rv = prompter2[msg.methodName](...args);
    return {
      rv,
      // Send the args back to content so out/inout args can be checked.
      args: msg.args,
    };
  });

  addMessageListener("getTimeLastUsed", async () => {
    let logins = await Services.logins.searchLoginsAsync({ origin: mozproxyURL, httpRealm: "Proxy Realm"});
    return logins[0].QueryInterface(Ci.nsILoginMetaInfo).timeLastUsed;
  });

  function init() {
    // Need to allow for arbitrary network servers defined in PAC instead of a hardcoded moz-proxy.
    let pps = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService();

    let channel = Services.io.newChannel(
      "http://example.com",
      null,
      null,
      null, // aLoadingNode
      systemPrincipal,
      null, // aTriggeringPrincipal
      Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
      Ci.nsIContentPolicy.TYPE_OTHER
    );
    pps.asyncResolve(channel, 0, resolveCallback);
  }

  class ProxyChannelListener {
    onStartRequest(request) {
      sendAsyncMessage("initDone");
    }
    onStopRequest(request, status) {}
  }

  async function initLogins(pi) {
    mozproxyURL = `moz-proxy://${pi.host}:${pi.port}`;

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

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

    await Services.logins.addLoginAsync(proxyLogin);
  }

  let resolveCallback = {
    QueryInterface(iid) {
      const interfaces = [Ci.nsIProtocolProxyCallback, Ci.nsISupports];

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

    async onProxyAvailable(req, uri, pi, status) {
      await 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 = Services.io.newChannel(
        "http://mochi.test:8888",
        null,
        null,
        null, // aLoadingNode
        systemPrincipal,
        null, // aTriggeringPrincipal
        Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
        Ci.nsIContentPolicy.TYPE_OTHER
      );
      proxyChannel.asyncOpen(new ProxyChannelListener());
    },
  };
});

let prompter2 = new PrompterProxy(chromeScript);

add_setup(async () => {
  let initComplete = new Promise((resolve) =>
    chromeScript.addMessageListener("initDone", resolve)
  );
  chromeScript.sendAsyncMessage("init");
  info("Waiting for startup to complete...");
  await initComplete;
});

add_task(async function test_noAutologin() {
  // test proxy login (default = no autologin), make sure it prompts.
  let 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",
  };
  let action = {
    buttonClick: "ok",
  };
  proxyAuthinfo.username = "";
  proxyAuthinfo.password = "";
  proxyAuthinfo.realm = "Proxy Realm";
  proxyAuthinfo.flags = Ci.nsIAuthInformation.AUTH_PROXY;

  let time1 = await chromeScript.sendQuery("getTimeLastUsed");
  promptDone = handlePrompt(state, action);
  let isOk = prompter2.promptAuth(null, LEVEL, proxyAuthinfo);
  await promptDone;
  let time2 = await chromeScript.sendQuery("getTimeLastUsed");

  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(async function test_autologin() {
  // test proxy login (with autologin)

  // Enable the autologin pref.
  await SpecialPowers.pushPrefEnv({
    set: [["signon.autologin.proxy", true]],
  });

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

  let time1 = await chromeScript.sendQuery("getTimeLastUsed");
  let isOk = prompter2.promptAuth(null, LEVEL, proxyAuthinfo);
  let time2 = await chromeScript.sendQuery("getTimeLastUsed");

  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(async function test_autologin_incorrect() {
  // test proxy login (with autologin), ensure it prompts after a failed auth.
  let 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",
  };
  let action = {
    buttonClick: "ok",
  };

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

  let time1 = await chromeScript.sendQuery("getTimeLastUsed");
  promptDone = handlePrompt(state, action);
  let isOk = prompter2.promptAuth(null, LEVEL, proxyAuthinfo);
  await promptDone;
  let time2 = await chromeScript.sendQuery("getTimeLastUsed");

  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");
});
</script>
</pre>
</body>
</html>