Bug 463154 - Form history should record creation/usage timestamps. r=mconnor, r=sdwilsh
[wine-gecko.git] / toolkit / components / passwordmgr / test / authenticate.sjs
blob534bb987275cf2226ead7a409a549224eec8eea6
1 function handleRequest(request, response)
3   try {
4     reallyHandleRequest(request, response);
5   } catch (e) {
6     response.setStatusLine("1.0", 200, "AlmostOK");
7     response.write("Error handling request: " + e);
8   }
12 function reallyHandleRequest(request, response) {
13   var match;
14   var requestAuth = true;
16   // Allow the caller to drive how authentication is processed via the query.
17   // Eg, http://localhost:8888/authenticate.sjs?user=foo&realm=bar
18   var query = request.queryString;
20   var expected_user = "", expected_pass = "", realm = "mochitest";
21   // user=xxx
22   match = /user=([^&]*)/.exec(query);
23   if (match)
24     expected_user = match[1];
26   // pass=xxx
27   match = /pass=([^&]*)/.exec(query);
28   if (match)
29     expected_pass = match[1];
31   // realm=xxx
32   match = /realm=([^&]*)/.exec(query);
33   if (match)
34     realm = match[1];
37   // Look for an authentication header, if any, in the request.
38   //
39   // EG: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
40   // 
41   // This test only supports Basic auth. The value sent by the client is
42   // "username:password", obscured with base64 encoding.
44   var actual_user = "", actual_pass = "", authHeader;
45   if (request.hasHeader("Authorization")) {
46     authHeader = request.getHeader("Authorization");
47     match = /Basic (.+)/.exec(authHeader);
48     if (match.length != 2)
49         throw "Couldn't parse auth header: " + authHeader;
51     var userpass = base64ToString(match[1]); // no atob() :-(
52     match = /(.*):(.*)/.exec(userpass);
53     if (match.length != 3)
54         throw "Couldn't decode auth header: " + userpass;
55     actual_user = match[1];
56     actual_pass = match[2];
57   } 
59   // Don't request authentication if the credentials we got were what we
60   // expected.
61   if (expected_user == actual_user &&
62       expected_pass == actual_pass) {
63       requestAuth = false;
64   }
66   if (requestAuth) {
67     response.setStatusLine("1.0", 401, "Authentication required");
68     response.setHeader("WWW-Authenticate", "basic realm=\"" + realm + "\"", false);
69   } else {
70     response.setStatusLine("1.0", 200, "OK");
71   }
73   response.setHeader("Content-Type", "application/xhtml+xml", false);
74   response.write("<html xmlns='http://www.w3.org/1999/xhtml'>");
75   response.write("<p>Login: <span id='ok'>" + (requestAuth ? "FAIL" : "PASS") + "</span></p>\n");
76   response.write("<p>Auth: <span id='auth'>" + authHeader + "</span></p>\n");
77   response.write("<p>User: <span id='user'>" + actual_user + "</span></p>\n");
78   response.write("<p>Pass: <span id='pass'>" + actual_pass + "</span></p>\n");
79   response.write("</html>");
83 // base64 decoder
85 // Yoinked from extensions/xml-rpc/src/nsXmlRpcClient.js because btoa()
86 // doesn't seem to exist. :-(
87 /* Convert Base64 data to a string */
88 const toBinaryTable = [
89     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
90     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
91     -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
92     52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
93     -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
94     15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
95     -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
96     41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
98 const base64Pad = '=';
100 function base64ToString(data) {
102     var result = '';
103     var leftbits = 0; // number of bits decoded, but yet to be appended
104     var leftdata = 0; // bits decoded, but yet to be appended
106     // Convert one by one.
107     for (var i = 0; i < data.length; i++) {
108         var c = toBinaryTable[data.charCodeAt(i) & 0x7f];
109         var padding = (data[i] == base64Pad);
110         // Skip illegal characters and whitespace
111         if (c == -1) continue;
112         
113         // Collect data into leftdata, update bitcount
114         leftdata = (leftdata << 6) | c;
115         leftbits += 6;
117         // If we have 8 or more bits, append 8 bits to the result
118         if (leftbits >= 8) {
119             leftbits -= 8;
120             // Append if not padding.
121             if (!padding)
122                 result += String.fromCharCode((leftdata >> leftbits) & 0xff);
123             leftdata &= (1 << leftbits) - 1;
124         }
125     }
127     // If there are any bits left, the base64 string was corrupted
128     if (leftbits)
129         throw Components.Exception('Corrupted base64 string');
131     return result;