forked from nodester/nodester
/
middle.js
138 lines (124 loc) · 3.65 KB
/
middle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var lib = require('./lib'),
cradle = require('cradle'),
config = require('../config');
/**
* Dynamic express route to authenticate a user from CouchDB
*/
var authenticate = function(req, res, next) {
var basicauth = req.headers.authorization;
if (typeof basicauth != 'undefined' && basicauth.length > 0) {
var buff = new Buffer(basicauth.substring(basicauth.indexOf(" ") + 1), encoding = 'base64');
var creds = buff.toString('ascii');
var username = creds.substring(0, creds.indexOf(":"));
var password = creds.substring(creds.indexOf(":") + 1);
var db = lib.get_couchdb_database('nodefu');
db.get(username, function(err, doc) {
if (err) {
console.log(err);
res.writeHead(401, {
'Content-Type': 'application/json'
});
// res.end('{"status" : "failure", "message": "couchdb error."}\n');
// Notifiy Admins that couchdb is failing maybe? if not not_found error
res.end('{"status" : "failure", "message": "Invalid username or password. CouchDB lookup failed."}\n');
return;
} else {
if (doc._id == username && doc.password == lib.md5(password)) {
req.user = doc;
next();
} else {
res.writeHead(401, {
'Content-Type': 'application/json'
});
res.end('{"status" : "failure", "message": "Invalid username or password."}\n');
return;
}
}
});
} else {
res.writeHead(401, {
'Content-Type': 'application/json'
});
res.write('{"status" : "No authentication data sent."}\n');
res.end();
return;
}
};
/**
* Use CouchDB to get repo information, this route must come AFTER middle.authenticate
*/
var authenticate_app = function(req, res, next) {
//GET Request
var appname = req.param("appname");
// var appname = req.params.appname;
//POST|DELETE|PUT requests
if (!appname && req.query && req.query.appname) {
appname = req.query.appname;
}
if (!appname && req.body && req.body.appname) {
appname = req.body.appname;
}
if (appname) {
appname = appname.toLowerCase();
}
if (!appname || appname === '') {
res.writeHead(400, {
'Content-Type': 'application/json'
});
res.end('{"status" : "Must pass an application name (appname)."}\n');
return;
}
if (req.user) {
var db = lib.get_couchdb_database('apps');
db.get(appname, function(err, doc) {
if (err) {
res.writeHead(400, {
'Content-Type': 'application/json'
});
res.end('{"status" : "failure - app not found (' + appname + ')"}\n');
return;
} else {
if (doc.username == req.user._id) {
req.appname = appname;
req.repo = req.app = doc;
next();
} else {
res.writeHead(400, {
'Content-Type': 'application/json'
});
res.end('{"status" : "failure - authentication for ' + appname + ' failed."}\n');
return;
}
}
});
} else {
res.writeHead(400, {
'Content-Type': 'application/json'
});
res.end('{"status" : "failure - authentication."}\n');
return;
}
};
var deprecated = function(req, res, next) {
res.header('X-Deprecated');
next();
};
module.exports.authenticate = authenticate;
module.exports.authenticate_app = authenticate_app;
module.exports.deprecated = deprecated;
/*
* Generic request error handler.
*/
module.exports.error = function() {
return function(req, res, next) {
res.error = function(code, message) {
res.writeHead(code, {
'Content-Type': 'application/json'
});
res.end(JSON.stringify({
status: message
}));
};
next();
};
};