@@ -3,7 +3,7 @@ import express from 'express';
33import { Activity , Credentials , IToken , JsonWebToken } from '@microsoft/teams.api' ;
44import { ConsoleLogger , ILogger } from '@microsoft/teams.common' ;
55
6- import { createServiceTokenValidator } from './auth/jwt-validator' ;
6+ import { createServiceTokenValidator , JwtValidator } from './auth/jwt-validator' ;
77
88export type JwtValidationParams = {
99 credentials ?: Credentials ;
@@ -18,15 +18,19 @@ export function withJwtValidation(params: JwtValidationParams) {
1818 const { credentials, logger : inputLogger } = params ;
1919 const logger = inputLogger ?. child ( 'jwt-validation-middleware' ) ?? new ConsoleLogger ( 'jwt-validation-middleware' ) ;
2020
21- // Create service token validator if credentials are provided and not in local env
22- const serviceTokenValidator = ( process . env . NODE_ENV !== 'local' && credentials ?. clientId )
23- ? createServiceTokenValidator (
21+ // Create service token validator if credentials are provided
22+ let serviceTokenValidator : JwtValidator | null ;
23+ if ( credentials ?. clientId ) {
24+ serviceTokenValidator = createServiceTokenValidator (
2425 credentials . clientId ,
2526 credentials . tenantId ,
2627 undefined ,
2728 logger
28- )
29- : null ;
29+ ) ;
30+ } else {
31+ logger . debug ( 'No credentials provided, skipping service token validation' ) ;
32+ serviceTokenValidator = null ;
33+ }
3034
3135 return async (
3236 req : JwtValidatedRequest ,
@@ -35,35 +39,30 @@ export function withJwtValidation(params: JwtValidationParams) {
3539 ) => {
3640 const authorization = req . headers . authorization ?. replace ( 'Bearer ' , '' ) ;
3741
38- if ( ! authorization && process . env . NODE_ENV !== 'local' ) {
42+ if ( ! authorization ) {
3943 res . status ( 401 ) . send ( 'unauthorized' ) ;
4044 return ;
4145 }
4246
43- if ( serviceTokenValidator ) {
44- if ( ! authorization ) {
45- res . status ( 401 ) . send ( 'unauthorized no authorization header' ) ;
46- return ;
47- }
47+ if ( ! serviceTokenValidator ) {
48+ logger . debug ( 'No service token validator configured, skipping validation' ) ;
49+ next ( ) ;
50+ return ;
51+ }
4852
49- const activity : Activity = req . body ;
50- // Use cached validator with per-request service URL validation
51- const validationResult = await serviceTokenValidator . validateAccessToken ( authorization , activity . serviceUrl ? {
52- validateServiceUrl : { expectedServiceUrl : activity . serviceUrl }
53- } : undefined ) ;
53+ const activity : Activity = req . body ;
54+ // Use cached validator with per-request service URL validation
55+ const validationResult = await serviceTokenValidator . validateAccessToken ( authorization , activity . serviceUrl ? {
56+ validateServiceUrl : { expectedServiceUrl : activity . serviceUrl }
57+ } : undefined ) ;
5458
55- if ( validationResult ) {
56- logger . debug ( `validated service token for activity ${ activity . id } ` ) ;
57- // Store the validated token in the request for use in subsequent handlers
58- req . validatedToken = new JsonWebToken ( authorization ) ;
59- next ( ) ;
60- } else {
61- res . status ( 401 ) . send ( 'Invalid token' ) ;
62- return ;
63- }
59+ if ( validationResult ) {
60+ logger . debug ( `validated service token for activity ${ activity . id } ` ) ;
61+ // Store the validated token in the request for use in subsequent handlers
62+ req . validatedToken = new JsonWebToken ( authorization ) ;
63+ next ( ) ;
64+ } else {
65+ res . status ( 401 ) . send ( 'Invalid token' ) ;
6466 }
65-
66- logger . debug ( 'Skipping JWT validation in local environment' ) ;
67- next ( ) ;
6867 } ;
6968}
0 commit comments