Skip to content

Commit bf79958

Browse files
author
Jon Slominski
committed
fix(auth): clean up Cognito client config and fix protocol detection
- Remove unnecessary callback/logout URLs from Cognito User Pool client - Remove unused UserPoolUserClientId output from security stack - Use X-Forwarded-Proto header for correct protocol detection behind load balancers
1 parent 202be66 commit bf79958

File tree

3 files changed

+8
-22
lines changed
  • source/cdk/ecs-and-lambda
    • lib/stacks
    • servers
      • sample-ecs-weather-streamablehttp-stateless-nodejs-express/src
      • sample-lambda-weather-streamablehttp-stateless-nodejs-express/src

3 files changed

+8
-22
lines changed

source/cdk/ecs-and-lambda/lib/stacks/security-stack.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,25 +118,11 @@ export class SecurityStack extends cdk.Stack {
118118
],
119119
callbackUrls: [
120120
"http://localhost:2299/callback", // for local development/testing with sample-auth-python server
121-
"http://localhost:2299", // for local development/testing with sample-auth-python server
122-
"https://${this.region}.console.aws.amazon.com/cognito/oauth2/idpresponse",
123-
],
124-
logoutUrls: [
125-
"http://localhost:2299/callback", // for local development/testing with sample-auth-python server
126-
"http://localhost:2299", // for local development/testing with sample-auth-python server
127-
"https://${this.region}.console.aws.amazon.com/cognito/oauth2",
128121
],
129122
},
130123
preventUserExistenceErrors: true,
131124
});
132125

133-
// Output user client ID
134-
new cdk.CfnOutput(this, "UserPoolUserClientId", {
135-
value: this.appClientUser.userPoolClientId,
136-
description:
137-
"The Client ID for the Cognito User Pool Client (User Authentication)",
138-
});
139-
140126
// Create WAF Web ACL
141127
this.webAcl = new wafv2.CfnWebACL(this, "MCPServerWAF", {
142128
name: "mcp-server-waf",

source/cdk/ecs-and-lambda/servers/sample-ecs-weather-streamablehttp-stateless-nodejs-express/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ app.use(express.json());
249249
* Get WWW-Authenticate header for 401 responses.
250250
*/
251251
function getWWWAuthenticateHeader(req: Request): string {
252-
const baseUrl =
253-
process.env.BASE_URL || `${req.protocol}://${req.get("host")}`;
252+
const protocol = req.get("X-Forwarded-Proto") || req.protocol;
253+
const baseUrl = process.env.BASE_URL || `${protocol}://${req.get("host")}`;
254254
const val = `Bearer realm="mcp-server", resource_metadata="${baseUrl}/weather-nodejs/.well-known/oauth-protected-resource"`;
255255
console.log(val);
256256
return val;
@@ -314,8 +314,8 @@ app.get(
314314
(req: Request, res: Response) => {
315315
const region = process.env.AWS_REGION || "us-west-2";
316316
const user_pool_id = process.env.COGNITO_USER_POOL_ID;
317-
const baseUrl =
318-
process.env.BASE_URL || `${req.protocol}://${req.get("host")}`;
317+
const protocol = req.get("X-Forwarded-Proto") || req.protocol;
318+
const baseUrl = process.env.BASE_URL || `${protocol}://${req.get("host")}`;
319319

320320
res.json({
321321
resource: `${baseUrl}/weather-nodejs/mcp`,

source/cdk/ecs-and-lambda/servers/sample-lambda-weather-streamablehttp-stateless-nodejs-express/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,9 @@ app.use(express.json());
249249
* Get WWW-Authenticate header for 401 responses.
250250
*/
251251
function getWWWAuthenticateHeader(req: Request): string {
252-
const baseUrl =
253-
process.env.BASE_URL || `${req.protocol}://${req.get("host")}`;
252+
// Check X-Forwarded-Proto from ALB/CloudFront, fallback to req.protocol for local testing
253+
const protocol = req.get("X-Forwarded-Proto") || req.protocol;
254+
const baseUrl = process.env.BASE_URL || `${protocol}://${req.get("host")}`;
254255
const val = `Bearer realm="mcp-server", resource_metadata="${baseUrl}/weather-nodejs-lambda/.well-known/oauth-protected-resource"`;
255256
console.log(val);
256257
return val;
@@ -314,8 +315,7 @@ app.get(
314315
(req: Request, res: Response) => {
315316
const region = process.env.AWS_REGION || "us-west-2";
316317
const user_pool_id = process.env.COGNITO_USER_POOL_ID;
317-
const baseUrl =
318-
process.env.BASE_URL || `${req.protocol}://${req.get("host")}`;
318+
const baseUrl = process.env.BASE_URL || `https://${req.get("host")}`;
319319

320320
res.json({
321321
resource: `${baseUrl}/weather-nodejs-lambda/mcp`,

0 commit comments

Comments
 (0)