botbuilder-dialogs
Version:
A dialog stack based conversation manager for Microsoft BotBuilder.
123 lines • 6.21 kB
JavaScript
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createConnectorClient = exports.exchangeToken = exports.signOutUser = exports.getSignInResource = exports.getUserToken = void 0;
const z = require("zod");
const ExtendedUserTokenProviderT = z.custom(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(val) => typeof val.exchangeToken === 'function' &&
typeof val.getSignInResource === 'function' &&
typeof val.getUserToken === 'function' &&
typeof val.signOutUser === 'function', {
message: 'ExtendedUserTokenProvider',
});
const ConnectorClientBuilderT = z.custom(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(val) => typeof val.createConnectorClientWithIdentity === 'function', {
message: 'ConnectorClientBuilder',
});
/**
* @internal
*/
function getUserToken(context, settings, magicCode) {
var _a, _b, _c;
return __awaiter(this, void 0, void 0, function* () {
const userTokenClient = context.turnState.get(context.adapter.UserTokenClientKey);
if (userTokenClient) {
return userTokenClient.getUserToken((_b = (_a = context.activity) === null || _a === void 0 ? void 0 : _a.from) === null || _b === void 0 ? void 0 : _b.id, settings.connectionName, (_c = context.activity) === null || _c === void 0 ? void 0 : _c.channelId, magicCode);
}
else if (ExtendedUserTokenProviderT.check(context.adapter)) {
return context.adapter.getUserToken(context, settings.connectionName, magicCode, settings.oAuthAppCredentials);
}
else {
throw new Error('OAuth prompt is not supported by the current adapter');
}
});
}
exports.getUserToken = getUserToken;
/**
* @internal
*/
function getSignInResource(context, settings) {
var _a, _b;
return __awaiter(this, void 0, void 0, function* () {
const userTokenClient = context.turnState.get(context.adapter.UserTokenClientKey);
if (userTokenClient) {
return userTokenClient.getSignInResource(settings.connectionName, context.activity, undefined);
}
else if (ExtendedUserTokenProviderT.check(context.adapter)) {
return context.adapter.getSignInResource(context, settings.connectionName, (_b = (_a = context.activity) === null || _a === void 0 ? void 0 : _a.from) === null || _b === void 0 ? void 0 : _b.id, undefined, settings.oAuthAppCredentials);
}
else {
throw new Error('OAuth prompt is not supported by the current adapter');
}
});
}
exports.getSignInResource = getSignInResource;
/**
* @internal
*/
function signOutUser(context, settings) {
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
const userTokenClient = context.turnState.get(context.adapter.UserTokenClientKey);
if (userTokenClient) {
yield userTokenClient.signOutUser((_b = (_a = context.activity) === null || _a === void 0 ? void 0 : _a.from) === null || _b === void 0 ? void 0 : _b.id, settings.connectionName, (_c = context.activity) === null || _c === void 0 ? void 0 : _c.channelId);
}
else if (ExtendedUserTokenProviderT.check(context.adapter)) {
yield context.adapter.signOutUser(context, settings.connectionName, (_e = (_d = context.activity) === null || _d === void 0 ? void 0 : _d.from) === null || _e === void 0 ? void 0 : _e.id, settings.oAuthAppCredentials);
}
else {
throw new Error('OAuth prompt is not supported by the current adapter');
}
});
}
exports.signOutUser = signOutUser;
/**
* @internal
*/
function exchangeToken(context, settings, tokenExchangeRequest) {
var _a, _b, _c, _d, _e;
return __awaiter(this, void 0, void 0, function* () {
const userTokenClient = context.turnState.get(context.adapter.UserTokenClientKey);
if (userTokenClient) {
return userTokenClient.exchangeToken((_b = (_a = context.activity) === null || _a === void 0 ? void 0 : _a.from) === null || _b === void 0 ? void 0 : _b.id, settings.connectionName, (_c = context.activity) === null || _c === void 0 ? void 0 : _c.channelId, tokenExchangeRequest);
}
else if (ExtendedUserTokenProviderT.check(context.adapter)) {
return context.adapter.exchangeToken(context, settings.connectionName, (_e = (_d = context.activity) === null || _d === void 0 ? void 0 : _d.from) === null || _e === void 0 ? void 0 : _e.id, tokenExchangeRequest);
}
else {
throw new Error('OAuth prompt is not supported by the current adapter');
}
});
}
exports.exchangeToken = exchangeToken;
/**
* @internal
*/
function createConnectorClient(context, serviceUrl, claimsIdentity, audience) {
return __awaiter(this, void 0, void 0, function* () {
const connectorFactory = context.turnState.get(context.adapter.ConnectorFactoryKey);
if (connectorFactory) {
return connectorFactory.create(serviceUrl, audience);
}
else if (ConnectorClientBuilderT.check(context.adapter)) {
return context.adapter.createConnectorClientWithIdentity(serviceUrl, claimsIdentity, audience);
}
else {
throw new Error('OAuth prompt is not supported by the current adapter');
}
});
}
exports.createConnectorClient = createConnectorClient;
//# sourceMappingURL=userTokenAccess.js.map
;