Add email option for no authentication

to close #989
Bump to v2 (remove external cred file hack)
This commit is contained in:
Dave Conway-Jones 2023-04-24 22:17:39 +01:00
parent dd6bfc6f51
commit 9f9d293461
No known key found for this signature in database
GPG Key ID: 1DDB0E91A28C2643
4 changed files with 69 additions and 78 deletions

View File

@ -44,6 +44,7 @@
<select type="text" id="node-input-authtype"> <select type="text" id="node-input-authtype">
<option value="BASIC">Basic</option> <option value="BASIC">Basic</option>
<option value="XOAUTH2">XOAuth2</option> <option value="XOAUTH2">XOAuth2</option>
<option value="NONE">None</option>
</select> </select>
</div> </div>
<div class="form-row node-input-userid"> <div class="form-row node-input-userid">
@ -64,7 +65,7 @@
<input type="text" id="node-input-token" placeholder="oauth2Response.access_token"> <input type="text" id="node-input-token" placeholder="oauth2Response.access_token">
</div> </div>
<br/> <br/>
<div class="form-row"> <div class="form-row node-input-useTLS">
<label for="node-input-useTLS"><i class="fa fa-lock"></i> <span data-i18n="email.label.useTLS"></label> <label for="node-input-useTLS"><i class="fa fa-lock"></i> <span data-i18n="email.label.useTLS"></label>
<input type="checkbox" id="node-input-tls" style="display:inline-block; width:20px; vertical-align:baseline;"> <input type="checkbox" id="node-input-tls" style="display:inline-block; width:20px; vertical-align:baseline;">
<span data-i18n="email.label.rejectUnauthorised"></span> <span data-i18n="email.label.rejectUnauthorised"></span>
@ -74,22 +75,6 @@
<input type="text" id="node-input-dname" data-i18n="[placeholder]node-red:common.label.name"> <input type="text" id="node-input-dname" data-i18n="[placeholder]node-red:common.label.name">
</div> </div>
<div class="form-tips" id="node-tip"><span data-i18n="[html]email.tip.cred"></span></div> <div class="form-tips" id="node-tip"><span data-i18n="[html]email.tip.cred"></span></div>
<script>
$("#node-input-authtype").change(function() {
var protocol = $("#node-input-authtype").val();
if (protocol === "BASIC") {
$(".node-input-password").show();
$(".node-input-saslformat").hide();
$(".node-input-token").hide();
} else {
$(".node-input-password").hide();
$(".node-input-saslformat").show();
$(".node-input-token").show();
$("#node-input-fetch").val("trigger");
$("#node-input-fetch").change();
}
});
</script>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -131,6 +116,30 @@
this.authtype = "BASIC"; this.authtype = "BASIC";
$("#node-input-authtype").val('BASIC'); $("#node-input-authtype").val('BASIC');
} }
$("#node-input-authtype").change(function() {
var protocol = $("#node-input-authtype").val();
if (protocol === "BASIC") {
$(".node-input-userid").show();
$(".node-input-password").show();
$(".node-input-saslformat").hide();
$(".node-input-token").hide();
$(".node-input-useTLS").show();
} else if (protocol === "NONE") {
$(".node-input-userid").hide();
$(".node-input-password").hide();
$(".node-input-saslformat").hide();
$(".node-input-token").hide();
$(".node-input-useTLS").hide();
} else {
$(".node-input-userid").show();
$(".node-input-password").hide();
$(".node-input-saslformat").show();
$(".node-input-token").show();
$("#node-input-fetch").val("trigger");
$("#node-input-fetch").change();
$(".node-input-useTLS").show();
}
});
if (this.credentials.global) { if (this.credentials.global) {
$('#node-tip').show(); $('#node-tip').show();
} else { } else {
@ -190,13 +199,14 @@
<select type="text" id="node-input-authtype"> <select type="text" id="node-input-authtype">
<option value="BASIC">Basic</option> <option value="BASIC">Basic</option>
<option value="XOAUTH2">XOAuth2</option> <option value="XOAUTH2">XOAuth2</option>
<option value="NONE">None</option>
</select> </select>
</div> </div>
<div class="form-row node-input-userid"> <div class="form-row node-input-userid" id="node-userid">
<label for="node-input-userid"><i class="fa fa-user"></i> <span data-i18n="email.label.userid"></span></label> <label for="node-input-userid"><i class="fa fa-user"></i> <span data-i18n="email.label.userid"></span></label>
<input type="text" id="node-input-userid"> <input type="text" id="node-input-userid">
</div> </div>
<div class="form-row node-input-password"> <div class="form-row node-input-password" id="node-password">
<label for="node-input-password"><i class="fa fa-lock"></i> <span data-i18n="email.label.password"></span></label> <label for="node-input-password"><i class="fa fa-lock"></i> <span data-i18n="email.label.password"></span></label>
<input type="password" id="node-input-password"> <input type="password" id="node-input-password">
</div> </div>
@ -284,10 +294,18 @@
$("#node-input-authtype").change(function() { $("#node-input-authtype").change(function() {
var protocol = $("#node-input-authtype").val(); var protocol = $("#node-input-authtype").val();
if (protocol === "BASIC") { if (protocol === "BASIC") {
$(".node-input-userid").show();
$(".node-input-password").show(); $(".node-input-password").show();
$(".node-input-saslformat").hide(); $(".node-input-saslformat").hide();
$(".node-input-token").hide(); $(".node-input-token").hide();
} else { } else if (protocol === "NONE") {
$(".node-input-userid").hide();
$(".node-input-password").hide();
$(".node-input-saslformat").hide();
$(".node-input-token").hide();
}
else {
$(".node-input-userid").show();
$(".node-input-password").hide(); $(".node-input-password").hide();
$(".node-input-saslformat").show(); $(".node-input-saslformat").show();
$(".node-input-token").show(); $(".node-input-token").show();

View File

@ -27,12 +27,6 @@ module.exports = function(RED) {
throw "Error : Requires nodejs version >= 8."; throw "Error : Requires nodejs version >= 8.";
} }
try {
var globalkeys = RED.settings.email || require(process.env.NODE_RED_HOME+"/../emailkeys.js");
}
catch(err) {
}
function EmailNode(n) { function EmailNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.topic = n.topic; this.topic = n.topic;
@ -41,7 +35,6 @@ module.exports = function(RED) {
this.outport = n.port; this.outport = n.port;
this.secure = n.secure; this.secure = n.secure;
this.tls = true; this.tls = true;
var flag = false;
this.authtype = n.authtype || "BASIC"; this.authtype = n.authtype || "BASIC";
if (this.authtype !== "BASIC") { if (this.authtype !== "BASIC") {
this.inputs = 1; this.inputs = 1;
@ -49,36 +42,26 @@ module.exports = function(RED) {
} }
if (this.credentials && this.credentials.hasOwnProperty("userid")) { if (this.credentials && this.credentials.hasOwnProperty("userid")) {
this.userid = this.credentials.userid; this.userid = this.credentials.userid;
} else { }
if (globalkeys) { else if (this.authtype !== "NONE") {
this.userid = globalkeys.user; this.error(RED._("email.errors.nouserid"));
flag = true;
} else {
this.error(RED._("email.errors.nouserid"));
}
} }
if (this.authtype === "BASIC" ) { if (this.authtype === "BASIC" ) {
if (this.credentials && this.credentials.hasOwnProperty("password")) { if (this.credentials && this.credentials.hasOwnProperty("password")) {
this.password = this.credentials.password; this.password = this.credentials.password;
} else {
if (globalkeys) {
this.password = globalkeys.pass;
flag = true;
} else {
this.error(RED._("email.errors.nopassword"));
}
} }
} else { else {
this.error(RED._("email.errors.nopassword"));
}
}
else if (this.authtype === "XOAUTH2") {
this.saslformat = n.saslformat; this.saslformat = n.saslformat;
if(n.token!=="") { if (n.token !== "") {
this.token = n.token; this.token = n.token;
} else { } else {
this.error(RED._("email.errors.notoken")); this.error(RED._("email.errors.notoken"));
} }
} }
if (flag) {
RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true});
}
if (n.tls === false) { this.tls = false; } if (n.tls === false) { this.tls = false; }
var node = this; var node = this;
@ -94,10 +77,11 @@ module.exports = function(RED) {
user: node.userid, user: node.userid,
pass: node.password pass: node.password
}; };
} else if(node.authtype == "XOAUTH2") { }
else if (node.authtype === "XOAUTH2") {
var value = RED.util.getMessageProperty(msg,node.token); var value = RED.util.getMessageProperty(msg,node.token);
if (value !== undefined) { if (value !== undefined) {
if(node.saslformat) { if (node.saslformat) {
//Make base64 string for access - compatible with outlook365 and gmail //Make base64 string for access - compatible with outlook365 and gmail
saslxoauth2 = Buffer.from("user="+node.userid+"\x01auth=Bearer "+value+"\x01\x01").toString('base64'); saslxoauth2 = Buffer.from("user="+node.userid+"\x01auth=Bearer "+value+"\x01\x01").toString('base64');
} else { } else {
@ -226,8 +210,8 @@ module.exports = function(RED) {
this.repeat = 1500; this.repeat = 1500;
} }
if (this.inputs === 1) { this.repeat = 0; } if (this.inputs === 1) { this.repeat = 0; }
this.inserver = n.server || (globalkeys && globalkeys.server) || "imap.gmail.com"; this.inserver = n.server || "imap.gmail.com";
this.inport = n.port || (globalkeys && globalkeys.port) || "993"; this.inport = n.port || "993";
this.box = n.box || "INBOX"; this.box = n.box || "INBOX";
this.useSSL= n.useSSL; this.useSSL= n.useSSL;
this.autotls= n.autotls; this.autotls= n.autotls;
@ -240,40 +224,28 @@ module.exports = function(RED) {
this.repeat = 0; this.repeat = 0;
} }
var flag = false;
if (this.credentials && this.credentials.hasOwnProperty("userid")) { if (this.credentials && this.credentials.hasOwnProperty("userid")) {
this.userid = this.credentials.userid; this.userid = this.credentials.userid;
} else { }
if (globalkeys) { else if (this.authtype !== "NONE") {
this.userid = globalkeys.user; this.error(RED._("email.errors.nouserid"));
flag = true;
} else {
this.error(RED._("email.errors.nouserid"));
}
} }
if (this.authtype === "BASIC" ) { if (this.authtype === "BASIC" ) {
if (this.credentials && this.credentials.hasOwnProperty("password")) { if (this.credentials && this.credentials.hasOwnProperty("password")) {
this.password = this.credentials.password; this.password = this.credentials.password;
} else {
if (globalkeys) {
this.password = globalkeys.pass;
flag = true;
} else {
this.error(RED._("email.errors.nopassword"));
}
} }
} else { else {
this.error(RED._("email.errors.nopassword"));
}
}
else if (this.authtype === "XOAUTH2") {
this.saslformat = n.saslformat; this.saslformat = n.saslformat;
if(n.token!=="") { if (n.token !== "") {
this.token = n.token; this.token = n.token;
} else { } else {
this.error(RED._("email.errors.notoken")); this.error(RED._("email.errors.notoken"));
} }
} }
if (flag) {
RED.nodes.addCredentials(n.id,{userid:this.userid, password:this.password, global:true});
}
var node = this; var node = this;
node.interval_id = null; node.interval_id = null;
@ -321,7 +293,7 @@ module.exports = function(RED) {
try { try {
node.status({fill:"grey",shape:"dot",text:"node-red:common.status.connecting"}); node.status({fill:"grey",shape:"dot",text:"node-red:common.status.connecting"});
await pop3.connect(); await pop3.connect();
if (node.authtype == "XOAUTH2") { if (node.authtype === "XOAUTH2") {
var value = RED.util.getMessageProperty(msg,node.token); var value = RED.util.getMessageProperty(msg,node.token);
if (value !== undefined) { if (value !== undefined) {
if (node.saslformat) { if (node.saslformat) {
@ -334,7 +306,7 @@ module.exports = function(RED) {
await pop3.command('AUTH', "XOAUTH2"); await pop3.command('AUTH', "XOAUTH2");
await pop3.command(saslxoauth2); await pop3.command(saslxoauth2);
} else if (node.authtype == "BASIC") { } else if (node.authtype === "BASIC") {
await pop3.command('USER', node.userid); await pop3.command('USER', node.userid);
await pop3.command('PASS', node.password); await pop3.command('PASS', node.password);
} }
@ -401,7 +373,7 @@ module.exports = function(RED) {
function checkIMAP(msg,send,done) { function checkIMAP(msg,send,done) {
var tout = (node.repeat > 0) ? node.repeat - 500 : 15000; var tout = (node.repeat > 0) ? node.repeat - 500 : 15000;
var saslxoauth2 = ""; var saslxoauth2 = "";
if (node.authtype == "XOAUTH2") { if (node.authtype === "XOAUTH2") {
var value = RED.util.getMessageProperty(msg,node.token); var value = RED.util.getMessageProperty(msg,node.token);
if (value !== undefined) { if (value !== undefined) {
if (node.saslformat) { if (node.saslformat) {

View File

@ -11,7 +11,9 @@ getting an application password if you have two-factor authentication enabled.
For Exchange and Outlook 365 you must use OAuth2.0. For Exchange and Outlook 365 you must use OAuth2.0.
**Note :** Version 1.x of this node requires **Node.js v8** or newer. **Notes **:
Version 2.x of this node required **Node.js v12** or newer.
Version 1.x of this node requires **Node.js v8** or newer.
Install Install
------- -------
@ -29,12 +31,11 @@ If you are accessing GMail you may need to either enable <a target="_new" href="
or enable <a target="_new" href="https://support.google.com/accounts/answer/6010255?hl=en">less secure access</a> via your Google account settings.</p> or enable <a target="_new" href="https://support.google.com/accounts/answer/6010255?hl=en">less secure access</a> via your Google account settings.</p>
Office 365 users Office 365 users
----------- ----------------
If you are accessing Exchnage you will need to register an application through their platform and use OAuth2.0. If you are accessing Exchnage you will need to register an application through their platform and use OAuth2.0.
<a target="_new" href="https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#get-an-access-token">Details on how to do this can be found here.</a> <a target="_new" href="https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#get-an-access-token">Details on how to do this can be found here.</a>
Usage Usage
----- -----

View File

@ -1,6 +1,6 @@
{ {
"name": "node-red-node-email", "name": "node-red-node-email",
"version": "1.19.1", "version": "2.0.0",
"description": "Node-RED nodes to send and receive simple emails.", "description": "Node-RED nodes to send and receive simple emails.",
"dependencies": { "dependencies": {
"imap": "^0.8.19", "imap": "^0.8.19",