Skip to content

Commit 300f6f1

Browse files
authored
Fix bot builder plugin so it receives messages (#290)
When I added the token validation, I Moved the path registration to onInit in HttpPlugin Removed credentials as a variable from the BotBuilderPlugin thinking it was accidentally left there In this PR: Call super.onInit so path registration is correctly done. This also registers the middleware to do token validation Add credentials back in (with a comment) because the Reflect api doesn't see the properties of parent classes which is required for Dependency Injection.
1 parent ced2614 commit 300f6f1

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

packages/botbuilder/src/plugin.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77

88
import express from 'express';
99

10-
import { $Activity, Activity, IToken, JsonWebToken } from '@microsoft/teams.api';
10+
import { $Activity, Activity, Credentials, IToken } from '@microsoft/teams.api';
1111
import {
1212
Dependency,
1313
Event,
@@ -19,6 +19,7 @@ import {
1919
Plugin,
2020
manifest,
2121
} from '@microsoft/teams.apps';
22+
import { JwtValidatedRequest } from '@microsoft/teams.apps/dist/middleware';
2223
import { ILogger } from '@microsoft/teams.common';
2324
import * as $http from '@microsoft/teams.common/http';
2425

@@ -43,6 +44,12 @@ export class BotBuilderPlugin extends HttpPlugin implements ISender {
4344
@Dependency()
4445
declare readonly manifest: Partial<manifest.Manifest>;
4546

47+
// Even though we don't use this in this plugin, the plugin chain
48+
// has it, and the dependency injection system only looks at the surface
49+
// level dependencies of the plugin.
50+
@Dependency({ optional: true })
51+
declare readonly credentials?: Credentials;
52+
4653
@Dependency({ optional: true })
4754
declare readonly botToken?: () => IToken;
4855

@@ -65,6 +72,7 @@ export class BotBuilderPlugin extends HttpPlugin implements ISender {
6572
}
6673

6774
onInit() {
75+
super.onInit();
6876
if (!this.adapter) {
6977
const clientId = this.credentials?.clientId;
7078
const clientSecret =
@@ -89,20 +97,27 @@ export class BotBuilderPlugin extends HttpPlugin implements ISender {
8997
}
9098

9199
protected async onRequest(
92-
req: express.Request,
100+
req: JwtValidatedRequest,
93101
res: express.Response,
94102
next: express.NextFunction
95103
) {
96104
if (!this.adapter) {
97105
throw new Error('plugin not registered');
98106
}
99107

108+
const activity: Activity = req.body;
100109
try {
101-
const authorization = req.headers.authorization?.replace('Bearer ', '');
102-
103-
if (!authorization) {
104-
res.status(401).send('unauthorized');
105-
return;
110+
let token: IToken | undefined;
111+
if (req.validatedToken) {
112+
token = req.validatedToken;
113+
} else {
114+
token = {
115+
appId: '',
116+
from: 'azure',
117+
fromId: '',
118+
serviceUrl: activity.serviceUrl || '',
119+
isExpired: () => false,
120+
};
106121
}
107122

108123
await this.adapter.process(req, res, async (context) => {
@@ -119,7 +134,7 @@ export class BotBuilderPlugin extends HttpPlugin implements ISender {
119134
this.pending[context.activity.id] = res;
120135
this.$onActivity({
121136
sender: this,
122-
token: new JsonWebToken(authorization),
137+
token,
123138
activity: new $Activity(context.activity as any) as Activity,
124139
});
125140
});

0 commit comments

Comments
 (0)