Permalink
Browse files

changed auth method

  • Loading branch information...
1 parent b14817a commit 1a28ed361975cd2875983a48629cdf3471c416ed @catalin- committed Oct 10, 2012
Showing with 45 additions and 26 deletions.
  1. +3 −2 lib/Dictionary.js
  2. +1 −0 lib/Proxy.js
  3. +33 −16 lib/server.js
  4. +3 −3 lib/session.js
  5. +2 −2 package.json
  6. +3 −3 start-server.js
View
@@ -38,14 +38,15 @@ Dictionary.prototype.fetchByProperty = function(property, value) {
}
Dictionary.prototype.hasKey = function(key) {
- for (var k in this.keys) {
+ return this.keys.hasOwnProperty(key);
+ /*for (var k in this.keys) {
if (key == k) {
return true;
} else {
return false;
}
};
- return false;
+ return false;*/
};
Dictionary.prototype.remove = function(key) {
View
@@ -108,6 +108,7 @@ Proxy.prototype = {
},
createServer: function(port) {
+
http.createServer(route).listen(port);
},
View
@@ -7,7 +7,7 @@
//-------- includes & variables ---------
//---------------------------------------
-var Proxy = require('./proxy.js');
+var Proxy = require('./Proxy.js');
var proxyUtil = require('./util.js');
var session = require('./session.js');
var querystring = require('querystring');
@@ -28,7 +28,8 @@ var crossDomainXml = '\<?xml version="1.0"?>' +
var developmentMode = false;
-var postRequestCallback = null;
+var afterRequestHandler = null;
+var validateAuthorization = null;
var proxy = new Proxy();
@@ -66,9 +67,9 @@ function getCommandResponseObject(command, path, params, content) {
}
function notifyPostRequestHandler(command, path, content) {
- if (postRequestCallback) {
+ if (afterRequestHandler) {
var cmd = getCommandResponseObject(command, path, null, content);
- postRequestCallback(cmd);
+ afterRequestHandler(cmd);
}
}
@@ -118,14 +119,30 @@ function handleCrossDomainXMLRequest(request,response, path, params, data) {
proxy.respond(response, 200, { 'content-type': proxyUtil.CONTENT_TYPE_XML }, crossDomainXml);
}
-function handleLoginNotification(request,response, path, params, data) {
- data = data ? querystring.parse(data.toString()) : null;
-
- if (data) {
- var token = session.update(data);
+function handleAuthorizeRequest(request,response, path, params, data) {
+ var authRequestData = null;
+ if (request.method.toUpperCase() == 'GET') {
+ authRequestData = params;
+ } else {
+ authRequestData = data ? querystring.parse(data.toString()) : null;
+ }
- proxy.respond( response, 200, updateCookieHeaders(token.session), '[DavProxy] Session created for user' + data.userName);
- notifyPostRequestHandler( 'session', '/loginNotification', { userName: token.userName, session: token.session });
+ if (authRequestData) {
+ //authenticate
+ if ( validateAuthorization ) {
+ validateAuthorization(authRequestData, function(authData){
+ if ( !authData) {
+ proxy.respond(response, 500, {"content-type": proxyUtil.CONTENT_TYPE_TEXT}, '[DavProxy] Invalid login notification');
+ } else {
+ var token = session.update(authData);
+
+ proxy.respond( response, 200, updateCookieHeaders(token.session), '[DavProxy] Session created for user' + token.userName);
+ notifyPostRequestHandler( 'session', '/authorize', { userName: token.userName, session: token.session });
+ }
+ });
+ } else {
+ proxy.respond(response, 500, {"content-type": proxyUtil.CONTENT_TYPE_TEXT}, '[DavProxy] Authorization handler not set');
+ }
} else {
proxy.respond(response, 500, {"content-type": proxyUtil.CONTENT_TYPE_TEXT}, '[DavProxy] Invalid login notification');
}
@@ -266,7 +283,8 @@ function handleAllTest(request,response, path, params, data, authToken) {
proxy.addRoute('get',/^\/icons(.*)$/, handleIconsRequest);
proxy.addRoute('get','/favicon.ico', handleIconsRequest);
proxy.addRoute('get','/crossdomain.xml', handleCrossDomainXMLRequest);
-proxy.addRoute('post','/loginNotification', handleLoginNotification);
+proxy.addRoute('post','/authorize', handleAuthorizeRequest);
+proxy.addRoute('get','/authorize', handleAuthorizeRequest);
proxy.addRoute('get','/loadSession', handleLoadSessionRequest);
proxy.addRoute('post',/^\/upload(.*)$/, handleUploadRequest); //handle multipart post
proxy.addRoute('all',/^\/json(.*)$/, handleJsonRequest);
@@ -276,7 +294,7 @@ proxy.addRoute('all',/(.*)$/, handleAllRequest);
//------------ initialize ---------------
//---------------------------------------
-function init(davHost, davPort, davPath, sessionID, proxyPort, postRequestHandler) {
+function init(davHost, davPort, davPath, sessionID, proxyPort, postRequestHandler, authorizationHandler) {
if ( !davHost) {
console.log('WebDav host not set');
process.exit(1);
@@ -303,9 +321,8 @@ function init(davHost, davPort, davPath, sessionID, proxyPort, postRequestHandle
proxyPort = DEFAULT_PORT;
}
- if (postRequestHandler) {
- postRequestCallback = postRequestHandler;
- }
+ afterRequestHandler = postRequestHandler;
+ validateAuthorization = authorizationHandler;
webDavHost = davHost;
webDavPort = davPort;
View
@@ -11,11 +11,11 @@ var SESSION_ID_PADDING = "";
var SESSION_COOKIE_ID = "WD_PROXY_SESSION";
-var AuthenticationToken = function(userName, email, digest) {
+var AuthenticationToken = function(userName, email, digest, token) {
this.userName = userName;
this.email = email;
this.digest = digest;
- this.session = uuid.v4();
+ this.session = token;//uuid.v4();
}
function getCookieID() {
@@ -29,7 +29,7 @@ function getCookieID() {
}
function update(authorisation) {
- var token = new AuthenticationToken(authorisation.userName, authorisation.email, authorisation.digest);
+ var token = new AuthenticationToken(authorisation.userName, authorisation.email, authorisation.digest, authorisation.token);
sessionHolder.store(token.userName, token);
return token;
}
View
@@ -1,7 +1,7 @@
{
"name": "dav-proxy",
"description": "WebDav Proxy",
- "version": "1.0.2",
+ "version": "1.0.3",
"homepage": "https://github.com/catalin-/node-dav-proxy",
"author": {
"name": "Catalin Manolescu",
@@ -46,6 +46,6 @@
},
"optionalDependencies": {},
"readme": "WebDav proxy for node js",
- "_id": "dav-proxy@1.0.2",
+ "_id": "dav-proxy@1.0.3",
"_from": "dav-proxy"
}
View
@@ -3,12 +3,12 @@
* @since 05 September 2012 - 10:12
*/
-var proxy = require('./lib/server.js');
-proxy.init(process.argv[2], process.argv[3], process.argv[4], process.argv[5], process.argv[6], postRequestHandler);
+var proxy = require('./lib/server.js');
+proxy.init(process.argv[2], process.argv[3], process.argv[4], process.argv[5], process.argv[6], afterRequestHandler);
proxy.enableDevelopmentMode(true);
proxy.createSession('demo', 'demo', 'WebDAV');
-function postRequestHandler( command ) {
+function afterRequestHandler( command ) {
console.log(command);
}

0 comments on commit 1a28ed3

Please sign in to comment.