Skip to content

Commit

Permalink
DIRTY DIRTY CODE that responds to a contacts and login request
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Percsi committed Sep 1, 2010
1 parent adbb497 commit 0d1bc5d
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 21 deletions.
38 changes: 38 additions & 0 deletions fixtures/contacts.js
@@ -0,0 +1,38 @@
exports.contacts = [
{
id: '1',
type: 'Contact',
firstName: 'Sean',
lastName: 'Eidemiller',
emailAddress: 'sean.eidemiller@eloqua.com',
subscribed: 'false',
bounceback: 'false',
accountName: 'Eloqua Limited',
title: 'Software Engineer',
activityDelta: 10
},
{
id: '2',
type: 'Contact',
firstName: 'Mike',
lastName: 'Ball',
emailAddress: 'mike.ball@eloqua.com',
subscribed: 'false',
bounceback: 'false',
accountName: 'Eloqua Limited',
title: 'Software Engineer',
activityDelta: 3
},
{
id: '3',
type: 'Contact',
firstName: 'Evin',
lastName: 'Grano',
emailAddress: 'evin.grano@eloqua.com',
subscribed: 'false',
bounceback: 'false',
accountName: 'Eloqua Limited',
title: 'Software Engineer',
activityDelta: -3
}
];
4 changes: 4 additions & 0 deletions fixtures/resource_paths.js
@@ -0,0 +1,4 @@
exports.paths = [
{ "file": "users", "resourcePath": "user", "pluralResourcePath": "users"},
{ "file": "contacts", "resourcePath": "contact", "pluralResourcePath": "contacts"}
]
10 changes: 7 additions & 3 deletions lib/OrionFileAuth.js
Expand Up @@ -17,19 +17,23 @@ global.OrionFileAuth = OrionAuth.extend({

_authData: null,

checkAuth: function(user,passwd,passwdIsMD5,callback){
checkAuth: function(user,passwd, company,passwdIsMD5, callback){
if(!this._authData){
var data = require('.'+this.fileName); // the data should already be in the right format
var data = require('.'+this.fileName+'.sample'); // the data should already be in the right format
this._authData = data.users;
}
// so the auth data is loaded, do a check
var userdata = this._authData[user];
if(userdata){
sys.puts(userdata.passwd);
sys.puts(userdata.company);
sys.puts(company);
sys.puts(passwd);
var ret = {
user: user,
role: userdata.role
}
if(userdata.passwd == passwd){
if(userdata.passwd == passwd && userdata.company === company){
callback(ret);
}
else callback(NO);
Expand Down
77 changes: 67 additions & 10 deletions lib/OrionServer.js
Expand Up @@ -79,7 +79,7 @@ global.OrionServer = SC.Object.extend({
if(serverObj.forceAuth){
// make sure that the user is authenticated,
// but only after we found out the current request doesn't turn out to be an auth request
if(method === 'POST' && resource == 'auth'){ // force auth with posting
if(method === 'POST' && resource.indexOf('auth')>=0){ // force auth with posting
sys.log('OrionServer: receiving an AUTH request on the REST side');
var authdata = "";
request.addListener("data", function(chunk){ // gather data
Expand All @@ -94,10 +94,11 @@ global.OrionServer = SC.Object.extend({
if(resource.indexOf('rpc') !== 0){ // allow non-auth requesting of rpc data
var receivedCookieHeader = request.headers['cookie'];
var receivedUserName = request.headers['username'];
//sys.puts('cookieHeader received: ' + receivedCookieHeader);
if(receivedCookieHeader && receivedUserName){
sys.puts('cookieHeader received: ' + receivedCookieHeader);
if(receivedCookieHeader){
//check the session
var hasSession = serverObj.sessionModule.checkSession(receivedUserName,receivedCookieHeader);
var hasSession = serverObj.sessionModule.checkSession(receivedUserName,receivedCookieHeader, true);
sys.puts(hasSession);
if(!hasSession){
response.writeHead(403, {'Content-Type':'text/html'});
response.write('Not logged in, invalid cookie'); // this can be much more fancy of course!
Expand All @@ -114,10 +115,28 @@ global.OrionServer = SC.Object.extend({
}
}
}

//split up request parameters
var prefix = serverObj.RESTPrefix;
sys.puts('prefix: '+prefix);
sys.puts('res: '+request.url);
if (request.url.indexOf(prefix)>=0){
var params = request.url.substr(request.url.indexOf(prefix)+prefix.length);
var paramsArr = params.split('/');
var endpointType = paramsArr[0],
dataType = paramsArr[1];
sys.puts('dt: '+dataType);
//TODO [AP]: take other params into account
request.dataType=dataType;
request.endpointType = endpointType;
request.paramsArr = paramsArr;
sys.puts('params '+params);
}

// handle all normal requests
switch(method){
case 'GET':
if(resource.indexOf('rpc'=== 0)){
if(resource.indexOf('rpc') === 0){
serverObj.RPC(request,resource,response);
}
else {
Expand Down Expand Up @@ -240,22 +259,60 @@ global.OrionServer = SC.Object.extend({
response.write(JSON.stringify({sessionCookie: receivedSessionKey}));
}
else {
response.writeHead(200, {'Content-Type': 'text/html'});
response.writeHead(403, {'Content-Type': 'text/html'});
response.write("<br/>auth result: " + authResult);
//response.write('<br/>received cookie: ' + givenCookieHeader);
}
response.end();
};
this.authModule.checkAuth(dataObj.user, dataObj.passwd,false,callback);
this.authModule.checkAuth(dataObj.user, dataObj.passwd, dataObj.company, false,callback);

},

GET: function(request,response){
console.log('get '+request.dataType+' '+request.endpointType);
var me = this;
var path = url.parse(request.url).pathname;
var resource = path.slice(1); // return the entire string except the first character (being a "/")
response.writeHead(200, {'Content-Type': 'text/html'});
response.write('OrionServer: GET: received resource: ' + resource);
var dataType = request.dataType,
endpointType = request.endpointType;
var paths = require('../fixtures/resource_paths').paths;

if (endpointType === 'system'){
if (dataType==='user' && request.paramsArr[2]==='current'){
var users = require('../myUsers.sample');
var userData = users.users['Alexandru.Percsi'];
response.writeHead(200, {'Content-Type': 'application/json'});
response.write(JSON.stringify(userData));
response.write('\r\n');
response.end();
}
}
else
{
sys.puts('endpoint type '+endpointType+' '+sys.inspect(paths.length));
for (var i=0;i<paths.length;i++){
if (paths[i].resourcePath===dataType){
sys.puts('if '+paths[i].pluralResourcePath)
var records = require('../fixtures/'+paths[i].file);
response.writeHead(200, {'Content-Type': 'application/json'});
//TODO [AP]: Return the one record that matches the id

break;
}
else
{
sys.puts('else '+paths[i].pluralResourcePath)
if (paths[i].pluralResourcePath===dataType){
var records = require('../fixtures/'+paths[i].file);
response.writeHead(200, {'Content-Type': 'application/json'});
response.write(JSON.stringify(eval('records.'+dataType)));
}
}
}
}

//response.write('OrionServer: GET: received resource: ' + resource);
sys.puts('ending');
response.end();
// for the moment don't parse the resource, but just assume it is the model name
//this.store.fetch(resource,"student/1",this.createFetchCallback(request,response));
Expand Down
17 changes: 13 additions & 4 deletions lib/OrionSession.js
Expand Up @@ -55,13 +55,18 @@ global.OrionSession = SC.Object.extend({
// lets force that behaviour for the moment, and rewrite the stuff when a better way can be found

// process sessionInfo
//HACK we wil fix this later
return YES;
var sessionName = this.sessionName;
var receivedSessionKey = "";
if(!sessionKeyOnly){
var sessionInfoObj = querystring.parse(sessionInfo,';','=');
receivedSessionKey = sessionInfoObj[sessionName];
}
else receivedSessionKey = sessionInfo;
else {
receivedSessionKey = sessionInfo;
sys.puts('sessionkeyonly');
}

//sys.puts(sys.inspect(sessionInfoObj));
// returns YES or NO depending on whether the user is still logged in
Expand All @@ -74,9 +79,10 @@ global.OrionSession = SC.Object.extend({
if(user){
curUserData = this._loggedInUsers[user]; // get the user data
}
if(curUserData){ // if it exists, check it
//sys.log('OrionSession: curUserData exists: ' + sys.inspect(curUserData));
if(curUserData || sessionKeyOnly){ // if it exists, check it
sys.log('OrionSession: curUserData exists: ' + sys.inspect(curUserData));
var sesKeyIndex = curUserData.sessionKeys.indexOf(receivedSessionKey);
sys.puts('seskeyindex: '+sesKeyIndex);
if(sesKeyIndex> -1){
var lastSeen = curUserData.lastSeen[sesKeyIndex];
var now = new Date().getTime();
Expand All @@ -93,7 +99,10 @@ global.OrionSession = SC.Object.extend({
}
else return NO; // receivedSessionKey given does not match any known session keys
}
else return NO; // no user data found for received user name
else {
sys.puts('no curUserData');
return NO; // no user data found for received user name
}
},

getUserData: function(user){
Expand Down
2 changes: 2 additions & 0 deletions myServer.sample.js
Expand Up @@ -17,6 +17,8 @@ require('./lib/OrionServer');
var myServer = OrionServer.create({
port: 8080,
store: OrionStore.create(),
RESTPrefix: '/API/REST/Service.svc/',
allowWebSocket: NO,
authModule: OrionFileAuth.create({ fileName: './myUsers'}),
sessionModule: OrionSession.create({ sessionName: 'OrionServer' }),
policyModule: OrionPolicies.create({ policyFile: './myPolicies'})
Expand Down
7 changes: 4 additions & 3 deletions myUsers.sample.js
@@ -1,8 +1,9 @@
/*
let's do this as a kind of node module, because it saves a lot on difficult file reading stuff
let"s do this as a kind of node module, because it saves a lot on difficult file reading stuff
*/

exports.users = {
'root': { passwd: 'password', role: 'admin'},
'test': { passwd: 'test', role:'user' }
"root": { passwd: "password", role: "admin"},
"test": { passwd: "test", role:"user" },
"Alexandru.Percsi": {id: 1, loginName: "Alexandru.Percsi", passwd: "Password1234", company: "Eloqua", role: "user"}
};
2 changes: 1 addition & 1 deletion riak-js
Submodule riak-js updated from 7f0f39 to 672ac0

0 comments on commit 0d1bc5d

Please sign in to comment.