summaryrefslogtreecommitdiffstats
path: root/devtools/shared/network-observer/NetworkAuthListener.sys.mjs
blob: 2ab5517aa17eea546f57d6f1957b7a2052a22a0d (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

const lazy = {};

ChromeUtils.defineESModuleGetters(lazy, {
  setTimeout: "resource://gre/modules/Timer.sys.mjs",
});

/**
 * This class is a simplified version from the AuthRequestor used by the
 * WebExtensions codebase at:
 *   https://searchfox.org/mozilla-central/rev/fd2325f5b2a5be8f8f2acf9307285f2b7de06582/toolkit/components/extensions/webrequest/WebRequest.sys.mjs#434-579
 *
 * The NetworkAuthListener will monitor the provided channel and will invoke the
 * owner's `onAuthPrompt` end point whenever an auth challenge is requested.
 *
 * The owner will receive several callbacks to proceed with the prompt:
 * - cancelAuthPrompt(): cancel the authentication attempt
 * - forwardAuthPrompt(): forward the auth prompt request to the next
 *   notification callback. If no other custom callback is set, this will
 *   typically lead to show the auth prompt dialog in the browser UI.
 * - provideAuthCredentials(username, password): attempt to authenticate with
 *   the provided username and password.
 *
 * Please note that the request will be blocked until the consumer calls one of
 * the callbacks listed above. Make sure to eventually unblock the request if
 * you implement `onAuthPrompt`.
 *
 * @param {nsIChannel} channel
 *     The channel to monitor.
 * @param {object} owner
 *     The owner object, expected to implement `onAuthPrompt`.
 */
export class NetworkAuthListener {
  constructor(channel, owner) {
    this.notificationCallbacks = channel.notificationCallbacks;
    this.loadGroupCallbacks =
      channel.loadGroup && channel.loadGroup.notificationCallbacks;
    this.owner = owner;

    // Setup the channel's notificationCallbacks to be handled by this instance.
    channel.notificationCallbacks = this;
  }

  // See https://searchfox.org/mozilla-central/source/netwerk/base/nsIAuthPrompt2.idl
  asyncPromptAuth(channel, callback, context, level, authInfo) {
    const isProxy = !!(authInfo.flags & authInfo.AUTH_PROXY);
    const cancelAuthPrompt = () => {
      if (channel.canceled) {
        return;
      }

      try {
        callback.onAuthCancelled(context, false);
      } catch (e) {
        console.error(`NetworkAuthListener failed to cancel auth prompt ${e}`);
      }
    };

    const forwardAuthPrompt = () => {
      if (channel.canceled) {
        return;
      }

      const prompt = this.#getForwardPrompt(isProxy);
      prompt.asyncPromptAuth(channel, callback, context, level, authInfo);
    };

    const provideAuthCredentials = (username, password) => {
      if (channel.canceled) {
        return;
      }

      authInfo.username = username;
      authInfo.password = password;
      try {
        callback.onAuthAvailable(context, authInfo);
      } catch (e) {
        console.error(
          `NetworkAuthListener failed to provide auth credentials ${e}`
        );
      }
    };

    const authDetails = {
      isProxy,
      realm: authInfo.realm,
      scheme: authInfo.authenticationScheme,
    };
    const authCallbacks = {
      cancelAuthPrompt,
      forwardAuthPrompt,
      provideAuthCredentials,
    };

    // The auth callbacks may only be called asynchronously after this method
    // successfully returned.
    lazy.setTimeout(() => this.#notifyOwner(authDetails, authCallbacks), 1);

    return {
      QueryInterface: ChromeUtils.generateQI(["nsICancelable"]),
      cancel: cancelAuthPrompt,
    };
  }

  getInterface(iid) {
    if (iid.equals(Ci.nsIAuthPromptProvider) || iid.equals(Ci.nsIAuthPrompt2)) {
      return this;
    }
    try {
      return this.notificationCallbacks.getInterface(iid);
    } catch (e) {}
    throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
  }

  // See https://searchfox.org/mozilla-central/source/netwerk/base/nsIAuthPromptProvider.idl
  getAuthPrompt(reason, iid) {
    // This should never get called without getInterface having been called first.
    if (iid.equals(Ci.nsIAuthPrompt2)) {
      return this;
    }
    return this.#getForwardedInterface(Ci.nsIAuthPromptProvider).getAuthPrompt(
      reason,
      iid
    );
  }

  // See https://searchfox.org/mozilla-central/source/netwerk/base/nsIAuthPrompt2.idl
  promptAuth(channel, level, authInfo) {
    this.#getForwardedInterface(Ci.nsIAuthPrompt2).promptAuth(
      channel,
      level,
      authInfo
    );
  }

  #getForwardedInterface(iid) {
    try {
      return this.notificationCallbacks.getInterface(iid);
    } catch (e) {
      return this.loadGroupCallbacks.getInterface(iid);
    }
  }

  #getForwardPrompt(isProxy) {
    const reason = isProxy
      ? Ci.nsIAuthPromptProvider.PROMPT_PROXY
      : Ci.nsIAuthPromptProvider.PROMPT_NORMAL;
    for (const callbacks of [
      this.notificationCallbacks,
      this.loadGroupCallbacks,
    ]) {
      try {
        return callbacks
          .getInterface(Ci.nsIAuthPromptProvider)
          .getAuthPrompt(reason, Ci.nsIAuthPrompt2);
      } catch (e) {}
      try {
        return callbacks.getInterface(Ci.nsIAuthPrompt2);
      } catch (e) {}
    }
    throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
  }

  #notifyOwner(authDetails, authCallbacks) {
    if (typeof this.owner.onAuthPrompt == "function") {
      this.owner.onAuthPrompt(authDetails, authCallbacks);
    } else {
      console.error(
        "NetworkObserver owner enabled the auth prompt listener " +
          "but does not implement 'onAuthPrompt'. " +
          "Forwarding the auth prompt to the next notification callback."
      );
      authCallbacks.forwardAuthPrompt();
    }
  }
}

NetworkAuthListener.prototype.QueryInterface = ChromeUtils.generateQI([
  "nsIInterfaceRequestor",
  "nsIAuthPromptProvider",
  "nsIAuthPrompt2",
]);