From 9597d4fe455748d53585779947490854dd61f27b Mon Sep 17 00:00:00 2001 From: Logan Patino Date: Mon, 20 Jan 2020 11:32:49 -0500 Subject: [PATCH] fix(auth): Ensure VCAP_SERVICES always get checked for and take priority --- app.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app.js b/app.js index fc8de4e..850a6d9 100644 --- a/app.js +++ b/app.js @@ -17,15 +17,15 @@ let bearerToken = process.env.SPEECH_TO_TEXT_BEARER_TOKEN; let username = process.env.SPEECH_TO_TEXT_USERNAME; let password = process.env.SPEECH_TO_TEXT_PASSWORD; +// On Cloud Foundry, we'll have a VCAP_SERVICES environment variable with credentials. +let vcapCredentials = vcapServices.getCredentials('speech_to_text'); + // Create appropriate token manager. let tokenManager; -if (apikey) { - // If we're using IAM, it's possible that we're running on Cloud Foundry, where we need to look at the VCAP_SERVICES. - const vcapCredentials = vcapServices.getCredentials('speech_to_text'); - if (vcapCredentials) { - apikey = vcapCredentials.apikey || apikey; - url = vcapCredentials.url || url; - } +if (vcapCredentials || apikey) { + // Choose credentials from VCAP if they exist. + apikey = (vcapCredentials && vcapCredentials.apikey) || apikey; + url = (vcapCredentials && vcapCredentials.url) || url; try { tokenManager = new IamTokenManager({ apikey });