Nodejs NTLM/Basic auth in SOAP - node.js

I try to connect nodeJs with soap service, using SOAP package.
In server I have a basic authentification.
How I can put basic authentification in soap library? I have this code:
soap.createClient(url, function(err, client) {
console.log(err)
});
in the log err, I receive the 401 page (for not auth). In web page of SOAP package I see this method:
client.setSecurity(new soap.BasicAuthSecurity('user', 'pass'));
Where I need to put this code? the reicevied client inside createClient are undefined.
EXTRA: The ideal was I can auth using NTLM, it's posible? I have a node app inside the same machine than soap server.
EDIT
I try to set NTLM credentials with soap-ntlm-2 library, using this code:
var url = "http://server/instance/ReportService2010.asmx";
var options = {
wsdl_options: {
ntlm: true,
username: "RSUser",
password: "Reporting2012",
workstation: "",
domain: ""
}
};
soap.createClient(url, options, function (err, client, body) {
if (err) {
console.log(err);
}
// normal use
//client.setSecurity(new soap.NtlmSecurity(options.wsdl_options.userName, options.wsdl_options.password, options.wsdl_options.domain, options.wsdl_options.workstation));
// or object can be passed
client.setSecurity(new soap.NtlmSecurity(options.wsdl_options));
console.log(client.describe());
report = client.ReportingService2010.ReportingService2010Soap;
});
But I have the same error massage: client is undefined.

Related

opentok interconnect with node js express

