loopback/lib/models/application.js

138 lines
4.2 KiB
JavaScript
Raw Normal View History

2013-07-09 22:06:42 +00:00
// Authentication schemes
2013-07-12 20:09:22 +00:00
var AuthenticationSchemeSchema = {
2013-07-09 22:06:42 +00:00
scheme: String, // local, facebook, google, twitter, linkedin, github
credential: Object // Scheme-specific credentials
}
2013-07-12 20:09:22 +00:00
var APNSSettingSchema = {
pushOptions: {type: {
gateway: String,
cert: String,
key: String
}},
feedbackOptions: {type: {
gateway: String,
cert: String,
key: String,
batchFeedback: Boolean,
interval: Number
}}
};
2013-07-09 22:06:42 +00:00
// Push notification settings
2013-07-12 20:09:22 +00:00
var PushNotificationSettingSchema = {
2013-07-09 22:06:42 +00:00
platform: {type: String, required: true}, // apns, gcm, mpns
2013-07-12 20:09:22 +00:00
// configuration: {type: Object} // platform-specific configurations
apns: APNSSettingSchema
2013-07-09 22:06:42 +00:00
}
2013-07-08 23:59:11 +00:00
/**
* Data model for Application
*/
var ApplicationSchema = {
// Basic information
2013-07-09 22:06:42 +00:00
id: {type: String, required: true}, // The id
name: {type: String, required: true}, // The name
description: String, // The description
2013-07-08 23:59:11 +00:00
icon: String, // The icon image url
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
// EMail
email: String, // e-mail address
emailVerified: Boolean, // Is the e-mail verified
2013-07-09 22:06:42 +00:00
// 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,
javaScriptKey: String,
restApiKey: String,
windowsKey: String,
masterKey: String,
// Push notification
2013-07-12 20:09:22 +00:00
pushSettings: [PushNotificationSettingSchema],
2013-07-08 23:59:11 +00:00
// User Authentication
2013-07-09 22:06:42 +00:00
authenticationEnabled: {type: Boolean, default: true},
anonymousAllowed: {type: Boolean, default: true},
2013-07-12 20:09:22 +00:00
authenticationSchemes: [AuthenticationSchemeSchema],
2013-07-09 22:06:42 +00:00
status: {type: String, default: 'sandbox'}, // Status of the application, production/sandbox/disabled
2013-07-09 22:06:42 +00:00
// Timestamps
created: {type: Date, default: Date},
modified: {type: Date, default: Date}
};
2013-07-08 23:59:11 +00:00
/**
* Application management functions
*/
2013-07-09 22:06:42 +00:00
var crypto = require('crypto');
function generateKey(hmacKey, algorithm) {
2013-07-16 17:42:47 +00:00
hmacKey = hmacKey || 'loopback';
2013-07-09 22:06:42 +00:00
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) {
2013-07-16 18:52:21 +00:00
dataSource = dataSource || new require('loopback-data').ModelBuilder();
2013-07-09 22:06:42 +00:00
2013-07-12 20:09:22 +00:00
// var AuthenticationScheme = dataSource.define('AuthenticationScheme', AuthenticationSchemeSchema);
// ApplicationSchema.authenticationSchemes = [AuthenticationScheme];
// var PushNotificationSetting = dataSource.define('PushNotificationSetting', PushNotificationSettingSchema);
// ApplicationSchema.pushSettings = [PushNotificationSetting];
2013-07-09 22:06:42 +00:00
var Application = dataSource.define('Application', ApplicationSchema);
2013-07-12 20:09:22 +00:00
// Application.hasMany(AuthenticationScheme, {as: 'authenticationSchemes', foreignKey: 'appId'});
// Application.hasMany(PushNotificationSetting, {as: 'pushNotificationSettings', foreignKey: 'appId'});
2013-07-09 22:06:42 +00:00
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
2013-07-12 20:09:22 +00:00
Application.register = function (name, description, owner, cb) {
Application.create({name: name, description: description, owner: owner}, cb);
}
Application.prototype.resetKeys = function(cb) {
this.clientKey = generateKey('client');
this.javaScriptKey = generateKey('javaScript');
this.restApiKey = generateKey('restApi');
this.windowsKey = generateKey('windows');
this.masterKey = generateKey('master');
this.save(cb);
2013-07-09 22:06:42 +00:00
}
return Application;
}
2013-07-08 23:59:11 +00:00