From 42f56e0671e2a089bb4ad0ec1e6a1ed28e24212d Mon Sep 17 00:00:00 2001 From: chughts Date: Thu, 24 May 2018 21:19:34 +0200 Subject: [PATCH] Implment switch between authentication mechanisms --- README.md | 5 +++- services/visual_recognition/v3.js | 38 +++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index d8e155e3..9edb5e00 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,11 @@ Node-RED Watson Nodes for IBM Cloud CLA assistant +### New in version 0.6.14 +- Visual Recognition instances created post May 22 2018, have a new authentication mechanism + ### New in version 0.6.13 -- Added opt-out option for collection parsing of strings in Natural Language Classifier Node. +- Added opt-out option for collection parsing of strings in Natural Language Classifier Node. ### New in version 0.6.12 - Fix to collection check in Natural Language Classification Node allowing for . in domain diff --git a/services/visual_recognition/v3.js b/services/visual_recognition/v3.js index 25ce7eb8..a8ab47cd 100644 --- a/services/visual_recognition/v3.js +++ b/services/visual_recognition/v3.js @@ -40,7 +40,10 @@ module.exports = function(RED) { async = require('async'), toArray = require('stream-to-array'), sAPIKey = null, - service = null; + iamAPIKey = false, + service = null, + endpoint = '', + sEndpoint = ''; // temp is being used for file streaming to allow the file to arrive so it can be processed. temp.track(); @@ -49,6 +52,7 @@ module.exports = function(RED) { if (service) { sAPIKey = service.api_key; + sEndpoint = service.url; } RED.httpAdmin.get('/watson-visual-recognition/vcap', function(req, res) { @@ -154,13 +158,23 @@ module.exports = function(RED) { } var serviceSettings = { - api_key: node.apikey, version_date: '2018-03-19', headers: { 'User-Agent': pkg.name + '-' + pkg.version } }; + if (endpoint) { + serviceSettings.url = endpoint; + } + + // VR instances created post 22 May 2018, are expecting an iam API Key + if (iamAPIKey) { + serviceSettings.iam_apikey = node.apikey; + } else { + serviceSettings.api_key = node.apikey; + } + // The change to watson-developer-cloud 3.0.x has resulted in a // change in how the Accept-Language is specified. It now needs // to go in as a header. @@ -469,6 +483,7 @@ module.exports = function(RED) { shape: 'dot', text: 'Calling ' + feature + ' ...' }); + if (feature === 'classifyImage' || feature === 'detectFaces') { return executeService(feature, params, node, msg); //return Promise.resolve(); @@ -481,6 +496,22 @@ module.exports = function(RED) { } } + function determineEndpoint(config) { + // Any VR instances created post 22 May 2018, have a different endpoint + // and mechanism for authentication. This function detemines if this + // new authentication mechanism is being utlised. + iamAPIKey = false; + + endpoint = sEndpoint; + if (!endpoint && config['vr-service-endpoint']) { + endpoint = config['vr-service-endpoint']; + } + if (endpoint && 'https://gateway.watsonplatform.net/visual-recognition/api' === endpoint) { + iamAPIKey = true; + } + return Promise.resolve(); + } + // This is the Watson Visual Recognition V3 Node function WatsonVisualRecognitionV3Node(config) { var node = this, @@ -506,6 +537,9 @@ module.exports = function(RED) { .then(function() { return verifyInputs(feature, msg); }) + .then(function() { + return determineEndpoint(config); + }) .then(function() { return verifyServiceCredentials(node, msg); })