@@ -670,23 +670,25 @@ export class MyHttpHeaderTool implements HttpHeaderTool<APIGatewayEventRequestCo
670670 } ,
671671 ) : Promise < T > {
672672 const isVerify = params ?. verify ?? true ;
673+ const errScope = `verifyJWT(http)` ;
673674
674675 //* it must be JWT Token. verify signature, and load.
675- if ( typeof token !== 'string' || ! token ) throw new Error ( `@token (string) is required - but ${ typeof token } ` ) ;
676+ if ( typeof token !== 'string' || ! token )
677+ throw new Error ( `@token (string) is required (but ${ typeof token } ) - ${ errScope } ` ) ;
676678 // STEP.1 decode jwt, and extract { iss, iat, exp }
677679 const current = params ?. current ?? $U . current_time_ms ( ) ;
678680 const sections = token . split ( '.' ) ;
679- if ( sections . length !== 3 ) throw new Error ( `@token[${ token } ] is invalid format! ` ) ;
681+ if ( sections . length !== 3 ) throw new Error ( `@token[${ token } ] is invalid ( format) - ${ errScope } ` ) ;
680682 const [ header , payload , signature ] = sections ;
681683 const $jwt = $U . jwt ( ) ;
682684 const data = $jwt . decode ( token , { complete : false , json : true } ) ;
683- if ( ! data ) throw new Error ( `@token[${ token } ] is invalid - failed to decode! ` ) ;
685+ if ( ! data ) throw new Error ( `@token[${ token } ] is invalid ( failed to decode) - ${ errScope } ` ) ;
684686 const { iss, iat, exp } = data ;
685687
686688 // STEP.1-1 validate parameters.
687- if ( typeof iss !== 'string' && iss !== null ) throw new Error ( `.iss (string) is required! ` ) ;
688- if ( typeof iat !== 'number' && iat !== null ) throw new Error ( `.iat (number) is required! ` ) ;
689- if ( typeof exp !== 'number' && exp !== null ) throw new Error ( `.exp (number) is required! ` ) ;
689+ if ( typeof iss !== 'string' && iss !== null ) throw new Error ( `.iss (string) is required - ${ errScope } ` ) ;
690+ if ( typeof iat !== 'number' && iat !== null ) throw new Error ( `.iat (number) is required - ${ errScope } ` ) ;
691+ if ( typeof exp !== 'number' && exp !== null ) throw new Error ( `.exp (number) is required - ${ errScope } ` ) ;
690692
691693 // STEP.2 validate signature by KMS(iss).verify()
692694 //TODO - iss 에 인증제공자의 api 넣기 (ex: api/lemon-backend-dev?)
@@ -697,13 +699,19 @@ export class MyHttpHeaderTool implements HttpHeaderTool<APIGatewayEventRequestCo
697699 } else if ( typeof iss === 'string' && iss . startsWith ( 'kms/' ) ) {
698700 const alias = _alias ( iss ) ;
699701 const $kms = alias ? this . findKMSService ( `alias/${ alias } ` ) : null ;
700- const verified = $kms ? await $kms . verify ( [ header , payload ] . join ( '.' ) , signature ) : false ;
701- if ( ! verified ) throw new Error ( `@signature[] is invalid - not be verified by iss:${ iss } !` ) ;
702- if ( ! exp || exp * 1000 < current ) throw new Error ( `.exp[${ $U . ts ( exp * 1000 ) } ] is invalid - expired!` ) ;
702+ const message = [ header , payload ] . join ( '.' ) ;
703+ const verified = $kms
704+ ? await $kms . verify ( message , signature , { throwable : true } ) . catch ( e => {
705+ throw new Error ( `@signature[] is invalid (kms: ${ GETERR ( e ) } ) - ${ errScope } ` ) ;
706+ } )
707+ : false ;
708+ if ( ! verified ) throw new Error ( `@signature[] is invalid (failed to verify by iss:${ iss } ) - ${ errScope } ` ) ;
709+ if ( ! exp ) throw new Error ( `.exp[${ exp } ] is invalid (empty) - ${ errScope } ` ) ;
710+ if ( exp * 1000 < current ) throw new Error ( `.exp[${ $U . ts ( exp * 1000 ) } ] is invalid (expired) - ${ errScope } ` ) ;
703711 return data as T ;
704712 }
705713 //* or throw
706- throw new Error ( `@iss[${ iss } ] is invalid - unsupportable issuer! ` ) ;
714+ throw new Error ( `@iss[${ iss } ] is invalid ( unsupportable issuer) - ${ errScope } ` ) ;
707715 }
708716
709717 /**
0 commit comments