Skip to content
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

CEC Auth changes #2483

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { AuthTokenRequestParameters, externalAppAuthenticationForCEC } from '@microsoft/teams-js';
import React from 'react';

import { ApiWithoutInput } from '../utils/ApiWithoutInput';
import { ApiWithTextInput } from '../utils/ApiWithTextInput';
import { ModuleWrapper } from '../utils/ModuleWrapper';

const CheckExternalAppAuthenticationCapability = (): React.ReactElement =>
ApiWithoutInput({
name: 'checkExternalAppAuthenticationCapability',
title: 'Check External App Authentication Capability',
onClick: async () =>
`External App Authentication module ${externalAppAuthenticationForCEC.isSupported() ? 'is' : 'is not'} supported`,
});

const AuthenticateWithOAuth = (): React.ReactElement =>
ApiWithTextInput<{
appId: string;
authenticateParameters: {
url: string;
width?: number;
height?: number;
isExternal?: boolean;
};
}>({
name: 'AuthenticateWithOAuth',
title: 'Authenticate With OAuth',
onClick: {
validateInput: (input) => {
if (!input.appId) {
throw new Error('appId is required');
}
if (!input.authenticateParameters) {
throw new Error('authenticateParameters is required');
}
},
submit: async (input) => {
const oAuthcallback = () => {
console.log('callback received');
};
const result = await externalAppAuthenticationForCEC.authenticateWithOAuth(
input.appId,
{ ...input.authenticateParameters, url: new URL(input.authenticateParameters.url) },
oAuthcallback,
);
return JSON.stringify(result);
},
},
defaultInput: JSON.stringify({
appId: 'b7f8c0a0-6c1d-4a9a-9c0a-2c3f1c0a3b0a',
authenticateParameters: {
url: 'https://www.example.com',
width: 100,
height: 100,
isExternal: true,
},
}),
});

const AuthenticateWithSSO = (): React.ReactElement =>
ApiWithTextInput<{
appId: string;
authTokenRequest: AuthTokenRequestParameters;
}>({
name: 'authenticateWithSSO',
title: 'Authenticate With SSO',
onClick: {
validateInput: (input) => {
if (!input.appId) {
throw new Error('appId is required');
}
if (!input.authTokenRequest) {
throw new Error('authTokenRequest is required');
}
},
submit: async (input, setResult) => {
const ssoCallback = () => {
console.log('callback received');
setResult('callback received');
};
await externalAppAuthenticationForCEC.authenticateWithSSO(input.appId, input.authTokenRequest, ssoCallback);
return 'Completed';
},
},
defaultInput: JSON.stringify({
appId: 'b7f8c0a0-6c1d-4a9a-9c0a-2c3f1c0a3b0a',
authTokenRequest: {
claims: ['https://graph.microsoft.com'],
silent: true,
},
}),
});

const ExternalAppAuthenticationForCECAPIs = (): React.ReactElement => (
<ModuleWrapper title="External App Authentication for CEC">
<CheckExternalAppAuthenticationCapability />
<AuthenticateWithOAuth />
<AuthenticateWithSSO />
</ModuleWrapper>
);

