node-red-contrib-viseo-google-authentication
Version:
VISEO Bot Maker - Google Authentication
181 lines (157 loc) • 8.51 kB
HTML
<script type="text/javascript">
RED.nodes.registerType('google-service-account',{
category: 'config',
defaults: {
name: { value: undefined, required: true },
scope: { value: undefined, required: false },
way: { value: "fields", required: false },
check_dialogflow: { value: false},
check_speech: { value: false}
},
credentials: {
projectId: { value: undefined },
client_email: { value: undefined },
private_key: { value: undefined },
json: { value: undefined }
},
label: function() {
return this.name || "Google API credential";
},
oneditprepare: function() {
if (this.scope && typeof (this.scope) !== "object") {
this.scope = this.scope.split('\n');
}
$("#node-config-input-projectId").typedInput({ default: 'str', types: ['str'], type: 'str' });
$("#node-config-input-client_email").typedInput({ default: 'str', types: ['str'], type: 'str' });
$("#node-config-input-private_key").typedInput({ default: 'str', types: ['str'], type: 'str' });
$("#node-config-input-json").typedInput({ default: 'json', types: ['json'], type: 'json' });
$("#node-config-input-way").change(function() {
$(".display_fields").hide();
$(".display_json").hide();
if ($(this).val() === "json") {
$(".display_json").show();
$("#node-config-input-json").typedInput('show');
}
else {
$(".display_fields").show();
$("#node-config-input-projectId").typedInput('show');
$("#node-config-input-client_email").typedInput('show');
$("#node-config-input-private_key").typedInput('show');
}
});
$("#node-config-input-check_dialogflow, #node-config-input-check_speech").click( function() {
let url = "https://www.googleapis.com/auth/cloud-platform";
if (!inList(url)) $("#node-input-container").editableList('addItem', { scope: url, i:i });
});
$("#node-config-input-check_sheets").click( function() {
let url = "https://www.googleapis.com/auth/spreadsheets";
if (!inList(url)) $("#node-input-container").editableList('addItem', { scope: url, i:i });
});
function inList (param) {
let items = $("#node-input-container").editableList('items');
let array = [];
items.each(function(i) {
array.push($(this).find('.node-input-scope-name').val());
});
for (let item of array) if (item === param) return true;
return false;
}
$("#node-input-container").css('min-height','100px').editableList({
addItem: function ( row, index, data ) {
var newScope = data.scope,
scope = '';
if (newScope) scope = newScope;
var hiddenField = $('<span/>', {}).appendTo(row);
var titleField = $('<input/>', { class:"node-input-scope-name", type:"text", style:"width:100%; ", value: scope})
.appendTo(row)
.typedInput({default: 'str' , types:['str'], type: 'str' });
},
sortable: false,
removable: true
});
this.scope = this.scope || new Array();
for (var i=0; i<this.scope.length; i++) {
var infos = this.scope[i];
$("#node-input-container").editableList('addItem', { scope: this.scope[i], i:i });
}
},
oneditsave: function(){
// Scopes
let results = new Array();
let scope = $("#node-input-container").editableList('items');
scope.each(function(i) {
let newScope = $(this).find('.node-input-scope-name').val();
if (newScope) results.push(newScope);
});
this.scope = results;
}
});
</script>
<style>
.input-info { font-size: 12px; padding-left: 104px; font-style: italic; }
</style>
<script type="text/x-red" data-template-name="google-service-account">
<div class="form-row">
<label for="node-config-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-config-input-name" placeholder="">
</div>
<br>
<div class="form-row">
<label for="node-config-input-way"><i class="fa fa-tasks"></i> Config</label>
<select id="node-config-input-way" style="width:70%;">
<option value="fields" selected>Copy/Paste fields</option>
<option value="json" >Copy/Paste JSON file</option>
</select>
</div>
<br>
<p class="input-info display_fields" style="min-width:500px;">
Create a Project in Google Cloud Platform with required APIs.<br/>
Then create a "Service Account" in Credential tab.<br/>
Download the credentials JSON file, and copy-paste the following fields:
</p>
<div class="form-row display_fields">
<label for="node-config-input-projectId"><i class="fa fa-lock"></i> project_id</label>
<input type="text" id="node-config-input-projectId" placeholder="" style="width:70%">
</div>
<div class="form-row display_fields">
<label for="node-config-input-private_key"><i class="fa fa-lock"></i> private_key</label>
<input type="text" id="node-config-input-private_key" placeholder="-----BEGIN PRIVATE KEY-----\nXXX...XXX\n-----END PRIVATE KEY-----\n" style="width:70%">
</div>
<div class="form-row display_fields">
<label for="node-config-input-client_email"><i class="fa fa-lock"></i> client_email</label>
<input type="text" id="node-config-input-client_email" placeholder="{user}@{project}.iam.gserviceaccount.com" style="width:70%">
</div>
<p class="input-info display_json" style="min-width:450px;">
Create a Project in Google Cloud Platform with required APIs.<br/>
Then create a "Service Account" in Credential tab.<br/>
Download the credentials JSON file, and copy-paste the content:
</p>
<div class="form-row display_json">
<label for="node-config-input-json"><i class="fa fa-lock"></i> JSON</label>
<input type="text" id="node-config-input-json" style="width:70%">
</div>
<br>
<div class="form-row">
<label><i class="fa fa-globe"></i> Scopes</label>
<p class="input-info" style="display:inline-block; padding-left: 0px;">
Scopes that you might need to request to access Google APIs, <br/>
depending on the level of access you need (not required): </p><br/>
<button style="margin-left:105px;" id="node-config-input-check_dialogflow">Dialogflow</button>
<button style="margin-left:10px;" id="node-config-input-check_speech">Speech</button><br/>
<button style="margin-left:10px;" id="node-config-input-check_sheets">Sheets</button><br/>
</div>
<div class="form-row" style="width:70%; margin-left:105px;">
<ol id="node-input-container"></ol>
</div>
</script>
<script type="text/x-red" data-help-name="google-service-account">
<p>Complete with data from a Google Service Account. </p>
<h3>Details</h3>
<p>The simplier way to complete data is to copy/paste the content of the JSON key file given by Google (Google service account key).
The needes scopes are given in Google documentations.</p>
<h3>References</h3>
<ul>
<li><a href="https://github.com/googleapis/google-auth-library-nodejs#readme">Google auth library</a> - the github repository</li>
<li><a href="https://github.com/NGRP/node-red-contrib-viseo/">VISEO BotMaker</a> - the nodes github repository</li>
</ul>
</script>