authenticate.sjs (723B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 function handleRequest(request, response) { 6 const expectedAuth = "Basic " + btoa("user:pass"); 7 8 let actualAuth = request.hasHeader("Authorization") 9 ? request.getHeader("Authorization") 10 : null; 11 12 if (actualAuth === expectedAuth) { 13 response.setStatusLine("1.1", 200, "OK"); 14 response.write("Authenticated"); 15 } else { 16 response.setStatusLine("1.1", 401, "Unauthorized"); 17 response.setHeader("WWW-Authenticate", 'Basic realm="TestRealm"'); 18 response.write("Authentication required"); 19 } 20 }