Skip to content

Commit 5a2876a

Browse files
authored
fix: refresh iam tokens in websocket methods (#913)
1 parent 2efbaaf commit 5a2876a

File tree

3 files changed

+2
-14
lines changed

3 files changed

+2
-14
lines changed

lib/recognize-stream.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ class RecognizeStream extends Duplex {
8787
private initialized: boolean;
8888
private finished: boolean;
8989
private socket;
90-
private authenticated: boolean;
91-
92-
9390

9491
/**
9592
* 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 {
150147
this.initialized = false;
151148
this.finished = false;
152149

153-
// is using iam, another authentication step is needed
154-
this.authenticated = options.token_manager ? false : true;
155-
156150
this.on('newListener', event => {
157151
if (!options.silent) {
158152
if (
@@ -535,14 +529,13 @@ class RecognizeStream extends Duplex {
535529
* @param {Function} callback
536530
*/
537531
setAuthorizationHeaderToken(callback) {
538-
if (!this.authenticated) {
532+
if (this.options.token_manager) {
539533
this.options.token_manager.getToken((err, token) => {
540534
if (err) {
541535
callback(err);
542536
}
543537
const authHeader = { authorization: 'Bearer ' + token };
544538
this.options.headers = extend(authHeader, this.options.headers);
545-
this.authenticated = true;
546539
callback(null);
547540
});
548541
} else {

lib/synthesize-stream.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ class SynthesizeStream extends Readable {
5757
private options;
5858
private socket;
5959
private initialized: boolean;
60-
private authenticated: boolean;
6160

6261

6362
/**
@@ -89,7 +88,6 @@ class SynthesizeStream extends Readable {
8988
super(options);
9089
this.options = options;
9190
this.initialized = false;
92-
this.authenticated = options.token_manager ? false : true;
9391
}
9492

9593
initialize() {
@@ -190,14 +188,13 @@ class SynthesizeStream extends Readable {
190188
* @param {Function} callback
191189
*/
192190
setAuthorizationHeaderToken(callback) {
193-
if (!this.authenticated) {
191+
if (this.options.token_manager) {
194192
this.options.token_manager.getToken((err, token) => {
195193
if (err) {
196194
callback(err);
197195
}
198196
const authHeader = { authorization: 'Bearer ' + token };
199197
this.options.headers = extend(authHeader, this.options.headers);
200-
this.authenticated = true;
201198
callback(null);
202199
});
203200
} else {

test/unit/speech-helpers.test.js

-2
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ describe('speech_to_text', function() {
3131
const stream = speech_to_text.recognizeUsingWebSocket();
3232
expect(stream.options.url).toBe(service.url);
3333
expect(stream.options.headers.authorization).toBeTruthy();
34-
expect(stream.authenticated).toBe(true);
3534
expect(stream.options.token_manager).toBeUndefined();
3635
});
3736

3837
it('should create a token manager in RecognizeStream if using IAM', function() {
3938
const stream = rc_speech_to_text.recognizeUsingWebSocket();
4039
expect(stream.options.url).toBe(service.url);
4140
expect(stream.options.headers.authorization).toBeUndefined();
42-
expect(stream.authenticated).toBe(false);
4341
expect(stream.options.token_manager).toBeDefined();
4442
});
4543
});

0 commit comments

Comments
 (0)