reformat according to jshint
This commit is contained in:
parent
51c4b83a3c
commit
37248a2c27
@ -1,28 +1,28 @@
|
|||||||
/**
|
/**
|
||||||
* GIST: https://gist.github.com/eordano/3e80ee3383554e94a08e
|
* GIST: https://gist.github.com/eordano/3e80ee3383554e94a08e
|
||||||
*/
|
*/
|
||||||
(function () {
|
(function() {
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var _ = require('lodash');
|
var _ = require('lodash');
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
var bitcore = require('bitcore');
|
var bitcore = require('bitcore');
|
||||||
var crypto = require('crypto');
|
var crypto = require('crypto');
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var levelup = require('levelup');
|
var levelup = require('levelup');
|
||||||
var nodemailer = require('nodemailer');
|
var nodemailer = require('nodemailer');
|
||||||
var querystring = require('querystring');
|
var querystring = require('querystring');
|
||||||
|
|
||||||
var logger = require('../lib/logger').logger;
|
var logger = require('../lib/logger').logger;
|
||||||
var globalConfig = require('../config/config');
|
var globalConfig = require('../config/config');
|
||||||
|
|
||||||
var emailPlugin = {};
|
var emailPlugin = {};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constant enum with the errors that the application may return
|
* Constant enum with the errors that the application may return
|
||||||
*/
|
*/
|
||||||
emailPlugin.errors = {
|
emailPlugin.errors = {
|
||||||
MISSING_PARAMETER: {
|
MISSING_PARAMETER: {
|
||||||
code: 400,
|
code: 400,
|
||||||
message: 'Missing required parameter'
|
message: 'Missing required parameter'
|
||||||
@ -47,38 +47,38 @@ emailPlugin.errors = {
|
|||||||
code: 403,
|
code: 403,
|
||||||
message: 'The provided code is invalid'
|
message: 'The provided code is invalid'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var EMAIL_TO_PASSPHRASE = 'email-to-passphrase-';
|
var EMAIL_TO_PASSPHRASE = 'email-to-passphrase-';
|
||||||
var STORED_VALUE = 'emailstore-';
|
var STORED_VALUE = 'emailstore-';
|
||||||
var PENDING = 'pending-';
|
var PENDING = 'pending-';
|
||||||
var VALIDATED = 'validated-';
|
var VALIDATED = 'validated-';
|
||||||
|
|
||||||
var SEPARATOR = '#';
|
var SEPARATOR = '#';
|
||||||
var MAX_ALLOWED_STORAGE = 1024 * 100 /* no more than 100 kb */;
|
var MAX_ALLOWED_STORAGE = 1024 * 100 /* no more than 100 kb */ ;
|
||||||
|
|
||||||
var valueKey = function(email, key) {
|
var valueKey = function(email, key) {
|
||||||
return STORED_VALUE + bitcore.util.twoSha256(email + SEPARATOR + key).toString('hex');
|
return STORED_VALUE + bitcore.util.twoSha256(email + SEPARATOR + key).toString('hex');
|
||||||
};
|
};
|
||||||
|
|
||||||
var pendingKey = function(email) {
|
var pendingKey = function(email) {
|
||||||
return PENDING + email;
|
return PENDING + email;
|
||||||
};
|
};
|
||||||
|
|
||||||
var validatedKey = function(email) {
|
var validatedKey = function(email) {
|
||||||
return VALIDATED + bitcore.util.twoSha256(email).toString('hex');
|
return VALIDATED + bitcore.util.twoSha256(email).toString('hex');
|
||||||
};
|
};
|
||||||
|
|
||||||
var emailToPassphrase = function(email) {
|
var emailToPassphrase = function(email) {
|
||||||
return EMAIL_TO_PASSPHRASE + bitcore.util.twoSha256(email).toString('hex');
|
return EMAIL_TO_PASSPHRASE + bitcore.util.twoSha256(email).toString('hex');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initializes the plugin
|
* Initializes the plugin
|
||||||
*
|
*
|
||||||
* @param {Object} config
|
* @param {Object} config
|
||||||
*/
|
*/
|
||||||
emailPlugin.init = function (config) {
|
emailPlugin.init = function(config) {
|
||||||
logger.info('Using emailstore plugin');
|
logger.info('Using emailstore plugin');
|
||||||
|
|
||||||
var path = globalConfig.leveldb + '/emailstore' + (globalConfig.name ? ('-' + globalConfig.name) : '');
|
var path = globalConfig.leveldb + '/emailstore' + (globalConfig.name ? ('-' + globalConfig.name) : '');
|
||||||
@ -92,18 +92,15 @@ emailPlugin.init = function (config) {
|
|||||||
emailPlugin.crypto = config.crypto || crypto;
|
emailPlugin.crypto = config.crypto || crypto;
|
||||||
|
|
||||||
emailPlugin.confirmUrl = (
|
emailPlugin.confirmUrl = (
|
||||||
process.env.INSIGHT_EMAIL_CONFIRM_HOST
|
process.env.INSIGHT_EMAIL_CONFIRM_HOST || config.confirmUrl || 'https://insight.bitpay.com'
|
||||||
|| config.confirmUrl
|
|
||||||
|| 'https://insight.bitpay.com'
|
|
||||||
) + globalConfig.apiPrefix + '/email/validate';
|
) + globalConfig.apiPrefix + '/email/validate';
|
||||||
|
|
||||||
emailPlugin.redirectUrl = (
|
emailPlugin.redirectUrl = (
|
||||||
config.redirectUrl
|
config.redirectUrl || 'https://copay.io/in/app?confirmed=true'
|
||||||
|| 'https://copay.io/in/app?confirmed=true'
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function that ends a requests showing the user an error. The response body will be a JSON
|
* Helper function that ends a requests showing the user an error. The response body will be a JSON
|
||||||
* encoded object with only one property with key "error" and value <tt>error.message</tt>, one of
|
* encoded object with only one property with key "error" and value <tt>error.message</tt>, one of
|
||||||
* the parameters of the function
|
* the parameters of the function
|
||||||
@ -114,19 +111,22 @@ emailPlugin.init = function (config) {
|
|||||||
* @param {Express.Response} response - the express.js response. the methods status, json, and end
|
* @param {Express.Response} response - the express.js response. the methods status, json, and end
|
||||||
* will be called, terminating the request.
|
* will be called, terminating the request.
|
||||||
*/
|
*/
|
||||||
emailPlugin.returnError = function (error, response) {
|
emailPlugin.returnError = function(error, response) {
|
||||||
response.status(error.code).json({error: error.message}).end();
|
response.status(error.code).json({
|
||||||
};
|
error: error.message
|
||||||
|
}).end();
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper that sends a verification email.
|
* Helper that sends a verification email.
|
||||||
*
|
*
|
||||||
* @param {string} email - the user's email
|
* @param {string} email - the user's email
|
||||||
* @param {string} secret - the verification secret
|
* @param {string} secret - the verification secret
|
||||||
*/
|
*/
|
||||||
emailPlugin.sendVerificationEmail = function (email, secret) {
|
emailPlugin.sendVerificationEmail = function(email, secret) {
|
||||||
var confirmUrl = emailPlugin.makeConfirmUrl(email, secret);
|
var confirmUrl = emailPlugin.makeConfirmUrl(email, secret);
|
||||||
async.series([
|
async.series([
|
||||||
|
|
||||||
function(callback) {
|
function(callback) {
|
||||||
emailPlugin.makeEmailBody({
|
emailPlugin.makeEmailBody({
|
||||||
email: email,
|
email: email,
|
||||||
@ -152,7 +152,7 @@ emailPlugin.sendVerificationEmail = function (email, secret) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// send mail with defined transport object
|
// send mail with defined transport object
|
||||||
emailPlugin.email.sendMail(mailOptions, function (err, info) {
|
emailPlugin.email.sendMail(mailOptions, function(err, info) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('An error occurred when trying to send email to ' + email, err);
|
logger.error('An error occurred when trying to send email to ' + email, err);
|
||||||
} else {
|
} else {
|
||||||
@ -160,19 +160,19 @@ emailPlugin.sendVerificationEmail = function (email, secret) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
emailPlugin.makeConfirmUrl = function(email, secret) {
|
emailPlugin.makeConfirmUrl = function(email, secret) {
|
||||||
return emailPlugin.confirmUrl + (
|
return emailPlugin.confirmUrl + (
|
||||||
'?email=' + encodeURIComponent(email) + '&verification_code='+secret
|
'?email=' + encodeURIComponent(email) + '&verification_code=' + secret
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a function that reads an underscore template and uses the `opts` param
|
* Returns a function that reads an underscore template and uses the `opts` param
|
||||||
* to build an email body
|
* to build an email body
|
||||||
*/
|
*/
|
||||||
var applyTemplate = function(templateFilename) {
|
var applyTemplate = function(templateFilename) {
|
||||||
return function(opts, callback) {
|
return function(opts, callback) {
|
||||||
fs.readFile(__dirname + '/emailTemplates/' + emailPlugin[templateFilename],
|
fs.readFile(__dirname + '/emailTemplates/' + emailPlugin[templateFilename],
|
||||||
function(err, template) {
|
function(err, template) {
|
||||||
@ -180,16 +180,16 @@ var applyTemplate = function(templateFilename) {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
emailPlugin.makeEmailBody = applyTemplate('textTemplate');
|
emailPlugin.makeEmailBody = applyTemplate('textTemplate');
|
||||||
emailPlugin.makeEmailHTMLBody = applyTemplate('htmlTemplate');
|
emailPlugin.makeEmailHTMLBody = applyTemplate('htmlTemplate');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} email
|
* @param {string} email
|
||||||
* @param {Function(err, boolean)} callback
|
* @param {Function(err, boolean)} callback
|
||||||
*/
|
*/
|
||||||
emailPlugin.exists = function(email, callback) {
|
emailPlugin.exists = function(email, callback) {
|
||||||
emailPlugin.db.get(emailToPassphrase(email), function(err, value) {
|
emailPlugin.db.get(emailToPassphrase(email), function(err, value) {
|
||||||
if (err && err.notFound) {
|
if (err && err.notFound) {
|
||||||
return callback(null, false);
|
return callback(null, false);
|
||||||
@ -198,14 +198,14 @@ emailPlugin.exists = function(email, callback) {
|
|||||||
}
|
}
|
||||||
return callback(null, true);
|
return callback(null, true);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} email
|
* @param {string} email
|
||||||
* @param {string} passphrase
|
* @param {string} passphrase
|
||||||
* @param {Function(err, boolean)} callback
|
* @param {Function(err, boolean)} callback
|
||||||
*/
|
*/
|
||||||
emailPlugin.checkPassphrase = function(email, passphrase, callback) {
|
emailPlugin.checkPassphrase = function(email, passphrase, callback) {
|
||||||
emailPlugin.db.get(emailToPassphrase(email), function(err, retrievedPassphrase) {
|
emailPlugin.db.get(emailToPassphrase(email), function(err, retrievedPassphrase) {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.notFound) {
|
if (err.notFound) {
|
||||||
@ -216,15 +216,15 @@ emailPlugin.checkPassphrase = function(email, passphrase, callback) {
|
|||||||
}
|
}
|
||||||
return callback(err, passphrase === retrievedPassphrase);
|
return callback(err, passphrase === retrievedPassphrase);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} email
|
* @param {string} email
|
||||||
* @param {string} passphrase
|
* @param {string} passphrase
|
||||||
* @param {Function(err)} callback
|
* @param {Function(err)} callback
|
||||||
*/
|
*/
|
||||||
emailPlugin.savePassphrase = function(email, passphrase, callback) {
|
emailPlugin.savePassphrase = function(email, passphrase, callback) {
|
||||||
emailPlugin.db.put(emailToPassphrase(email), passphrase, function(err) {
|
emailPlugin.db.put(emailToPassphrase(email), passphrase, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('error saving passphrase', err);
|
logger.error('error saving passphrase', err);
|
||||||
@ -232,15 +232,15 @@ emailPlugin.savePassphrase = function(email, passphrase, callback) {
|
|||||||
}
|
}
|
||||||
return callback(null);
|
return callback(null);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} email
|
* @param {string} email
|
||||||
* @param {string} key
|
* @param {string} key
|
||||||
* @param {string} record
|
* @param {string} record
|
||||||
* @param {Function(err)} callback
|
* @param {Function(err)} callback
|
||||||
*/
|
*/
|
||||||
emailPlugin.saveEncryptedData = function(email, key, record, callback) {
|
emailPlugin.saveEncryptedData = function(email, key, record, callback) {
|
||||||
emailPlugin.db.put(valueKey(email, key), record, function(err) {
|
emailPlugin.db.put(valueKey(email, key), record, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('error saving encrypted data', email, key, record, err);
|
logger.error('error saving encrypted data', email, key, record, err);
|
||||||
@ -248,9 +248,9 @@ emailPlugin.saveEncryptedData = function(email, key, record, callback) {
|
|||||||
}
|
}
|
||||||
return callback();
|
return callback();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
emailPlugin.createVerificationSecretAndSendEmail = function (email, callback) {
|
emailPlugin.createVerificationSecretAndSendEmail = function(email, callback) {
|
||||||
emailPlugin.createVerificationSecret(email, function(err, secret) {
|
emailPlugin.createVerificationSecret(email, function(err, secret) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('error saving verification secret', email, secret, err);
|
logger.error('error saving verification secret', email, secret, err);
|
||||||
@ -261,19 +261,19 @@ emailPlugin.createVerificationSecretAndSendEmail = function (email, callback) {
|
|||||||
}
|
}
|
||||||
callback();
|
callback();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates and stores a verification secret in the database.
|
* Creates and stores a verification secret in the database.
|
||||||
*
|
*
|
||||||
* @param {string} email - the user's email
|
* @param {string} email - the user's email
|
||||||
* @param {Function} callback - will be called with params (err, secret)
|
* @param {Function} callback - will be called with params (err, secret)
|
||||||
*/
|
*/
|
||||||
emailPlugin.createVerificationSecret = function (email, callback) {
|
emailPlugin.createVerificationSecret = function(email, callback) {
|
||||||
emailPlugin.db.get(pendingKey(email), function(err, value) {
|
emailPlugin.db.get(pendingKey(email), function(err, value) {
|
||||||
if (err && err.notFound) {
|
if (err && err.notFound) {
|
||||||
var secret = emailPlugin.crypto.randomBytes(16).toString('hex');
|
var secret = emailPlugin.crypto.randomBytes(16).toString('hex');
|
||||||
emailPlugin.db.put(pendingKey(email), secret, function (err) {
|
emailPlugin.db.put(pendingKey(email), secret, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error('error saving pending data:', email, secret);
|
logger.error('error saving pending data:', email, secret);
|
||||||
return callback(emailPlugin.errors.INTERNAL_ERROR);
|
return callback(emailPlugin.errors.INTERNAL_ERROR);
|
||||||
@ -284,13 +284,13 @@ emailPlugin.createVerificationSecret = function (email, callback) {
|
|||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} email
|
* @param {string} email
|
||||||
* @param {Function(err)} callback
|
* @param {Function(err)} callback
|
||||||
*/
|
*/
|
||||||
emailPlugin.retrieveByEmailAndKey = function(email, key, callback) {
|
emailPlugin.retrieveByEmailAndKey = function(email, key, callback) {
|
||||||
emailPlugin.db.get(valueKey(email, key), function(error, value) {
|
emailPlugin.db.get(valueKey(email, key), function(error, value) {
|
||||||
if (error) {
|
if (error) {
|
||||||
if (error.notFound) {
|
if (error.notFound) {
|
||||||
@ -300,9 +300,9 @@ emailPlugin.retrieveByEmailAndKey = function(email, key, callback) {
|
|||||||
}
|
}
|
||||||
return callback(null, value);
|
return callback(null, value);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
emailPlugin.retrieveDataByEmailAndPassphrase = function(email, key, passphrase, callback) {
|
emailPlugin.retrieveDataByEmailAndPassphrase = function(email, key, passphrase, callback) {
|
||||||
emailPlugin.checkPassphrase(email, passphrase, function(err, matches) {
|
emailPlugin.checkPassphrase(email, passphrase, function(err, matches) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -313,9 +313,9 @@ emailPlugin.retrieveDataByEmailAndPassphrase = function(email, key, passphrase,
|
|||||||
return callback(emailPlugin.errors.INVALID_CODE);
|
return callback(emailPlugin.errors.INVALID_CODE);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Store a record in the database. The underlying database is merely a levelup instance (a key
|
* Store a record in the database. The underlying database is merely a levelup instance (a key
|
||||||
* value store) that uses the email concatenated with the secret as a key to store the record.
|
* value store) that uses the email concatenated with the secret as a key to store the record.
|
||||||
* The request is expected to contain the parameters:
|
* The request is expected to contain the parameters:
|
||||||
@ -326,7 +326,7 @@ emailPlugin.retrieveDataByEmailAndPassphrase = function(email, key, passphrase,
|
|||||||
* @param {Express.Request} request
|
* @param {Express.Request} request
|
||||||
* @param {Express.Response} response
|
* @param {Express.Response} response
|
||||||
*/
|
*/
|
||||||
emailPlugin.save = function (request, response) {
|
emailPlugin.save = function(request, response) {
|
||||||
|
|
||||||
var queryData = '';
|
var queryData = '';
|
||||||
var credentials = emailPlugin.getCredentialsFromRequest(request);
|
var credentials = emailPlugin.getCredentialsFromRequest(request);
|
||||||
@ -336,14 +336,16 @@ emailPlugin.save = function (request, response) {
|
|||||||
var email = credentials.email;
|
var email = credentials.email;
|
||||||
var passphrase = credentials.passphrase;
|
var passphrase = credentials.passphrase;
|
||||||
|
|
||||||
request.on('data', function (data) {
|
request.on('data', function(data) {
|
||||||
queryData += data;
|
queryData += data;
|
||||||
if (queryData.length > MAX_ALLOWED_STORAGE) {
|
if (queryData.length > MAX_ALLOWED_STORAGE) {
|
||||||
queryData = '';
|
queryData = '';
|
||||||
response.writeHead(413, {'Content-Type': 'text/plain'}).end();
|
response.writeHead(413, {
|
||||||
|
'Content-Type': 'text/plain'
|
||||||
|
}).end();
|
||||||
request.connection.destroy();
|
request.connection.destroy();
|
||||||
}
|
}
|
||||||
}).on('end', function () {
|
}).on('end', function() {
|
||||||
var params = querystring.parse(queryData);
|
var params = querystring.parse(queryData);
|
||||||
var key = params.key;
|
var key = params.key;
|
||||||
var record = params.record;
|
var record = params.record;
|
||||||
@ -353,14 +355,14 @@ emailPlugin.save = function (request, response) {
|
|||||||
|
|
||||||
emailPlugin.processPost(request, response, email, key, passphrase, record);
|
emailPlugin.processPost(request, response, email, key, passphrase, record);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
emailPlugin.processPost = function(request, response, email, key, passphrase, record) {
|
emailPlugin.processPost = function(request, response, email, key, passphrase, record) {
|
||||||
async.series([
|
async.series([
|
||||||
/**
|
/**
|
||||||
* Try to fetch this user's email. If it exists, check the secret is the same.
|
* Try to fetch this user's email. If it exists, check the secret is the same.
|
||||||
*/
|
*/
|
||||||
function (callback) {
|
function(callback) {
|
||||||
emailPlugin.exists(email, function(err, exists) {
|
emailPlugin.exists(email, function(err, exists) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -388,7 +390,7 @@ emailPlugin.processPost = function(request, response, email, key, passphrase, re
|
|||||||
/**
|
/**
|
||||||
* Save the encrypted private key in the storage.
|
* Save the encrypted private key in the storage.
|
||||||
*/
|
*/
|
||||||
function (callback) {
|
function(callback) {
|
||||||
emailPlugin.saveEncryptedData(email, key, record, function(err) {
|
emailPlugin.saveEncryptedData(email, key, record, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
@ -399,27 +401,31 @@ emailPlugin.processPost = function(request, response, email, key, passphrase, re
|
|||||||
/**
|
/**
|
||||||
* Create and store the verification secret. If successful, send a verification email.
|
* Create and store the verification secret. If successful, send a verification email.
|
||||||
*/
|
*/
|
||||||
function (callback) {
|
function(callback) {
|
||||||
emailPlugin.createVerificationSecretAndSendEmail(email, function (err) {
|
emailPlugin.createVerificationSecretAndSendEmail(email, function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
callback({code: 500, message: err});
|
callback({
|
||||||
|
code: 500,
|
||||||
|
message: err
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
], function (err) {
|
], function(err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
emailPlugin.returnError(err, response);
|
emailPlugin.returnError(err, response);
|
||||||
} else {
|
} else {
|
||||||
response.json({success: true}).end();
|
response.json({
|
||||||
|
success: true
|
||||||
|
}).end();
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
);
|
};
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
emailPlugin.getCredentialsFromRequest = function(request) {
|
emailPlugin.getCredentialsFromRequest = function(request) {
|
||||||
if (!request.header('authorization')) {
|
if (!request.header('authorization')) {
|
||||||
return emailPlugin.errors.INVALID_REQUEST;
|
return emailPlugin.errors.INVALID_REQUEST;
|
||||||
}
|
}
|
||||||
@ -431,13 +437,16 @@ emailPlugin.getCredentialsFromRequest = function(request) {
|
|||||||
var email = authHeader.substr(0, splitIndex);
|
var email = authHeader.substr(0, splitIndex);
|
||||||
var passphrase = authHeader.substr(splitIndex + 1);
|
var passphrase = authHeader.substr(splitIndex + 1);
|
||||||
|
|
||||||
return {email: email, passphrase: passphrase};
|
return {
|
||||||
};
|
email: email,
|
||||||
|
passphrase: passphrase
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve a record from the database
|
* Retrieve a record from the database
|
||||||
*/
|
*/
|
||||||
emailPlugin.retrieve = function (request, response) {
|
emailPlugin.retrieve = function(request, response) {
|
||||||
var credentialsResult = emailPlugin.getCredentialsFromRequest(request);
|
var credentialsResult = emailPlugin.getCredentialsFromRequest(request);
|
||||||
if (_.contains(emailPlugin.errors, credentialsResult)) {
|
if (_.contains(emailPlugin.errors, credentialsResult)) {
|
||||||
return emailPlugin.returnError(credentialsResult);
|
return emailPlugin.returnError(credentialsResult);
|
||||||
@ -450,15 +459,15 @@ emailPlugin.retrieve = function (request, response) {
|
|||||||
return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response);
|
return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
emailPlugin.retrieveDataByEmailAndPassphrase(email, key, passphrase, function (err, value) {
|
emailPlugin.retrieveDataByEmailAndPassphrase(email, key, passphrase, function(err, value) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return emailPlugin.returnError(err, response);
|
return emailPlugin.returnError(err, response);
|
||||||
}
|
}
|
||||||
response.send(value).end();
|
response.send(value).end();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Marks an email as validated
|
* Marks an email as validated
|
||||||
*
|
*
|
||||||
* The two expected params are:
|
* The two expected params are:
|
||||||
@ -468,29 +477,38 @@ emailPlugin.retrieve = function (request, response) {
|
|||||||
* @param {Express.Request} request
|
* @param {Express.Request} request
|
||||||
* @param {Express.Response} response
|
* @param {Express.Response} response
|
||||||
*/
|
*/
|
||||||
emailPlugin.validate = function (request, response) {
|
emailPlugin.validate = function(request, response) {
|
||||||
var email = request.param('email');
|
var email = request.param('email');
|
||||||
var secret = request.param('verification_code');
|
var secret = request.param('verification_code');
|
||||||
if (!email || !secret) {
|
if (!email || !secret) {
|
||||||
return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response);
|
return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
emailPlugin.db.get(pendingKey(email), function (err, value) {
|
emailPlugin.db.get(pendingKey(email), function(err, value) {
|
||||||
if (err) {
|
if (err) {
|
||||||
if (err.notFound) {
|
if (err.notFound) {
|
||||||
return emailPlugin.returnError(emailPlugin.errors.NOT_FOUND, response);
|
return emailPlugin.returnError(emailPlugin.errors.NOT_FOUND, response);
|
||||||
}
|
}
|
||||||
return emailPlugin.returnError({code: 500, message: err}, response);
|
return emailPlugin.returnError({
|
||||||
|
code: 500,
|
||||||
|
message: err
|
||||||
|
}, response);
|
||||||
} else if (value !== secret) {
|
} else if (value !== secret) {
|
||||||
return emailPlugin.returnError(emailPlugin.errors.INVALID_CODE, response);
|
return emailPlugin.returnError(emailPlugin.errors.INVALID_CODE, response);
|
||||||
} else {
|
} else {
|
||||||
emailPlugin.db.put(validatedKey(email), true, function (err, value) {
|
emailPlugin.db.put(validatedKey(email), true, function(err, value) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return emailPlugin.returnError({code: 500, message: err}, response);
|
return emailPlugin.returnError({
|
||||||
|
code: 500,
|
||||||
|
message: err
|
||||||
|
}, response);
|
||||||
} else {
|
} else {
|
||||||
emailPlugin.db.del(pendingKey(email), function (err, value) {
|
emailPlugin.db.del(pendingKey(email), function(err, value) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return emailPlugin.returnError({code: 500, message: err}, response);
|
return emailPlugin.returnError({
|
||||||
|
code: 500,
|
||||||
|
message: err
|
||||||
|
}, response);
|
||||||
} else {
|
} else {
|
||||||
response.redirect(emailPlugin.redirectUrl);
|
response.redirect(emailPlugin.redirectUrl);
|
||||||
}
|
}
|
||||||
@ -499,15 +517,15 @@ emailPlugin.validate = function (request, response) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Changes an user's passphrase
|
* Changes an user's passphrase
|
||||||
*
|
*
|
||||||
* @param {Express.Request} request
|
* @param {Express.Request} request
|
||||||
* @param {Express.Response} response
|
* @param {Express.Response} response
|
||||||
*/
|
*/
|
||||||
emailPlugin.changePassphrase = function (request, response) {
|
emailPlugin.changePassphrase = function(request, response) {
|
||||||
var credentialsResult = emailPlugin.getCredentialsFromRequest(request);
|
var credentialsResult = emailPlugin.getCredentialsFromRequest(request);
|
||||||
if (_.contains(emailPlugin.errors, credentialsResult)) {
|
if (_.contains(emailPlugin.errors, credentialsResult)) {
|
||||||
return emailPlugin.returnError(credentialsResult);
|
return emailPlugin.returnError(credentialsResult);
|
||||||
@ -516,37 +534,41 @@ emailPlugin.changePassphrase = function (request, response) {
|
|||||||
var passphrase = credentialsResult.passphrase;
|
var passphrase = credentialsResult.passphrase;
|
||||||
|
|
||||||
var queryData = '';
|
var queryData = '';
|
||||||
request.on('data', function (data) {
|
request.on('data', function(data) {
|
||||||
queryData += data;
|
queryData += data;
|
||||||
if (queryData.length > MAX_ALLOWED_STORAGE) {
|
if (queryData.length > MAX_ALLOWED_STORAGE) {
|
||||||
queryData = '';
|
queryData = '';
|
||||||
response.writeHead(413, {'Content-Type': 'text/plain'}).end();
|
response.writeHead(413, {
|
||||||
|
'Content-Type': 'text/plain'
|
||||||
|
}).end();
|
||||||
request.connection.destroy();
|
request.connection.destroy();
|
||||||
}
|
}
|
||||||
}).on('end', function () {
|
}).on('end', function() {
|
||||||
var params = querystring.parse(queryData);
|
var params = querystring.parse(queryData);
|
||||||
var newPassphrase = params.newPassphrase;
|
var newPassphrase = params.newPassphrase;
|
||||||
if (!email || !passphrase || !newPassphrase) {
|
if (!email || !passphrase || !newPassphrase) {
|
||||||
return emailPlugin.returnError(emailPlugin.errors.INVALID_REQUEST, response);
|
return emailPlugin.returnError(emailPlugin.errors.INVALID_REQUEST, response);
|
||||||
}
|
}
|
||||||
emailPlugin.checkPassphrase(email, passphrase, function (error) {
|
emailPlugin.checkPassphrase(email, passphrase, function(error) {
|
||||||
if (error) {
|
if (error) {
|
||||||
return emailPlugin.returnError(error, response);
|
return emailPlugin.returnError(error, response);
|
||||||
}
|
}
|
||||||
emailPlugin.savePassphrase(email, newPassphrase, function (error) {
|
emailPlugin.savePassphrase(email, newPassphrase, function(error) {
|
||||||
if (error) {
|
if (error) {
|
||||||
return emailPlugin.returnError(error, response);
|
return emailPlugin.returnError(error, response);
|
||||||
}
|
}
|
||||||
return response.json({success: true}).end();
|
return response.json({
|
||||||
|
success: true
|
||||||
|
}).end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// Backwards compatibility
|
// Backwards compatibility
|
||||||
|
|
||||||
emailPlugin.oldRetrieve = function (request, response) {
|
emailPlugin.oldRetrieve = function(request, response) {
|
||||||
var email = request.param('email');
|
var email = request.param('email');
|
||||||
var key = request.param('key');
|
var key = request.param('key');
|
||||||
var secret = request.param('secret');
|
var secret = request.param('secret');
|
||||||
@ -554,25 +576,27 @@ emailPlugin.oldRetrieve = function (request, response) {
|
|||||||
return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response);
|
return emailPlugin.returnError(emailPlugin.errors.MISSING_PARAMETER, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
emailPlugin.retrieveDataByEmailAndPassphrase(email, key, secret, function (err, value) {
|
emailPlugin.retrieveDataByEmailAndPassphrase(email, key, secret, function(err, value) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return emailPlugin.returnError(err, response);
|
return emailPlugin.returnError(err, response);
|
||||||
}
|
}
|
||||||
response.send(value).end();
|
response.send(value).end();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
emailPlugin.oldSave = function (request, response) {
|
emailPlugin.oldSave = function(request, response) {
|
||||||
var queryData = '';
|
var queryData = '';
|
||||||
|
|
||||||
request.on('data', function (data) {
|
request.on('data', function(data) {
|
||||||
queryData += data;
|
queryData += data;
|
||||||
if (queryData.length > MAX_ALLOWED_STORAGE) {
|
if (queryData.length > MAX_ALLOWED_STORAGE) {
|
||||||
queryData = '';
|
queryData = '';
|
||||||
response.writeHead(413, {'Content-Type': 'text/plain'}).end();
|
response.writeHead(413, {
|
||||||
|
'Content-Type': 'text/plain'
|
||||||
|
}).end();
|
||||||
request.connection.destroy();
|
request.connection.destroy();
|
||||||
}
|
}
|
||||||
}).on('end', function () {
|
}).on('end', function() {
|
||||||
var params = querystring.parse(queryData);
|
var params = querystring.parse(queryData);
|
||||||
var email = params.email;
|
var email = params.email;
|
||||||
var passphrase = params.secret;
|
var passphrase = params.secret;
|
||||||
@ -584,8 +608,8 @@ emailPlugin.oldSave = function (request, response) {
|
|||||||
|
|
||||||
emailPlugin.processPost(request, response, email, key, passphrase, record);
|
emailPlugin.processPost(request, response, email, key, passphrase, record);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = emailPlugin;
|
module.exports = emailPlugin;
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user