From 2e8e8f35e7e31c9d4deff3057cd8cb0520297fa5 Mon Sep 17 00:00:00 2001 From: Ruben Meyer <46384706+rxbnDE@users.noreply.github.com> Date: Thu, 20 Jun 2019 23:08:24 +0200 Subject: [PATCH] basic database models --- bin/database/models.js | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 bin/database/models.js diff --git a/bin/database/models.js b/bin/database/models.js new file mode 100644 index 0000000..14924af --- /dev/null +++ b/bin/database/models.js @@ -0,0 +1,89 @@ +/* + * This file is part of the authRxbn eco-system. + * + * (c) Ruben Meyer + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +var mongoose = require('mongoose'); +var models = {}; + +const Schema = mongoose.Schema; +const ObjectId = Schema.ObjectId; + +// user +models.user = new Schema({ + nickname: String, // Nickname + email: { + type: String, + validate: [ + { validator: function(value) { + let regex = /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i; + return regex.test(value); + } + } + ] + }, + passhash: String, // hashed password (String + separator + Salt) + token: { // last remember me cookie + type: String, + default: "" + }, + mfa: { // multi factor authentication + active: {type: Boolean, default: false}, + type: {type: String, default: ""}, + data: {type: String, default: ""} // tel number or secret token + }, + settings: {type: Object, default: {}}, // custom settings (theme etc. pp.) + roles: {type: String, default: ""}, // user-defined roles and permissions + group: {type: Number, default: 0}, // group-id for group-defined roles and permissions + reg_date: {type: Date, default: Date.now}, // registration date + last_action: {type: Date, default: Date.now}, // last action (activity date) +}); + +// group +models.group = new Schema({ + name: String, // recognizable application name; ex. "Administration" + created: {type: Date, default: Date.now}, + roles: {type: String, default: ""} // roles; separated by commas "a,b,a.b,c.*,d.z.*" +}); + +// application | service +models.application = new Schema({ + name: String, // recognizable application name; ex. "passRXBN - Password Manager" + access: String, // redirected uri; do not trust user input + secret: String, // application secret; authorize requests + description: String // service description on dashboard; markdown support +}); + +// activities +models.activity = new Schema({ + userId: Schema.Types.ObjectId, + date: { type: Date, default: Date.now}, + uri: { type: String, default: ""}, + state: { type: Boolean, default: false } +}); + +// used authcodes +models.authCode = new Schema({ + applicationId: Schema.Types.ObjectId, + userId: Schema.Types.ObjectId, + token: String, // generated token, only usable in combination with userId and applicationId + timestamp: { type: Date, default: Date.now } +}); + +module.exports = (con) => { + let mdls = {}; + + // initialize models + mdls.user = con.model('User', models.user); + mdls.group = con.model('Group', models.group); + mdls.application = con.model('Application', models.application); + mdls.activity = con.model('Activity', models.activity); + mdls.authCode = con.model('AuthCode', models.authCode); + + // return models for further processing + return mdls; +};