1 | "use strict";
|
2 |
|
3 |
|
4 | var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
5 | function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
6 | return new (P || (P = Promise))(function (resolve, reject) {
|
7 | function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
8 | function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
9 | function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
10 | step((generator = generator.apply(thisArg, _arguments || [])).next());
|
11 | });
|
12 | };
|
13 | Object.defineProperty(exports, "__esModule", { value: true });
|
14 | exports.TeamsSSOTokenExchangeMiddleware = void 0;
|
15 | const z = require("zod");
|
16 | const botbuilder_core_1 = require("botbuilder-core");
|
17 | function getStorageKey(context) {
|
18 | var _a;
|
19 | const activity = context.activity;
|
20 | const channelId = activity.channelId;
|
21 | if (!channelId) {
|
22 | throw new Error('invalid activity. Missing channelId');
|
23 | }
|
24 | const conversationId = (_a = activity.conversation) === null || _a === void 0 ? void 0 : _a.id;
|
25 | if (!conversationId) {
|
26 | throw new Error('invalid activity. Missing conversation.id');
|
27 | }
|
28 | const value = activity.value;
|
29 | if (!(value === null || value === void 0 ? void 0 : value.id)) {
|
30 | throw new Error('Invalid signin/tokenExchange. Missing activity.value.id.');
|
31 | }
|
32 | return `${channelId}/${conversationId}/${value.id}`;
|
33 | }
|
34 | function sendInvokeResponse(context, body = null, status = botbuilder_core_1.StatusCodes.OK) {
|
35 | return __awaiter(this, void 0, void 0, function* () {
|
36 | yield context.sendActivity({
|
37 | type: botbuilder_core_1.ActivityTypes.InvokeResponse,
|
38 | value: { body, status },
|
39 | });
|
40 | });
|
41 | }
|
42 | const ExchangeToken = z.custom((val) => typeof val.exchangeToken === 'function', { message: 'ExtendedUserTokenProvider' });
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 | class TeamsSSOTokenExchangeMiddleware {
|
59 | |
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | constructor(storage, oAuthConnectionName) {
|
66 | this.storage = storage;
|
67 | this.oAuthConnectionName = oAuthConnectionName;
|
68 | if (!storage) {
|
69 | throw new TypeError('`storage` parameter is required');
|
70 | }
|
71 | if (!oAuthConnectionName) {
|
72 | throw new TypeError('`oAuthConnectionName` parameter is required');
|
73 | }
|
74 | }
|
75 | |
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | onTurn(context, next) {
|
82 | return __awaiter(this, void 0, void 0, function* () {
|
83 | if (context.activity.channelId === botbuilder_core_1.Channels.Msteams && context.activity.name === botbuilder_core_1.tokenExchangeOperationName) {
|
84 |
|
85 | if (!(yield this.exchangedToken(context))) {
|
86 | return;
|
87 | }
|
88 |
|
89 |
|
90 | if (!(yield this.deduplicatedTokenExchangeId(context))) {
|
91 |
|
92 | return;
|
93 | }
|
94 | }
|
95 | yield next();
|
96 | });
|
97 | }
|
98 | deduplicatedTokenExchangeId(context) {
|
99 | var _a, _b;
|
100 | return __awaiter(this, void 0, void 0, function* () {
|
101 |
|
102 | const storeItem = {
|
103 | eTag: (_a = context.activity.value) === null || _a === void 0 ? void 0 : _a.id,
|
104 | };
|
105 | try {
|
106 |
|
107 | yield this.storage.write({
|
108 | [getStorageKey(context)]: storeItem,
|
109 | });
|
110 | }
|
111 | catch (err) {
|
112 | const message = (_b = err.message) === null || _b === void 0 ? void 0 : _b.toLowerCase();
|
113 |
|
114 |
|
115 | if (message.includes('etag conflict') || message.includes('precondition is not met')) {
|
116 | yield sendInvokeResponse(context);
|
117 | return false;
|
118 | }
|
119 | throw err;
|
120 | }
|
121 | return true;
|
122 | });
|
123 | }
|
124 | exchangedToken(context) {
|
125 | return __awaiter(this, void 0, void 0, function* () {
|
126 | let tokenExchangeResponse;
|
127 | const tokenExchangeRequest = context.activity.value;
|
128 | try {
|
129 | const userTokenClient = context.turnState.get(context.adapter.UserTokenClientKey);
|
130 | const exchangeToken = ExchangeToken.safeParse(context.adapter);
|
131 | if (userTokenClient) {
|
132 | tokenExchangeResponse = yield userTokenClient.exchangeToken(context.activity.from.id, this.oAuthConnectionName, context.activity.channelId, { token: tokenExchangeRequest.token });
|
133 | }
|
134 | else if (exchangeToken.success) {
|
135 | tokenExchangeResponse = yield exchangeToken.data.exchangeToken(context, this.oAuthConnectionName, context.activity.from.id, { token: tokenExchangeRequest.token });
|
136 | }
|
137 | else {
|
138 | new Error('Token Exchange is not supported by the current adapter.');
|
139 | }
|
140 | }
|
141 | catch (_err) {
|
142 |
|
143 |
|
144 |
|
145 | }
|
146 | if (!(tokenExchangeResponse === null || tokenExchangeResponse === void 0 ? void 0 : tokenExchangeResponse.token)) {
|
147 |
|
148 |
|
149 | const invokeResponse = {
|
150 | id: tokenExchangeRequest.id,
|
151 | connectionName: this.oAuthConnectionName,
|
152 | failureDetail: 'The bot is unable to exchange token. Proceed with regular login.',
|
153 | };
|
154 | yield sendInvokeResponse(context, invokeResponse, botbuilder_core_1.StatusCodes.PRECONDITION_FAILED);
|
155 | return false;
|
156 | }
|
157 | return true;
|
158 | });
|
159 | }
|
160 | }
|
161 | exports.TeamsSSOTokenExchangeMiddleware = TeamsSSOTokenExchangeMiddleware;
|
162 |
|
\ | No newline at end of file |