@@ -68,6 +68,11 @@ type EncodingJwtErrors = {
6868 encodingErrors : string [ ] | null ;
6969} ;
7070
71+ type EncodingResult = {
72+ jwt : string ;
73+ signingErrors : string [ ] | null ;
74+ }
75+
7176class _TokenEncoderService {
7277 async selectEncodingExample (
7378 algorithmPickerOptionValue : string ,
@@ -183,7 +188,8 @@ class _TokenEncoderService {
183188 }
184189
185190 if ( encodeJWTResult . isOk ( ) ) {
186- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
191+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
192+ stateUpdate . signingErrors = encodeJWTResult . value . signingErrors ;
187193 }
188194
189195 return {
@@ -214,7 +220,7 @@ class _TokenEncoderService {
214220 }
215221
216222 if ( encodeJWTResult . isOk ( ) ) {
217- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
223+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
218224
219225 useDebuggerStore . getState ( ) . setStash$ ( {
220226 asymmetricPublicKey : digitallySignedToken . publicKey ,
@@ -379,7 +385,7 @@ class _TokenEncoderService {
379385 }
380386
381387 if ( encodeJWTResult . isOk ( ) ) {
382- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
388+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
383389 }
384390
385391 return {
@@ -409,7 +415,7 @@ class _TokenEncoderService {
409415 }
410416
411417 if ( encodeJWTResult . isOk ( ) ) {
412- stateUpdate . jwt = encodeJWTResult . value . trim ( ) ;
418+ stateUpdate . jwt = encodeJWTResult . value . jwt . trim ( ) ;
413419 }
414420
415421 return {
@@ -484,48 +490,61 @@ class _TokenEncoderService {
484490 payload : DecodedJwtPayloadModel ,
485491 key : string ,
486492 encodingFormat : EncodingValues ,
487- ) : Promise < Result < string , DebuggerErrorModel > > {
488- if ( isHmacAlg ( header . alg ) ) {
489- if ( ! key ) {
490- return err ( {
491- task : DebuggerTaskValues . ENCODE ,
492- input : DebuggerInputValues . KEY ,
493- message : "Secret must not be empty." ,
494- } ) ;
495- }
493+ ) : Promise < Result < EncodingResult , DebuggerErrorModel > > {
494+ if ( ! isHmacAlg ( header . alg ) ) {
495+ return err ( {
496+ task : DebuggerTaskValues . ENCODE ,
497+ input : DebuggerInputValues . HEADER ,
498+ message : `Invalid MAC algorithm. Only use MAC "alg" parameter values in the header as defined by [RFC 7518 (JSON Web Algorithms)](https://datatracker.ietf.org/doc/html/rfc7518#section-3.1).` ,
499+ } ) ;
500+ }
496501
497- const getAlgSizeResult = getAlgSize ( header . alg ) ;
502+ if ( ! key ) {
503+ return err ( {
504+ task : DebuggerTaskValues . ENCODE ,
505+ input : DebuggerInputValues . KEY ,
506+ message : "Secret must not be empty." ,
507+ } ) ;
508+ }
498509
499- if ( getAlgSizeResult . isErr ( ) ) {
500- return err ( {
501- task : DebuggerTaskValues . ENCODE ,
502- input : DebuggerInputValues . KEY ,
503- message : getAlgSizeResult . error ,
504- } ) ;
505- }
510+ const getAlgSizeResult = getAlgSize ( header . alg ) ;
506511
507- const checkHmacSecretLengthResult = checkHmacSecretLength (
508- key ,
509- getAlgSizeResult . value . size ,
510- encodingFormat ,
511- ) ;
512+ if ( getAlgSizeResult . isErr ( ) ) {
513+ return err ( {
514+ task : DebuggerTaskValues . ENCODE ,
515+ input : DebuggerInputValues . KEY ,
516+ message : getAlgSizeResult . error ,
517+ } ) ;
518+ }
512519
513- if ( checkHmacSecretLengthResult . isErr ( ) ) {
514- return err ( checkHmacSecretLengthResult . error ) ;
515- }
520+ const checkHmacSecretLengthResult = checkHmacSecretLength (
521+ key ,
522+ getAlgSizeResult . value . size ,
523+ encodingFormat ,
524+ ) ;
516525
517- return await signWithSymmetricSecretKey (
518- header as CompactJWSHeaderParameters ,
519- payload ,
520- key ,
521- encodingFormat ,
522- ) ;
526+ const signingError = checkHmacSecretLengthResult . isErr ( )
527+ ? [ checkHmacSecretLengthResult . error . message ]
528+ : null ;
529+
530+ const signWithSymmetricSecretKeyResult = await signWithSymmetricSecretKey (
531+ header as CompactJWSHeaderParameters ,
532+ payload ,
533+ key ,
534+ encodingFormat ,
535+ ) ;
536+
537+ if ( signWithSymmetricSecretKeyResult . isErr ( ) ) {
538+ return err ( {
539+ task : DebuggerTaskValues . ENCODE ,
540+ input : DebuggerInputValues . KEY ,
541+ message : signWithSymmetricSecretKeyResult . error . message ,
542+ } ) ;
523543 }
524544
525- return err ( {
526- task : DebuggerTaskValues . ENCODE ,
527- input : DebuggerInputValues . HEADER ,
528- message : `Invalid MAC algorithm. Only use MAC "alg" parameter values in the header as defined by [RFC 7518 (JSON Web Algorithms)](https://datatracker.ietf.org/doc/html/rfc7518#section-3.1).` ,
545+ return ok < EncodingResult > ( {
546+ jwt : signWithSymmetricSecretKeyResult . value ,
547+ signingErrors : signingError ,
529548 } ) ;
530549 }
531550
@@ -534,7 +553,7 @@ class _TokenEncoderService {
534553 payload : DecodedJwtPayloadModel ,
535554 key : string ,
536555 keyFormat : AsymmetricKeyFormatValues ,
537- ) : Promise < Result < string , DebuggerErrorModel > > {
556+ ) : Promise < Result < EncodingResult , DebuggerErrorModel > > {
538557 if ( isDigitalSignatureAlg ( header . alg ) ) {
539558 if ( ! key ) {
540559 return err ( {
@@ -544,12 +563,25 @@ class _TokenEncoderService {
544563 } ) ;
545564 }
546565
547- return await signWithAsymmetricPrivateKey (
566+ const jwt = await signWithAsymmetricPrivateKey (
548567 header as CompactJWSHeaderParameters ,
549568 payload ,
550569 key ,
551570 keyFormat ,
552571 ) ;
572+
573+ if ( jwt . isErr ( ) ) {
574+ return err ( {
575+ task : DebuggerTaskValues . ENCODE ,
576+ input : DebuggerInputValues . KEY ,
577+ message : "Private key must not be empty." ,
578+ } )
579+ }
580+
581+ return ok ( {
582+ jwt : jwt . value ,
583+ signingErrors : null ,
584+ } ) ;
553585 }
554586
555587 return err ( {
@@ -684,9 +716,7 @@ class _TokenEncoderService {
684716 symmetricSecretKeyEncoding : EncodingValues ;
685717 } ) : Promise <
686718 Result <
687- {
688- jwt : string ;
689- } ,
719+ EncodingResult ,
690720 EncodingSymmetricSecretKeyErrors
691721 >
692722 > {
@@ -767,6 +797,7 @@ class _TokenEncoderService {
767797
768798 return ok ( {
769799 jwt : encodeJwtResult . value . jwt . trim ( ) ,
800+ signingErrors : encodeJwtResult . value . signingErrors ,
770801 } ) ;
771802 }
772803
@@ -861,17 +892,15 @@ class _TokenEncoderService {
861892 } ,
862893 ) : Promise <
863894 Result <
864- {
865- jwt : string ;
866- } ,
895+ EncodingResult ,
867896 EncodingJwtErrors
868897 >
869898 > {
870899 const algType = params . algType ;
871900 const header = params . header ;
872901 const payload = params . payload ;
873902
874- let encodeJWTResult : Result < string , DebuggerErrorModel > | null = null ;
903+ let encodeJWTResult : Result < EncodingResult , DebuggerErrorModel > | null = null ;
875904
876905 if ( algType === SigningAlgCategoryValues . ANY ) {
877906 const symmetricSecretKey = params . symmetricSecretKey ;
@@ -998,8 +1027,9 @@ class _TokenEncoderService {
9981027 }
9991028 }
10001029
1001- return ok ( {
1002- jwt : encodeJWTResult . value ,
1030+ return ok < EncodingResult > ( {
1031+ jwt : encodeJWTResult . value . jwt ,
1032+ signingErrors : encodeJWTResult . value . signingErrors ,
10031033 } ) ;
10041034 }
10051035
@@ -1235,6 +1265,7 @@ class _TokenEncoderService {
12351265 }
12361266
12371267 stateUpdate . jwt = processSymmetricSecretKeyResult . value . jwt . trim ( ) ;
1268+ stateUpdate . signingErrors = processSymmetricSecretKeyResult . value . signingErrors ;
12381269
12391270 return stateUpdate ;
12401271 }
@@ -1269,6 +1300,7 @@ class _TokenEncoderService {
12691300 }
12701301
12711302 stateUpdate . jwt = processSymmetricSecretKeyResult . value . jwt . trim ( ) ;
1303+ stateUpdate . signingErrors = processSymmetricSecretKeyResult . value . signingErrors ;
12721304
12731305 return stateUpdate ;
12741306 }
0 commit comments