diff --git a/lib/httputils.js b/lib/httputils.js index 2491e5ee34a978b30c91aa53c4b69d772f89887d..671fa5a16554c0e36b46519611c4e774be7c9917 100644 --- a/lib/httputils.js +++ b/lib/httputils.js @@ -42,6 +42,16 @@ exports.forbidden = function(resp, reason) resp.end(); }; +exports.throttled = function(resp, reason) +{ + resp.writeHead(429, {"Content-Type": "text/plain"}); + resp.write("Too Many Requests"); + if (reason) { + resp.write(": " + reason); + } + resp.end(); +}; + exports.jsonResponse = function(resp, obj) { resp.writeHead(200, {"Content-Type": "application/json"}); diff --git a/lib/wsapi/stage_email.js b/lib/wsapi/stage_email.js index c5b562304f4ca262e5eff7b2e66e025bb435b783..9b4061bc8cba06187a672636e37eaaf85bf4dcdc 100644 --- a/lib/wsapi/stage_email.js +++ b/lib/wsapi/stage_email.js @@ -26,7 +26,7 @@ exports.process = function(req, res) { if (last && (new Date() - last) < config.get('min_time_between_emails_ms')) { logger.warn('throttling request to stage email address ' + req.body.email + ', only ' + ((new Date() - last) / 1000.0) + "s elapsed"); - return httputils.forbidden(res, "throttling. try again later."); + return httputils.throttled(res, "Too many emails sent to that address, try again later."); } try { diff --git a/lib/wsapi/stage_user.js b/lib/wsapi/stage_user.js index dc5f0aef04cb392a3e02de3f31de20f450bf2242..580838037c2d40eee2c74117eb1f93830fed5f9d 100644 --- a/lib/wsapi/stage_user.js +++ b/lib/wsapi/stage_user.js @@ -31,7 +31,7 @@ exports.process = function(req, resp) { if (last && (new Date() - last) < config.get('min_time_between_emails_ms')) { logger.warn('throttling request to stage email address ' + req.body.email + ', only ' + ((new Date() - last) / 1000.0) + "s elapsed"); - return httputils.forbidden(resp, "throttling. try again later."); + return httputils.throttled(resp, "Too many emails sent to that address, try again later."); } try { diff --git a/resources/static/shared/network.js b/resources/static/shared/network.js index a16b38df9b737888eb61b435ac5e0df12f4a7931..ce84f5616a89d131fbfccd70e9a04056af01f12e 100644 --- a/resources/static/shared/network.js +++ b/resources/static/shared/network.js @@ -186,8 +186,8 @@ BrowserID.Network = (function() { complete(onComplete, status.success); }, error: function(info) { - // 403 is throttling. - if (info.network.status === 403) { + // 429 is throttling. + if (info.network.status === 429) { complete(onComplete, false); } else complete(onFailure, info); @@ -392,8 +392,8 @@ BrowserID.Network = (function() { complete(onComplete, response.success); }, error: function(info) { - // 403 is throttling. - if (info.network.status === 403) { + // 429 is throttling. + if (info.network.status === 429) { complete(onComplete, false); } else complete(onFailure, info); diff --git a/tests/email-throttling-test.js b/tests/email-throttling-test.js index db6a7d1a38193463945238af2bcc1bd0626bf402..ba807a7829aa692ca705946e767ec9c461a7a7de 100755 --- a/tests/email-throttling-test.js +++ b/tests/email-throttling-test.js @@ -52,7 +52,7 @@ suite.addBatch({ site:'fakesite.com' }), "is throttled": function(err, r) { - assert.strictEqual(r.code, 403); + assert.strictEqual(r.code, 429); } } }); @@ -101,8 +101,8 @@ suite.addBatch({ email: 'second@fakeemail.com', site:'fakesite.com' }), - "is throttled with a 403": function(err, r) { - assert.strictEqual(r.code, 403); + "is throttled with a 429": function(err, r) { + assert.strictEqual(r.code, 429); } } });