Skip to content

Commit c8fe9ff

Browse files
author
Jesus Terrazas
committed
Add useJwtMiddleware if agent is hosted publicly
1 parent 9d40ca6 commit c8fe9ff

File tree

3 files changed

+25
-16
lines changed

3 files changed

+25
-16
lines changed

nodejs/claude/sample-agent/src/index.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { configDotenv } from 'dotenv';
66

77
configDotenv();
88

9-
import { AuthConfiguration, authorizeJWT, CloudAdapter, Request } from '@microsoft/agents-hosting';
9+
import { AuthConfiguration, authorizeJWT, CloudAdapter, loadAuthConfigFromEnv, Request } from '@microsoft/agents-hosting';
1010
import express, { Response } from 'express'
1111
import { agentApplication } from './agent';
1212

13-
const authConfig: AuthConfiguration = {};
13+
// Use request validation middleware only if hosting publicly
14+
const useJwtMiddleware = Boolean(process.env.WEBSITE_SITE_NAME) || process.env.NODE_ENV === 'production';
15+
const authConfig: AuthConfiguration = useJwtMiddleware ? loadAuthConfigFromEnv() : {};
1416

1517
const server = express()
1618
server.use(express.json())
@@ -23,13 +25,14 @@ server.post('/api/messages', (req: Request, res: Response) => {
2325
})
2426
})
2527

26-
const port = process.env.PORT || 3978
27-
server.listen(port, async () => {
28-
console.log(`\nServer listening to port ${port} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`)
28+
const port = Number(process.env.PORT) || 3978
29+
const host = useJwtMiddleware ? '0.0.0.0' : '127.0.0.1';
30+
server.listen(port, host, async () => {
31+
console.log(`\nServer listening on ${host}:${port} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`)
2932
}).on('error', async (err) => {
3033
console.error(err);
3134
process.exit(1);
3235
}).on('close', async () => {
3336
console.log('Server closed');
3437
process.exit(0);
35-
});
38+
});

nodejs/langchain/sample-agent/src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { configDotenv } from 'dotenv';
33

44
configDotenv();
55

6-
import { AuthConfiguration, authorizeJWT, CloudAdapter, Request } from '@microsoft/agents-hosting';
6+
import { AuthConfiguration, authorizeJWT, CloudAdapter, loadAuthConfigFromEnv, Request } from '@microsoft/agents-hosting';
77
import express, { Response } from 'express'
88
import { agentApplication } from './agent';
99

10-
const authConfig: AuthConfiguration = {};
10+
// Use request validation middleware only if hosting publicly
11+
const useJwtMiddleware = Boolean(process.env.WEBSITE_SITE_NAME) || process.env.NODE_ENV === 'production';
12+
const authConfig: AuthConfiguration = useJwtMiddleware ? loadAuthConfigFromEnv() : {};
1113

1214
const server = express()
1315
server.use(express.json())
@@ -20,9 +22,10 @@ server.post('/api/messages', (req: Request, res: Response) => {
2022
})
2123
})
2224

23-
const port = process.env.PORT || 3978
24-
server.listen(port, async () => {
25-
console.log(`\nServer listening to port ${port} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`)
25+
const port = Number(process.env.PORT) || 3978
26+
const host = useJwtMiddleware ? '0.0.0.0' : '127.0.0.1';
27+
server.listen(port, host, async () => {
28+
console.log(`\nServer listening on ${host}:${port} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`)
2629
}).on('error', async (err) => {
2730
console.error(err);
2831
process.exit(1);

nodejs/openai/sample-agent/src/index.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import { configDotenv } from 'dotenv';
66

77
configDotenv();
88

9-
import { AuthConfiguration, authorizeJWT, CloudAdapter, Request } from '@microsoft/agents-hosting';
9+
import { AuthConfiguration, authorizeJWT, CloudAdapter, loadAuthConfigFromEnv, Request } from '@microsoft/agents-hosting';
1010
import express, { Response } from 'express'
1111
import { agentApplication } from './agent';
1212

13-
const authConfig: AuthConfiguration = {};
13+
// Use request validation middleware only if hosting publicly
14+
const useJwtMiddleware = Boolean(process.env.WEBSITE_SITE_NAME) || process.env.NODE_ENV === 'production';
15+
const authConfig: AuthConfiguration = useJwtMiddleware ? loadAuthConfigFromEnv() : {};
1416

1517
const server = express()
1618
server.use(express.json())
@@ -23,9 +25,10 @@ server.post('/api/messages', (req: Request, res: Response) => {
2325
})
2426
})
2527

26-
const port = process.env.PORT || 3978
27-
server.listen(port, async () => {
28-
console.log(`\nServer listening to port ${port} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`)
28+
const port = Number(process.env.PORT) || 3978
29+
const host = useJwtMiddleware ? '0.0.0.0' : '127.0.0.1';
30+
server.listen(port, host, async () => {
31+
console.log(`\nServer listening on ${host}:${port} for appId ${authConfig.clientId} debug ${process.env.DEBUG}`)
2932
}).on('error', async (err) => {
3033
console.error(err);
3134
process.exit(1);

0 commit comments

Comments
 (0)