diff --git a/lib/models/acl.js b/lib/models/acl.js index db341f62..9fe28310 100644 --- a/lib/models/acl.js +++ b/lib/models/acl.js @@ -39,7 +39,13 @@ var ACLSchema = { permission: {type: String, enum: ['Allow', 'Deny']}, // Allow/Deny status: String, // Enabled/disabled created: Date, - lastUpdated: Date + modified: Date } -// readAccess, writeAccess --> public, userId, role \ No newline at end of file +// readAccess, writeAccess --> public, userId, role + +module.exports = function(dataSource) { + dataSource = dataSource || new require('jugglingdb').ModelBuilder(); + var ACL = dataSource.define('ACL', ACLSchema); + return ACL; +} \ No newline at end of file diff --git a/lib/models/application.js b/lib/models/application.js index b19b445b..c4761c5f 100644 --- a/lib/models/application.js +++ b/lib/models/application.js @@ -1,17 +1,26 @@ +// Authentication schemes +var AuthenticationScheme = { + scheme: String, // local, facebook, google, twitter, linkedin, github + credential: Object // Scheme-specific credentials +} + +// Push notification settings +var PushNotificationSetting = { + platform: {type: String, required: true}, // apns, gcm, mpns + configuration: Object // platform-specific configurations +} + /** * Data model for Application */ var ApplicationSchema = { // Basic information - id: {type: String, required: true}, - name: {type: String, required: true}, - description: String, // description + id: {type: String, required: true}, // The id + name: {type: String, required: true}, // The name + description: String, // The description icon: String, // The icon image url - public: Boolean, // Do we list the application in the public catalog? - permissions: [String], - owner: String, // The user id of the developer who registers the application collaborators: [String], // A list of users ids who have permissions to work on this app @@ -19,8 +28,10 @@ var ApplicationSchema = { email: String, // e-mail address emailVerified: Boolean, // Is the e-mail verified - - status: String, // Status of the application, enabled/disabled + // oAuth 2.0 settings + url: String, // The application url + callbackUrls: [String], // oAuth 2.0 code/token callback url + permissions: [String], // A list of permissions required by the application // Keys clientKey: String, @@ -30,18 +41,18 @@ var ApplicationSchema = { masterKey: String, // Push notification - pushPlatforms: [String], - pushCredentials: [], + pushSettings: [PushNotificationSetting], // User Authentication - authenticationEnabled: Boolean, - anonymousAllowed: Boolean, - schemes: [String], // Basic, facebook, github, google - attachedCredentials: [], + authenticationEnabled: {type: Boolean, default: true}, + anonymousAllowed: {type: Boolean, default: true}, + authenticationSchemes: [AuthenticationScheme], + status: {type: String, default: 'sandbox'}, // Status of the application, production/sandbox/disabled - created: Date, - lastUpdated: Date + // Timestamps + created: {type: Date, default: Date}, + modified: {type: Date, default: Date} }; @@ -49,7 +60,43 @@ var ApplicationSchema = { * Application management functions */ -// Register a new application +var crypto = require('crypto'); + +function generateKey(hmacKey, algorithm) { + hmacKey = hmacKey || 'asteroid'; + algorithm = algorithm || 'sha256'; + var hmac = crypto.createHmac(algorithm, hmacKey); + var buf = crypto.randomBytes(64); + hmac.update(buf); + return hmac.digest('base64'); +} + +module.exports = function (dataSource) { + dataSource = dataSource || new require('jugglingdb').ModelBuilder(); + + + var Application = dataSource.define('Application', ApplicationSchema); + + Application.afterInitialize = function () { + var app = this; + // use data argument to update object + app.created = app.modified = new Date(); + app.id = generateKey('id', 'sha1'); + app.clientKey = generateKey('client'); + app.javaScriptKey = generateKey('javaScript'); + app.restApiKey = generateKey('restApi'); + app.windowsKey = generateKey('windows'); + app.masterKey = generateKey('master'); + }; + + // Register a new application + Application.register = function (name, description, owner) { + return new Application({name: name, description: description, owner: owner}); + } + + return Application; +} + diff --git a/lib/models/index.js b/lib/models/index.js new file mode 100644 index 00000000..f0d994ff --- /dev/null +++ b/lib/models/index.js @@ -0,0 +1,16 @@ +var ModelBuilder = require('jugglingdb').ModelBuilder; + +var ds = new ModelBuilder(); + +exports.Application = require('./application')(ds); +exports.ACL = require('./acl')(ds); +exports.Role = require('./role')(ds); +exports.Installation = require('./installation')(ds); + +/* +console.log(new exports.Role()); +console.log(new exports.ACL()); +console.log(new exports.Application()); +console.log(new exports.Installation()); +*/ + diff --git a/lib/models/installation.js b/lib/models/installation.js index a734dc64..28d8ee8d 100644 --- a/lib/models/installation.js +++ b/lib/models/installation.js @@ -5,13 +5,22 @@ var InstallationSchema = { required: true, id: 1 }, - appId: String, - appVersion: String, - userId: String, + appId: String, // Application id + appVersion: String, // Application version + userId: String, // User id deviceToken: String, deviceType: String, subscriptions: [String], - created: Date, - lastModified: Date, - status: String + + status: {type: String, default: 'active'}, // Status of the application, production/sandbox/disabled + + // Timestamps + created: {type: Date, default: Date}, + modified: {type: Date, default: Date} }; + +module.exports = function(dataSource) { + dataSource = dataSource || new require('jugglingdb').ModelBuilder(); + var Installation = dataSource.define('Installation', InstallationSchema); + return Installation; +} diff --git a/lib/models/role.js b/lib/models/role.js index 74421708..5259d8b3 100644 --- a/lib/models/role.js +++ b/lib/models/role.js @@ -1,10 +1,18 @@ // Role model var RoleSchema = { - id: {type: String, required: true}, + id: {type: String, required: true}, // Id name: {type: String, required: true}, // The name of a role - description: String, + description: String, // Description roles: [String], // A role can be an aggregate of other roles - users: [String], // A role contains a list of users - created: Date, - lastUpdated: Date + users: [String], // A role contains a list of user ids + + // Timestamps + created: {type: Date, default: Date}, + modified: {type: Date, default: Date} +} + +module.exports = function(dataSource) { + dataSource = dataSource || new require('jugglingdb').ModelBuilder(); + var Role = dataSource.define('Role', RoleSchema); + return Role; } \ No newline at end of file diff --git a/lib/models/user.js b/lib/models/user.js index 0f170093..02215c31 100644 --- a/lib/models/user.js +++ b/lib/models/user.js @@ -57,5 +57,5 @@ var UserSchema = { */ status: String, created: Date, - lastUpdated: Date + modified: Date } \ No newline at end of file