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