Skip to content

Commit 374fe12

Browse files
committed
tests: Handling of multiple available Digest Auth strategies + Opting-in
1 parent 45114b3 commit 374fe12

File tree

2 files changed

+92
-2
lines changed

2 files changed

+92
-2
lines changed

test/fixtures/servers/_servers.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,8 @@ function createDigestServer (options) {
669669
expectedUsername = options.username || 'username',
670670
expectedPassword = options.password || 'password';
671671

672-
passport.use(new DigestStrategy({ qop: 'auth' },
672+
// Register Digest strategies for MD5 and SHA-256
673+
passport.use('digest-md5', new DigestStrategy({ qop: 'auth', algorithm: 'MD5' },
673674
function (username, done) {
674675
if (username !== expectedUsername) {
675676
return done(null, false);
@@ -678,8 +679,30 @@ function createDigestServer (options) {
678679
return done(null, username, expectedPassword);
679680
}));
680681

682+
passport.use('digest-sha256', new DigestStrategy({ qop: 'auth', algorithm: 'SHA-256' },
683+
function (username, done) {
684+
if (username !== expectedUsername) {
685+
return done(null, false);
686+
}
687+
688+
return done(null, username, expectedPassword);
689+
}));
690+
691+
app.use((req, res, next) => {
692+
if (!req.headers.authorization) {
693+
res.status(401);
694+
res.set('WWW-Authenticate',
695+
'Digest realm="Users", qop="auth", algorithm="MD5", nonce="md5nonce"');
696+
res.append('WWW-Authenticate',
697+
'Digest realm="Users", qop="auth", algorithm="SHA-256", nonce="sha256nonce"');
698+
699+
return res.send('Unauthorized');
700+
}
701+
next();
702+
});
703+
681704
app.all('*',
682-
passport.authenticate('digest', { session: false }),
705+
passport.authenticate(['digest-md5', 'digest-sha256'], { session: false }),
683706
function (req, res) {
684707
res.send(req.users);
685708
});

test/integration/auth-methods/digest.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,4 +1031,71 @@ describe('digest auth', function () {
10311031
expect(secondCall.args[2]).to.have.property('code', 200);
10321032
});
10331033
});
1034+
1035+
describe('with opted-in algorithm other than the default (md5)', function () {
1036+
before(function (done) {
1037+
var runOptions = {
1038+
collection: {
1039+
item: {
1040+
name: 'DigestAuth',
1041+
request: {
1042+
url: global.servers.digest,
1043+
auth: {
1044+
type: 'digest',
1045+
digest: {
1046+
algorithm: 'SHA-256',
1047+
username: '{{uname}}',
1048+
password: '{{pass}}'
1049+
}
1050+
}
1051+
}
1052+
}
1053+
},
1054+
environment: {
1055+
values: [{
1056+
key: 'uname',
1057+
value: USERNAME
1058+
}, {
1059+
key: 'pass',
1060+
value: PASSWORD
1061+
}]
1062+
}
1063+
};
1064+
1065+
// perform the collection run
1066+
this.run(runOptions, function (err, results) {
1067+
testrun = results;
1068+
done(err);
1069+
});
1070+
});
1071+
1072+
it('should have completed the run', function () {
1073+
expect(testrun).to.be.ok;
1074+
expect(testrun).to.nested.include({
1075+
'done.callCount': 1
1076+
});
1077+
testrun.done.getCall(0).args[0] && console.error(testrun.done.getCall(0).args[0].stack);
1078+
expect(testrun.done.getCall(0).args[0]).to.be.null;
1079+
expect(testrun).to.nested.include({
1080+
'start.callCount': 1
1081+
});
1082+
});
1083+
1084+
it('should have tried twice and succeeded the second time', function () {
1085+
expect(testrun).to.nested.include({
1086+
'io.callCount': 2,
1087+
'request.callCount': 2
1088+
});
1089+
1090+
var firstError = testrun.io.firstCall.args[0],
1091+
secondError = testrun.io.secondCall.args[0],
1092+
firstResponse = testrun.io.firstCall.args[3],
1093+
secondResponse = testrun.io.secondCall.args[3];
1094+
1095+
expect(firstError).to.be.null;
1096+
expect(secondError).to.be.null;
1097+
expect(firstResponse).to.have.property('code', 401);
1098+
expect(secondResponse).not.to.have.property('code', 401);
1099+
});
1100+
});
10341101
});

0 commit comments

Comments
 (0)