outsource db routes, schemata and logic into dbs.js
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
// Original file created by Prof.Dr. Matthias Hopf
|
||||
|
||||
/*
|
||||
* Authorization
|
||||
*/
|
||||
|
||||
var common, User;
|
||||
const ldap = require ('./ldap_ohm');
|
||||
//const crypto = require ("./crypto");
|
||||
const ldap = require ('./ldap_ohm'),
|
||||
crypto = require ("../server/crypto");
|
||||
|
||||
// deactivated is not used yet
|
||||
const serverVisibleSession = { user: true, name: true, type: true, mail: true, roles: true, deactivated: true, host: true };
|
||||
@@ -66,18 +67,16 @@ const authorization = {
|
||||
return returnError();
|
||||
return returnSession ();
|
||||
}
|
||||
|
||||
// check local database
|
||||
/*
|
||||
// check local database, then ldap
|
||||
User.findById (req.body.user) .exec (function (err, entry) {
|
||||
// If there is a local user AND it has a password associated, test against this, and only this
|
||||
/*
|
||||
if (entry != null && entry.pwd) {
|
||||
if (crypto.checkLocalAuth (entry, req.body.pwd)) {
|
||||
return fillSession (req, entry, common.arrayToHash(entry.roles), returnSession);
|
||||
}
|
||||
return returnError ();
|
||||
}
|
||||
*/
|
||||
|
||||
// check ldap
|
||||
ldap.authorize (user.toLowerCase(), pwd, function (found) {
|
||||
@@ -101,7 +100,7 @@ const authorization = {
|
||||
// Otherwise create standard user entry
|
||||
return fillSession (req, found, {user:true}, returnSession);
|
||||
});
|
||||
});
|
||||
});*/
|
||||
},
|
||||
logout: function (req, res, next) {
|
||||
fillSession (req, undefined, undefined, function (err) {
|
||||
@@ -111,7 +110,7 @@ const authorization = {
|
||||
init: function (_common) {
|
||||
common = _common;
|
||||
ldap.init (_common);
|
||||
User = require('../database/user.model.js');;
|
||||
//User = require('../database/user.model.js');;
|
||||
},
|
||||
};
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// Original file created by Prof.Dr. Matthias Hopf
|
||||
|
||||
/*
|
||||
* Common functions and imports
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
// Original file created by Prof.Dr. Matthias Hopf
|
||||
|
||||
/*
|
||||
* Crypto routines for Authorization
|
||||
*/
|
||||
|
||||
const crypto = require ("crypto");
|
||||
|
||||
const defaultHash = "sha256";
|
||||
const defaultSaltLen = 16; // More (e.g. 256) for extra paranoia
|
||||
|
||||
const mod = {
|
||||
encodePwd: function (entry, pwd) {
|
||||
return crypto.createHash (entry.hash) .update (entry.salt + ":" + pwd, 'utf8') .digest ('base64');
|
||||
},
|
||||
checkLocalAuth: function (entry, pwd) {
|
||||
if (!entry || !entry._id || !entry.hash || !entry.salt || !entry.hash || !entry.pwd ||
|
||||
!pwd || pwd === '')
|
||||
return false;
|
||||
return mod.encodePwd (entry, pwd) === entry.pwd;
|
||||
},
|
||||
fillLocalAuth: function (entry, pwd) {
|
||||
if (!entry.hash)
|
||||
entry.hash = defaultHash;
|
||||
entry.salt = crypto.randomBytes (defaultSaltLen) .toString('base64');
|
||||
entry.pwd = mod.encodePwd (entry, pwd);
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
module.exports = mod;
|
||||
+222
@@ -0,0 +1,222 @@
|
||||
/*
|
||||
* Main database access functions
|
||||
*/
|
||||
var common,
|
||||
model = {};
|
||||
|
||||
const dbs = {
|
||||
/* Method API route
|
||||
* <- to server
|
||||
* -> to client
|
||||
* Description
|
||||
*/
|
||||
routes: {
|
||||
"msg/ids": {
|
||||
/* GET /api/msg/ids [no args]
|
||||
* -> Array of message schema object ids
|
||||
* Get ALL known message ids
|
||||
*/
|
||||
get: function(req, res) {
|
||||
model.Messages.find({}, {_id: true}).exec()
|
||||
.then(results => {
|
||||
//selects id from message:
|
||||
var parsed = [];
|
||||
for (var i in results) {
|
||||
parsed.push (results[i]._id);
|
||||
}
|
||||
//var parsed = results.map (x => x._id);
|
||||
res.send(parsed);
|
||||
} )
|
||||
.catch(err => {
|
||||
console.log (err);
|
||||
res.status(500).json(err);
|
||||
});
|
||||
},
|
||||
},
|
||||
"msg/id": {
|
||||
params: ":id",
|
||||
/* GET /api/msg/id/[massage-id]
|
||||
* -> Message schema
|
||||
* Get a particular message
|
||||
*/
|
||||
get: function(req, res) {
|
||||
model.Messages.findById(req.params.id) .exec(function(err, results) {
|
||||
if (err) {
|
||||
console.log (err);
|
||||
res.status(404).json(err);
|
||||
} else {
|
||||
//console.log(JSON.stringify(results));
|
||||
res.json(results);
|
||||
}
|
||||
});
|
||||
},
|
||||
},
|
||||
"msg/search": {},
|
||||
"msg": {
|
||||
/* POST /api/msg
|
||||
* <- Message schema
|
||||
* -> Message schema
|
||||
* Create a new message
|
||||
*/
|
||||
post: function(req, res) {
|
||||
/*
|
||||
if ( !(req.body.tags instanceof Array) ) {
|
||||
return res.status(400).json({ error: "bad request" });
|
||||
}*/
|
||||
console.log("Subject: "+JSON.stringify(req.body));
|
||||
model.Messages.create({
|
||||
subject: req.body.subject,
|
||||
message: req.body.message,
|
||||
user: req.body.user,
|
||||
tags: req.body.tag
|
||||
}, function(err, result) {
|
||||
if (err) {
|
||||
return res.status(401).json(err.message);
|
||||
} else {
|
||||
res.json({message: "Message created!!"});
|
||||
}
|
||||
if (result == null) {
|
||||
return res.status(500).json("Can not create message.")
|
||||
}
|
||||
});
|
||||
},
|
||||
/* PUT /api/msg
|
||||
* <-
|
||||
* ->
|
||||
* Update a message
|
||||
*/
|
||||
//put: function(req, res) {},
|
||||
},
|
||||
"tag/ids": {},
|
||||
"tag": {},
|
||||
},
|
||||
/* Initialize requirements
|
||||
* - DB connection
|
||||
* - DB schemata
|
||||
*/
|
||||
init: function (_common) {
|
||||
common = _common;
|
||||
|
||||
/* DB Connection
|
||||
* Local db: common.config.dbLocalConn
|
||||
* TH db: common.config.dbConn
|
||||
*/
|
||||
common.mongoose.connect (common.config.dbLocalConn, {
|
||||
useNewUrlParser: true
|
||||
}).then(() => {
|
||||
console.log("Database connected successfully.");
|
||||
}).catch(err => {
|
||||
console.log('Database connection error.');
|
||||
process.exit();
|
||||
});
|
||||
|
||||
/* DB Schemata
|
||||
* Privat fields:
|
||||
* - per model: _list: Elements that are included in list fetch
|
||||
* - per entry: _comment: Comment for Admin UI - TODO: not working yet
|
||||
*/
|
||||
var messageSchema = common.mongoose.Schema({
|
||||
subject: { type: String, required: true,
|
||||
_comment: "" },
|
||||
message: { type: String, required: true,
|
||||
_comment: "" },
|
||||
user: { type: String, required: true,
|
||||
_comment: "" },
|
||||
tags: { type: [String],
|
||||
_comment: "" },
|
||||
//createtime: { type: Date, default: Date.now },
|
||||
});
|
||||
messageSchema.index({ tag:'text' });
|
||||
model.Messages = common.mongoose.model('messages', messageSchema);
|
||||
model.Messages._list = [ "" ];
|
||||
|
||||
var tagSchema = common.mongoose.Schema({
|
||||
name: { type: String, required: true,
|
||||
_comment: "" }, //unique
|
||||
});
|
||||
model.Tags = common.mongoose.model('tags', messageSchema);
|
||||
model.Tags._list = [ "" ];
|
||||
|
||||
var userSchema = common.mongoose.Schema({
|
||||
//_id: { type: String },
|
||||
name: { type: String, required: true,
|
||||
_comment: "" },
|
||||
pwd: { type: String,
|
||||
_comment: "" },
|
||||
//hash: { type: String },
|
||||
//salt: { type: String },
|
||||
//type: { type: String },
|
||||
roles: { type: [String], required: true,
|
||||
_comment: "" },
|
||||
tags: { type: [String],
|
||||
_comment: "" },
|
||||
//deactivated: { type: Boolean },
|
||||
//participating: { type: [String] },
|
||||
//host: { type: Boolean },
|
||||
bookmarks: { type: [String],
|
||||
_comment: "" },
|
||||
});
|
||||
model.Users = common.mongoose.model('users', userSchema);
|
||||
model.Users._list = [ "" ];
|
||||
},
|
||||
models: model,
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*
|
||||
app.get ('/api/ids', function (req, res) {
|
||||
Message.find({},{id: true}) .exec () .then(results => {
|
||||
//selects id from message:
|
||||
var parsed = [];
|
||||
for (var i in results) {
|
||||
parsed.push (results[i].id);
|
||||
}
|
||||
//var parsed = results.map (x => x._id);
|
||||
res.send(parsed);
|
||||
} )
|
||||
.catch(err => {
|
||||
console.log (err);
|
||||
res .status(500) .json (err);
|
||||
});
|
||||
});
|
||||
|
||||
app.get ("/api/msg/:id", function (req, res) {
|
||||
Message.findOne ({_id: req.params.id}) .exec (function (err, results){
|
||||
if (err) {
|
||||
console.log (err);
|
||||
res .status(404) .json (err);
|
||||
} else {
|
||||
console.log(JSON.stringify(results));
|
||||
res.json(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
/*app.get ("/api/msg/search/:phrase", function (req, res) {
|
||||
Message.find ({$text: {$search: req.params.phrase}) .then (function (err, results){
|
||||
if (err) {
|
||||
console.log (err);
|
||||
res .status(404) .json (err);
|
||||
} else {
|
||||
console.log(JSON.stringify(results));
|
||||
res.json(results);
|
||||
}
|
||||
});
|
||||
});
|
||||
*/
|
||||
/*
|
||||
app.post("/api/createMsg", function(req, res){
|
||||
console.log("Subject: "+JSON.stringify(req.body));
|
||||
var message = new Message( {subject: req.body.sub, message: req.body.mess, user: req.body.use, tag: req.body.ta } );
|
||||
|
||||
message.save(function(err,result){
|
||||
if(err){
|
||||
return res .status(401) .send(err.message);
|
||||
}else{
|
||||
res.json({message: "Message created!!"});
|
||||
}
|
||||
});
|
||||
});
|
||||
*/
|
||||
module.exports = dbs;
|
||||
@@ -1,3 +1,5 @@
|
||||
// Original file created by Prof.Dr. Matthias Hopf
|
||||
|
||||
/*
|
||||
* Valdiate ohm logins with ldap service
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user