loopback/lib/models/application.js

200 lines
5.1 KiB
JavaScript
Raw Normal View History

var loopback = require('../loopback');
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-07-09 22:06:42 +00:00
scheme: String, // local, facebook, google, twitter, linkedin, github
credential: Object // Scheme-specific credentials
};
2013-07-09 22:06:42 +00:00
// See https://github.com/argon/node-apn/blob/master/doc/apn.markdown
2013-07-12 20:09:22 +00:00
var APNSSettingSchema = {
production: Boolean, // production or development mode
certData: String, // The certificate data
keyData: String, // The key data
2013-07-12 20:09:22 +00:00
pushOptions: {type: {
gateway: {type: String, default: 'gateway.sandbox.push.apple.com'},
port: {type: Number, default: 2195}
2013-07-12 20:09:22 +00:00
}},
feedbackOptions: {type: {
gateway: {type: String, default: 'feedback.sandbox.push.apple.com'},
port: {type: Number, default: 2196},
2013-07-12 20:09:22 +00:00
batchFeedback: Boolean,
interval: Number
}}
};
var GcmSettingsSchema = {
serverApiKey: String
};
2013-07-09 22:06:42 +00:00
// Push notification settings
2013-07-12 20:09:22 +00:00
var PushNotificationSettingSchema = {
apns: APNSSettingSchema,
gcm: GcmSettingsSchema
};
2013-07-09 22:06:42 +00:00
2013-07-08 23:59:11 +00:00
/**
* Data model for Application
*/
var ApplicationSchema = {
id: {type: String, id: true, generated: true},
// Basic information
2013-07-09 22:06:42 +00:00
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
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, encoding) {
2013-07-16 17:42:47 +00:00
hmacKey = hmacKey || 'loopback';
2013-07-09 22:06:42 +00:00
algorithm = algorithm || 'sha256';
encoding = encoding || 'base64';
2013-07-09 22:06:42 +00:00
var hmac = crypto.createHmac(algorithm, hmacKey);
var buf = crypto.randomBytes(64);
hmac.update(buf);
return hmac.digest('base64');
}
var Application = loopback.createModel('Application', ApplicationSchema);
2013-10-23 20:25:50 +00:00
/*!
* A hook to generate keys before creation
* @param next
*/
Application.beforeCreate = function (next) {
var app = this;
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');
next();
};
/**
* Register a new application
* @param owner Owner's user id
* @param name Name of the application
* @param options Other options
* @param cb Callback function
*/
Application.register = function (owner, name, options, cb) {
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
}
Application.create(props, cb);
};
2013-07-12 20:09:22 +00:00
/**
* Reset keys for the application instance
* @param 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.modified = new Date();
this.save(cb);
};
2013-07-09 22:06:42 +00:00
/**
* Reset keys for a given application by the appId
* @param appId
* @param cb
*/
Application.resetKeys = function(appId, cb) {
Application.findById(appId, function(err, app) {
if(err) {
cb && cb(err, app);
return;
}
app.resetKeys(cb);
});
};
/**
*
* @param appId
* @param key
* @param cb
*/
Application.authenticate = function(appId, key, cb) {
Application.findById(appId, function(err, app) {
if(err || !app) {
cb && cb(err, null);
return;
}
var matched = null;
['clientKey', 'javaScriptKey', 'restApiKey', 'windowsKey', 'masterKey'].forEach(function(k) {
if(app[k] === key) {
matched = k;
}
});
cb && cb(null, matched);
});
};
module.exports = Application;
2013-07-09 22:06:42 +00:00
2013-07-08 23:59:11 +00:00