From bcb6d1cf933fabacf1ecb57f987a95ab618b52dc Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Fri, 11 Jun 2021 14:25:18 +0100 Subject: [PATCH 1/3] Fix for basic auth with @ in username --- .../@node-red/nodes/core/network/21-httprequest.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js b/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js index e6c31bfaa..42e403540 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js @@ -207,12 +207,18 @@ module.exports = function(RED) { } if (this.credentials) { if (this.authType === "basic") { + var cred = "" if (this.credentials.user) { - opts.username = this.credentials.user; + // opts.username = this.credentials.user; + cred = this.credentials.user } if (this.credentials.password) { - opts.password = this.credentials.password; + // opts.password = this.credentials.password; + cred += ":" + this.credentials.password } + // build own basic auth header + // Workaround for https://github.com/sindresorhus/got/issues/1169 + opts.headers.Authorization = "Basic " + Buffer.from(cred).toString("base64"); } else if (this.authType === "digest") { let digestCreds = this.credentials; let sentCreds = false; From 553bec1a1fa349b830c0ec83d764625409446eb2 Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Fri, 11 Jun 2021 14:34:16 +0100 Subject: [PATCH 2/3] Add testcase --- .../nodes/core/network/21-httprequest_spec.js | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/nodes/core/network/21-httprequest_spec.js b/test/nodes/core/network/21-httprequest_spec.js index d00dd5043..c37265d1c 100644 --- a/test/nodes/core/network/21-httprequest_spec.js +++ b/test/nodes/core/network/21-httprequest_spec.js @@ -1624,6 +1624,26 @@ describe('HTTP Request Node', function() { n1.receive({payload:"foo"}); }); }); + it('should authenticate on server - basic', function(done) { + var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",url:getTestURL('/authenticate')}, + {id:"n2", type:"helper"}]; + helper.load(httpRequestNode, flow, function() { + var n1 = helper.getNode("n1"); + var n2 = helper.getNode("n2"); + n1.credentials = {user:'foo@example.com', password:'passwordfoo'}; + n2.on("input", function(msg) { + try { + msg.should.have.property('statusCode',200); + msg.payload.should.have.property('user', 'foo@example.com'); + msg.payload.should.have.property('pass', 'passwordfoo'); + done(); + } catch(err) { + done(err); + } + }); + n1.receive({payload:"foo"}); + }); + }); it('should authenticate on server - bearer', function(done) { var flow = [{id:"n1",type:"http request",wires:[["n2"]],method:"GET",ret:"obj",authType:"bearer", url:getTestURL('/authenticate')}, {id:"n2", type:"helper"}]; From 919aee64f9fcc622988e6db8f652146dfca8a7e3 Mon Sep 17 00:00:00 2001 From: Ben Hardill Date: Fri, 11 Jun 2021 14:48:41 +0100 Subject: [PATCH 3/3] Add support for user/pass in URL --- .../@node-red/nodes/core/network/21-httprequest.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js b/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js index 42e403540..a498d4ac7 100644 --- a/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js +++ b/packages/node_modules/@node-red/nodes/core/network/21-httprequest.js @@ -207,17 +207,22 @@ module.exports = function(RED) { } if (this.credentials) { if (this.authType === "basic") { + // Workaround for https://github.com/sindresorhus/got/issues/1169 var cred = "" + var parsedURL = new URL(url) if (this.credentials.user) { // opts.username = this.credentials.user; cred = this.credentials.user + } else if (parsedURL.username) { + cred = parsedURL.username } if (this.credentials.password) { // opts.password = this.credentials.password; cred += ":" + this.credentials.password + } else if (parsedURL.password) { + cred += ":" + parsedURL.password } // build own basic auth header - // Workaround for https://github.com/sindresorhus/got/issues/1169 opts.headers.Authorization = "Basic " + Buffer.from(cred).toString("base64"); } else if (this.authType === "digest") { let digestCreds = this.credentials;