From 489c6c7a866d4bbd3f621d64b20a4b04a3c695fb Mon Sep 17 00:00:00 2001 From: glenn Date: Wed, 9 Aug 2023 10:01:41 -0400 Subject: [PATCH 1/8] add empty token handling method in ConversationTranslatorRecognizer --- .../Transcription/ConversationServiceAdapter.ts | 1 + .../Transcription/ConversationTranslatorRecognizer.ts | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/src/common.speech/Transcription/ConversationServiceAdapter.ts b/src/common.speech/Transcription/ConversationServiceAdapter.ts index 0323d648..bc46240b 100644 --- a/src/common.speech/Transcription/ConversationServiceAdapter.ts +++ b/src/common.speech/Transcription/ConversationServiceAdapter.ts @@ -412,6 +412,7 @@ export class ConversationServiceAdapter extends ServiceRecognizerBase { return Promise.resolve(authorizationToken); }); this.authentication = token; + this.privConversationServiceConnector.onConversationToken(token); break; diff --git a/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts b/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts index ca3ea7aa..9a657b68 100644 --- a/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts +++ b/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts @@ -3,6 +3,7 @@ // eslint-disable-next-line max-classes-per-file import { + CognitiveTokenAuthentication, IAuthentication, IConnectionFactory, RecognizerConfig, @@ -207,6 +208,13 @@ export class ConversationTranslatorRecognizer extends Recognizer implements Conv } } + /** + * Handle update of conversation token (#694) + */ + public onConversationToken(token: CognitiveTokenAuthentication): void { + void token; + } + /** * Close and dispose the recognizer */ From 43db7ff932aa8ba757c0307819863f5cfd35e155 Mon Sep 17 00:00:00 2001 From: glenn Date: Thu, 10 Aug 2023 08:30:32 -0400 Subject: [PATCH 2/8] update conv. translation service recognizer auth on conversation token renewal (#694) --- .../Transcription/ConversationServiceAdapter.ts | 2 +- .../Transcription/ConversationTranslatorRecognizer.ts | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/common.speech/Transcription/ConversationServiceAdapter.ts b/src/common.speech/Transcription/ConversationServiceAdapter.ts index bc46240b..ca36fc35 100644 --- a/src/common.speech/Transcription/ConversationServiceAdapter.ts +++ b/src/common.speech/Transcription/ConversationServiceAdapter.ts @@ -412,7 +412,7 @@ export class ConversationServiceAdapter extends ServiceRecognizerBase { return Promise.resolve(authorizationToken); }); this.authentication = token; - this.privConversationServiceConnector.onConversationToken(token); + this.privConversationServiceConnector.onToken(token); break; diff --git a/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts b/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts index 9a657b68..ef5639cf 100644 --- a/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts +++ b/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts @@ -209,10 +209,10 @@ export class ConversationTranslatorRecognizer extends Recognizer implements Conv } /** - * Handle update of conversation token (#694) + * Handle update of service auth token (#694) */ - public onConversationToken(token: CognitiveTokenAuthentication): void { - void token; + public onToken(token: CognitiveTokenAuthentication): void { + this.privReco.authentication = token; } /** From 122f02342655200fe25806449a45654e8bac5603 Mon Sep 17 00:00:00 2001 From: "glharper@microsoft.com" Date: Tue, 22 Aug 2023 10:40:46 -0700 Subject: [PATCH 3/8] update proper service recognizer with new auth token --- .../Transcription/ConversationServiceAdapter.ts | 2 +- .../Transcription/ConversationTranslatorRecognizer.ts | 9 ++++++--- src/sdk/Transcription/Conversation.ts | 4 ++++ src/sdk/Transcription/ConversationTranslator.ts | 6 ++++++ 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/common.speech/Transcription/ConversationServiceAdapter.ts b/src/common.speech/Transcription/ConversationServiceAdapter.ts index ca36fc35..687b8ede 100644 --- a/src/common.speech/Transcription/ConversationServiceAdapter.ts +++ b/src/common.speech/Transcription/ConversationServiceAdapter.ts @@ -412,7 +412,7 @@ export class ConversationServiceAdapter extends ServiceRecognizerBase { return Promise.resolve(authorizationToken); }); this.authentication = token; - this.privConversationServiceConnector.onToken(token); + await this.privConversationServiceConnector.onToken(token); break; diff --git a/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts b/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts index ef5639cf..a5028286 100644 --- a/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts +++ b/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts @@ -3,6 +3,7 @@ // eslint-disable-next-line max-classes-per-file import { + AuthInfo, CognitiveTokenAuthentication, IAuthentication, IConnectionFactory, @@ -13,7 +14,8 @@ import { import { BackgroundEvent, Events, - Timeout + Timeout, + createNoDashGuid } from "../../common/Exports"; import { AudioConfigImpl } from "../../sdk/Audio/AudioConfig"; import { Contracts } from "../../sdk/Contracts"; @@ -211,8 +213,9 @@ export class ConversationTranslatorRecognizer extends Recognizer implements Conv /** * Handle update of service auth token (#694) */ - public onToken(token: CognitiveTokenAuthentication): void { - this.privReco.authentication = token; + public async onToken(token: CognitiveTokenAuthentication): Promise { + const value: AuthInfo = await token.fetch(createNoDashGuid()); + this.privConversation.onToken(value.token); } /** diff --git a/src/sdk/Transcription/Conversation.ts b/src/sdk/Transcription/Conversation.ts index 841247a3..69594056 100644 --- a/src/sdk/Transcription/Conversation.ts +++ b/src/sdk/Transcription/Conversation.ts @@ -5,6 +5,7 @@ /* eslint-disable max-classes-per-file */ import { + CognitiveTokenAuthentication, ConversationConnectionConfig, ConversationManager, ConversationReceivedTranslationEventArgs, @@ -323,6 +324,9 @@ export class ConversationImpl extends Conversation implements IDisposable { this.privConversationTranslator = conversationTranslator; } + public onToken(token: string): void { + this.privConversationTranslator.onToken(token); + } /** * Create a new conversation as Host diff --git a/src/sdk/Transcription/ConversationTranslator.ts b/src/sdk/Transcription/ConversationTranslator.ts index 503d5716..ead806f1 100644 --- a/src/sdk/Transcription/ConversationTranslator.ts +++ b/src/sdk/Transcription/ConversationTranslator.ts @@ -5,6 +5,7 @@ /* eslint-disable max-classes-per-file */ import { + CognitiveTokenAuthentication, ConversationConnectionConfig, ServicePropertiesPropertyName, } from "../../common.speech/Exports"; @@ -13,6 +14,7 @@ import { IDisposable, IErrorMessages, IStringDictionary, + createNoDashGuid, marshalPromiseToCallbacks } from "../../common/Exports"; import { Contracts } from "../Contracts"; @@ -219,6 +221,10 @@ export class ConversationTranslator extends ConversationCommon implements IConve return true; } + public onToken(token: string): void { + this.privCTRecognizer.authorizationToken = token; + } + public setServiceProperty(name: string, value: string): void { const currentProperties: IStringDictionary = JSON.parse(this.privProperties.getProperty(ServicePropertiesPropertyName, "{}")) as IStringDictionary; From 08c755f55eb68faab36f80e0e695b23e2e443820 Mon Sep 17 00:00:00 2001 From: "glharper@microsoft.com" Date: Tue, 22 Aug 2023 10:41:42 -0700 Subject: [PATCH 4/8] remove unused vars --- src/sdk/Transcription/Conversation.ts | 1 - src/sdk/Transcription/ConversationTranslator.ts | 2 -- 2 files changed, 3 deletions(-) diff --git a/src/sdk/Transcription/Conversation.ts b/src/sdk/Transcription/Conversation.ts index 69594056..3c82900c 100644 --- a/src/sdk/Transcription/Conversation.ts +++ b/src/sdk/Transcription/Conversation.ts @@ -5,7 +5,6 @@ /* eslint-disable max-classes-per-file */ import { - CognitiveTokenAuthentication, ConversationConnectionConfig, ConversationManager, ConversationReceivedTranslationEventArgs, diff --git a/src/sdk/Transcription/ConversationTranslator.ts b/src/sdk/Transcription/ConversationTranslator.ts index ead806f1..8f3bae53 100644 --- a/src/sdk/Transcription/ConversationTranslator.ts +++ b/src/sdk/Transcription/ConversationTranslator.ts @@ -5,7 +5,6 @@ /* eslint-disable max-classes-per-file */ import { - CognitiveTokenAuthentication, ConversationConnectionConfig, ServicePropertiesPropertyName, } from "../../common.speech/Exports"; @@ -14,7 +13,6 @@ import { IDisposable, IErrorMessages, IStringDictionary, - createNoDashGuid, marshalPromiseToCallbacks } from "../../common/Exports"; import { Contracts } from "../Contracts"; From bf05de36309afe1c42aa2d30b0fec28bfe658a78 Mon Sep 17 00:00:00 2001 From: "glharper@microsoft.com" Date: Tue, 22 Aug 2023 18:30:50 -0700 Subject: [PATCH 5/8] cleaner implementation --- src/common.speech/ServiceRecognizerBase.ts | 4 ++-- .../ConversationTranslatorConnectionFactory.ts | 2 +- .../Transcription/ConversationTranslatorRecognizer.ts | 8 ++------ src/sdk/Transcription/Conversation.ts | 3 ++- src/sdk/Transcription/ConversationTranslator.ts | 10 ++++++++-- 5 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/common.speech/ServiceRecognizerBase.ts b/src/common.speech/ServiceRecognizerBase.ts index c283ebc8..8fabd1a3 100644 --- a/src/common.speech/ServiceRecognizerBase.ts +++ b/src/common.speech/ServiceRecognizerBase.ts @@ -863,8 +863,8 @@ export abstract class ServiceRecognizerBase implements IDisposable { // Get the auth information for the connection. This is a bit of overkill for the current API surface, but leaving the plumbing in place to be able to raise a developer-customer // facing event when a connection fails to let them try and provide new auth information. - const authPromise = isUnAuthorized ? this.privAuthentication.fetchOnExpiry(this.privAuthFetchEventId) : this.privAuthentication.fetch(this.privAuthFetchEventId); - const auth: AuthInfo = await authPromise; + const authPromise = isMultiAuth ? this.privAuthentication.fetchAll(createNoDashGuid()) : isUnAuthorized ? this.privAuthentication.fetchOnExpiry(this.privAuthFetchEventId) : this.privAuthentication.fetch(this.privAuthFetchEventId); + const auth: AuthInfo | AuthInfo[] = await authPromise; await this.privRequestSession.onAuthCompleted(false); diff --git a/src/common.speech/Transcription/ConversationTranslatorConnectionFactory.ts b/src/common.speech/Transcription/ConversationTranslatorConnectionFactory.ts index e7d7ba44..fe7b4868 100644 --- a/src/common.speech/Transcription/ConversationTranslatorConnectionFactory.ts +++ b/src/common.speech/Transcription/ConversationTranslatorConnectionFactory.ts @@ -121,7 +121,7 @@ export class ConversationTranslatorConnectionFactory extends ConnectionFactoryBa headers[HeaderNames.ConnectionId] = connectionId; headers[RestConfigBase.configParams.token] = convInfo.token; - if (authInfo.token) { + if (!!authInfo.token) { headers[authInfo.headerName] = authInfo.token; } diff --git a/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts b/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts index a5028286..b23f34e0 100644 --- a/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts +++ b/src/common.speech/Transcription/ConversationTranslatorRecognizer.ts @@ -3,8 +3,6 @@ // eslint-disable-next-line max-classes-per-file import { - AuthInfo, - CognitiveTokenAuthentication, IAuthentication, IConnectionFactory, RecognizerConfig, @@ -15,7 +13,6 @@ import { BackgroundEvent, Events, Timeout, - createNoDashGuid } from "../../common/Exports"; import { AudioConfigImpl } from "../../sdk/Audio/AudioConfig"; import { Contracts } from "../../sdk/Contracts"; @@ -213,9 +210,8 @@ export class ConversationTranslatorRecognizer extends Recognizer implements Conv /** * Handle update of service auth token (#694) */ - public async onToken(token: CognitiveTokenAuthentication): Promise { - const value: AuthInfo = await token.fetch(createNoDashGuid()); - this.privConversation.onToken(value.token); + public onToken(token: IAuthentication): void { + this.privConversation.onToken(token); } /** diff --git a/src/sdk/Transcription/Conversation.ts b/src/sdk/Transcription/Conversation.ts index 3c82900c..0da469a9 100644 --- a/src/sdk/Transcription/Conversation.ts +++ b/src/sdk/Transcription/Conversation.ts @@ -12,6 +12,7 @@ import { ConversationRecognizerFactory, ConversationTranslatorCommandTypes, ConversationTranslatorMessageTypes, + IAuthentication, IInternalConversation, IInternalParticipant, InternalParticipants, @@ -323,7 +324,7 @@ export class ConversationImpl extends Conversation implements IDisposable { this.privConversationTranslator = conversationTranslator; } - public onToken(token: string): void { + public onToken(token: IAuthentication): void { this.privConversationTranslator.onToken(token); } diff --git a/src/sdk/Transcription/ConversationTranslator.ts b/src/sdk/Transcription/ConversationTranslator.ts index 8f3bae53..63c8e13a 100644 --- a/src/sdk/Transcription/ConversationTranslator.ts +++ b/src/sdk/Transcription/ConversationTranslator.ts @@ -6,6 +6,7 @@ import { ConversationConnectionConfig, + IAuthentication, ServicePropertiesPropertyName, } from "../../common.speech/Exports"; import { ConversationTranslatorConnectionFactory } from "../../common.speech/Transcription/ConversationTranslatorConnectionFactory"; @@ -108,6 +109,11 @@ class ConversationTranslationRecognizer extends TranslationRecognizer { this.privSpeechState = newState; } + public set authentication(token: IAuthentication) { + this.privReco.authentication = token; + } + + public onConnection(): void { this.privSpeechState = SpeechState.Connected; } @@ -219,8 +225,8 @@ export class ConversationTranslator extends ConversationCommon implements IConve return true; } - public onToken(token: string): void { - this.privCTRecognizer.authorizationToken = token; + public onToken(token: IAuthentication): void { + this.privCTRecognizer.authentication = token; } public setServiceProperty(name: string, value: string): void { From e0dc28f25d40b17c0aac06a390f58ea8421d2332 Mon Sep 17 00:00:00 2001 From: "glharper@microsoft.com" Date: Tue, 22 Aug 2023 18:35:19 -0700 Subject: [PATCH 6/8] fix bad changes --- src/common.speech/ServiceRecognizerBase.ts | 4 ++-- src/common.speech/Transcription/ConversationServiceAdapter.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/common.speech/ServiceRecognizerBase.ts b/src/common.speech/ServiceRecognizerBase.ts index 8fabd1a3..c283ebc8 100644 --- a/src/common.speech/ServiceRecognizerBase.ts +++ b/src/common.speech/ServiceRecognizerBase.ts @@ -863,8 +863,8 @@ export abstract class ServiceRecognizerBase implements IDisposable { // Get the auth information for the connection. This is a bit of overkill for the current API surface, but leaving the plumbing in place to be able to raise a developer-customer // facing event when a connection fails to let them try and provide new auth information. - const authPromise = isMultiAuth ? this.privAuthentication.fetchAll(createNoDashGuid()) : isUnAuthorized ? this.privAuthentication.fetchOnExpiry(this.privAuthFetchEventId) : this.privAuthentication.fetch(this.privAuthFetchEventId); - const auth: AuthInfo | AuthInfo[] = await authPromise; + const authPromise = isUnAuthorized ? this.privAuthentication.fetchOnExpiry(this.privAuthFetchEventId) : this.privAuthentication.fetch(this.privAuthFetchEventId); + const auth: AuthInfo = await authPromise; await this.privRequestSession.onAuthCompleted(false); diff --git a/src/common.speech/Transcription/ConversationServiceAdapter.ts b/src/common.speech/Transcription/ConversationServiceAdapter.ts index 687b8ede..ca36fc35 100644 --- a/src/common.speech/Transcription/ConversationServiceAdapter.ts +++ b/src/common.speech/Transcription/ConversationServiceAdapter.ts @@ -412,7 +412,7 @@ export class ConversationServiceAdapter extends ServiceRecognizerBase { return Promise.resolve(authorizationToken); }); this.authentication = token; - await this.privConversationServiceConnector.onToken(token); + this.privConversationServiceConnector.onToken(token); break; From e62db91683ef1b7eaee9642247dbb3b239bc5dc9 Mon Sep 17 00:00:00 2001 From: "glharper@microsoft.com" Date: Wed, 23 Aug 2023 06:25:31 -0700 Subject: [PATCH 7/8] bug fix for auth assignment --- src/common.speech/ServiceRecognizerBase.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common.speech/ServiceRecognizerBase.ts b/src/common.speech/ServiceRecognizerBase.ts index c283ebc8..7de81b89 100644 --- a/src/common.speech/ServiceRecognizerBase.ts +++ b/src/common.speech/ServiceRecognizerBase.ts @@ -279,7 +279,7 @@ export abstract class ServiceRecognizerBase implements IDisposable { } public set authentication(auth: IAuthentication) { - this.privAuthentication = this.authentication; + this.privAuthentication = auth; } public isDisposed(): boolean { From f6e9259a675f2cfeb43b74a570733ac5527b437e Mon Sep 17 00:00:00 2001 From: "glharper@microsoft.com" Date: Wed, 23 Aug 2023 07:09:06 -0700 Subject: [PATCH 8/8] capitalize Bearer for auth token --- src/common.speech/CognitiveTokenAuthentication.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common.speech/CognitiveTokenAuthentication.ts b/src/common.speech/CognitiveTokenAuthentication.ts index ecde21f2..220bb98e 100644 --- a/src/common.speech/CognitiveTokenAuthentication.ts +++ b/src/common.speech/CognitiveTokenAuthentication.ts @@ -6,7 +6,7 @@ import { AuthInfo, IAuthentication } from "./IAuthentication"; import { HeaderNames } from "./HeaderNames"; export class CognitiveTokenAuthentication implements IAuthentication { - private static privTokenPrefix: string = "bearer "; + private static privTokenPrefix: string = "Bearer "; private privFetchCallback: (authFetchEventId: string) => Promise; private privFetchOnExpiryCallback: (authFetchEventId: string) => Promise;