From 5a2876a4b32a85a0a072bd95af9c836349cdef8c Mon Sep 17 00:00:00 2001 From: Dustin Popp Date: Wed, 17 Jul 2019 16:31:06 -0500 Subject: [PATCH] fix: refresh iam tokens in websocket methods (#913) --- lib/recognize-stream.ts | 9 +-------- lib/synthesize-stream.ts | 5 +---- test/unit/speech-helpers.test.js | 2 -- 3 files changed, 2 insertions(+), 14 deletions(-) diff --git a/lib/recognize-stream.ts b/lib/recognize-stream.ts index c872855f72..bfd5a0a3ee 100644 --- a/lib/recognize-stream.ts +++ b/lib/recognize-stream.ts @@ -87,9 +87,6 @@ class RecognizeStream extends Duplex { private initialized: boolean; private finished: boolean; private socket; - private authenticated: boolean; - - /** * pipe()-able Node.js Duplex stream - accepts binary audio and emits text/objects in it's `data` events. @@ -150,9 +147,6 @@ class RecognizeStream extends Duplex { this.initialized = false; this.finished = false; - // is using iam, another authentication step is needed - this.authenticated = options.token_manager ? false : true; - this.on('newListener', event => { if (!options.silent) { if ( @@ -535,14 +529,13 @@ class RecognizeStream extends Duplex { * @param {Function} callback */ setAuthorizationHeaderToken(callback) { - if (!this.authenticated) { + if (this.options.token_manager) { this.options.token_manager.getToken((err, token) => { if (err) { callback(err); } const authHeader = { authorization: 'Bearer ' + token }; this.options.headers = extend(authHeader, this.options.headers); - this.authenticated = true; callback(null); }); } else { diff --git a/lib/synthesize-stream.ts b/lib/synthesize-stream.ts index 995ef74737..a434071af8 100644 --- a/lib/synthesize-stream.ts +++ b/lib/synthesize-stream.ts @@ -57,7 +57,6 @@ class SynthesizeStream extends Readable { private options; private socket; private initialized: boolean; - private authenticated: boolean; /** @@ -89,7 +88,6 @@ class SynthesizeStream extends Readable { super(options); this.options = options; this.initialized = false; - this.authenticated = options.token_manager ? false : true; } initialize() { @@ -190,14 +188,13 @@ class SynthesizeStream extends Readable { * @param {Function} callback */ setAuthorizationHeaderToken(callback) { - if (!this.authenticated) { + if (this.options.token_manager) { this.options.token_manager.getToken((err, token) => { if (err) { callback(err); } const authHeader = { authorization: 'Bearer ' + token }; this.options.headers = extend(authHeader, this.options.headers); - this.authenticated = true; callback(null); }); } else { diff --git a/test/unit/speech-helpers.test.js b/test/unit/speech-helpers.test.js index 171b1e0df9..9a5bc6a9b7 100644 --- a/test/unit/speech-helpers.test.js +++ b/test/unit/speech-helpers.test.js @@ -31,7 +31,6 @@ describe('speech_to_text', function() { const stream = speech_to_text.recognizeUsingWebSocket(); expect(stream.options.url).toBe(service.url); expect(stream.options.headers.authorization).toBeTruthy(); - expect(stream.authenticated).toBe(true); expect(stream.options.token_manager).toBeUndefined(); }); @@ -39,7 +38,6 @@ describe('speech_to_text', function() { const stream = rc_speech_to_text.recognizeUsingWebSocket(); expect(stream.options.url).toBe(service.url); expect(stream.options.headers.authorization).toBeUndefined(); - expect(stream.authenticated).toBe(false); expect(stream.options.token_manager).toBeDefined(); }); });