export default ExternalAppAuthenticationForCECAPIs;
2 changes: 2 additions & 0 deletions apps/teams-test-app/src/pages/TestApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import PeopleAPIs from '../components/PeopleAPIs';
import ChatAPIs from '../components/privateApis/ChatAPIs';
import CopilotAPIs from '../components/privateApis/CopilotAPIs';
import ExternalAppAuthenticationAPIs from '../components/privateApis/ExternalAppAuthenticationAPIs';
import ExternalAppAuthenticationForCECAPIs from '../components/privateApis/ExternalAppAuthenticationCECAPIs';
import ExternalAppCardActionsAPIs from '../components/privateApis/ExternalAppCardActionsAPIs';
import ExternalAppCommandsAPIs from '../components/privateApis/ExternalAppCommandsAPIs';
import FilesAPIs from '../components/privateApis/FilesAPIs';
Expand Down Expand Up @@ -93,6 +94,7 @@ export const TestApp: React.FC = () => {
<DialogUrlBotAPIs />
<DialogUrlParentCommunicationAPIs childWindowRef={dialogWindowRef} />
<ExternalAppAuthenticationAPIs />
<ExternalAppAuthenticationForCECAPIs />
<ExternalAppCardActionsAPIs />
<ExternalAppCommandsAPIs />
<FilesAPIs />
Expand Down
4 changes: 4 additions & 0 deletions packages/teams-js/src/internal/telemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ export const enum ApiName {
ExternalAppAuthentication_AuthenticateWithSSOAndResendRequest = 'externalAppAuthentication.authenticateWithSSOAndResendRequest',
ExternalAppAuthentication_AuthenticateWithOauth2 = 'externalAppAuthentication.authenticateWithOauth2',
ExternalAppAuthentication_AuthenticateWithPowerPlatformConnectorPlugins = 'externalAppAuthentication.authenticateWithPowerPlatformConnectorPlugins',
ExternalAppAuthenticationForCEC_AuthenticateWithOAuth = 'externalAppAuthentication.cec.authenticateWithOAuth',
MengyiGong marked this conversation as resolved.
Show resolved Hide resolved
ExternalAppAuthenticationForCEC_AuthenticateWithSSO = 'externalAppAuthentication.cec.AuthenticateWithSSO',
ExternalAppAuthenticationForCEC_SSOAuthCompleted = 'externalAppAuthentication.cec.SSOAuthCompleted',
ExternalAppAuthenticationForCEC_OAuthCompleted = 'externalAppAuthentication.cec.OAuthCompleted',
ExternalAppCardActions_ProcessActionOpenUrl = 'externalAppCardActions.processActionOpenUrl',
ExternalAppCardActions_ProcessActionSubmit = 'externalAppCardActions.processActionSubmit',
ExternalAppCommands_ProcessActionCommands = 'externalAppCommands.processActionCommand',
Expand Down
129 changes: 129 additions & 0 deletions packages/teams-js/src/private/externalAppAuthenticationForCEC.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { sendMessageToParentAsync } from '../internal/communication';
import { registerHandler, removeHandler } from '../internal/handlers';
import { ensureInitialized } from '../internal/internalAPIs';
import { ApiName, ApiVersionNumber, getApiVersionTag } from '../internal/telemetry';
import { validateId } from '../internal/utils';
import { errorNotSupportedOnPlatform, FrameContexts } from '../public/constants';
import { runtime } from '../public/runtime';
import { AuthenticatePopUpParameters, AuthTokenRequestParameters, InvokeError } from './interfaces';

const externalAppAuthenticationTelemetryVersionNumber: ApiVersionNumber = ApiVersionNumber.V_3;

// export namespace externalAppAuthenticationForCEC {
// export function authenticateWithSSO(
// appId: string,
// authTokenRequest: AuthTokenRequestParameters,
// SSOAuthCompletedCallback: () => void,
// ): void {
// ensureInitialized(runtime, FrameContexts.content);

// if (!isSupported()) {
// throw errorNotSupportedOnPlatform;
// }
// validateId(appId, new Error('App id is not valid.'));
// registerHandler(
// getApiVersionTag(ApiVersionNumber.V_3, ApiName.ExternalAppAuthenticationForCEC_SSOAuthCompleted),
// 'ssoAuthCompleted',
// SSOAuthCompletedCallback,
// );
// return sendMessageToParent(
// getApiVersionTag(
// externalAppAuthenticationTelemetryVersionNumber,
// ApiName.ExternalAppAuthenticationForCEC_AuthenticateWithSSO,
// ),
// 'externalAppAuthenticationForCEC.authenticateWithSSO',
// [appId, authTokenRequest.claims, authTokenRequest.silent],
// (wasSuccessful, error) => {
// if (!wasSuccessful) {
// console.log('not successfull ' + error);
// } else {
// console.log('successfull ' + error);
// }
// },
// );
// // .then(([wasSuccessful, error]: [boolean, InvokeError]) => {
// // if (!wasSuccessful) {
// // throw error;
// // }
// // })
// // .finally(() => {
// // // removeHandler('ssoAuthCompleted');
// // });
// }

export namespace externalAppAuthenticationForCEC {
export function authenticateWithSSO(
appId: string,
authTokenRequest: AuthTokenRequestParameters,
SSOAuthCompletedCallback: () => void,
): Promise<void> {
ensureInitialized(runtime, FrameContexts.content);

if (!isSupported()) {
throw errorNotSupportedOnPlatform;
}
validateId(appId, new Error('App id is not valid.'));
registerHandler(
getApiVersionTag(ApiVersionNumber.V_3, ApiName.ExternalAppAuthenticationForCEC_SSOAuthCompleted),
'ssoAuthCompleted',
SSOAuthCompletedCallback,
);
return sendMessageToParentAsync(
getApiVersionTag(
externalAppAuthenticationTelemetryVersionNumber,
ApiName.ExternalAppAuthenticationForCEC_AuthenticateWithSSO,
),
'externalAppAuthenticationForCEC.authenticateWithSSO',
[appId, authTokenRequest.claims, authTokenRequest.silent],
).then(([wasSuccessful, error]: [boolean, InvokeError]) => {
removeHandler('ssoAuthCompleted');
if (!wasSuccessful) {
throw error;
}
});
}

export function authenticateWithOAuth(
appId: string,
authenticateParameters: AuthenticatePopUpParameters,
// callback that will be called when hubsdk is done with Authentication
OAuthCompletedCallback: () => void,
): Promise<void> {
ensureInitialized(runtime, FrameContexts.content);

if (!isSupported()) {
throw errorNotSupportedOnPlatform;
}
validateId(appId, new Error('App id is not valid.'));
registerHandler(
getApiVersionTag(ApiVersionNumber.V_3, ApiName.ExternalAppAuthenticationForCEC_SSOAuthCompleted),
'oAuthCompleted',
OAuthCompletedCallback,
);

// Ask the parent window to open an authentication window with the parameters provided by the caller.
return sendMessageToParentAsync(
getApiVersionTag(
externalAppAuthenticationTelemetryVersionNumber,
ApiName.ExternalAppAuthenticationForCEC_AuthenticateWithOAuth,
),
'externalAppAuthenticationForCEC.authenticateWithOAuth',
[
appId,
authenticateParameters.url.href,
authenticateParameters.width,
authenticateParameters.height,
authenticateParameters.isExternal,
],
).then(([wasSuccessful, error]: [boolean, InvokeError]) => {
removeHandler('oAuthCompleted');
if (!wasSuccessful) {
throw error;
}
});
}

export function isSupported(): boolean {
return ensureInitialized(runtime) && runtime.supports.externalAppAuthenticationForCEC ? true : false;
}
}
1 change: 1 addition & 0 deletions packages/teams-js/src/private/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
export { conversations } from './conversations';
export { copilot } from './copilot';
export { externalAppAuthentication } from './externalAppAuthentication';
export { externalAppAuthenticationForCEC } from './externalAppAuthenticationForCEC';
export { externalAppCardActions } from './externalAppCardActions';
export { externalAppCommands } from './externalAppCommands';
export { files } from './files';
Expand Down
Loading
Loading