From 6a08b1c02263a344533521cc89da4d20f7009ae7 Mon Sep 17 00:00:00 2001 From: Lakhveer Kaur Date: Mon, 30 Sep 2024 11:57:41 -0700 Subject: [PATCH] changing incoming response from hubsdk --- .../ExternalAppAuthenticationForCEAAPIs.tsx | 9 ++-- .../externalAppAuthenticationForCEA.ts | 44 +++++++++++++------ packages/teams-js/src/private/interfaces.ts | 19 ++++++++ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/apps/teams-test-app/src/components/privateApis/ExternalAppAuthenticationForCEAAPIs.tsx b/apps/teams-test-app/src/components/privateApis/ExternalAppAuthenticationForCEAAPIs.tsx index 015cf9950b..6140091509 100644 --- a/apps/teams-test-app/src/components/privateApis/ExternalAppAuthenticationForCEAAPIs.tsx +++ b/apps/teams-test-app/src/components/privateApis/ExternalAppAuthenticationForCEAAPIs.tsx @@ -1,4 +1,5 @@ import { + AppId, AuthTokenRequestParameters, externalAppAuthenticationForCEA, IActionExecuteInvokeRequest, @@ -40,7 +41,7 @@ const AuthenticateWithOAuth = (): React.ReactElement => } }, submit: async (input) => { - await externalAppAuthenticationForCEA.authenticateWithOAuth(input.appId, input.conversationId, { + await externalAppAuthenticationForCEA.authenticateWithOAuth(new AppId(input.appId), input.conversationId, { ...input.authenticateParameters, url: new URL(input.authenticateParameters.url), }); @@ -78,7 +79,7 @@ const AuthenticateWithSSO = (): React.ReactElement => }, submit: async (input) => { await externalAppAuthenticationForCEA.authenticateWithSSO( - input.appId, + new AppId(input.appId), input.conversationId, input.authTokenRequest, ); @@ -124,7 +125,7 @@ const AuthenticateAndResendRequest = (): React.ReactElement => }, submit: async (input) => { const result = await externalAppAuthenticationForCEA.authenticateAndResendRequest( - input.appId, + new AppId(input.appId), input.conversationId, { ...input.authenticateParameters, url: new URL(input.authenticateParameters.url) }, input.originalRequestInfo, @@ -173,7 +174,7 @@ const AuthenticateWithSSOAndResendRequest = (): React.ReactElement => }, submit: async (input) => { const result = await externalAppAuthenticationForCEA.authenticateWithSSOAndResendRequest( - input.appId, + new AppId(input.appId), input.conversationId, input.authTokenRequest, input.originalRequestInfo, diff --git a/packages/teams-js/src/private/externalAppAuthenticationForCEA.ts b/packages/teams-js/src/private/externalAppAuthenticationForCEA.ts index d519caa83c..93247d93ef 100644 --- a/packages/teams-js/src/private/externalAppAuthenticationForCEA.ts +++ b/packages/teams-js/src/private/externalAppAuthenticationForCEA.ts @@ -9,11 +9,14 @@ import { ActionExecuteInvokeRequestType, AuthenticatePopUpParameters, AuthTokenRequestParameters, + defaultExternalAppError, IActionExecuteInvokeRequest, IActionExecuteResponse, InvokeError, InvokeErrorCode, InvokeErrorWrapper, + InvokeResponseType, + isInvokeErrorWrapper, } from './interfaces'; const externalAppAuthenticationTelemetryVersionNumber: ApiVersionNumber = ApiVersionNumber.V_2; @@ -57,7 +60,7 @@ export namespace externalAppAuthenticationForCEA { ApiName.ExternalAppAuthenticationForCEA_AuthenticateWithSSO, ), 'externalAppAuthenticationForCEA.authenticateWithSSO', - [appId, conversationId, authTokenRequest.claims, authTokenRequest.silent], + [appId.toString(), conversationId, authTokenRequest.claims, authTokenRequest.silent], ); if (error) { throw error; @@ -97,7 +100,7 @@ export namespace externalAppAuthenticationForCEA { ), 'externalAppAuthenticationForCEA.authenticateWithOauth', [ - appId, + appId.toString(), conversationId, authenticateParameters.url.href, authenticateParameters.width, @@ -140,14 +143,14 @@ export namespace externalAppAuthenticationForCEA { validateOriginalRequestInfo(originalRequestInfo); // Ask the parent window to open an authentication window with the parameters provided by the caller. - const [error, response] = await sendMessageToParentAsync<[InvokeErrorWrapper, IActionExecuteResponse]>( + const [response] = await sendMessageToParentAsync<[InvokeErrorWrapper | IActionExecuteResponse]>( getApiVersionTag( externalAppAuthenticationTelemetryVersionNumber, ApiName.ExternalAppAuthentication_AuthenticateAndResendRequest, ), 'externalAppAuthenticationForCEA.authenticateAndResendRequest', [ - appId, + appId.toString(), conversationId, originalRequestInfo, authenticateParameters.url.href, @@ -156,13 +159,26 @@ export namespace externalAppAuthenticationForCEA { authenticateParameters.isExternal, ], ); - if (response && response.responseType != null) { - return response as IActionExecuteResponse; + if (isActionExecuteResponse(response)) { + return response; + } else if (isInvokeErrorWrapper(response)) { + throw response; } else { - throw error; + throw defaultExternalAppError; } } + function isActionExecuteResponse(response: unknown): response is IActionExecuteResponse { + const actionResponse = response as IActionExecuteResponse; + + return ( + actionResponse.responseType === InvokeResponseType.ActionExecuteInvokeResponse && + actionResponse.value !== undefined && + actionResponse.statusCode !== undefined && + actionResponse.type !== undefined + ); + } + /** * @beta * @hidden @@ -192,20 +208,20 @@ export namespace externalAppAuthenticationForCEA { validateOriginalRequestInfo(originalRequestInfo); - const [error, response] = await sendMessageToParentAsync< - [InvokeErrorWrapper, IActionExecuteResponse | InvokeErrorWrapper] - >( + const [response] = await sendMessageToParentAsync<[IActionExecuteResponse | InvokeErrorWrapper]>( getApiVersionTag( externalAppAuthenticationTelemetryVersionNumber, ApiName.ExternalAppAuthentication_AuthenticateWithSSOAndResendRequest, ), 'externalAppAuthenticationForCEA.authenticateWithSSOAndResendRequest', - [appId, conversationId, originalRequestInfo, authTokenRequest.claims, authTokenRequest.silent], + [appId.toString(), conversationId, originalRequestInfo, authTokenRequest.claims, authTokenRequest.silent], ); - if (response && response.responseType != null) { - return response as IActionExecuteResponse; + if (isActionExecuteResponse(response)) { + return response; + } else if (isInvokeErrorWrapper(response)) { + throw response; } else { - throw error; + throw defaultExternalAppError; } } diff --git a/packages/teams-js/src/private/interfaces.ts b/packages/teams-js/src/private/interfaces.ts index f70e5e5bb4..8d991707d8 100644 --- a/packages/teams-js/src/private/interfaces.ts +++ b/packages/teams-js/src/private/interfaces.ts @@ -614,3 +614,22 @@ export enum InvokeErrorCode { */ export type InvokeErrorWrapper = InvokeError & { responseType: undefined }; export const ActionExecuteInvokeRequestType = 'Action.Execute'; + +export function isInvokeErrorWrapper(err: unknown): err is InvokeErrorWrapper { + if (typeof err !== 'object' || err === null) { + return false; + } + + const errorWrapper = err as InvokeErrorWrapper; + + return ( + errorWrapper?.errorCode === InvokeErrorCode.INTERNAL_ERROR && + (errorWrapper.message === undefined || typeof errorWrapper.message === 'string') && + errorWrapper.responseType === undefined + ); +} + +export const defaultExternalAppError = { + errorCode: ExternalAppErrorCode.INTERNAL_ERROR, + message: 'An internal error occurred', +};