summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/addrbook/test/browser/data/token.sjs
blob: e070f8d55f4cacdf6c1aa9440b924ef4d9c1d167 (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
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

// Serves as the token endpoint for OAuth2 testing.

/* eslint-disable-next-line mozilla/reject-importGlobalProperties */
Cu.importGlobalProperties(["URLSearchParams"]);

function handleRequest(request, response) {
  let stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
    Ci.nsIBinaryInputStream
  );
  stream.setInputStream(request.bodyInputStream);

  let input = stream.readBytes(request.bodyInputStream.available());
  let params = new URLSearchParams(input);

  response.setHeader("Content-Type", "application/json", false);

  if (params.get("refresh_token") == "expired_token") {
    response.setStatusLine("1.1", 400, "Bad Request");
    response.write(JSON.stringify({ error: "invalid_grant" }));
    return;
  }

  let data = { access_token: "bobs_access_token" };

  if (params.get("code") == "success") {
    // Authorisation just happened, set a different access token so the test
    // can detect it, and provide a refresh token.
    data.access_token = "new_access_token";
    data.refresh_token = "new_refresh_token";
  }

  response.write(JSON.stringify(data));
}