I am implementing opentok interconnect with nodejs express.
Getting error:
No callback given to dial, while implementing dial-out.
My use case is as per documentation.
I am able to get all required values like config, sessionId, token, auth. , but still unable to connect the call.
opentok.dial(sessionId, token, config.uri, options, {
auth: {
username: config.uname, password: config.pass }
},
function (err, sipCall) {
if (err)
return res.status(500).send('Platform error starting SIP Call:'+err);
res.send(sipCall);
});
It looks like you are posting too many arguments into the function before the callback.
From the github repo we can see the example:
opentok.dial(sessionId, token, sipUri, options, function (error, sipCall) {
We can see the arguments:
sessionId
token
sipUri
options
callback
You however are passing in an object between options and the callback.
opentok.dial(sessionId, token, config.uri, options,
// Extra object argument, is this meant to be options?
{
auth: {
username: config.uname, password: config.pass}
},
function (err, sipCall) {
if (err)
return res.status(500).send('Platform error starting SIP
Call:'+err);
res.send(sipCall);
});

npm soap with auth header

I am attempting to us the npm soap package to create a series of endpoints to a remote server that I can interface with through angular 4. I have read the documentation, but I am still unclear with regards to its usage. Below is the WSDL. How do I create a client that I can use to interface with the endpoint below? Here is the WSDL.
http://208.180.122.191:8081/niku/wsdl/Query/ts_pending_approvals?tenantId=clarity
My expectation is that I should get a response with the following:
var soap = require('soap');
var url = 'http://208.180.122.191:8081/niku/wsdl/Query/ts_pending_approvals?tenantId=clarity';
var args = {Username: "jdoe", Password: "*******"};
soap.createClient(url, function(err, client) {
client.Login(args, function(err, result) {
console.log(result);
});
});
When i call console.log(client.describe()), I get the following:
{ ts_pending_approvalsQueryService:
{ ts_pending_approvalsQueryService:
{ Query: [Object],
Login: [Object],
WrappedLogin: [Object],
Logout: [Object] } } }
However, when I call login and pass the username and password, i get undefined. using SoapUI, I was able to successfully complete the request, using the following. My question is how do I simulate this in node.
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:quer="http://www.niku.com/xog/Query">
<soapenv:Header/>
<soapenv:Body>
<quer:Login>
<quer:Username>jdoe</quer:Username>
<quer:Password>******</quer:Password>
</quer:Login>
</soapenv:Body>
</soapenv:Envelope>
I was able to resolve this on my own by setting the endpoint, which gave me the expected response token: 6312078__98C024DA-25CF-441E-A47B-A84DDE2FF140
var soap = require('soap');
var url = 'http://208.180.122.191:8081/niku/wsdl/Query/ts_pending_approvals';
var args = {Username: "jdoe", Password: "*****"};
soap.createClient(url, function(err, client) {
client.setEndpoint("http://208.180.122.191:8081/niku/xog")
client.Login(args,(error,result)=>{
if (error) throw error;
console.log(result)
})
});
It is also worth noting that when you're utilizing the package and you have send additional parameters, you may also have to send headers, which map to a namespace specified in the WSDL, in addition to complex structures that require multiple parameters. I was able to figure this out after some trial and error. See working example below:
/////////////////////////////////////////////////////////////////////////////////////////////////////
// 1TS Open Timesheet
ppmRouter.get("/open_time_sheet",(req,res,next) => {
var resourceid = req.query.param_resourceid
var soap = require('soap');
var url = config.wsdlQueryPath + 'open_time_sheet';
var sheader = { Auth: {Username: config.xog_user, Password: config.password}}
var args = {
Query: {Code: "open_time_sheet"},
Filter: {
param_resourceid: resourceid
}
};
soap.createClient(url, function(err, client) {
client.addSoapHeader(sheader,"","tns","Auth");
client.setEndpoint(config.xog_url)
client.Query(args,(error,result)=>{
if (error) throw error;
console.log(result)
res.send(result)
})
});
})

Consuming DynamicsNAV WebService with Node.JS node-soap

I want to consume the WebService from Microsoft Dynamics NAV 2009 with a small node.js application. The Service itself works fine, I am using it with a c# application, now I want to get data into my nodejs/expressjs application but, I always get Invalid WSDL URL as an error message.
Here is the WSDL as my Browser sees it.
Now I tried to connect with node-soap, following the documentation, by normal and by basic auth, but everytime I get an Invalid WSDL URL error.
Here are the methods I tried for a test connection:
var url = "http://navsrv:7047/DynamicsNAV2/WS/Produktiv/Page/WDCETA";
var auth = "Basic " + new Buffer("*********" + ":" + ****************").toString("base64");
soap.createClient(url, function(err, client) {
console.log('Without basic out:');
if (err)
{
console.log('[ERROR] -> ');
console.log(err);
}
console.log(client);
});
soap.createClient(url, {wsdl_headers: {Authorization: auth} }, function(err, client) {
console.log('With basic out:');
if (err)
{
console.log('[ERROR] -> ');
console.log(err);
}
console.log(client);
});
And this is the response I get:
Without basic out:
[ERROR] ->
[Error: Invalid WSDL URL: http://navsrv:7047/DynamicsNAV2/WS/Produktiv/Page/WDDCETA
Code: 401
Response Body: ]
undefined
With basic out:
[ERROR] ->
[Error: Invalid WSDL URL: http://navsrv:7047/DynamicsNAV2/WS/Produktiv/Page/WDDCETA
Code: 401
Response Body: ]
undefined
As it turned out, the build in HTTP-Server from DyanmicsNAV requires SPNEGO or NTLM as authentication. After some tries creating a proper SPNEGO request with nodejs/node-soap I turned off SPNEGO and enabled NTLM.
With the help of soap-ntlm and httpntlm I could retrieve the wsdl.
This is some testing code how I could manage to retrieve the WSDL file. For now I am happy, but I guess when it comes to invoke function there will be some other issues :)
var soap = require('soap-ntlm');
var fs = require('fs');
var httpntlm = require('httpntlm');
var url = 'http://navsrv:7047/DynamicsNAV2/WS/Produktiv/Page/WDCETA';
var username = '*******';
var password = '***********';
httpntlm.get({
url: url,
password: password,
username: username
}, function(err, wsdl) {
if (err)
{
console.log('ERR: -> ');
console.log(err);
return;
}
fs.writeFile('wsdl_cache/WDCETA.wsdl', wsdl.body, function() {
soap.createClient(__dirname + '/wsdl_cache/WDCETA.wsdl', function(err, client) {
if (err) {
console.log('SOAP ERR: ->');
console.log(err);
return;
}
client.setSecurity(new soap.NtlmSecurity(username, password));
console.log(client);
});
})
});

Node soap, consume password protected WSDL

I'm trying to build a SOAP client with Node, I'm using "soap" package (https://www.npmjs.org/package/soap) trying to consume a user/password protected WSDL.
I can't find how to pass those credentials before creating the client by "soap.createClient", and of course, I can't retrieve the WSDL if I don't provide the right credentials.
I've tried doing:
soap.security.WSSecurity('user', 'pass');
and then calling "createClient" but to no avail.
Also, I've tried to do it with the node-soap-client, with this client I (apparently) can connect to the WSDL, but after that, I've no idea where to go (how to invoke methods).
What am I doing wrong?
Thanks for all your help!
Username and password credentials can be passed like this:
var soap = require('soap');
var url = 'your WSDL url';
var auth = "Basic " + new Buffer("your username" + ":" + "your password").toString("base64");
soap.createClient(url, { wsdl_headers: {Authorization: auth} }, function(err, client) {
});
(derived from https://github.com/vpulim/node-soap/issues/56, thank you Gabriel Lucena https://github.com/glucena)
If its password protected you also need to check the correct security mechanism. I spend a day trying to figure out that the service used NTLM security(it was a clients project and I only got username and password to access the wsdl). In that case, you would need to pass the correct wsdl_options object
var wsdl_options = {
ntlm: true,
username: "your username",
password: "your password",
domain: "domain",
workstation: "workstation"
}
soap.createClient(data.credentials[data.type], {
wsdl_options
},
function(err, client) {
console.log(client.describe());
});
Also, you would need to setSecurity on the client before using any service.
the link to complete explanation: https://codecalls.com/2020/05/17/using-soap-with-node-js/
when I added the auth to the headers I still had a problem. After reading the code and a number of articles I found this to work.
// or use local wsdl if security required
let url = 'http://service.asmx?wsdl'
let wsdl = 'wsdl.wsdl';
let soap = require('soap');
let util = require('util')
soap.createClient(wsdl, function(err, client) {
//don't forget to double slash the string or else the base64 will be incorrect
client.setSecurity(new soap.BasicAuthSecurity('admin\\userName', 'password'));
client.MethodFromWSDL(args, function (err, result) {
console.log(util.inspect(result,{depth: null}))
});
});
This worked for me, the API required the auth parameters as
<UserDetails xmlns="http://url/">';
<userName>{$Username}</userName>
<password>{$Password}</password>
<program>{$program}</program>
</UserDetails>
After lots of trial and error - this ended working
const soapHeader = {
UserDetails: {
userName: process.env.userName,
password: process.env.password,
program: process.env.program
}
}
...
soap.createClient(path, function (err, client) {
if (err) {
console.log('Error creating SOAP client: ' + err);
}
client.addSoapHeader(soapHeader, "", "tns", process.env.URN);
client[process.env.FUNCTION](sargs, function (err, result, rawResponse, soapHeader, rawRequest) {
if (err) {
console.log('Error call SOAP function ' + process.env.FUNCTION + ': ', err);
}
else {
console.log(result);
}
...

Does the Gmail API support JWT?

I want to access the Gmail API using NodeJS.
I'm using a server-to-server approach (see this) but when I execute the code below, I get a backEndError, code 500 from the Google API.
Any ideas?
var authClient = new google.auth.JWT(
'email',
'key.pem',
// Contents of private_key.pem if you want to load the pem file yourself
// (do not use the path parameter above if using this param)
'key',
// Scopes can be specified either as an array or as a single, space-delimited string
['https://www.googleapis.com/auth/gmail.readonly']
);
authClient.authorize(function(err, tokens) {
if (err)
console.log(err);
gmail.users.messages.list({ userId: 'me', auth: authClient }, function(err, resp) {
// handle err and response
if (err) {
console.log(err);
});
Yes, I have the same problem. If I use the scope "https://mail.google.com", I get
403
{
"error" : "access_denied",
"error_description" : "Requested client not authorized."
}
And if I use the scope "https://mail.google.com/" (notice the / at the end), I get
403
'Forbidden'
It seems to be related to using JWT and service account.

Resources