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

Force audience to incoming host when checkAudience is false #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions lib/passport-persona/strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ var passport = require('passport')
function Strategy(options, verify) {
if (!options.audience) throw new Error('Persona authentication requires an audience option');
if (!verify) throw new Error('Persona authentication strategy requires a verify function');

passport.Strategy.call(this);
this.name = 'persona';
this._verify = verify;
this._passReqToCallback = options.passReqToCallback;

this._audience = options.audience;
this._assertionField = options.assertionField || 'assertion';
this._checkAudience = options.checkAudience !== undefined ? options.checkAudience : true;

// options used to inject mock objects for testing purposes
this._https = options.transport || https;
}
Expand All @@ -80,19 +80,27 @@ util.inherits(Strategy, passport.Strategy);
*/
Strategy.prototype.authenticate = function(req) {
var self = this;

if (!req.body || !req.body[this._assertionField]) {
return this.fail(new BadRequestError('Missing assertion'));
}

var assertion = req.body[this._assertionField];


// If we are not checking the audience, then we need to set our audience to
// the the host header from the incoming request. Otherwise, passport will
// error with 'audience mismatch: domain mismatch'.
if(!this._checkAudience)
{
this._audience = req.headers.host;
} // end if

var query = querystring.stringify({ assertion: assertion, audience: this._audience });
var headers = {};
headers['Host'] = 'verifier.login.persona.org';
headers['Content-Type'] = 'application/x-www-form-urlencoded';
headers['Content-Length'] = query.length;

var options = {
host: 'verifier.login.persona.org',
port: 443,
Expand Down Expand Up @@ -122,19 +130,19 @@ Strategy.prototype.authenticate = function(req) {
});
});
vreq.end(query, 'utf8');


function verified(result) {
if (self._audience !== result.audience && self._checkAudience) {
return self.error(new Error('audience mismatch in verification result'));
}

function done(err, user, info) {
if (err) { return self.error(err); }
if (!user) { return self.fail(info); }
self.success(user, info);
}

if (self._passReqToCallback) {
var arity = self._verify.length;
if (arity == 4) {
Expand All @@ -156,5 +164,5 @@ Strategy.prototype.authenticate = function(req) {

/**
* Expose `Strategy`.
*/
*/
module.exports = Strategy;
Loading