diff --git a/packages/teams-js/src/private/externalAppAuthenticationForCEA.ts b/packages/teams-js/src/private/externalAppAuthenticationForCEA.ts index f1c5e72424..e88db74639 100644 --- a/packages/teams-js/src/private/externalAppAuthenticationForCEA.ts +++ b/packages/teams-js/src/private/externalAppAuthenticationForCEA.ts @@ -5,6 +5,7 @@ import { validateId } from '../internal/utils'; import { AppId } from '../public'; import { errorNotSupportedOnPlatform, FrameContexts } from '../public/constants'; import { runtime } from '../public/runtime'; +import { assertIsArray, assertIsBoolean, assertIsString } from '../typeAssertions'; import { externalAppAuthentication } from './externalAppAuthentication'; const externalAppAuthenticationTelemetryVersionNumber: ApiVersionNumber = ApiVersionNumber.V_2; @@ -39,8 +40,7 @@ export namespace externalAppAuthenticationForCEA { if (!isSupported()) { throw errorNotSupportedOnPlatform; } - - validateId(conversationId, new Error('conversation id is not valid.')); + validateInput(appId, conversationId, authTokenRequest); return callFunctionInHost( ApiName.ExternalAppAuthenticationForCEA_AuthenticateWithSSO, @@ -213,21 +213,18 @@ export namespace externalAppAuthenticationForCEA { return ensureInitialized(runtime) && runtime.supports.externalAppAuthenticationForCEA ? true : false; } - /** - * @hidden - * @internal - * Limited to Microsoft-internal use - * @beta - */ - function validateOriginalRequestInfo( - actionExecuteRequest: externalAppAuthentication.IActionExecuteInvokeRequest, + function validateInput( + appId: AppId, + conversationId: string, + authTokenRequest: externalAppAuthentication.AuthTokenRequestParameters, ): void { - if (actionExecuteRequest.type !== externalAppAuthentication.ActionExecuteInvokeRequestType) { - const error: externalAppAuthentication.InvokeError = { - errorCode: externalAppAuthentication.InvokeErrorCode.INTERNAL_ERROR, - message: `Invalid action type ${actionExecuteRequest.type}. Action type must be "${externalAppAuthentication.ActionExecuteInvokeRequestType}"`, - }; - throw error; + if (!(appId instanceof AppId)) { + throw new Error('appId must be an instance of AppId class'); } + assertIsString(conversationId); + validateId(conversationId, new Error('conversation id is not valid.')); + + authTokenRequest.claims && assertIsArray(authTokenRequest.claims, assertIsString); + authTokenRequest.silent && assertIsBoolean(authTokenRequest.silent); } } diff --git a/packages/teams-js/src/typeAssertions.ts b/packages/teams-js/src/typeAssertions.ts new file mode 100644 index 0000000000..bff491e579 --- /dev/null +++ b/packages/teams-js/src/typeAssertions.ts @@ -0,0 +1,33 @@ +export function assertIsString(value: unknown): asserts value is string { + if (typeof value !== 'string') { + throw new Error(`Expected a string but received ${typeof value}`); + } +} + +export function assertIsNumber(value: unknown): asserts value is number { + if (typeof value !== 'number') { + throw new Error(`Expected a number but received ${typeof value}`); + } +} + +export function assertIsBoolean(value: unknown): asserts value is boolean { + if (typeof value !== 'boolean') { + throw new Error(`Expected a boolean but received ${typeof value}`); + } +} + +export function assertIsArray(value: unknown, validateElement: (element: unknown) => void): asserts value is T[] { + if (!Array.isArray(value)) { + throw new Error(`Expected an array but received ${typeof value}`); + } + + for (const element of value) { + validateElement(element); + } +} + +export function assertIsObject(value: unknown): asserts value is object { + if (value === null || typeof value !== 'object') { + throw new Error(`Expected an object but received ${typeof value}`); + } +}