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

Add option to pass Authentication Scheme #110

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ httpntlm.get({
- `password:` _{String}_ Password (optional, default: '')
- `workstation:` _{String}_ Name of workstation (optional, default: '')
- `domain:` _{String}_ Name of domain (optional, default: '')
- `auth_scheme:` _{String}_ [Authentication Scheme](https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/understanding-http-authentication#http-authentication-schemes) to use (optional, default: 'NTLM')
- `agent:` _{Agent}_ In case you want to reuse the keepaliveAgent over different calls (optional)
-
- `headers:` _{Object}_ Add in custom headers. The following headers are used by NTLM and cannot be passed: `Connection`, `Authorization` (optional)

if you already got the encrypted password,you should use this two param to replace the 'password' param.
Expand Down
9 changes: 8 additions & 1 deletion httpntlm.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,15 @@ exports.method = function(method, options, finalCallback){
if(!res.headers['www-authenticate'])
return callback(new Error('www-authenticate not found on response of second request'));

// pass along the auth_scheme option to the parseType2Message function
var type2options = options.auth_scheme ? {auth_scheme: options.auth_scheme} : undefined;

// parse type2 message from server:
var type2msg = ntlm.parseType2Message(res.headers['www-authenticate'], callback); //callback only happens on errors
var type2msg = ntlm.parseType2Message(
res.headers["www-authenticate"],
callback,
type2options
); //callback only happens on errors
if(!type2msg) return; // if callback returned an error, the parse-function returns with null

// create type3 message:
Expand Down
15 changes: 10 additions & 5 deletions ntlm.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,18 @@ function createType1Message(options){
if(workstation.length !=0) buf.write(workstation, pos, workstation.length, 'ascii'); pos += workstation.length; // workstation string
if(domain.length !=0) buf.write(domain , pos, domain.length , 'ascii'); pos += domain.length; // domain string

return 'NTLM ' + buf.toString('base64');
return `${options?.auth_scheme ?? "NTLM"} ` + buf.toString("base64");
}

function parseType2Message(rawmsg, callback){
var match = rawmsg.match(/NTLM (.+)?/);
function parseType2Message(rawmsg, callback, options){
var authScheme = options?.auth_scheme ?? "NTLM";
var match = rawmsg.match(new RegExp(`${authScheme} (.+)?`));
if(!match || !match[1]) {
callback(new Error("Couldn't find NTLM in the message type2 coming from the server"));
callback(
new Error(
`Couldn't find ${authScheme} in the message type2 coming from the server`
)
);
return null;
}

Expand Down Expand Up @@ -267,7 +272,7 @@ function createType3Message(msg2, options){
ntChallengeResponse.copy(buf, pos); pos += ntChallengeResponse.length;
encryptedRandomSessionKeyBytes.copy(buf, pos); pos += encryptedRandomSessionKeyBytes.length;

return 'NTLM ' + buf.toString('base64');
return `${options?.auth_scheme ?? "NTLM"} ` + buf.toString("base64");
}

function create_LM_hashed_password_v1(password){
Expand Down
Loading