Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Digest and Bearer Auth modes to http request node #2061

Merged
merged 11 commits into from Feb 27, 2019
30 changes: 28 additions & 2 deletions packages/node_modules/@node-red/nodes/core/io/21-httprequest.html
Expand Up @@ -49,11 +49,19 @@
<label for="node-input-useAuth" style="width: 70%;"><span data-i18n="httpin.basicauth"></span></label>
<div style="margin-left: 20px" class="node-input-useAuth-row hide">
<div class="form-row">
<label for="node-input-authType"><i class="fa fa-user-secret "></i> <span data-i18n="httpin.label.authType"></span></label>
<select type="text" id="node-input-authType" style="width:70%;">
<option value="basic" data-i18n="httpin.basic"></option>
<option value="digest" data-i18n="httpin.digest"></option>
<option value="bearer" data-i18n="httpin.bearer"></option>
</select>
</div>
<div class="form-row node-input-basic-row">
<label for="node-input-user"><i class="fa fa-user"></i> <span data-i18n="common.label.username"></span></label>
<input type="text" id="node-input-user">
</div>
<div class="form-row">
<label for="node-input-password"><i class="fa fa-lock"></i> <span data-i18n="common.label.password"></span></label>
<label for="node-input-password"> <i class="fa fa-lock"></i> <span data-i18n="common.label.password" id="node-span-password"></span><span data-i18n="httpin.label.bearerToken" id="node-span-token" style="hide"></span></label>
<input type="password" id="node-input-password">
</div>
</div>
Expand Down Expand Up @@ -93,7 +101,8 @@
paytoqs: {value: false},
url:{value:"",validate:function(v) { return (v.trim().length === 0) || (v.indexOf("://") === -1) || (v.trim().indexOf("http") === 0)} },
tls: {type:"tls-config",required: false},
proxy: {type:"http proxy",required: false}
proxy: {type:"http proxy",required: false},
authType: {value: "basic"}
},
credentials: {
user: {type:"text"},
Expand All @@ -115,12 +124,29 @@
$("#node-input-useAuth").change(function() {
if ($(this).is(":checked")) {
$(".node-input-useAuth-row").show();
// Nodes (< version 0.20.x) with credentials but without authentication type, need type 'basic'
if (!$('#node-input-authType').val()) {
$('#node-input-authType').val('basic');
}
} else {
$(".node-input-useAuth-row").hide();
$('#node-input-authType').val('');
$('#node-input-user').val('');
$('#node-input-password').val('');
}
});
$("#node-input-authType").change(function() {
if ($(this).val() == "basic" || $(this).val() == "digest") {
$(".node-input-basic-row").show();
$('#node-span-password').show();
$('#node-span-token').hide();
} else if ($(this).val() == "bearer") {
$(".node-input-basic-row").hide();
$('#node-span-password').hide();
$('#node-span-token').show();
$('#node-input-user').val('');
}
});
$("#node-input-method").change(function() {
if ($(this).val() == "GET") {
$(".node-input-paytoqs-row").show();
Expand Down
29 changes: 24 additions & 5 deletions packages/node_modules/@node-red/nodes/core/io/21-httprequest.js
Expand Up @@ -33,6 +33,7 @@ module.exports = function(RED) {
var tlsNode = RED.nodes.getNode(n.tls);
}
this.ret = n.ret || "txt";
this.authType = n.authType || "basic";
if (RED.settings.httpRequestTimeout) { this.reqTimeout = parseInt(RED.settings.httpRequestTimeout) || 120000; }
else { this.reqTimeout = 120000; }

Expand Down Expand Up @@ -175,11 +176,29 @@ module.exports = function(RED) {
}
}
}
if (this.credentials && this.credentials.user) {
opts.auth = {
user: this.credentials.user,
pass: this.credentials.password||""
};
if (this.credentials) {
if (this.authType === "basic") {
if (this.credentials.user) {
opts.auth = {
user: this.credentials.user,
pass: this.credentials.password || ""
};
}
} else if (this.authType === "digest") {
if (this.credentials.user) {
// The first request will be send without auth information. Based on the 401 response, the library can determine
// which auth type is required by the server. Then the request is resubmitted the with the appropriate auth header.
opts.auth = {
user: this.credentials.user,
pass: this.credentials.password || "",
sendImmediately: false
};
}
} else if (this.authType === "bearer") {
opts.auth = {
bearer: this.credentials.password || ""
};
}
}
var payload = null;

Expand Down
Expand Up @@ -380,12 +380,17 @@
"status": "Status code",
"headers": "Headers",
"other": "other",
"authType": "Type",
"bearerToken": "Token",
"paytoqs" : "Append msg.payload as query string parameters"
},
"setby": "- set by msg.method -",
"basicauth": "Use basic authentication",
"basicauth": "Use authentication",
"use-tls": "Enable secure (SSL/TLS) connection",
"tls-config":"TLS Configuration",
"basic": "basic authentication",
"digest": "digest authentication",
"bearer": "bearer authentication",
"use-proxy": "Use proxy",
"proxy-config": "Proxy Configuration",
"use-proxyauth": "Use proxy authentication",
Expand Down