tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

file_invalidEmailCase.sjs (2501B)


      1 /* Any copyright is dedicated to the Public Domain.
      2    http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 /**
      5  * This server simulates the behavior of /account/login on the Firefox Accounts
      6  * auth server in the case where the user is trying to sign in with an email
      7  * with the wrong capitalization.
      8  *
      9  * https://github.com/mozilla/fxa-auth-server/blob/master/docs/api.md#post-v1accountlogin
     10  *
     11  * The expected behavior is that on the first attempt, with the wrong email,
     12  * the server will respond with a 400 and the canonical email capitalization
     13  * that the client should use.  The client then has one chance to sign in with
     14  * this different capitalization.
     15  *
     16  * In this test, the user with the account id "Greta.Garbo@gmail.COM" initially
     17  * tries to sign in as "greta.garbo@gmail.com".
     18  *
     19  * On success, the client is responsible for updating its sign-in user state
     20  * and recording the proper email capitalization.
     21  */
     22 
     23 const CC = Components.Constructor;
     24 const BinaryInputStream = CC(
     25   "@mozilla.org/binaryinputstream;1",
     26   "nsIBinaryInputStream",
     27   "setInputStream"
     28 );
     29 
     30 const goodEmail = "Greta.Garbo@gmail.COM";
     31 const badEmail = "greta.garbo@gmail.com";
     32 
     33 function handleRequest(request, response) {
     34   let body = new BinaryInputStream(request.bodyInputStream);
     35   let bytes = [];
     36   let available;
     37   while ((available = body.available()) > 0) {
     38     Array.prototype.push.apply(bytes, body.readByteArray(available));
     39   }
     40 
     41   let data = JSON.parse(String.fromCharCode.apply(null, bytes));
     42   let message;
     43 
     44   switch (data.email) {
     45     case badEmail:
     46       // Almost - try again with fixed email case
     47       message = {
     48         code: 400,
     49         errno: 120,
     50         error: "Incorrect email case",
     51         email: goodEmail,
     52       };
     53       response.setStatusLine(request.httpVersion, 400, "Almost");
     54       break;
     55 
     56     case goodEmail:
     57       // Successful login.
     58       message = {
     59         uid: "your-uid",
     60         sessionToken: "your-sessionToken",
     61         keyFetchToken: "your-keyFetchToken",
     62         verified: true,
     63         authAt: 1392144866,
     64       };
     65       response.setStatusLine(request.httpVersion, 200, "Yay");
     66       break;
     67 
     68     default:
     69       // Anything else happening in this test is a failure.
     70       message = {
     71         code: 400,
     72         errno: 999,
     73         error: "What happened!?",
     74       };
     75       response.setStatusLine(request.httpVersion, 400, "Ouch");
     76       break;
     77   }
     78 
     79   let messageStr = JSON.stringify(message);
     80   response.bodyOutputStream.write(messageStr, messageStr.length);
     81 }