Skip to content

Instantly share code, notes, and snippets.

@gautamsi
Last active February 7, 2018 00:09
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gautamsi/28211eda711e3e7dc04c to your computer and use it in GitHub Desktop.
Save gautamsi/28211eda711e3e7dc04c to your computer and use it in GitHub Desktop.
ews-javascript-api NTLM Auth with NodeJS
//see reference implementation in ews-javascript-api_repo@github\test\MockXHRApi.ts, this one is transpiled JS
// ----- Updated to process errors in ntlm library gracefully
var PromiseFactory = require("ews-javascript-api").PromiseFactory;
var httpntlm = require('httpntlm');
var ntlmXHRApi = (function () {
function ntlmXHRApi(user, password) {
this.userName = user;
this.password = password;
}
ntlmXHRApi.prototype.xhr = function (xhroptions) {
var _this = this;
var userName = _this.userName;
var domain = '';
if (userName.indexOf("\\") >= 0) {
var usersplit = userName.split("\\", 2);
userName = usersplit[1];
domain = usersplit[0];
}
var headers = xhroptions.headers;
if (headers["Authorization"]) {
delete headers["Authorization"];
}
var xhr = {
url: xhroptions.url,
username: userName,
password: _this.password,
workstation: '',
domain: domain,
body: xhroptions.data,
headers: headers
}
// no need to use PromiseFactory.create, just return Q promise in your implementation.
return PromiseFactory.create(function (successDelegate, errorDelegate, progressDelegate) {
httpntlm.post(xhr, function (err, res) {
res.getAllResponseHeaders = function () {
var header = "";
if (res.headers) {
for (var key in res.headers) {
header += key + " : " + res.headers[key] + "\r\n";
}
}
return header;
}
if (err) {
errorDelegate(err);
}
else {
//mapping properties to expected return properties.
res['responseText'] = res.body;
res['status'] = res.statusCode;
if (res.statusCode === 200) {
successDelegate(res);
}
else {
errorDelegate(res);
}
}
});
});
};
Object.defineProperty(ntlmXHRApi.prototype, "type", {
get: function () {
return "ntlmXHR";
},
enumerable: true,
configurable: true
});
return ntlmXHRApi;
})();
exports.ntlmXHRApi = ntlmXHRApi;
// uncomment next line to disable certificate chain test - only for development
//process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0';
var ews = require("ews-javascript-api");
var ntlmXHR = require("./ntlmXHRApi");
var ntlmXHRApi = new ntlmXHR.ntlmXHRApi("domain\\username or user@domain.com","actual password");
var exch = new ews.ExchangeService(ews.ExchangeVersion.Exchange2013);
// next line hijacks the XHR request from ews-javascript-api and use ntlmxhrapi
exch.XHRApi = ntlmXHRApi;
exch.Credentials = new ews.ExchangeCredentials("userName:just fake it", "password:just fake it"); //otherwise error out on null credential
exch.Url = new ews.Uri("https://host.domain.com/ews/exchange.asmx");
exch.GetPasswordExpirationDate("user@domain.com").then(function(value){
console.log("successsssssssssssssssssssssssssss");
console.log(value);
console.log(value.toString());
},function(value){
console.log("Errorrrrrrrrrrrrrrrrrrrrrrrrrr");
console.log(value);
});
The MIT License (MIT)
Copyright (c) 2014-2016 Gautam Singh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
@gautamsi
Copy link
Author

updated to make sure it calls errorDelegeate when HTTP error code is not 200.

@rrubio
Copy link

rrubio commented Feb 7, 2018

Hello,

Currently trying to implement the above code but getting the following error:

TypeError: Cannot read property 'create' of undefined at ntlmXHRApi.xhr ntlmXHRApi.js:34:31

Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment