michael@0: function handleRequest(request, response) michael@0: { michael@0: try { michael@0: reallyHandleRequest(request, response); michael@0: } catch (e) { michael@0: response.setStatusLine("1.0", 200, "AlmostOK"); michael@0: response.write("Error handling request: " + e); michael@0: } michael@0: } michael@0: michael@0: michael@0: function reallyHandleRequest(request, response) { michael@0: var match; michael@0: var requestAuth = true, requestProxyAuth = true; michael@0: michael@0: // Allow the caller to drive how authentication is processed via the query. michael@0: // Eg, http://localhost:8888/authenticate.sjs?user=foo&realm=bar michael@0: // The extra ? allows the user/pass/realm checks to succeed if the name is michael@0: // at the beginning of the query string. michael@0: var query = "?" + request.queryString; michael@0: michael@0: var expected_user = "", expected_pass = "", realm = "mochitest"; michael@0: var proxy_expected_user = "", proxy_expected_pass = "", proxy_realm = "mochi-proxy"; michael@0: var huge = false, plugin = false, anonymous = false; michael@0: var authHeaderCount = 1; michael@0: // user=xxx michael@0: match = /[^_]user=([^&]*)/.exec(query); michael@0: if (match) michael@0: expected_user = match[1]; michael@0: michael@0: // pass=xxx michael@0: match = /[^_]pass=([^&]*)/.exec(query); michael@0: if (match) michael@0: expected_pass = match[1]; michael@0: michael@0: // realm=xxx michael@0: match = /[^_]realm=([^&]*)/.exec(query); michael@0: if (match) michael@0: realm = match[1]; michael@0: michael@0: // proxy_user=xxx michael@0: match = /proxy_user=([^&]*)/.exec(query); michael@0: if (match) michael@0: proxy_expected_user = match[1]; michael@0: michael@0: // proxy_pass=xxx michael@0: match = /proxy_pass=([^&]*)/.exec(query); michael@0: if (match) michael@0: proxy_expected_pass = match[1]; michael@0: michael@0: // proxy_realm=xxx michael@0: match = /proxy_realm=([^&]*)/.exec(query); michael@0: if (match) michael@0: proxy_realm = match[1]; michael@0: michael@0: // huge=1 michael@0: match = /huge=1/.exec(query); michael@0: if (match) michael@0: huge = true; michael@0: michael@0: // plugin=1 michael@0: match = /plugin=1/.exec(query); michael@0: if (match) michael@0: plugin = true; michael@0: michael@0: // multiple=1 michael@0: match = /multiple=([^&]*)/.exec(query); michael@0: if (match) michael@0: authHeaderCount = match[1]+0; michael@0: michael@0: // anonymous=1 michael@0: match = /anonymous=1/.exec(query); michael@0: if (match) michael@0: anonymous = true; michael@0: michael@0: // Look for an authentication header, if any, in the request. michael@0: // michael@0: // EG: Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ== michael@0: // michael@0: // This test only supports Basic auth. The value sent by the client is michael@0: // "username:password", obscured with base64 encoding. michael@0: michael@0: var actual_user = "", actual_pass = "", authHeader, authPresent = false; michael@0: if (request.hasHeader("Authorization")) { michael@0: authPresent = true; michael@0: authHeader = request.getHeader("Authorization"); michael@0: match = /Basic (.+)/.exec(authHeader); michael@0: if (match.length != 2) michael@0: throw "Couldn't parse auth header: " + authHeader; michael@0: michael@0: var userpass = base64ToString(match[1]); // no atob() :-( michael@0: match = /(.*):(.*)/.exec(userpass); michael@0: if (match.length != 3) michael@0: throw "Couldn't decode auth header: " + userpass; michael@0: actual_user = match[1]; michael@0: actual_pass = match[2]; michael@0: } michael@0: michael@0: var proxy_actual_user = "", proxy_actual_pass = ""; michael@0: if (request.hasHeader("Proxy-Authorization")) { michael@0: authHeader = request.getHeader("Proxy-Authorization"); michael@0: match = /Basic (.+)/.exec(authHeader); michael@0: if (match.length != 2) michael@0: throw "Couldn't parse auth header: " + authHeader; michael@0: michael@0: var userpass = base64ToString(match[1]); // no atob() :-( michael@0: match = /(.*):(.*)/.exec(userpass); michael@0: if (match.length != 3) michael@0: throw "Couldn't decode auth header: " + userpass; michael@0: proxy_actual_user = match[1]; michael@0: proxy_actual_pass = match[2]; michael@0: } michael@0: michael@0: // Don't request authentication if the credentials we got were what we michael@0: // expected. michael@0: if (expected_user == actual_user && michael@0: expected_pass == actual_pass) { michael@0: requestAuth = false; michael@0: } michael@0: if (proxy_expected_user == proxy_actual_user && michael@0: proxy_expected_pass == proxy_actual_pass) { michael@0: requestProxyAuth = false; michael@0: } michael@0: michael@0: if (anonymous) { michael@0: if (authPresent) { michael@0: response.setStatusLine("1.0", 400, "Unexpected authorization header found"); michael@0: } else { michael@0: response.setStatusLine("1.0", 200, "Authorization header not found"); michael@0: } michael@0: } else { michael@0: if (requestProxyAuth) { michael@0: response.setStatusLine("1.0", 407, "Proxy authentication required"); michael@0: for (i = 0; i < authHeaderCount; ++i) michael@0: response.setHeader("Proxy-Authenticate", "basic realm=\"" + proxy_realm + "\"", true); michael@0: } else if (requestAuth) { michael@0: response.setStatusLine("1.0", 401, "Authentication required"); michael@0: for (i = 0; i < authHeaderCount; ++i) michael@0: response.setHeader("WWW-Authenticate", "basic realm=\"" + realm + "\"", true); michael@0: } else { michael@0: response.setStatusLine("1.0", 200, "OK"); michael@0: } michael@0: } michael@0: michael@0: response.setHeader("Content-Type", "application/xhtml+xml", false); michael@0: response.write(""); michael@0: response.write("
Login: " + (requestAuth ? "FAIL" : "PASS") + "
\n"); michael@0: response.write("Proxy: " + (requestProxyAuth ? "FAIL" : "PASS") + "
\n"); michael@0: response.write("Auth: " + authHeader + "
\n"); michael@0: response.write("User: " + actual_user + "
\n"); michael@0: response.write("Pass: " + actual_pass + "
\n"); michael@0: michael@0: if (huge) { michael@0: response.write(" "); michael@0: response.write("This is a footnote after the huge content fill"); michael@0: } michael@0: michael@0: if (plugin) { michael@0: response.write("\n"); michael@0: } michael@0: michael@0: response.write(""); michael@0: } michael@0: michael@0: michael@0: // base64 decoder michael@0: // michael@0: // Yoinked from extensions/xml-rpc/src/nsXmlRpcClient.js because btoa() michael@0: // doesn't seem to exist. :-( michael@0: /* Convert Base64 data to a string */ michael@0: const toBinaryTable = [ michael@0: -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, michael@0: -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, michael@0: -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, michael@0: 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, michael@0: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, michael@0: 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, michael@0: -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, michael@0: 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 michael@0: ]; michael@0: const base64Pad = '='; michael@0: michael@0: function base64ToString(data) { michael@0: michael@0: var result = ''; michael@0: var leftbits = 0; // number of bits decoded, but yet to be appended michael@0: var leftdata = 0; // bits decoded, but yet to be appended michael@0: michael@0: // Convert one by one. michael@0: for (var i = 0; i < data.length; i++) { michael@0: var c = toBinaryTable[data.charCodeAt(i) & 0x7f]; michael@0: var padding = (data[i] == base64Pad); michael@0: // Skip illegal characters and whitespace michael@0: if (c == -1) continue; michael@0: michael@0: // Collect data into leftdata, update bitcount michael@0: leftdata = (leftdata << 6) | c; michael@0: leftbits += 6; michael@0: michael@0: // If we have 8 or more bits, append 8 bits to the result michael@0: if (leftbits >= 8) { michael@0: leftbits -= 8; michael@0: // Append if not padding. michael@0: if (!padding) michael@0: result += String.fromCharCode((leftdata >> leftbits) & 0xff); michael@0: leftdata &= (1 << leftbits) - 1; michael@0: } michael@0: } michael@0: michael@0: // If there are any bits left, the base64 string was corrupted michael@0: if (leftbits) michael@0: throw Components.Exception('Corrupted base64 string'); michael@0: michael@0: return result; michael@0: }