2018-01-03 04:05:53 +00:00
|
|
|
// Copyright IBM Corp. 2014,2018. All Rights Reserved.
|
2016-05-03 22:50:21 +00:00
|
|
|
// Node module: loopback
|
|
|
|
// This file is licensed under the MIT License.
|
|
|
|
// License text available at https://opensource.org/licenses/MIT
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
'use strict';
|
2013-07-18 18:44:25 +00:00
|
|
|
var assert = require('assert');
|
2015-07-20 05:47:38 +00:00
|
|
|
var utils = require('../../lib/utils');
|
2013-07-18 18:44:25 +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.
|
2014-10-13 06:41:31 +00:00
|
|
|
*
|
2014-10-09 00:26:45 +00:00
|
|
|
* @property {String} id Generated ID.
|
|
|
|
* @property {String} name Name; required.
|
|
|
|
* @property {String} description Text description
|
|
|
|
* @property {String} icon String Icon image URL.
|
|
|
|
* @property {String} owner User ID of the developer who registers the application.
|
|
|
|
* @property {String} email E-mail address
|
|
|
|
* @property {Boolean} emailVerified Whether the e-mail is verified.
|
|
|
|
* @property {String} url OAuth 2.0 application URL.
|
|
|
|
* @property {String}[] callbackUrls The OAuth 2.0 code/token callback URL.
|
|
|
|
* @property {String} status Status of the application; Either `production`, `sandbox` (default), or `disabled`.
|
|
|
|
* @property {Date} created Date Application object was created. Default: current date.
|
|
|
|
* @property {Date} modified Date Application object was modified. Default: current date.
|
2014-10-13 06:41:31 +00:00
|
|
|
*
|
|
|
|
* @property {Object} pushSettings.apns APNS configuration, see the options
|
|
|
|
* below and also
|
|
|
|
* https://github.com/argon/node-apn/blob/master/doc/apn.markdown
|
2014-10-09 00:26:45 +00:00
|
|
|
* @property {Boolean} pushSettings.apns.production Whether to use production Apple Push Notification Service (APNS) servers to send push notifications.
|
|
|
|
* If true, uses `gateway.push.apple.com:2195` and `feedback.push.apple.com:2196`.
|
|
|
|
* If false, uses `gateway.sandbox.push.apple.com:2195` and `feedback.sandbox.push.apple.com:2196`
|
|
|
|
* @property {String} pushSettings.apns.certData The certificate data loaded from the cert.pem file (APNS).
|
|
|
|
* @property {String} pushSettings.apns.keyData The key data loaded from the key.pem file (APNS).
|
|
|
|
* @property {String} pushSettings.apns.pushOptions.gateway (APNS).
|
|
|
|
* @property {Number} pushSettings.apns.pushOptions.port (APNS).
|
|
|
|
* @property {String} pushSettings.apns.feedbackOptions.gateway (APNS).
|
|
|
|
* @property {Number} pushSettings.apns.feedbackOptions.port (APNS).
|
|
|
|
* @property {Boolean} pushSettings.apns.feedbackOptions.batchFeedback (APNS).
|
|
|
|
* @property {Number} pushSettings.apns.feedbackOptions.interval (APNS).
|
|
|
|
* @property {String} pushSettings.gcm.serverApiKey: Google Cloud Messaging API key.
|
2014-10-13 06:41:31 +00:00
|
|
|
*
|
|
|
|
* @property {Boolean} authenticationEnabled
|
|
|
|
* @property {Boolean} anonymousAllowed
|
|
|
|
* @property {Array} authenticationSchemes List of authentication schemes
|
|
|
|
* (see below).
|
|
|
|
* @property {String} authenticationSchemes.scheme Scheme name.
|
|
|
|
* Supported values: `local`, `facebook`, `google`,
|
|
|
|
* `twitter`, `linkedin`, `github`.
|
|
|
|
* @property {Object} authenticationSchemes.credential
|
|
|
|
* Scheme-specific credentials.
|
|
|
|
*
|
|
|
|
* @class Application
|
|
|
|
* @inherits {PersistedModel}
|
2013-12-20 01:49:47 +00:00
|
|
|
*/
|
|
|
|
|
2014-10-13 06:41:31 +00:00
|
|
|
module.exports = function(Application) {
|
|
|
|
/*!
|
|
|
|
* A hook to generate keys before creation
|
|
|
|
* @param next
|
|
|
|
*/
|
2015-03-02 12:09:14 +00:00
|
|
|
Application.observe('before save', function(ctx, next) {
|
|
|
|
if (!ctx.instance) {
|
|
|
|
// Partial update - don't generate new keys
|
|
|
|
// NOTE(bajtos) This also means that an atomic updateOrCreate
|
|
|
|
// will not generate keys when a new record is creatd
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
|
|
|
|
var app = ctx.instance;
|
2014-10-13 06:41:31 +00:00
|
|
|
app.created = app.modified = new Date();
|
2015-10-08 05:10:14 +00:00
|
|
|
if (!app.id) {
|
|
|
|
app.id = generateKey('id', 'md5');
|
|
|
|
}
|
2014-10-13 06:41:31 +00:00
|
|
|
app.clientKey = generateKey('client');
|
|
|
|
app.javaScriptKey = generateKey('javaScript');
|
|
|
|
app.restApiKey = generateKey('restApi');
|
|
|
|
app.windowsKey = generateKey('windows');
|
|
|
|
app.masterKey = generateKey('master');
|
|
|
|
next();
|
2015-03-02 12:09:14 +00:00
|
|
|
});
|
2013-07-09 22:06:42 +00:00
|
|
|
|
2014-10-13 06:41:31 +00:00
|
|
|
/**
|
|
|
|
* Register a new application
|
|
|
|
* @param {String} owner Owner's user ID.
|
|
|
|
* @param {String} name Name of the application
|
|
|
|
* @param {Object} options Other options
|
2016-02-04 20:31:39 +00:00
|
|
|
* @callback {Function} callback Callback function
|
|
|
|
* @param {Error} err
|
|
|
|
* @promise
|
2014-10-13 06:41:31 +00:00
|
|
|
*/
|
|
|
|
Application.register = function(owner, name, options, cb) {
|
|
|
|
assert(owner, 'owner is required');
|
|
|
|
assert(name, 'name is required');
|
2013-07-18 18:44:25 +00:00
|
|
|
|
2014-10-13 06:41:31 +00:00
|
|
|
if (typeof options === 'function' && !cb) {
|
|
|
|
cb = options;
|
|
|
|
options = {};
|
2013-12-18 19:49:09 +00:00
|
|
|
}
|
2015-07-20 05:47:38 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2016-11-15 21:46:23 +00:00
|
|
|
var props = {owner: owner, name: name};
|
2014-10-13 06:41:31 +00:00
|
|
|
for (var p in options) {
|
|
|
|
if (!(p in props)) {
|
|
|
|
props[p] = options[p];
|
2013-12-18 19:49:09 +00:00
|
|
|
}
|
2014-02-07 19:14:01 +00:00
|
|
|
}
|
2014-10-13 06:41:31 +00:00
|
|
|
this.create(props, cb);
|
2015-07-20 05:47:38 +00:00
|
|
|
return cb.promise;
|
2014-10-13 06:41:31 +00:00
|
|
|
};
|
2013-10-11 20:44:10 +00:00
|
|
|
|
2014-10-13 06:41:31 +00:00
|
|
|
/**
|
|
|
|
* Reset keys for the application instance
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
*/
|
|
|
|
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-07-22 18:15:02 +00:00
|
|
|
|
2014-10-13 06:41:31 +00:00
|
|
|
/**
|
|
|
|
* Reset keys for a given application by the appId
|
|
|
|
* @param {Any} appId
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
2016-02-04 20:31:39 +00:00
|
|
|
* @promise
|
2014-10-13 06:41:31 +00:00
|
|
|
*/
|
|
|
|
Application.resetKeys = function(appId, cb) {
|
2015-07-20 05:47:38 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
2014-10-13 06:41:31 +00:00
|
|
|
this.findById(appId, function(err, app) {
|
|
|
|
if (err) {
|
2014-11-04 12:52:49 +00:00
|
|
|
if (cb) cb(err, app);
|
2014-10-13 06:41:31 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
app.resetKeys(cb);
|
|
|
|
});
|
2015-07-20 05:47:38 +00:00
|
|
|
return cb.promise;
|
2014-10-13 06:41:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Authenticate the application id and key.
|
|
|
|
*
|
2014-10-15 07:07:30 +00:00
|
|
|
* @param {Any} appId
|
|
|
|
* @param {String} key
|
|
|
|
* @callback {Function} callback
|
|
|
|
* @param {Error} err
|
|
|
|
* @param {String} matched The matching key; one of:
|
2014-10-13 06:41:31 +00:00
|
|
|
* - clientKey
|
|
|
|
* - javaScriptKey
|
|
|
|
* - restApiKey
|
|
|
|
* - windowsKey
|
|
|
|
* - masterKey
|
2016-02-04 20:31:39 +00:00
|
|
|
* @promise
|
2014-10-13 06:41:31 +00:00
|
|
|
*/
|
|
|
|
Application.authenticate = function(appId, key, cb) {
|
2015-07-20 05:47:38 +00:00
|
|
|
cb = cb || utils.createPromiseCallback();
|
|
|
|
|
2014-10-13 06:41:31 +00:00
|
|
|
this.findById(appId, function(err, app) {
|
|
|
|
if (err || !app) {
|
2015-07-20 05:47:38 +00:00
|
|
|
cb(err, null);
|
|
|
|
return cb.promise;
|
2014-10-13 06:41:31 +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,
|
2016-04-01 09:14:26 +00:00
|
|
|
keyType: keyNames[i],
|
2014-10-13 06:41:31 +00:00
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2015-07-20 05:47:38 +00:00
|
|
|
cb(null, result);
|
2014-10-13 06:41:31 +00:00
|
|
|
});
|
2015-07-20 05:47:38 +00:00
|
|
|
return cb.promise;
|
2014-10-13 06:41:31 +00:00
|
|
|
};
|
|
|
|
};
|