2013-10-11 20:44:10 +00:00
|
|
|
var loopback = require('../loopback');
|
2013-07-18 18:44:25 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
|
2013-07-09 22:06:42 +00:00
|
|
|
// Authentication schemes
|
2013-07-12 20:09:22 +00:00
|
|
|
var AuthenticationSchemeSchema = {
|
2013-12-18 19:49:09 +00:00
|
|
|
scheme: String, // local, facebook, google, twitter, linkedin, github
|
|
|
|
credential: Object // Scheme-specific credentials
|
2013-10-11 20:44:10 +00:00
|
|
|
};
|
2013-07-09 22:06:42 +00:00
|
|
|
|
2013-12-18 18:58:34 +00:00
|
|
|
// See https://github.com/argon/node-apn/blob/master/doc/apn.markdown
|
2013-07-12 20:09:22 +00:00
|
|
|
var APNSSettingSchema = {
|
2013-12-18 20:28:48 +00:00
|
|
|
/**
|
|
|
|
* production or development mode. It denotes what default APNS servers to be
|
|
|
|
* used to send notifications
|
|
|
|
* - true (production mode)
|
|
|
|
* - push: gateway.push.apple.com:2195
|
|
|
|
* - feedback: feedback.push.apple.com:2196
|
|
|
|
* - false (development mode, the default)
|
|
|
|
* - push: gateway.sandbox.push.apple.com:2195
|
|
|
|
* - feedback: feedback.sandbox.push.apple.com:2196
|
|
|
|
*/
|
|
|
|
production: Boolean,
|
|
|
|
certData: String, // The certificate data loaded from the cert.pem file
|
|
|
|
keyData: String, // The key data loaded from the key.pem file
|
2013-12-18 19:49:09 +00:00
|
|
|
|
|
|
|
pushOptions: {type: {
|
2013-12-18 20:28:48 +00:00
|
|
|
gateway: String,
|
|
|
|
port: Number
|
2013-12-18 19:49:09 +00:00
|
|
|
}},
|
|
|
|
|
|
|
|
feedbackOptions: {type: {
|
2013-12-18 20:28:48 +00:00
|
|
|
gateway: String,
|
|
|
|
port: Number,
|
2013-12-18 19:49:09 +00:00
|
|
|
batchFeedback: Boolean,
|
|
|
|
interval: Number
|
|
|
|
}}
|
2013-07-12 20:09:22 +00:00
|
|
|
};
|
|
|
|
|
2013-12-17 17:44:13 +00:00
|
|
|
var GcmSettingsSchema = {
|
2013-12-18 19:49:09 +00:00
|
|
|
serverApiKey: String
|
2013-12-18 18:58:34 +00:00
|
|
|
};
|
2013-12-17 17:44:13 +00:00
|
|
|
|
2013-07-09 22:06:42 +00:00
|
|
|
// Push notification settings
|
2013-07-12 20:09:22 +00:00
|
|
|
var PushNotificationSettingSchema = {
|
2013-12-18 19:49:09 +00:00
|
|
|
apns: APNSSettingSchema,
|
|
|
|
gcm: GcmSettingsSchema
|
2013-10-11 20:44:10 +00:00
|
|
|
};
|
2013-07-09 22:06:42 +00:00
|
|
|
|
2014-06-09 22:50:04 +00:00
|
|
|
/*!
|
2013-07-08 23:59:11 +00:00
|
|
|
* Data model for Application
|
|
|
|
*/
|
2013-06-26 23:25:51 +00:00
|
|
|
var ApplicationSchema = {
|
2014-03-17 17:01:29 +00:00
|
|
|
id: {type: String, id: true},
|
2013-12-18 19:49:09 +00:00
|
|
|
// Basic information
|
|
|
|
name: {type: String, required: true}, // The name
|
|
|
|
description: String, // The description
|
|
|
|
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
|
|
|
|
|
|
|
|
// 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
|
|
|
|
pushSettings: PushNotificationSettingSchema,
|
|
|
|
|
|
|
|
// User Authentication
|
|
|
|
authenticationEnabled: {type: Boolean, default: true},
|
|
|
|
anonymousAllowed: {type: Boolean, default: true},
|
|
|
|
authenticationSchemes: [AuthenticationSchemeSchema],
|
|
|
|
|
|
|
|
status: {type: String, default: 'sandbox'}, // Status of the application, production/sandbox/disabled
|
|
|
|
|
|
|
|
// Timestamps
|
|
|
|
created: {type: Date, default: Date},
|
|
|
|
modified: {type: Date, default: Date}
|
2013-06-26 23:25:51 +00:00
|
|
|
};
|
2013-07-08 23:59:11 +00:00
|
|
|
|
2014-03-14 00:25:21 +00:00
|
|
|
/*!
|
2013-07-08 23:59:11 +00:00
|
|
|
* Application management functions
|
|
|
|
*/
|
|
|
|
|
2013-07-09 22:06:42 +00:00
|
|
|
var crypto = require('crypto');
|
|
|
|
|
2013-07-18 18:44:25 +00:00
|
|
|
function generateKey(hmacKey, algorithm, encoding) {
|
2013-12-18 19:49:09 +00:00
|
|
|
hmacKey = hmacKey || 'loopback';
|
2014-02-07 19:14:01 +00:00
|
|
|
algorithm = algorithm || 'sha1';
|
|
|
|
encoding = encoding || 'hex';
|
2013-12-18 19:49:09 +00:00
|
|
|
var hmac = crypto.createHmac(algorithm, hmacKey);
|
2014-02-07 19:14:01 +00:00
|
|
|
var buf = crypto.randomBytes(32);
|
2013-12-18 19:49:09 +00:00
|
|
|
hmac.update(buf);
|
2014-02-07 19:14:01 +00:00
|
|
|
var key = hmac.digest(encoding);
|
|
|
|
return key;
|
2013-07-09 22:06:42 +00:00
|
|
|
}
|
|
|
|
|
2013-12-20 01:49:47 +00:00
|
|
|
/**
|
|
|
|
* Manage client applications and organize their users.
|
|
|
|
* @class
|
|
|
|
* @inherits {Model}
|
|
|
|
*/
|
|
|
|
|
2013-10-11 20:44:10 +00:00
|
|
|
var Application = loopback.createModel('Application', ApplicationSchema);
|
|
|
|
|
2013-10-23 20:25:50 +00:00
|
|
|
/*!
|
|
|
|
* A hook to generate keys before creation
|
|
|
|
* @param next
|
|
|
|
*/
|
2013-10-11 20:44:10 +00:00
|
|
|
Application.beforeCreate = function (next) {
|
2013-12-18 19:49:09 +00:00
|
|
|
var app = this;
|
|
|
|
app.created = app.modified = new Date();
|
2014-02-07 19:14:01 +00:00
|
|
|
app.id = generateKey('id', 'md5');
|
2013-12-18 19:49:09 +00:00
|
|
|
app.clientKey = generateKey('client');
|
|
|
|
app.javaScriptKey = generateKey('javaScript');
|
|
|
|
app.restApiKey = generateKey('restApi');
|
|
|
|
app.windowsKey = generateKey('windows');
|
|
|
|
app.masterKey = generateKey('master');
|
|
|
|
next();
|
2013-10-11 20:44:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a new application
|
2014-06-09 22:50:04 +00:00
|
|
|
* @param {String} owner Owner's user ID.
|
|
|
|
* @param {String} name Name of the application
|
|
|
|
* @param {Object} options Other options
|
|
|
|
* @param {Function} callback Callback function
|
2013-10-11 20:44:10 +00:00
|
|
|
*/
|
|
|
|
Application.register = function (owner, name, options, cb) {
|
2013-12-18 19:49:09 +00:00
|
|
|
assert(owner, 'owner is required');
|
|
|
|
assert(name, 'name is required');
|
|
|
|
|
|
|
|
if (typeof options === 'function' && !cb) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
|
|
|
}
|
|
|
|
var props = {owner: owner, name: name};
|
|
|
|
for (var p in options) {
|
|
|
|
if (!(p in props)) {
|
|
|
|
props[p] = options[p];
|
2013-07-12 20:09:22 +00:00
|
|
|
}
|
2013-12-18 19:49:09 +00:00
|
|
|
}
|
2014-01-14 23:34:44 +00:00
|
|
|
this.create(props, cb);
|
2013-10-11 20:44:10 +00:00
|
|
|
};
|
2013-07-12 20:09:22 +00:00
|
|
|
|
2013-10-11 20:44:10 +00:00
|
|
|
/**
|
|
|
|
* Reset keys for the application instance
|
2013-12-20 01:49:47 +00:00
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
2013-10-11 20:44:10 +00:00
|
|
|
*/
|
2013-12-18 19:49:09 +00:00
|
|
|
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.modified = new Date();
|
|
|
|
this.save(cb);
|
2013-10-11 20:44:10 +00:00
|
|
|
};
|
2013-07-09 22:06:42 +00:00
|
|
|
|
2013-10-11 20:44:10 +00:00
|
|
|
/**
|
|
|
|
* Reset keys for a given application by the appId
|
2013-12-20 01:49:47 +00:00
|
|
|
* @param {Any} appId
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
2013-10-11 20:44:10 +00:00
|
|
|
*/
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.resetKeys = function (appId, cb) {
|
2014-01-14 23:34:44 +00:00
|
|
|
this.findById(appId, function (err, app) {
|
2013-12-18 19:49:09 +00:00
|
|
|
if (err) {
|
|
|
|
cb && cb(err, app);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
app.resetKeys(cb);
|
|
|
|
});
|
2013-10-11 20:44:10 +00:00
|
|
|
};
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2013-10-11 20:44:10 +00:00
|
|
|
/**
|
2013-12-20 01:49:47 +00:00
|
|
|
* Authenticate the application id and key.
|
|
|
|
*
|
2014-03-14 00:25:21 +00:00
|
|
|
* `matched` parameter is one of:
|
2013-12-20 01:49:47 +00:00
|
|
|
* - clientKey
|
|
|
|
* - javaScriptKey
|
|
|
|
* - restApiKey
|
|
|
|
* - windowsKey
|
|
|
|
* - masterKey
|
2013-10-11 20:44:10 +00:00
|
|
|
*
|
2013-12-20 01:49:47 +00:00
|
|
|
* @param {Any} appId
|
|
|
|
* @param {String} key
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
2014-03-14 00:25:21 +00:00
|
|
|
* @param {String} matched The matching key
|
2013-10-11 20:44:10 +00:00
|
|
|
*/
|
2013-12-18 19:49:09 +00:00
|
|
|
Application.authenticate = function (appId, key, cb) {
|
2014-01-14 23:34:44 +00:00
|
|
|
this.findById(appId, function (err, app) {
|
2013-12-18 19:49:09 +00:00
|
|
|
if (err || !app) {
|
|
|
|
cb && cb(err, null);
|
|
|
|
return;
|
|
|
|
}
|
2014-02-07 19:14:01 +00:00
|
|
|
var result = null;
|
|
|
|
var keyNames = ['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey'];
|
|
|
|
for (var i = 0; i < keyNames.length; i++) {
|
|
|
|
if (app[keyNames[i]] === key) {
|
|
|
|
result = {
|
|
|
|
application: app,
|
|
|
|
keyType: keyNames[i]
|
|
|
|
};
|
|
|
|
break;
|
2013-12-18 19:49:09 +00:00
|
|
|
}
|
2014-02-07 19:14:01 +00:00
|
|
|
}
|
|
|
|
cb && cb(null, result);
|
2013-12-18 19:49:09 +00:00
|
|
|
});
|
2013-10-11 20:44:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = Application;
|
2013-07-22 18:15:02 +00:00
|
|
|
|