gautamsi / ews-javascript-api-auth Goto Github PK
View Code? Open in Web Editor NEWHelper methods to support NTLM and Cookies authentication with ews-javascript-api
License: MIT License
Helper methods to support NTLM and Cookies authentication with ews-javascript-api
License: MIT License
Hi,
I am student trying to create a simple app to communicate with a local exchange server. the server uses ntlm authentication only, and i was wondering if ews-javascript-api-auth and ews-js-api-browser work together in ionic?
some quick experimentation gives me errors ENOENT 'ews-javascript-api-auth\src\ntlmAuthXhrApi.js.map' no such file found.
thanks in advance for any given insight and your time ^^
How were you able to compile this in typescript?
There is no types definition for fetch and there are other issues with bluebird.
Default domain set to .
is probably wrong. Should empty string.
https://github.com/gautamsi/ews-javascript-api-auth/blob/master/src/ntlmAuthXhrApi.ts#L21
Is it possible to use ews-javascript-api-auth with react-native?
Now I see errors like "Unable to resolve module https in ntlmAuthXhrApi.js"
Hi,
I try to use the lib ews-javascript-api with this module using outlook365.
Everytime i get 'www-authenticate not found on response of second request´. What must I do to resolve this?
I used to instanciate the exchnage service like:
const exchange = new ews.ExchangeService(version);
exchange.Credentials = new ews.ExchangeCredentials(login, pass);
exchange.Url = new ews.Uri(externalServerUrl);
Since the app has to support multiple accounts, calling the code above multiple time with different user and password works fine.
If, however, I use the new ConfigurationApi for NLTM this will change the credentials of all previous instances of ExchangeService, won't it?
ews.ConfigurationApi.ConfigureXHR(new ewsAuth.ntlmAuthXhrApi(login, pass));
How can I use multiple Exchange Services, each with his own credentials (either Basic or NLTM)?
Hi, I use the following code to send a request to Exchange Server:
var ews = require("ews-javascript-api");
var ewsAuth = require("ews-javascript-api-auth");
var credentials = {
userName: "myusername",
password: "mypassword"
}
ews.ConfigurationApi.ConfigureXHR(new ewsAuth.ntlmAuthXhrApi(credentials.userName, credentials.password, true));
var exch = new ews.ExchangeService(ews.ExchangeVersion.Exchange2013_SP1);
exch.Credentials = new ews.WebCredentials(credentials.userName, credentials.password);
exch.Url = new ews.Uri("https://outlook.mydomen.ru/Ews/Exchange.asmx");
exch.FindItems(ews.WellKnownFolderName.Inbox, new ews.ItemView(20))
.then(function (response) {
console.log('Inbox:', response)
}, function (errors) {
console.log('ERROR:', errors)
});
Then I see in logs that there was Basic
in requet Authorization header instead of NTLM
:
{
"url": "https://outlook.mydomen.ru/Ews/Exchange.asmx",
"headers": {
"Content-Type": "text/xml; charset=utf-8",
"Accept": "text/xml",
"Authorization": "Basic bXl1c2VybmFtZTpteXBhc3N3b3Jk"
},
"type": "POST",
"data": "<soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:m='http://schemas.microsoft.com/exchange/services/2006/messages' xmlns:t='http://schemas.microsoft.com/exchange/services/2006/types'><soap:Header><t:RequestServerVersion Version='Exchange2013_SP1'></t:RequestServerVersion></soap:Header><soap:Body><m:FindItem Traversal='Shallow'><m:ItemShape><t:BaseShape>AllProperties</t:BaseShape></m:ItemShape><m:IndexedPageItemView MaxEntriesReturned='20' Offset='0' BasePoint='Beginning'></m:IndexedPageItemView><m:ParentFolderIds><t:DistinguishedFolderId Id='inbox'></t:DistinguishedFolderId></m:ParentFolderIds></m:FindItem></soap:Body></soap:Envelope>"
}
What should I do to make a NTLM request?
I am writing an application to retrieve some emails from a mailbox.
I use "Folder.FindItems" function to search the email items, then loop the search result and bind the EmailMessage.
But our exchange server is not stable. It may throw "connect ECONNREFUSED " error when I bind over 4000 emails.
I would like to catch the error and retry the bind function.
But "ntlmAuthXhrApi.xhr" did not catch the error.
When the mail server throw any error, the application will stop at here.
Could you please change the code to catch the error?
Error Log:
2017-07-20T10:21:06.405Z - error: Error: connect ECONNREFUSED XXX.XXX.XXX.XXX:443
at Object.exports._errnoException (util.js:1034:11)
at exports._exceptionWithHostPort (util.js:1057:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1096:14)
From previous event:
at ntlmAuthXhrApi.ntlmPreCall (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api-auth/src/ntlmAuthXhrApi.js:122:16)
at ntlmAuthXhrApi.xhr (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api-auth/src/ntlmAuthXhrApi.js:44:19)
From previous event:
at ntlmAuthXhrApi.xhr (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api-auth/src/ntlmAuthXhrApi.js:43:16)
at GetItemRequest.ServiceRequestBase.ValidateAndEmitRequest (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api/js/Core/Requests/ServiceRequestBase.js:408:36)
at SimpleServiceRequestBase.InternalExecute (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api/js/Core/Requests/SimpleServiceRequestBase.js:50:19)
From previous event:
at GetItemRequest.SimpleServiceRequestBase.InternalExecute (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api/js/Core/Requests/SimpleServiceRequestBase.js:47:16)
at MultiResponseServiceRequest.Execute (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api/js/Core/Requests/MultiResponseServiceRequest.js:45:19)
From previous event:
at GetItemRequest.MultiResponseServiceRequest.Execute (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api/js/Core/Requests/MultiResponseServiceRequest.js:44:16)
at ExchangeService.InternalBindToItems (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api/js/Core/ExchangeService.js:656:24)
at ExchangeService.BindToItem (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api/js/Core/ExchangeService.js:438:21)
at Function.EmailMessage.Bind (/home/uat/CX/cx_omnichannel_email_extract/node_modules/ews-javascript-api/js/Core/ServiceObjects/Items/EmailMessage.js:277:24)
at _loadEmailInFolder (/home/uat/CX/cx_omnichannel_email_extract/app/facade/email-extraction-server-facade.js:1261:60)
Example:
ews-javascript-api-auth/src/ntlmAuthXhrApi.js
ntlmAuthXhrApi.prototype.xhr = function (xhroptions, progressDelegate) {
var _this = this;
//setup xhr for github.com/andris9/fetch options
var options = {
url: xhroptions.url,
//payload: xhroptions.data,
headers: xhroptions.headers,
method: 'GET',
agent: new https_1.Agent({ keepAlive: true, rejectUnauthorized: !this.allowUntrustedCertificate }) //keepaliveAgent
};
return new Promise(function (resolve, reject) {
_this.ntlmPreCall(options).then(function (optionsWithNtlmHeader) {
optionsWithNtlmHeader['payload'] = xhroptions.data;
optionsWithNtlmHeader['method'] = xhroptions.type;
fetch_1.fetchUrl(xhroptions.url, optionsWithNtlmHeader, function (error, meta, body) {
if (error) {
reject(error);
}
else {
var xhrResponse = {
response: body.toString(),
status: meta.status,
redirectCount: meta.redirectCount,
headers: meta.responseHeaders,
finalUrl: meta.finalUrl,
responseType: '',
statusText: undefined,
};
if (xhrResponse.status === 200) {
resolve(utils_1.setupXhrResponse(xhrResponse));
}
else {
reject(utils_1.setupXhrResponse(xhrResponse));
}
}
});
});
});
};
Change to:
ntlmAuthXhrApi.prototype.xhr = function (xhroptions, progressDelegate) {
var _this = this;
//setup xhr for github.com/andris9/fetch options
var options = {
url: xhroptions.url,
//payload: xhroptions.data,
headers: xhroptions.headers,
method: 'GET',
agent: new https_1.Agent({ keepAlive: true, rejectUnauthorized: !this.allowUntrustedCertificate }) //keepaliveAgent
};
return new Promise(function (resolve, reject) {
_this.ntlmPreCall(options).then(function (optionsWithNtlmHeader) {
optionsWithNtlmHeader['payload'] = xhroptions.data;
optionsWithNtlmHeader['method'] = xhroptions.type;
fetch_1.fetchUrl(xhroptions.url, optionsWithNtlmHeader, function (error, meta, body) {
if (error) {
reject(error);
}
else {
var xhrResponse = {
response: body.toString(),
status: meta.status,
redirectCount: meta.redirectCount,
headers: meta.responseHeaders,
finalUrl: meta.finalUrl,
responseType: '',
statusText: undefined,
};
if (xhrResponse.status === 200) {
resolve(utils_1.setupXhrResponse(xhrResponse));
}
else {
reject(utils_1.setupXhrResponse(xhrResponse));
}
}
});
}).catch(function(err){
reject(err);
});
});
};
ews-javascript-api/js/Core/Requests/SimpleServiceRequestBase.js
SimpleServiceRequestBase.prototype.InternalExecute = function () {
//var writer = new Data.EwsServiceXmlWriter();
//this.WriteSoapRequest(this.url, writer);
var _this = this;
//if (!this.Service && !this.Service.Credentials && (!this.Service.Credentials.UserName || this.service.Credentials.Password))
// throw new Error("missing credential");
//var cred = "Basic " + btoa(this.Service.Credentials.UserName + ":" + this.Service.Credentials.Password);
//var cc = writer.GetXML();
//var xhrOptions: IXHROptions = {
// type: "POST",
// data: cc,
// url: "https://pod51045.outlook.com/autodiscover/autodiscover.svc",
// headers: { "Content-Type": "text/xml", "Authorization": cred },
// //customRequestInitializer: function (x) {
// // var m = x;
// //}
//};
return new Promise_1.Promise(function (successDelegate, errorDelegate) {
var request = _this.BuildXHR();
//this.ReadResponsePrivate(response);
_this.ValidateAndEmitRequest(request).then(function (xhrResponse) {
var dom = new ExtensionMethods_1.DOMParser();
var xml2js = new ExtensionMethods_1.xml2JsObject();
var req = xml2js.parseXMLNode(dom.parseFromString(request.data, "text/xml").documentElement, true);
EwsLogging_1.EwsLogging.DebugLog(req, true);
if (xhrResponse.status == 200) {
EwsLogging_1.EwsLogging.DebugLog(xhrResponse, true);
var ewsXmlReader = new EwsServiceXmlReader_1.EwsServiceXmlReader(xhrResponse.responseText || xhrResponse.response, _this.Service);
//EwsLogging.DebugLog(ewsXmlReader.JsObject, true);
var serviceResponse = _this.ReadResponsePrivate(ewsXmlReader.JsObject);
if (successDelegate)
successDelegate(serviceResponse || xhrResponse.responseText || xhrResponse.response);
}
else {
if (errorDelegate)
errorDelegate(_this.ProcessWebException(serviceResponse || xhrResponse.responseText || xhrResponse.response) || serviceResponse);
}
}, function (resperr) {
EwsLogging_1.EwsLogging.Log("Error in calling service, error code:" + resperr.status + "\r\n" + resperr.getAllResponseHeaders());
if (errorDelegate)
errorDelegate(_this.ProcessWebException(resperr) || resperr);
});
});
};
Change To:
SimpleServiceRequestBase.prototype.InternalExecute = function () {
//var writer = new Data.EwsServiceXmlWriter();
//this.WriteSoapRequest(this.url, writer);
var _this = this;
//if (!this.Service && !this.Service.Credentials && (!this.Service.Credentials.UserName || this.service.Credentials.Password))
// throw new Error("missing credential");
//var cred = "Basic " + btoa(this.Service.Credentials.UserName + ":" + this.Service.Credentials.Password);
//var cc = writer.GetXML();
//var xhrOptions: IXHROptions = {
// type: "POST",
// data: cc,
// url: "https://pod51045.outlook.com/autodiscover/autodiscover.svc",
// headers: { "Content-Type": "text/xml", "Authorization": cred },
// //customRequestInitializer: function (x) {
// // var m = x;
// //}
//};
return new Promise_1.Promise(function (successDelegate, errorDelegate) {
var request = _this.BuildXHR();
//this.ReadResponsePrivate(response);
_this.ValidateAndEmitRequest(request).then(function (xhrResponse) {
var dom = new ExtensionMethods_1.DOMParser();
var xml2js = new ExtensionMethods_1.xml2JsObject();
var req = xml2js.parseXMLNode(dom.parseFromString(request.data, "text/xml").documentElement, true);
EwsLogging_1.EwsLogging.DebugLog(req, true);
if (xhrResponse.status == 200) {
EwsLogging_1.EwsLogging.DebugLog(xhrResponse, true);
var ewsXmlReader = new EwsServiceXmlReader_1.EwsServiceXmlReader(xhrResponse.responseText || xhrResponse.response, _this.Service);
//EwsLogging.DebugLog(ewsXmlReader.JsObject, true);
var serviceResponse = _this.ReadResponsePrivate(ewsXmlReader.JsObject);
if (successDelegate)
successDelegate(serviceResponse || xhrResponse.responseText || xhrResponse.response);
}
else {
if (errorDelegate)
errorDelegate(_this.ProcessWebException(serviceResponse || xhrResponse.responseText || xhrResponse.response) || serviceResponse);
}
}, function (resperr) {
EwsLogging_1.EwsLogging.Log("Error in calling service, error code:" + resperr.status + "\r\n" + ((resperr.getAllResponseHeaders)?resperr.getAllResponseHeaders():""));
if (errorDelegate)
errorDelegate(_this.ProcessWebException(resperr) || resperr);
});
});
};
Anyone, please post the source code on how to send a email using ews-javascript-api-auth and ews-javascript-api. Having issue in authentication.
Can't get it work. I get always the message:
"credentials are required to make a service request",
var ews = require("ews-javascript-api");
var ewsAuth = require("ews-javascript-api-auth");
ews.ConfigurationApi.ConfigureXHR(new ewsAuth.ntlmAuthXhrApi(credentials.user, credentials.password));
var exch = new ews.ExchangeService(ews.ExchangeVersion.Exchange2013);
exch.Url = new ews.Uri("https://myexchange/ews/exchange.asmx");
exch.GetPasswordExpirationDate("[email protected]").then(function(value){
console.log(value);
},function(err){
console.log(err);
});
any plans to add typings like in the ews-javascript-api ?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.