Skip to content

Commit 0135759

Browse files
committed
Add the option to override “failCallback” when creating middleware.
- This required changing the signature for getMiddleware. Switched to taking an options object to avoid this issue in the future.
1 parent 45ffed7 commit 0135759

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

index.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,16 @@ var ExpressBrute = module.exports = function (store, options) {
4141
// generate "prevent" middleware
4242
this.prevent = this.getMiddleware();
4343
};
44-
ExpressBrute.prototype.getMiddleware = function (key) {
44+
ExpressBrute.prototype.getMiddleware = function (options) {
4545
// standardize input
46-
var keyFunc = key;
46+
options = _.extend({}, options);
47+
var keyFunc = options.key;
4748
if (typeof keyFunc !== 'function') {
48-
keyFunc = function (req, res, next) { next(key); };
49+
keyFunc = function (req, res, next) { next(options.key); };
4950
}
51+
var getFailCallback = _.bind(function () {
52+
return typeof options.failCallback === 'undefined' ? this.options.failCallback : options.failCallback;
53+
}, this);
5054

5155
// create middleware
5256
return _.bind(function (req, res, next) {
@@ -106,7 +110,8 @@ ExpressBrute.prototype.getMiddleware = function (key) {
106110
typeof next == 'function' && next();
107111
});
108112
} else {
109-
this.options.failCallback(req, res, next, new Date(nextValidRequestTime));
113+
var failCallback = getFailCallback();
114+
typeof failCallback === 'function' && failCallback(req, res, next, new Date(nextValidRequestTime));
110115
}
111116
}, this));
112117
},this));

spec/ExpessBrute.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,26 @@ describe("express brute", function () {
170170
expect(errorSpy.calls.length).toEqual(1);
171171
});
172172
});
173+
it('allows failCallback to be overridden', function () {
174+
brute = new ExpressBrute(store, {
175+
freeRetries: 0,
176+
minWait: 10000,
177+
maxWait: 10000,
178+
lifetime: 1,
179+
failCallback: errorSpy
180+
});
181+
var errorSpy2 = jasmine.createSpy();
182+
var mid = brute.getMiddleware({
183+
failCallback: errorSpy2
184+
});
185+
186+
mid(req(), new ResponseMock(), nextSpy);
187+
expect(errorSpy).not.toHaveBeenCalled();
188+
expect(errorSpy2).not.toHaveBeenCalled();
189+
mid(req(), new ResponseMock(), nextSpy);
190+
expect(errorSpy).not.toHaveBeenCalled();
191+
expect(errorSpy2).toHaveBeenCalled();
192+
});
173193
});
174194
describe("multiple keys", function () {
175195
var brute, store, errorSpy, nextSpy, req, done;
@@ -186,8 +206,8 @@ describe("express brute", function () {
186206
});
187207
});
188208
it ('tracks keys separately', function () {
189-
var first = brute.getMiddleware('first');
190-
var second = brute.getMiddleware('second');
209+
var first = brute.getMiddleware({key: 'first' });
210+
var second = brute.getMiddleware({key: 'second' });
191211

192212
first(req(), new ResponseMock(), nextSpy);
193213
expect(nextSpy.calls.length).toEqual(1);
@@ -208,8 +228,8 @@ describe("express brute", function () {
208228
someData: "something cool"
209229
};
210230
};
211-
var first = brute.getMiddleware(function(req, res, next) { next(req.someData); });
212-
var second = brute.getMiddleware("something cool");
231+
var first = brute.getMiddleware({key: function(req, res, next) { next(req.someData); } });
232+
var second = brute.getMiddleware({key: "something cool" });
213233

214234
first(req(), new ResponseMock(), nextSpy);
215235
expect(nextSpy.calls.length).toEqual(1);
@@ -219,7 +239,7 @@ describe("express brute", function () {
219239
expect(nextSpy.calls.length).toEqual(1);
220240
});
221241
it ('supports brute.reset', function () {
222-
var mid = brute.getMiddleware('withAKey');
242+
var mid = brute.getMiddleware({key: 'withAKey' });
223243

224244
mid(req(), new ResponseMock(), nextSpy);
225245
expect(nextSpy.calls.length).toEqual(1);
@@ -228,7 +248,7 @@ describe("express brute", function () {
228248
expect(nextSpy.calls.length).toEqual(2);
229249
});
230250
it ('supports req.reset shortcut', function () {
231-
var firstReq, mid = brute.getMiddleware('withAKey');
251+
var firstReq, mid = brute.getMiddleware({key: 'withAKey' });
232252

233253
mid(firstReq = req(), new ResponseMock(), nextSpy);
234254
expect(nextSpy.calls.length).toEqual(1);

0 commit comments

Comments
 (0)