UNPKG

node-red-contrib-credentials

Version:

Provides a credentials node to store one or more private values; preventing export to flows or version control.

154 lines (149 loc) 6.04 kB
<script type="text/javascript"> RED.nodes.registerType('credentials',{ category: 'storage', color: '#E9967A', defaults: { name: {value:""}, props: [] }, credentials: { creds: {type: "text", required: true}, }, inputs:1, outputs:1, icon: "credentials.png", label: function() { return this.name||"credentials"; }, labelStyle: function() { return this.name?"node_label_italic":""; }, oneditprepare: function() { var node = this; var hiddenType = { value:"hidden", label:"hidden", icon:"icons/node-red-contrib-credentials/credpass.png", validate:function(e){ return e; } }; node.resizeRule = function(width) { $('#node-input-value-container .red-ui-typedInput-container').width(width-150); } $('#node-input-value-container').css('min-height','300px').editableList({ addItem: function(container, i, value) { var row1 = $('<div/>').appendTo(container); var row2 = $('<div/>',{style:"margin-top:8px;"}).appendTo(container); $('<label/>',{for:"node-input-value-"+i,style:"margin-right:8px;text-align:right;width:40px;"}) .text("private").appendTo(row1); $('<input/>',{id:"node-input-value-"+i,class:"node-input-value",type:"text",style:"display:none;width:100%;",placeholder:"value"}) .appendTo(row1) .typedInput({default:'str',types:['str',hiddenType]}) .change(function(type, value) { if (typeof value != 'undefined') { var niv = $("#node-input-value-" + i + " ~ .red-ui-typedInput-container > .red-ui-typedInput-input-wrap > input"); if (value == 'hidden'){ $('#node-input-value-' + i).attr('type', 'password'); niv.attr('type', 'password'); }else{ if ($('#node-input-value-' + i).attr('orig') == 'hidden'){ $('#node-input-value-' + i).val(''); niv.val(''); } $('#node-input-value-' + i).attr('type', 'text'); niv.attr('type', 'text'); } } $('#node-input-value-' + i).css('outline','none').css('border-radius', '10px').css('width','100%'); }); $('<label/>',{for:"node-input-prop-"+i,style:"margin-right:8px;text-align:right;width:40px;"}) .text("to").appendTo(row2); $('<input/>',{id:"node-input-prop-"+i,class:"node-input-prop",type:"text",style:"width:100%;",placeholder:"property"}) .appendTo(row2).typedInput({default:'msg',types:['msg','flow','global']}); node.resizeRule($('.editor-tray-content form').width()); // Load existing data if (typeof value.cred != 'undefined') { if (value.cred.type == 'hidden') { $('#node-input-value-' + i).attr('type', 'password'); } row1.find('.node-input-value').typedInput('value', value.cred.value); row1.find('.node-input-value').typedInput('type', value.cred.type).attr('orig', value.cred.type); } if (typeof value.prop != 'undefined') { row2.find('.node-input-prop').typedInput('value', value.prop.value); row2.find('.node-input-prop').typedInput('type', value.prop.type); } }, removable: true, sortable: true }); // Load existing data if (typeof node.props != 'undefined') { if (typeof node.credentials.creds != 'undefined') { if (node.credentials.creds != '') { node.creds = JSON.parse(node.credentials.creds); } } if (typeof node.creds == 'undefined') { node.creds = []; } for (var i = 0;i < node.props.length;i++) { var value = {}; if (typeof node.creds[i] != 'undefined') { value.cred = node.creds[i]; } value.prop = node.props[i]; $('#node-input-value-container').editableList('addItem', value); } } }, oneditresize:function(size) { this.resizeRule(size.width); }, oneditsave: function() { var creds = []; var props = []; $('.node-input-value').each(function() { var cred = {}; cred.value = $(this).typedInput("value"); cred.type = $(this).typedInput("type"); creds.push(cred); }); this.creds = creds; $('#node-input-creds').val(JSON.stringify(creds)); $('.node-input-prop').each(function() { var prop = {}; prop.value = $(this).typedInput("value"); prop.type = $(this).typedInput("type"); props.push(prop); }); this.props = props; } }); </script> <script type="text/x-red" data-template-name="credentials"> <div class="form-row"> <label for="node-input-name"><i class="icon-tag"></i> Name</label> <input type="text" id="node-input-name" placeholder="Name"> </div> <div class="form-row"> <input type="hidden" id="node-input-creds"> </div> <div class="form-row" style="margin-bottom:0;"> <label><i class="fa fa-list"></i> <span>Values</span></label> </div> <div class="form-row node-input-value-container-row"> <ol id="node-input-value-container"></ol> </div> </script> <script type="text/x-red" data-help-name="credentials"> <p>A flow passed through this node will have the given values applied to the selected context properties. Information is stored using the Node-RED credentials API (flows_cred.json). Flows can be exported or committed without having these values stored in the flows.json. </p> <h3>References</h3> <ul> <li>The official <a href="https://github.com/Steveorevo/node-red-contrib-credentials" target=_blank>node-red-contrib-credentials</a> project on GitHub.</li> </ul> </script>