Skip to content

Added typeAssertion for externalAppAuthenticationForCEA inputs in teamsjs #2573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions packages/teams-js/src/private/externalAppAuthenticationForCEA.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check can go to typeAssertion.ts file as well

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<string>(authTokenRequest.claims, assertIsString);
authTokenRequest.silent && assertIsBoolean(authTokenRequest.silent);
}
}
33 changes: 33 additions & 0 deletions packages/teams-js/src/typeAssertions.ts
Original file line number Diff line number Diff line change
@@ -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<T>(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}`);
}
}
Loading