Skip to content

Commit 42f56e0

Browse files
committed
Implment switch between authentication mechanisms
1 parent 2bd3699 commit 42f56e0

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,11 @@ Node-RED Watson Nodes for IBM Cloud
77

88
<a href="https://cla-assistant.io/watson-developer-cloud/node-red-node-watson"><img src="https://cla-assistant.io/readme/badge/watson-developer-cloud/node-red-node-watson" alt="CLA assistant" /></a>
99

10+
### New in version 0.6.14
11+
- Visual Recognition instances created post May 22 2018, have a new authentication mechanism
12+
1013
### New in version 0.6.13
11-
- Added opt-out option for collection parsing of strings in Natural Language Classifier Node.
14+
- Added opt-out option for collection parsing of strings in Natural Language Classifier Node.
1215

1316
### New in version 0.6.12
1417
- Fix to collection check in Natural Language Classification Node allowing for . in domain

services/visual_recognition/v3.js

+36-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ module.exports = function(RED) {
4040
async = require('async'),
4141
toArray = require('stream-to-array'),
4242
sAPIKey = null,
43-
service = null;
43+
iamAPIKey = false,
44+
service = null,
45+
endpoint = '',
46+
sEndpoint = '';
4447

4548
// temp is being used for file streaming to allow the file to arrive so it can be processed.
4649
temp.track();
@@ -49,6 +52,7 @@ module.exports = function(RED) {
4952

5053
if (service) {
5154
sAPIKey = service.api_key;
55+
sEndpoint = service.url;
5256
}
5357

5458
RED.httpAdmin.get('/watson-visual-recognition/vcap', function(req, res) {
@@ -154,13 +158,23 @@ module.exports = function(RED) {
154158
}
155159

156160
var serviceSettings = {
157-
api_key: node.apikey,
158161
version_date: '2018-03-19',
159162
headers: {
160163
'User-Agent': pkg.name + '-' + pkg.version
161164
}
162165
};
163166

167+
if (endpoint) {
168+
serviceSettings.url = endpoint;
169+
}
170+
171+
// VR instances created post 22 May 2018, are expecting an iam API Key
172+
if (iamAPIKey) {
173+
serviceSettings.iam_apikey = node.apikey;
174+
} else {
175+
serviceSettings.api_key = node.apikey;
176+
}
177+
164178
// The change to watson-developer-cloud 3.0.x has resulted in a
165179
// change in how the Accept-Language is specified. It now needs
166180
// to go in as a header.
@@ -469,6 +483,7 @@ module.exports = function(RED) {
469483
shape: 'dot',
470484
text: 'Calling ' + feature + ' ...'
471485
});
486+
472487
if (feature === 'classifyImage' || feature === 'detectFaces') {
473488
return executeService(feature, params, node, msg);
474489
//return Promise.resolve();
@@ -481,6 +496,22 @@ module.exports = function(RED) {
481496
}
482497
}
483498

499+
function determineEndpoint(config) {
500+
// Any VR instances created post 22 May 2018, have a different endpoint
501+
// and mechanism for authentication. This function detemines if this
502+
// new authentication mechanism is being utlised.
503+
iamAPIKey = false;
504+
505+
endpoint = sEndpoint;
506+
if (!endpoint && config['vr-service-endpoint']) {
507+
endpoint = config['vr-service-endpoint'];
508+
}
509+
if (endpoint && 'https://gateway.watsonplatform.net/visual-recognition/api' === endpoint) {
510+
iamAPIKey = true;
511+
}
512+
return Promise.resolve();
513+
}
514+
484515
// This is the Watson Visual Recognition V3 Node
485516
function WatsonVisualRecognitionV3Node(config) {
486517
var node = this,
@@ -506,6 +537,9 @@ module.exports = function(RED) {
506537
.then(function() {
507538
return verifyInputs(feature, msg);
508539
})
540+
.then(function() {
541+
return determineEndpoint(config);
542+
})
509543
.then(function() {
510544
return verifyServiceCredentials(node, msg);
511545
})

0 commit comments

Comments
 (0)