Skip to content

Commit

Permalink
refactor: simplify bot sso (#943)
Browse files Browse the repository at this point in the history
* refactor: simply bot sso

* refactor: update

* refactor: updat

* refactor: update
  • Loading branch information
yukun-dong committed Jul 19, 2023
1 parent 7de40a9 commit d70dfc5
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 267 deletions.
45 changes: 0 additions & 45 deletions bot-sso/adaptiveCards/learn.json

This file was deleted.

30 changes: 0 additions & 30 deletions bot-sso/adaptiveCards/welcome.json

This file was deleted.

8 changes: 0 additions & 8 deletions bot-sso/appPackage/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,6 @@
"groupchat"
],
"commands": [
{
"title": "welcome",
"description": "Resend welcome card of this Bot"
},
{
"title": "learn",
"description": "Learn about Adaptive Card and Bot Command"
},
{
"title": "show",
"description": "Show user profile using Single Sign On feature"
Expand Down
8 changes: 8 additions & 0 deletions bot-sso/commands/SSOCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { TurnContext } from "botbuilder";

export interface SSOCommand {
commandMessage: string;
operationWithSSOToken(
context: TurnContext, ssoToken: string
): Promise<any> | undefined;
}
11 changes: 11 additions & 0 deletions bot-sso/commands/SSOCommandMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { SSOCommand } from "./SSOCommand";
import { ShowUserProfile } from "./showUserProfile";


export const SSOCommands: SSOCommand[] = [
new ShowUserProfile(),
];

export const SSOCommandMap: Map<string, SSOCommand> = new Map(
SSOCommands.map((command) => [command.commandMessage, command])
);
18 changes: 0 additions & 18 deletions bot-sso/commands/index.ts

This file was deleted.

23 changes: 0 additions & 23 deletions bot-sso/commands/learn.ts

This file was deleted.

16 changes: 6 additions & 10 deletions bot-sso/commands/showUserProfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@ import {
createMicrosoftGraphClientWithCredential,
OnBehalfOfUserCredential,
} from "@microsoft/teamsfx";
import { SSOCommand } from "../helpers/botCommand";
import { SSOCommand } from "./SSOCommand";
import oboAuthConfig from "../authConfig";

export class ShowUserProfile extends SSOCommand {
constructor() {
super();
this.commandMessage = 'show';
this.operationWithSSOToken = this.showUserInfo;
}
export class ShowUserProfile implements SSOCommand {
commandMessage = 'show';

async showUserInfo(context: TurnContext, ssoToken: string) {
async operationWithSSOToken(context: TurnContext, ssoToken: string) {
await context.sendActivity("Retrieving user information from Microsoft Graph ...");

// Call Microsoft Graph half of user
Expand All @@ -25,8 +21,7 @@ export class ShowUserProfile extends SSOCommand {
const me = await graphClient.api("/me").get();
if (me) {
await context.sendActivity(
`You're logged in as ${me.displayName} (${me.userPrincipalName})${
me.jobTitle ? `; your job title is: ${me.jobTitle}` : ""
`You're logged in as ${me.displayName} (${me.userPrincipalName})${me.jobTitle ? `; your job title is: ${me.jobTitle}` : ""
}.`
);

Expand Down Expand Up @@ -54,4 +49,5 @@ export class ShowUserProfile extends SSOCommand {
);
}
}

}
15 changes: 0 additions & 15 deletions bot-sso/commands/welcome.ts

This file was deleted.

40 changes: 0 additions & 40 deletions bot-sso/helpers/botCommand.ts

This file was deleted.

12 changes: 0 additions & 12 deletions bot-sso/helpers/commandHelper.ts

This file was deleted.

12 changes: 0 additions & 12 deletions bot-sso/helpers/utils.ts

This file was deleted.

2 changes: 0 additions & 2 deletions bot-sso/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
"@azure/msal-node": "^1.1.0",
"@microsoft/microsoft-graph-client": "^3.0.1",
"@microsoft/teamsfx": "^2.0.0",
"adaptive-expressions": "^4.15.0",
"adaptivecards-templating": "2.1.0",
"botbuilder": "^4.18.0",
"botbuilder-dialogs": "^4.18.0",
"isomorphic-fetch": "^3.0.0",
Expand Down
17 changes: 9 additions & 8 deletions bot-sso/helpers/ssoDialog.ts → bot-sso/ssoDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import {
import {
Activity,
ActivityTypes,
StatePropertyAccessor,
Storage,
tokenExchangeOperationName,
TurnContext,
} from "botbuilder";
import { TeamsBotSsoPrompt } from "@microsoft/teamsfx";
import "isomorphic-fetch";
import oboAuthConfig from "../authConfig";
import config from "../config";
import { SSOCommandMap } from "../commands";
import { TeamsBotSsoPrompt } from "@microsoft/teamsfx";
import oboAuthConfig from "./authConfig";
import config from "./config";
import { SSOCommandMap } from "./commands/SSOCommandMap";

const DIALOG_NAME = "SSODialog";
const MAIN_WATERFALL_DIALOG = "MainWaterfallDialog";
Expand Down Expand Up @@ -63,7 +64,7 @@ export class SSODialog extends ComponentDialog {
* If no dialog is active, it will start the default dialog.
* @param {*} dialogContext
*/
async run(context: TurnContext, dialogState: any) {
async run(context: TurnContext, dialogState: StatePropertyAccessor) {
const dialogSet = new DialogSet(dialogState);
dialogSet.add(this);

Expand Down Expand Up @@ -95,11 +96,11 @@ export class SSODialog extends ComponentDialog {
throw new Error("There is an issue while trying to sign you in and run your command. Please try again.");
}
// Once got ssoToken, run operation that depends on ssoToken
const operationWithSSO = SSOCommandMap.get(stepContext.options.commandMessage);
if (!operationWithSSO) {
const SSOCommand = SSOCommandMap.get(stepContext.options.commandMessage);
if (!SSOCommand) {
throw new Error("Can not get sso operation. Please try again.");
}
await operationWithSSO(stepContext.context, tokenResponse.ssoToken);
await SSOCommand.operationWithSSOToken(stepContext.context, tokenResponse.ssoToken);
return await stepContext.endDialog();
}

Expand Down
Loading

0 comments on commit d70dfc5

Please sign in to comment.