Skip to content

Instantly share code, notes, and snippets.

@gautamsi
Last active June 3, 2017 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gautamsi/4aec3307942de9087e89 to your computer and use it in GitHub Desktop.
Save gautamsi/4aec3307942de9087e89 to your computer and use it in GitHub Desktop.
ews-javascript-api with cookies Authentication of TMG (Thread management Gateway) (or ISA Server)
var ews = require("ews-javascript-api");
var PromiseFactory = ews.PromiseFactory;
var request = require('httpreq');
var cookiesXHRApi = (function () {
function cookiesXHRApi(user, password) {
this.cookies = [];
this.userName = user;
this.password = password;
}
cookiesXHRApi.prototype.xhr = function (xhroptions) {
var _this = this;
var headers = xhroptions.headers;
if (headers["Authorization"]) {
delete headers["Authorization"];
}
var xhr = {
method: "POST",
url: xhroptions.url,
body: xhroptions.data,
headers: headers,
cookies: _this.cookies
}
// no need to use PromiseFactory.create, just return Q promise in your implementation.
return PromiseFactory.create(function (successDelegate, errorDelegate, progressDelegate) {
//checking if cookiesAuth is needed
if (!_this.cookies || _this.cookies.length < 1) {
var uri = new ews.Uri(xhroptions.url);
var baseUrl = uri.Scheme + "://" + uri.Host + "/CookieAuth.dll?Logon";
var preauthOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: 'curl=Z2F&flags=0&forcedownlevel=0&formdir=1&trusted=0&username=' + _this.userName + '&password=' + _this.password,
url: baseUrl
}
//obtaining cookies
request.doRequest(preauthOptions, function (err, res) {
if (err) {
errorDelegate(err);
}
else {
//set cookies
_this.cookies = res.cookies;
xhr.cookies = _this.cookies;
request.doRequest(xhr, function (err, res) {
if (err) {
errorDelegate(err);
}
else {
// storing more cookies for next request
res.cookies.forEach(function (cookie) { // TODO: fix multiple x-backendcookies if needed
if (cookie.indexOf('X-BackEndCookie') < 0) { //Exchange 2013 returns different X-BackEndCookie each time
if (_this.cookies.indexOf(cookie) < 0) {
_this.cookies.push(cookie);
}
}
});
//mapping properties to expected return properties.
res['responseText'] = res.body;
res['status'] = res.statusCode;
successDelegate(res);
//console.log(_this.cookies);
}
});
}
});
}
else {
request.doRequest(xhr, function (err, res) {
if (err) {
errorDelegate(err);
}
else {
// storing more cookies for next request
res.cookies.forEach(function (cookie, index) { // TODO: fix multiple x-backendcookies if needed
if (cookie.indexOf('X-BackEndCookie') < 0) { //Exchange 2013 returns different X-BackEndCookie each time
if (_this.cookies.indexOf(cookie) < 0) {
_this.cookies.push(cookie);
}
}
});
//mapping properties to expected return properties.
res['responseText'] = res.body;
res['status'] = res.statusCode;
successDelegate(res);
//console.log(_this.cookies);
}
});
}
});
};
Object.defineProperty(cookiesXHRApi.prototype, "type", {
get: function () {
return "cookiesXHR";
},
enumerable: true,
configurable: true
});
return cookiesXHRApi;
})();
exports.cookiesXHRApi = cookiesXHRApi;
// 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 cookiesXHR = require("./cookiesAuthXHRAPI");
var cookiesXHRApi = new cookiesXHR.cookiesXHRApi("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 = cookiesXHRApi;
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment