Skip to content

Commit

Permalink
fix: properly store refreshed tokens in websocket libraries (#920)
Browse files Browse the repository at this point in the history
this fixes a bug that overrided the refresh token with the old token, causing token expirations to be unavoidable
  • Loading branch information
dpopp07 authored Jul 23, 2019
1 parent 7021798 commit 4b8df28
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/recognize-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ class RecognizeStream extends Duplex {
callback(err);
}
const authHeader = { authorization: 'Bearer ' + token };
this.options.headers = extend(authHeader, this.options.headers);
this.options.headers = extend(this.options.headers, authHeader);
callback(null);
});
} else {
Expand Down
2 changes: 1 addition & 1 deletion lib/synthesize-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class SynthesizeStream extends Readable {
callback(err);
}
const authHeader = { authorization: 'Bearer ' + token };
this.options.headers = extend(authHeader, this.options.headers);
this.options.headers = extend(this.options.headers, authHeader);
callback(null);
});
} else {
Expand Down
31 changes: 26 additions & 5 deletions test/unit/speech-helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,45 @@ const rc_service = {
const speech_to_text = new SpeechToTextV1(service);
const rc_speech_to_text = new SpeechToTextV1(rc_service);

describe('speech_to_text', function() {
describe('recognizeUsingWebSocket()', function() {
it('should return a stream', function() {
describe('speech_to_text', () => {
describe('recognizeUsingWebSocket()', () => {
it('should return a stream', () => {
expect(isStream(speech_to_text.recognizeUsingWebSocket())).toBe(true);
});

it('should pass the correct parameters into RecognizeStream', function() {
it('should pass the correct parameters into RecognizeStream', () => {
const stream = speech_to_text.recognizeUsingWebSocket();
expect(stream.options.url).toBe(service.url);
expect(stream.options.headers.authorization).toBeTruthy();
expect(stream.options.token_manager).toBeUndefined();
});

it('should create a token manager in RecognizeStream if using IAM', function() {
it('should create a token manager in RecognizeStream if using IAM', () => {
const stream = rc_speech_to_text.recognizeUsingWebSocket();
expect(stream.options.url).toBe(service.url);
expect(stream.options.headers.authorization).toBeUndefined();
expect(stream.options.token_manager).toBeDefined();
});

it('should override stored header with new token on refresh', done => {
// create stream object
const stream = rc_speech_to_text.recognizeUsingWebSocket();

// mock the token request method
stream.options.token_manager.getToken = jest.fn(cb => cb(null, 'abc'));

// verify no header is set
expect(stream.options.headers.authorization).toBeUndefined();

// explicitly set a new header, simulating the first token call
stream.options.headers.authorization = 'Bearer xyz';

// request a new token and verify it has overriden the old one
stream.setAuthorizationHeaderToken(err => {
expect(err).toBeNull();
expect(stream.options.headers.authorization).toBe('Bearer abc');
done();
});
});
});
});

0 comments on commit 4b8df28

Please sign in to comment.