Skip to content

Commit c1035b5

Browse files
authored
Copy req.user to user interface if present (fixes #160) (#177)
1 parent 242efbf commit c1035b5

File tree

2 files changed

+82
-6
lines changed

2 files changed

+82
-6
lines changed

lib/parsers.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,5 +166,26 @@ module.exports.parseRequest = function parseRequest(req, kwargs) {
166166
// expose http interface
167167
kwargs.request = http;
168168

169+
// user
170+
//
171+
// typically found on req.user according to Express and Passport
172+
173+
var user = {};
174+
if (req.user && !kwargs.user) {
175+
// shallow copy is okay because we are only modifying top-level
176+
// object (req.user)
177+
for (var key in req.user) {
178+
if ({}.hasOwnProperty.call(req.user, key)) {
179+
user[key] = req.user[key];
180+
}
181+
}
182+
183+
if (ip) {
184+
user.ip_address = ip;
185+
}
186+
187+
kwargs.user = user;
188+
}
189+
169190
return kwargs;
170191
};

test/raven.parsers.js

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ describe('raven.parsers', function() {
4747
encrypted: true
4848
},
4949
connection: {
50-
remoteAddress: '69.69.69.69'
50+
remoteAddress: '127.0.0.1'
5151
}
5252
};
5353
var parsed = raven.parsers.parseRequest(mockReq);
5454
parsed.should.have.property('request');
5555
parsed.request.url.should.equal('https://mattrobenolt.com/some/path?key=value');
5656
parsed.request.env.NODE_ENV.should.equal(process.env.NODE_ENV);
57-
parsed.request.env.REMOTE_ADDR.should.equal('69.69.69.69');
57+
parsed.request.env.REMOTE_ADDR.should.equal('127.0.0.1');
5858
});
5959

6060
describe('`headers` detection', function() {
@@ -303,12 +303,12 @@ describe('raven.parsers', function() {
303303
headers: {
304304
hostname: 'mattrobenolt.com',
305305
},
306-
ip: '69.69.69.69'
306+
ip: '127.0.0.1'
307307
};
308308

309309
var parsed = raven.parsers.parseRequest(mockReq);
310310

311-
parsed.request.env.REMOTE_ADDR.should.equal('69.69.69.69');
311+
parsed.request.env.REMOTE_ADDR.should.equal('127.0.0.1');
312312
});
313313

314314
it('should detect ip via `req.connection.remoteAddress`', function() {
@@ -319,13 +319,13 @@ describe('raven.parsers', function() {
319319
hostname: 'mattrobenolt.com',
320320
},
321321
connection: {
322-
remoteAddress: '69.69.69.69'
322+
remoteAddress: '127.0.0.1'
323323
}
324324
};
325325

326326
var parsed = raven.parsers.parseRequest(mockReq);
327327

328-
parsed.request.env.REMOTE_ADDR.should.equal('69.69.69.69');
328+
parsed.request.env.REMOTE_ADDR.should.equal('127.0.0.1');
329329
});
330330
});
331331

@@ -409,6 +409,61 @@ describe('raven.parsers', function() {
409409
parsed.request.data.should.equal('{\"foo\":true}');
410410
});
411411
});
412+
413+
describe('`user` detection', function () {
414+
it('should assign req.user to kwargs', function () {
415+
var mockReq = {
416+
method: 'POST',
417+
hostname: 'example.org',
418+
url: '/some/path?key=value',
419+
user: {
420+
username: 'janedoe',
421+
email: 'hello@janedoe.com'
422+
}
423+
};
424+
425+
var parsed = raven.parsers.parseRequest(mockReq);
426+
parsed.should.have.property('user', {
427+
username: 'janedoe',
428+
email: 'hello@janedoe.com'
429+
});
430+
});
431+
432+
it('should NOT assign req.user if already present in kwargs', function () {
433+
var mockReq = {
434+
method: 'POST',
435+
hostname: 'example.org',
436+
url: '/some/path?key=value',
437+
user: {
438+
username: 'janedoe',
439+
email: 'hello@janedoe.com'
440+
}
441+
};
442+
443+
var parsed = raven.parsers.parseRequest(mockReq, { user: {} });
444+
parsed.should.have.property('user', {});
445+
});
446+
447+
it('should add ip address to user if available', function () {
448+
var mockReq = {
449+
method: 'POST',
450+
hostname: 'example.org',
451+
url: '/some/path?key=value',
452+
ip: '127.0.0.1',
453+
user: {
454+
username: 'janedoe',
455+
email: 'hello@janedoe.com'
456+
}
457+
};
458+
459+
var parsed = raven.parsers.parseRequest(mockReq);
460+
parsed.should.have.property('user', {
461+
username: 'janedoe',
462+
email: 'hello@janedoe.com',
463+
ip_address: '127.0.0.1'
464+
});
465+
});
466+
});
412467
});
413468

414469
describe('#parseError()', function() {

0 commit comments

Comments
 (0)