UNPKG

@microsoft/teams-ai

Version:

SDK focused on building AI based applications for Microsoft Teams.

98 lines 4.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.MessageExtensionAuthenticationBase = void 0; const botbuilder_1 = require("botbuilder"); const MessageExtensions_1 = require("../MessageExtensions"); /** * @internal * Base class to handle authentication for Teams Message Extension. */ class MessageExtensionAuthenticationBase { title; text; constructor(title, text) { this.title = title ?? 'Bot Service OAuth'; this.text = text ?? "You'll need to signin to use this app."; } /** * Authenticates the user. * @param {TurnContext} context - The turn context. * @returns {Promise<string | undefined>} - The authentication token, or undefined if authentication failed. Teams will ask user to sign-in if authentication failed. */ async authenticate(context) { const value = context.activity.value; const tokenExchangeRequest = value.authentication; // Token Exchange, this happens when a silentAuth action is sent to Teams if (tokenExchangeRequest && tokenExchangeRequest.token) { // Message extension token exchange invoke activity try { const tokenExchangeResponse = await this.handleSsoTokenExchange(context); if (tokenExchangeResponse && tokenExchangeResponse.token) { return tokenExchangeResponse.token; } } catch (err) { // Ignore Exceptions // If token exchange failed for any reason, tokenExchangeResponse above stays null, and hence we send back a failure invoke response to the caller. console.log('tokenExchange error: ' + err); } // Token exchange failed, asks user to sign in and consent. await context.sendActivity({ value: { status: 412 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); return undefined; } // When the Bot Service Auth flow completes, the query.State will contain a magic code used for verification. const magicCode = value.state && Number.isInteger(Number(value.state)) ? value.state : ''; if (magicCode) { // User sign in completes, the query.State will contain a magic code used for verification. // This happens when an "auth" action is sent to Teams, or the token exchange failed in above step. const tokenResponse = await this.handleUserSignIn(context, magicCode); if (tokenResponse && tokenResponse.token) { return tokenResponse.token; } } // No auth/silentAuth action sent to Teams yet // Retrieve the OAuth Sign in Link to use in the MessageExtensionResult Suggested Actions const signInLink = await this.getSignInLink(context); // Do 'silentAuth' if this is a composeExtension/query request otherwise do normal `auth` flow. const authType = this.isSsoSignIn(context) ? 'silentAuth' : 'auth'; const response = { composeExtension: { type: authType, suggestedActions: { actions: [ { type: 'openUrl', value: signInLink, title: this.title, text: this.text, displayText: this.text } ] } } }; // Queue up invoke response await context.sendActivity({ value: { body: response, status: 200 }, type: botbuilder_1.ActivityTypes.InvokeResponse }); return; } /** * Checks if the activity is a valid Message Extension activity that supports authentication. * @param {TurnContext} context - The turn context. * @returns {boolean} - A boolean indicating if the activity is valid. */ isValidActivity(context) { return (context.activity.type == botbuilder_1.ActivityTypes.Invoke && (context.activity.name == MessageExtensions_1.MessageExtensionsInvokeNames.QUERY_INVOKE || context.activity.name == MessageExtensions_1.MessageExtensionsInvokeNames.FETCH_TASK_INVOKE || context.activity.name == MessageExtensions_1.MessageExtensionsInvokeNames.QUERY_LINK_INVOKE || context.activity.name == MessageExtensions_1.MessageExtensionsInvokeNames.ANONYMOUS_QUERY_LINK_INVOKE)); } } exports.MessageExtensionAuthenticationBase = MessageExtensionAuthenticationBase; //# sourceMappingURL=MessageExtensionAuthenticationBase.js.map