Permalink
Browse files

skeleton of HTTP Basic auth added

  • Loading branch information...
danja committed Jun 25, 2012
1 parent 260fba9 commit 2cc8488cb9254ccf4ab4fc92d5b2fa85c6560fef
Showing with 64 additions and 2 deletions.
  1. +1 −0 misc/dir/text.txt
  2. +1 −0 misc/text.txt
  3. +40 −0 src/Authenticator.js
  4. +20 −2 src/seki.js
  5. +1 −0 www/dir
  6. +1 −0 www/text.txt
View
@@ -0,0 +1 @@
+More text for testing fileserver - got symlink issues
View
@@ -0,0 +1 @@
+Text file for testing fileserver.
View
@@ -0,0 +1,40 @@
+//Constructor
+function Authenticator() {
+
+}
+
+// properties and methods
+Authenticator.prototype = {
+
+ value1 : "default_value",
+
+ // HTTP Basic Authentication
+ // http://en.wikipedia.org/wiki/Basic_access_authentication
+ "Basic" : function(request) {
+ var header = request.headers['authorization'] || '', // get the header
+ token = header.split(/\s+/).pop() || '', // and the encoded auth
+ // token
+ auth = new Buffer(token, 'base64').toString(), // convert from base64
+ parts = auth.split(/:/), // split on colon
+ username = parts[0], password = parts[1];
+
+ if(username == "danja" && password =="sasha") {
+ return true;
+ }
+ return false;
+ },
+
+ // Dummy methods for testing
+ "T" : function(request) { // always true
+ // this.value2 = argument + 100;
+ console.log("Auth.true called");
+ return true;
+ },
+
+ "F" : function(request) { // always false
+ console.log("Auth.false called");
+ return false;
+ }
+};
+
+module.exports = Authenticator;
View
@@ -16,6 +16,7 @@
/*
* library module imports
*/
+var sys = require('sys');
var http = require('http');
var fs = require('fs'); // filesystem module
var qs = require('querystring'); // POST parameters parser
@@ -30,6 +31,7 @@ var verbose = true;
var Constants = require('./Constants');
var Utils = require('./Utils');
+var Authenticator = require('./Authenticator');
var templater = require('./templater');
var sparqlTemplates = require('./sparqlTemplates');
var htmlTemplates = require('./htmlTemplates');
@@ -67,6 +69,12 @@ var postHeaders = {
'Content-Type' : 'application/x-www-form-urlencoded'
};
+var notAuthHeaders = {
+ "Host" : config.sekiHost + ":" + config.sekiPort,
+ 'Content-Type' : 'text/plain',
+ 'WWW-Authenticate' : 'Basic realm="Secure Area"'
+ };
+
/*
* mapping URIs to static files on the filesystem
*
@@ -100,17 +108,27 @@ function onRequest(sekiRequest, sekiResponse) {
verbosity("REQUEST METHOD = " + sekiRequest.method);
// check for corresponding files on the filesystem
- if (sekiRequest.method == "GET") {
+ if (sekiRequest.method == "GET" || sekiRequest.method == "HEAD") {
file.serve(sekiRequest, sekiResponse, function(err, res) {
if (err) { // the file doesn't exist, leave it to Seki
+ // sys.error("Error serving " + sekiRequest.url + " - " + err.message); // temp for debugging
} else { // The file was served successfully
verbosity(sekiRequest.url + " - " + res.message);
}
});
}
verbosity("got past file server");
-
+
+ var auth = new Authenticator();
+
+ if (sekiRequest.method == "POST") {
+ if(!auth.Basic(sekiRequest)){
+ sekiResponse.writeHead(401, notAuthHeaders);
+ sekiResponse.end("401 Not Authorized");
+ return;
+ }
+ }
// handle admin requests/commands
if (sekiRequest.method == "POST") {
if (sekiRequest.url.substring(0, 7) == "/admin/") {
View
View

0 comments on commit 2cc8488

Please sign in to comment.