Permalink
Browse files

Add Authbox support

  • Loading branch information...
1 parent b6c8301 commit 26104246ec87a8223af8ccdcdf837871440bc0f7 @petehunt petehunt committed Dec 31, 2014
Showing with 36 additions and 8 deletions.
  1. +1 −0 .gitignore
  2. +1 −0 package.json
  3. +34 −8 server/app.js
View
@@ -1,5 +1,6 @@
banned_numbers.js
mixpanel_config.js
+authbox_config.js
keys.json
*.swp
View
@@ -6,6 +6,7 @@
}
, "dependencies": {
"express": "~3.5.1"
+ , "authbox": "^0.9.2"
, "jade": "~0.14.2"
, "nodemailer": "~0.6.1"
, "redis-url": "~0.2.0"
View
@@ -1,5 +1,6 @@
var express = require('express')
, app = express()
+ , authbox = require('authbox')
, _ = require('underscore')
, fs = require('fs')
, mixpanel = require('mixpanel')
@@ -21,14 +22,22 @@ try {
}
var mpq
- , mixpanel_config;
+ , mixpanel_config
+ , authbox_config;
try {
mixpanel_config = require('./mixpanel_config.js');
mpq = new mixpanel.Client(mixpanel_config.api_key);
} catch(e) {
mpq = {track: function() {}};
}
+try {
+ authbox_config = require('./authbox_config.js');
+ authbox.configure(authbox_config);
+} catch(e) {
+ authbox = {log: function() {}};
+}
+
var access_keys;
try {
// 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.static(__dirname + '/public'));
+app.use(authbox.middleware);
app.use(express.bodyParser());
// App routes
@@ -79,13 +89,32 @@ app.post('/intl', function(req, res) {
// App helper functions
function textRequestHandler(req, res, number, region, key) {
+ var authbox_details = {
+ $actionName: 'text'
+ };
+
if (!number || !req.body.message) {
mpq.track('incomplete request');
+ authbox.log(req, _.extend(authbox_details, {$failureReason: 'incomplete_request'}));
res.send({success:false, message:'Number and message parameters are required.'});
return;
}
+
+ var message = req.body.message;
+ if (message.indexOf(':') > -1) {
+ // Handle problem with vtext where message would not get sent properly if it
+ // contains a colon
+ 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;
}
@@ -95,13 +124,6 @@ function textRequestHandler(req, res, number, region, key) {
ip = req.header('X-Real-IP');
}
- var message = req.body.message;
- if (message.indexOf(':') > -1) {
- // Handle problem with vtext where message would not get sent properly if it
- // contains a colon
- message = ' ' + message;
- }
-
var tracking_details = {
number: number,
message: req.body.message,
@@ -115,6 +137,7 @@ function textRequestHandler(req, res, number, region, key) {
text.send(number, message, region, function(err) {
if (err) {
mpq.track('sendText failed', tracking_details);
+ authbox.log(req, _.extend(authbox_details, {$failureReason: 'gateway_failed'}));
res.send(_.extend(response_obj,
{
success:false,
@@ -123,6 +146,7 @@ function textRequestHandler(req, res, number, region, key) {
}
else {
mpq.track('sendText success', tracking_details);
+ authbox.log(req, _.extend(authbox_details, {$success: true}));
res.send(_.extend(response_obj, {success:true}));
}
});
@@ -160,6 +184,7 @@ function textRequestHandler(req, res, number, region, key) {
}, 1000*60*3);
if (num > 3) {
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});
return;
}
@@ -173,6 +198,7 @@ function textRequestHandler(req, res, number, region, key) {
}
if (num > 75) {
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});
return;
}

0 comments on commit 2610424

Please sign in to comment.