bartbutenaers / node-red-contrib-sse-client

Node-Red node to receive Server Sent Events
Apache License 2.0
16 stars 6 forks source link

[Request] Digest Auth #2

Closed L-P-G closed 3 years ago

L-P-G commented 3 years ago

Any chance you can add digest authentication like the standard http node has? Or if you know how to pass the auth cookie from the http request node to your node that would also be great.

bartbutenaers commented 3 years ago

Hi @injector22,

Well I have added - at the time being - the digest authentication to the standard httprequest-node (see this pull-request and this discussion). However the httprequest-node uses the request library under the cover, which supports digest authentication out of the box. Although there is also a problem, because that library is deprecated and there is still no successor found (as you can see here. So the digest authentication is a more general problem within Node-RED at the moment ...

This node uses the "eventsource" library under the cover, which in turn uses the standard NodeJs https library (as you can see here). And - as far as I know - that library doesn't support digest authentication at all...

That is as far as my knowledge about digest authentication reaches ... So I don't "think" I can support digest authentication in this node.

I see in this StackOverflow discussion somebody that uses following headers:

Authorization: 'Digest username="uname", realm="API realm", nonce="UZ43b0FWC9591pMjy1i6H2okVwgMbDVO6fcgcQ", uri="/path/to/uri", qop="auth", response="09c536e22bca031cdbcb289e4065064a", nc="1", cnonce=""' } 

You could try to pass something like that via the msg.headers, but I have no clue whether that would work...

You could ask your question to the author of the "eventsource" library, to check whether he can add it (and then I can add it afterwards). But I'm out of ideas ...

Bart

L-P-G commented 3 years ago

I did some digging and I think I figured out how to extract the session cookie from the http node. I was looking through your docs but what I don't see is how the header should be formatted when passing it using msg.header. Since it needs to be a value pair of

cookie: sessionid=XXXXXXXXX

Does the SSE node accept the pair value using JSON?

bartbutenaers commented 3 years ago

I expect it to look like this: {'Cookie': 'test=test'} When I look at the docs of the eventsource node which I'm using

L-P-G commented 3 years ago

Perfect, I got it to work. Here's the flow in case you want to add it to your examples.

[{"id":"830c8b80.f89718","type":"inject","z":"f194162.538f0e8","name":"Start / Restart / Unpause stream","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":290,"y":520,"wires":[["5944b091.17285"]]},{"id":"de79afb2.f459b","type":"inject","z":"f194162.538f0e8","name":"Pause stream","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":350,"y":640,"wires":[["31c2c1a4.b6aabe"]]},{"id":"3e4f28c7.35f5c8","type":"debug","z":"f194162.538f0e8","name":"Display event","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":980,"y":560,"wires":[]},{"id":"31c2c1a4.b6aabe","type":"change","z":"f194162.538f0e8","name":"","rules":[{"t":"set","p":"pause","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":580,"y":640,"wires":[["50364aa0.f4b3d4"]]},{"id":"fc7709e7.f95788","type":"inject","z":"f194162.538f0e8","name":"Stop stream","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":350,"y":600,"wires":[["fc633663.b0ce48"]]},{"id":"fc633663.b0ce48","type":"change","z":"f194162.538f0e8","name":"","rules":[{"t":"set","p":"stop","pt":"msg","to":"true","tot":"bool"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":600,"wires":[["50364aa0.f4b3d4"]]},{"id":"50364aa0.f4b3d4","type":"sse-client","z":"f194162.538f0e8","name":"","url":" http://10.10.99.8/stream/meter","events":["message"],"headers":{},"proxy":"","restart":true,"rejectUnauthorized":false,"withCredentials":true,"timeout":"10","x":810,"y":620,"wires":[["3e4f28c7.35f5c8"]]},{"id":"5944b091.17285","type":"http request","z":"f194162.538f0e8","name":"","method":"GET","ret":"txt","paytoqs":false,"url":"http://10.10.99.8/installer/setup/home","tls":"","persist":false,"proxy":"","authType":"digest","x":530,"y":520,"wires":[["5e0c1620.061b18"]]},{"id":"5e0c1620.061b18","type":"function","z":"f194162.538f0e8","name":"Set Cookie","func":"msg.headers = {'Cookie': 'sessionId=' + msg.responseCookies.sessionId.value};\nreturn msg;","outputs":1,"noerr":0,"x":730,"y":520,"wires":[["50364aa0.f4b3d4"]]}]

Thanks for your help/

bartbutenaers commented 3 years ago

Wow, cool... Thanks a lot for sharing your solution!!! I will add your fliw on the readme page.