Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set req.authInfo when using assignProperty option #1012

Merged
merged 4 commits into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/middleware/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,16 @@ module.exports = function authenticate(passport, name, options, callback) {
}
if (options.assignProperty) {
req[options.assignProperty] = user;
return next();
if (options.authInfo !== false) {
passport.transformAuthInfo(info, req, function(err, tinfo) {
if (err) { return next(err); }
req.authInfo = tinfo;
next();
});
} else {
next();
}
return;
}

req.logIn(user, options, function(err) {
Expand Down
49 changes: 49 additions & 0 deletions test/authenticator.middleware.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,55 @@ describe('Authenticator', function() {
expect(request.account.username).to.equal('jaredhanson');
});

it('should set authInfo to empty object', function() {
expect(request.authInfo).to.deep.equal({});
});
});

describe('handling a request with authInfo disabled', function() {
function Strategy() {
}
Strategy.prototype.authenticate = function(req) {
var user = { id: '1', username: 'jaredhanson' };
this.success(user);
};

var passport = new Authenticator();
passport.use('success', new Strategy());

var request, error;

before(function(done) {
chai.connect.use(passport.authorize('success', { authInfo: false }))
.req(function(req) {
request = req;

req.logIn = function(user, options, done) {
this.user = user;
done();
};
})
.next(function(err) {
error = err;
done();
})
.dispatch();
});

it('should not error', function() {
expect(error).to.be.undefined;
});

it('should not set user', function() {
expect(request.user).to.be.undefined;
});

it('should set account', function() {
expect(request.account).to.be.an('object');
expect(request.account.id).to.equal('1');
expect(request.account.username).to.equal('jaredhanson');
});

it('should not set authInfo', function() {
expect(request.authInfo).to.be.undefined;
});
Expand Down
49 changes: 49 additions & 0 deletions test/middleware/authenticate.success.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,55 @@ describe('middleware/authenticate', function() {
expect(request.account.username).to.equal('jaredhanson');
});

it('should set authInfo to empty object', function() {
expect(request.authInfo).to.deep.equal({});
});
});

describe('success that assigns a specific property with authInfo disabled', function() {
function Strategy() {
}
Strategy.prototype.authenticate = function(req) {
var user = { id: '1', username: 'jaredhanson' };
this.success(user);
};

var passport = new Passport();
passport.use('success', new Strategy());

var request, error;

before(function(done) {
chai.connect.use(authenticate(passport, 'success', { assignProperty: 'account', authInfo: false }))
.req(function(req) {
request = req;

req.logIn = function(user, options, done) {
this.user = user;
done();
};
})
.next(function(err) {
error = err;
done();
})
.dispatch();
});

it('should not error', function() {
expect(error).to.be.undefined;
});

it('should not set user', function() {
expect(request.user).to.be.undefined;
});

it('should set account', function() {
expect(request.account).to.be.an('object');
expect(request.account.id).to.equal('1');
expect(request.account.username).to.equal('jaredhanson');
});

it('should not set authInfo', function() {
expect(request.authInfo).to.be.undefined;
});
Expand Down