@microsoft/teams-ai
Version:
SDK focused on building AI based applications for Microsoft Teams.
117 lines • 5.71 kB
JavaScript
;
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
Object.defineProperty(exports, "__esModule", { value: true });
exports.FilteredTeamsSSOTokenExchangeMiddleware = exports.OAuthBotAuthentication = void 0;
const botbuilder_dialogs_1 = require("botbuilder-dialogs");
const botbuilder_1 = require("botbuilder");
const BotAuthenticationBase_1 = require("./BotAuthenticationBase");
const TurnStateProperty_1 = require("../TurnStateProperty");
const UserTokenAccess_1 = require("./UserTokenAccess");
/**
* @internal
*
* Handles authentication for Teams bots.
* @template TState - The type of the turn state object.
*/
class OAuthBotAuthentication extends BotAuthenticationBase_1.BotAuthenticationBase {
_oauthPrompt;
_oauthSettings;
/**
* Initializes a new instance of the OAuthBotAuthentication class.
* @param {Application} app - The application object.
* @param {OAuthSettings} oauthSettings - The settings for OAuthPrompt.
* @param {string} settingName - The name of the setting.
* @param {Storage} storage - The storage object for storing state.
*/
constructor(app, oauthSettings, // Child classes will have different types for this
settingName, storage) {
super(app, settingName, storage);
// Create OAuthPrompt
this._oauthPrompt = new botbuilder_dialogs_1.OAuthPrompt('OAuthPrompt', oauthSettings);
this._oauthSettings = oauthSettings;
// Handles deduplication of token exchange event when using SSO with Bot Authentication
app.adapter.use(new FilteredTeamsSSOTokenExchangeMiddleware(this._storage, oauthSettings.connectionName));
}
/**
* Run or continue the OAuthPrompt dialog and returns the result.
* @param {TurnContext} context - The turn context object.
* @param {TState} state - The turn state object.
* @param {string} dialogStateProperty - The name of the dialog state property.
* @returns {Promise<DialogTurnResult<TokenResponse>>} A promise that resolves to the dialog turn result containing the token response.
*/
async runDialog(context, state, dialogStateProperty) {
const dialogContext = await this.createDialogContext(context, state, dialogStateProperty);
let results = await dialogContext.continueDialog();
if (results.status === botbuilder_dialogs_1.DialogTurnStatus.empty) {
const card = await this.createOAuthCard(context);
results = await dialogContext.beginDialog(this._oauthPrompt.id, {
prompt: {
attachments: [card]
}
});
}
return results;
}
/**
* Continue the OAuthPrompt dialog and returns the result.
* @param {TurnContext} context - The turn context object.
* @param {TState} state - The turn state object.
* @param {string} dialogStateProperty - The name of the dialog state property.
* @returns {Promise<DialogTurnResult<TokenResponse>>} A promise that resolves to the dialog turn result containing the token response.
*/
async continueDialog(context, state, dialogStateProperty) {
const dialogContext = await this.createDialogContext(context, state, dialogStateProperty);
return await dialogContext.continueDialog();
}
/**
* Creates a new DialogContext for OAuthPrompt.
* @param {TurnContext} context - The turn context object.
* @param {TState} state - The turn state object.
* @param {string} dialogStateProperty - The name of the dialog state property.
* @returns {Promise<DialogContext>} A promise that resolves to the dialog context.
*/
async createDialogContext(context, state, dialogStateProperty) {
const accessor = new TurnStateProperty_1.TurnStateProperty(state, 'conversation', dialogStateProperty);
const dialogSet = new botbuilder_dialogs_1.DialogSet(accessor);
dialogSet.add(this._oauthPrompt);
return await dialogSet.createContext(context);
}
/**
* Creates an OAuthCard that will be sent to the user.
* @param {TurnContext} context - The turn context.
* @returns {Promise<Attachment>} The OAuthCard.
*/
async createOAuthCard(context) {
const signInResource = await (0, UserTokenAccess_1.getSignInResource)(context, this._oauthSettings);
const link = signInResource.signInLink;
let tokenExchangeResource;
if (this._oauthSettings.enableSso == true) {
tokenExchangeResource = signInResource.tokenExchangeResource;
}
return botbuilder_1.CardFactory.oauthCard(this._oauthSettings.connectionName, this._oauthSettings.title, this._oauthSettings.text, link, tokenExchangeResource, signInResource.tokenPostResource);
}
}
exports.OAuthBotAuthentication = OAuthBotAuthentication;
/**
* @internal
* SSO Token Exchange Middleware for Teams that filters based on the connection name.
*/
class FilteredTeamsSSOTokenExchangeMiddleware extends botbuilder_1.TeamsSSOTokenExchangeMiddleware {
_oauthConnectionName;
constructor(storage, oauthConnectionName) {
super(storage, oauthConnectionName);
this._oauthConnectionName = oauthConnectionName;
}
async onTurn(context, next) {
// If connection name matches then continue to the Teams SSO Token Exchange Middleware.
if (context.activity.value?.connectionName == this._oauthConnectionName) {
await super.onTurn(context, next);
}
else {
await next();
}
}
}
exports.FilteredTeamsSSOTokenExchangeMiddleware = FilteredTeamsSSOTokenExchangeMiddleware;
//# sourceMappingURL=OAuthBotAuthentication.js.map