Skip to content

Commit

Permalink
fix: refresh iam tokens in websocket methods (#913)
Browse files Browse the repository at this point in the history
  • Loading branch information
dpopp07 authored Jul 17, 2019
1 parent 2efbaaf commit 5a2876a
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 14 deletions.
9 changes: 1 addition & 8 deletions lib/recognize-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 (
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 1 addition & 4 deletions lib/synthesize-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ class SynthesizeStream extends Readable {
private options;
private socket;
private initialized: boolean;
private authenticated: boolean;


/**
Expand Down Expand Up @@ -89,7 +88,6 @@ class SynthesizeStream extends Readable {
super(options);
this.options = options;
this.initialized = false;
this.authenticated = options.token_manager ? false : true;
}

initialize() {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 0 additions & 2 deletions test/unit/speech-helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ 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();
});

it('should create a token manager in RecognizeStream if using IAM', 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();
});
});
Expand Down

0 comments on commit 5a2876a

Please sign in to comment.