Add Authbox support

This commit is contained in:
petehunt 2014-12-31 03:52:59 -05:00
parent b6c8301ad4
commit 26104246ec
3 changed files with 39 additions and 11 deletions

1
.gitignore vendored
View File

@ -1,5 +1,6 @@
banned_numbers.js banned_numbers.js
mixpanel_config.js mixpanel_config.js
authbox_config.js
keys.json keys.json
*.swp *.swp

View File

@ -6,6 +6,7 @@
} }
, "dependencies": { , "dependencies": {
"express": "~3.5.1" "express": "~3.5.1"
, "authbox": "^0.9.2"
, "jade": "~0.14.2" , "jade": "~0.14.2"
, "nodemailer": "~0.6.1" , "nodemailer": "~0.6.1"
, "redis-url": "~0.2.0" , "redis-url": "~0.2.0"

View File

@ -1,5 +1,6 @@
var express = require('express') var express = require('express')
, app = express() , app = express()
, authbox = require('authbox')
, _ = require('underscore') , _ = require('underscore')
, fs = require('fs') , fs = require('fs')
, mixpanel = require('mixpanel') , mixpanel = require('mixpanel')
@ -21,7 +22,8 @@ try {
} }
var mpq var mpq
, mixpanel_config; , mixpanel_config
, authbox_config;
try { try {
mixpanel_config = require('./mixpanel_config.js'); mixpanel_config = require('./mixpanel_config.js');
mpq = new mixpanel.Client(mixpanel_config.api_key); mpq = new mixpanel.Client(mixpanel_config.api_key);
@ -29,6 +31,13 @@ try {
mpq = {track: function() {}}; mpq = {track: function() {}};
} }
try {
authbox_config = require('./authbox_config.js');
authbox.configure(authbox_config);
} catch(e) {
authbox = {log: function() {}};
}
var access_keys; var access_keys;
try { try {
// Optionally, you may specify special access keys in a keys.json file. // Optionally, you may specify special access keys in a keys.json file.
@ -45,6 +54,7 @@ app.set('view engine', 'jade');
app.use(express.cookieParser()); app.use(express.cookieParser());
app.use(express.static(__dirname + '/public')); app.use(express.static(__dirname + '/public'));
app.use(authbox.middleware);
app.use(express.bodyParser()); app.use(express.bodyParser());
// App routes // App routes
@ -79,21 +89,16 @@ app.post('/intl', function(req, res) {
// App helper functions // App helper functions
function textRequestHandler(req, res, number, region, key) { function textRequestHandler(req, res, number, region, key) {
var authbox_details = {
$actionName: 'text'
};
if (!number || !req.body.message) { if (!number || !req.body.message) {
mpq.track('incomplete request'); mpq.track('incomplete request');
authbox.log(req, _.extend(authbox_details, {$failureReason: 'incomplete_request'}));
res.send({success:false, message:'Number and message parameters are required.'}); res.send({success:false, message:'Number and message parameters are required.'});
return; return;
} }
if (banned_numbers.BLACKLIST[number]) {
mpq.track('banned number');
res.send({success:false,message:'Sorry, texts to this number are disabled.'});
return;
}
var ip = req.connection.remoteAddress;
if (!ip || ip === '127.0.0.1') {
ip = req.header('X-Real-IP');
}
var message = req.body.message; var message = req.body.message;
if (message.indexOf(':') > -1) { if (message.indexOf(':') > -1) {
@ -102,6 +107,23 @@ function textRequestHandler(req, res, number, region, key) {
message = ' ' + message; message = ' ' + message;
} }
_.extend(authbox_details, {
recipient__phone: number,
message__text: message
});
if (banned_numbers.BLACKLIST[number]) {
mpq.track('banned number');
authbox.log(req, _.extend(authbox_details, {$failureReason: 'banned_number'}));
res.send({success:false,message:'Sorry, texts to this number are disabled.'});
return;
}
var ip = req.connection.remoteAddress;
if (!ip || ip === '127.0.0.1') {
ip = req.header('X-Real-IP');
}
var tracking_details = { var tracking_details = {
number: number, number: number,
message: req.body.message, message: req.body.message,
@ -115,6 +137,7 @@ function textRequestHandler(req, res, number, region, key) {
text.send(number, message, region, function(err) { text.send(number, message, region, function(err) {
if (err) { if (err) {
mpq.track('sendText failed', tracking_details); mpq.track('sendText failed', tracking_details);
authbox.log(req, _.extend(authbox_details, {$failureReason: 'gateway_failed'}));
res.send(_.extend(response_obj, res.send(_.extend(response_obj,
{ {
success:false, success:false,
@ -123,6 +146,7 @@ function textRequestHandler(req, res, number, region, key) {
} }
else { else {
mpq.track('sendText success', tracking_details); mpq.track('sendText success', tracking_details);
authbox.log(req, _.extend(authbox_details, {$success: true}));
res.send(_.extend(response_obj, {success:true})); res.send(_.extend(response_obj, {success:true}));
} }
}); });
@ -160,6 +184,7 @@ function textRequestHandler(req, res, number, region, key) {
}, 1000*60*3); }, 1000*60*3);
if (num > 3) { if (num > 3) {
mpq.track('exceeded phone quota'); mpq.track('exceeded phone quota');
authbox.log(req, _.extend(authbox_details, {$failureReason: 'exceeded_phone_quota'}));
res.send({success:false, message:'Exceeded quota for this phone number. ' + number}); res.send({success:false, message:'Exceeded quota for this phone number. ' + number});
return; return;
} }
@ -173,6 +198,7 @@ function textRequestHandler(req, res, number, region, key) {
} }
if (num > 75) { if (num > 75) {
mpq.track('exceeded ip quota'); mpq.track('exceeded ip quota');
authbox.log(req, _.extend(authbox_details, {$failureReason: 'exceeded_ip_quota'}));
res.send({success:false, message:'Exceeded quota for this IP address. ' + ip}); res.send({success:false, message:'Exceeded quota for this IP address. ' + ip});
return; return;
} }