Skip to content

Commit

Permalink
Reset NTLM auth state on every exit from replay loop
Browse files Browse the repository at this point in the history
  • Loading branch information
coditva committed Dec 19, 2019
1 parent 36533b3 commit 58fe97b
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions lib/authorizer/ntlm.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,18 @@ module.exports = {
challengeMessage, // type 2
authenticateMessage, // type 3
ntlmType2Header,
parsedParameters;
parsedParameters,

if (response.code !== 401 && response.code !== 403) {
auth.set(STATE, STATES.INITIALIZED);
auth.set(NTLM_HEADER, undefined);
// resets the state and NTLM header and exits replay loop
resetStateAndStop = function (err) {
auth.set(STATE, STATES.INITIALIZED);
auth.set(NTLM_HEADER, undefined);

return done(null, true);
return done(err || null, true);
};

if (response.code !== 401 && response.code !== 403) {
return resetStateAndStop();
}

// we try to extract domain from username if not specified.
Expand All @@ -178,7 +183,7 @@ module.exports = {
// Nothing to do if the server does not ask us for auth in the first place.
if (!(response.headers.has(WWW_AUTHENTICATE, NTLM) ||
response.headers.has(WWW_AUTHENTICATE, NEGOTIATE))) {
return done(null, true);
return resetStateAndStop();
}

// Create a type 1 message to send to the server
Expand Down Expand Up @@ -208,13 +213,13 @@ module.exports = {
});

if (!ntlmType2Header) {
return done(new Error('ntlm: server did not send NTLM type 2 message'));
return resetStateAndStop(new Error('ntlm: server did not send NTLM type 2 message'));
}

challengeMessage = ntlmUtil.parseType2Message(ntlmType2Header.valueOf(), _.noop);

if (!challengeMessage) {
return done(new Error('ntlm: server did not correctly process authentication request'));
return resetStateAndStop(new Error('ntlm: server did not correctly process authentication request'));
}

authenticateMessage = ntlmUtil.createType3Message(challengeMessage, {
Expand All @@ -233,11 +238,11 @@ module.exports = {
}
else if (state === STATES.T3_MSG_CREATED) {
// Means we have tried to authenticate, so we should stop here without worrying about anything
return done(null, true);
return resetStateAndStop();
}

// We are in an undefined state
return done(null, true);
return resetStateAndStop();
},

/**
Expand Down

0 comments on commit 58fe97b

Please sign in to comment.