@microsoft/teams-ai
Version:
SDK focused on building AI based applications for Microsoft Teams.
79 lines • 3.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.AdaptiveCardAuthenticationBase = void 0;
const botbuilder_1 = require("botbuilder");
const AdaptiveCards_1 = require("../AdaptiveCards");
/**
* @internal
*
* Base class to handle adaptive card authentication.
*/
class AdaptiveCardAuthenticationBase {
/**
* 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
if (tokenExchangeRequest && tokenExchangeRequest.token) {
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: {
body: {
statusCode: 412,
type: 'application/vnd.microsoft.error.preconditionFailed',
value: {
code: '412',
message: 'Failed to exchange token'
}
},
status: 200
},
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;
}
}
// There is no token, so the user has not signed in yet.
const response = await this.getLoginRequest(context);
// 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 Adaptive Card 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 == AdaptiveCards_1.ACTION_INVOKE_NAME;
}
}
exports.AdaptiveCardAuthenticationBase = AdaptiveCardAuthenticationBase;
//# sourceMappingURL=AdaptiveCardAuthenticationBase.js.map